gpt4 book ai didi

android - 关闭 USB 设备连接

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:03 25 4
gpt4 key购买 nike

我在 Android 操作系统上使用热敏打印机。我需要能够关闭与连接的 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 - 关闭 USB 设备连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47460206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com