首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android:连接到USB设备,等待许可

Android:连接到USB设备,等待许可
EN

Stack Overflow用户
提问于 2015-04-24 18:18:38
回答 1查看 3.6K关注 0票数 1

我正在尝试这样做:连接到USB设备并获得打开(或失败)连接。我是根据我所找到的例子和解释来做逻辑的,但我在等待批准时有问题。首先,我尝试了一种“很好”的方法来使用wait()+notifyAll(),而不是使用带有检查的简单循环,但两次等待方法(waitConnection())都阻塞了我给它的超时,直到消息被接收之后。所以我尝试了这两个版本。

  1. 等待/通知: 公共(intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,startConnection(上下文上下文){ BroadcastReceiver接收方=新BroadcastReceiver() {公共BroadcastReceiver()(上下文,意图){ String action = intent.getAction();if (ACTION_USB_PERMISSION.equals(action)) {同步(syncObj) { if BroadcastReceiver false){ UsbDevice设备= intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);if (device != null) { if (device.getVendorId() == vendorId && device.getProductId() == productId) { connection = usbManager.openDevice(device);connectedDevice = device;}} syncObj.notyfyAll();};尝试{ usbManager = (UsbManager) UsbManager用于(最终UsbDevice设备: usbManager.getDeviceList().values()) { if (device.getVendorId() == this.vendorId & device.getProductId() == this.productId) {context.registerReceiver(接收者,新IntentFilter(ACTION_USB_PERMISSION));usbManager.requestPermission(设备,PendingIntent.getBroadcast(上下文,0,新意图(ACTION_USB_PERMISSION),0));中断;}} catch (异常e) {}返回此;} public UsbDeviceConnection waitConnection(int超时值){ Thread.sleep(10,0);syncObj.wait(超时);} catch (InterruptedException e) {}返回getConnection();}
  2. 直通回路 公共(intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,startConnection(上下文上下文){ BroadcastReceiver接收方=新BroadcastReceiver() {公共BroadcastReceiver()(上下文,意图){ String action = intent.getAction();if (ACTION_USB_PERMISSION.equals(action)) {同步(此){ if (如果为false)) { UsbDevice设备= intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);if (device != null) { if (device.getVendorId() == vendorId && device.getProductId() == productId) { connection = usbManager.openDevice(device);connectedDevice = device;};尝试{ context.getSystemService(Context.USB_SERVICE);= false;usbManager = (UsbManager) UsbManager对于(最终的UsbDevice设备: usbManager.getDeviceList().values()) { if (device.getVendorId() == this.vendorId & device.getProductId() == this.productId) { permissionRequested = true;context.registerReceiver(receiver,new IntentFilter(ACTION_USB_PERMISSION));usbManager.requestPermission( device,PendingIntent.getBroadcast(上下文,0,新意图(ACTION_USB_PERMISSION),0));中断;} catch (异常e) {}返回此;} public UsbDeviceConnection waitConnection(int超时值){ int等待=超时;而(permissionRequested && 10;0) { try { Thread.sleep(10,0);}InterruptedException (InterruptedException e) {}等待-= 10;}返回getConnection();}

因此,在这两种情况下,根据日志,waitConnection()方法(在startConnection()之后由使用者立即调用)似乎阻止了执行(我给了它超时10秒,它被阻塞了10秒),只有在它完成之后,BroadcastReceiver才会收到消息。看来requestPermission()不是异步的(正如我所想的那样),但是在这种情况下,startConnection()怎么可能在接收消息之前立即退出呢?我怎样才能等待BroadcastReceiver收到消息呢?如果我不使用waitConnection()方法,那么我的使用者应该如何知道何时才能开始检查连接可用性呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-24 19:20:47

“只有在它完成后,BroadcastReceiver才会收到消息”

默认情况下,在主线程上调用onReceived回调。听起来你也在主线程上调用waitConnection()。由于waitConnection()阻塞,主线程在waitConnection()返回之前不能处理任何附加消息。这意味着在onReceived超时之前不会调用waitConnection()

阻止主线程通常是个坏主意。在这里读

相反,您可以让onReceive启动一个新的活动,然后做您需要做的任何事情,一旦您获得USB许可。这也许是最好的解决方案,也可能不是最好的解决方案,但无论如何,这里的关键是永远不要阻塞主线程。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29854748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档