关闭 USB 设备连接
Close USB device connection
我正在 Android OS 上使用热敏打印机。我需要能够关闭与连接的 USB 设备的连接。
实例化UsbManager并获取USB设备列表
mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
mDeviceList = mUsbManager.getDeviceList();
正在设置 USB 通信
//Broadcast receiver to obtain permission from user for connection
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
//call method to set up device communication
mInterface = device.getInterface(0);
mEndPoint = mInterface.getEndpoint(0);
mConnection = mUsbManager.openDevice(device);
//setup();
}
} else {
//Log.d("SUB", "permission denied for device " + device);
Toast.makeText(context, "PERMISSION DENIED FOR THIS DEVICE", Toast.LENGTH_SHORT).show();
}
}
}
}
};
如果您已经连接了 USB 设备:
UsbDeviceConnection mConnection = usbManager.openDevice(device);
...
您可以通过以下方式发布它:
mConnection.close();
我正在 Android OS 上使用热敏打印机。我需要能够关闭与连接的 USB 设备的连接。
实例化UsbManager并获取USB设备列表
mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
mDeviceList = mUsbManager.getDeviceList();
正在设置 USB 通信
//Broadcast receiver to obtain permission from user for connection
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
//call method to set up device communication
mInterface = device.getInterface(0);
mEndPoint = mInterface.getEndpoint(0);
mConnection = mUsbManager.openDevice(device);
//setup();
}
} else {
//Log.d("SUB", "permission denied for device " + device);
Toast.makeText(context, "PERMISSION DENIED FOR THIS DEVICE", Toast.LENGTH_SHORT).show();
}
}
}
}
};
如果您已经连接了 USB 设备:
UsbDeviceConnection mConnection = usbManager.openDevice(device);
...
您可以通过以下方式发布它:
mConnection.close();