在我的应用程序中,正在使用USB主机模式,该模式提供有关已连接的USB大容量存储设备的信息,例如用例中的Usb闪存驱动器.现在,我需要在已连接的闪存驱动器上创建一个文件,并将一些数据保存在该文件中.到目前为止,我发现正在按以下方式连接设备,
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private Button mCheckForDevice;
private TextView mDeviceInfo;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private PendingIntent mPermissionIntent;
private UsbManager mUsbManager;
private UsbDevice mDeviceFound;
private UsbDeviceConnection mConnection;
private UsbInterface mUsbInterface = null;
private UsbEndpoint mInputEndpoint = null;
private UsbEndpoint mOutputEndpoint = null;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCheckForDevice = (Button) findViewById(R.id.check);
mDeviceInfo = (TextView) findViewById(R.id.deviceInfo);
count=0;
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
final HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
while (deviceIterator.hasNext()) {