Android USB 连接仅在应用程序第二次打开时建立
Android USB connection established only the second time the app is opened
对于我的应用程序,我需要与附加的 Arduino 设备建立连接,代码如下:
public String openConnection(UsbManager manager, Context context) {
// getting the driver with an external library...
UsbSerialDriver driver = availableDrivers.get(0);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
manager.requestPermission(driver.getDevice(), pi);
UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
if (connection == null) {
return "Found a device, but cannot connect";
}
// otherwise, continue and do stuff
}
问题是,当连接设备时,我第一次打开应用程序时它会显示请求权限的警报,但如果我单击 "OK",连接为空,所以它 returns早。然而,第二次它没有请求任何权限,但连接打开并且一切正常。
为什么会这样?
我知道这不是打开 USB 连接的最正确方法,但我还有其他问题不属于该问题,所以我很想了解 为什么 发生这种情况比 我应该怎么做
我正在 Android 8.1.0
上测试
尝试事先请求许可并从侦听授予的 USB 许可的广播接收器启动其余代码。
这显示在 Google 的文档中:
private static final String ACTION_USB_PERMISSION =
"com.android.example.USB_PERMISSION";
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
}
}
else {
Log.d(TAG, "permission denied for device " + device);
}
}
}
}
};
注册广播接收器的方法如下:
UsbManager mUsbManager = (UsbManager)
getSystemService(Context.USB_SERVICE);
private static final String ACTION_USB_PERMISSION =
"com.android.example.USB_PERMISSION";
...
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new
Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
然后你开始这一切:
UsbDevice device;
...
mUsbManager.requestPermission(device, mPermissionIntent);
按照您的方式,设备甚至在授予权限之前就尝试连接到 USB,因此失败。
对于我的应用程序,我需要与附加的 Arduino 设备建立连接,代码如下:
public String openConnection(UsbManager manager, Context context) {
// getting the driver with an external library...
UsbSerialDriver driver = availableDrivers.get(0);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
manager.requestPermission(driver.getDevice(), pi);
UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
if (connection == null) {
return "Found a device, but cannot connect";
}
// otherwise, continue and do stuff
}
问题是,当连接设备时,我第一次打开应用程序时它会显示请求权限的警报,但如果我单击 "OK",连接为空,所以它 returns早。然而,第二次它没有请求任何权限,但连接打开并且一切正常。
为什么会这样?
我知道这不是打开 USB 连接的最正确方法,但我还有其他问题不属于该问题,所以我很想了解 为什么 发生这种情况比 我应该怎么做
我正在 Android 8.1.0
上测试尝试事先请求许可并从侦听授予的 USB 许可的广播接收器启动其余代码。 这显示在 Google 的文档中:
private static final String ACTION_USB_PERMISSION =
"com.android.example.USB_PERMISSION";
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
}
}
else {
Log.d(TAG, "permission denied for device " + device);
}
}
}
}
};
注册广播接收器的方法如下:
UsbManager mUsbManager = (UsbManager)
getSystemService(Context.USB_SERVICE);
private static final String ACTION_USB_PERMISSION =
"com.android.example.USB_PERMISSION";
...
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new
Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
然后你开始这一切:
UsbDevice device;
...
mUsbManager.requestPermission(device, mPermissionIntent);
按照您的方式,设备甚至在授予权限之前就尝试连接到 USB,因此失败。