java - Android: connecting to USB device, wait for permission -


i'm trying this: connect usb device , opened (or failed) connection. did logic according examples , explanations have found, have problem waiting permission grant. first tried "good" way of using wait()+notifyall(), tried straightforward loop checks, both times waiting method (waitconnection()) blocking timeout gave it, , after message received. tried these 2 versions.

  1. wait/notifyall:

    public usbconnector startconnection(context context) {  broadcastreceiver receiver = new broadcastreceiver() {     public void onreceive(context context, intent intent) {         string action = intent.getaction();         if (action_usb_permission.equals(action)) {             synchronized (syncobj) {                 if (intent.getbooleanextra(usbmanager.extra_permission_granted, false)) {                     usbdevice device = intent.getparcelableextra(usbmanager.extra_device);                     if (device != null) {                         if (device.getvendorid() == vendorid && device.getproductid() == productid) {                             connection = usbmanager.opendevice(device);                             connecteddevice = device;                         }                     }                 }                 syncobj.notyfyall();             }         }     } };  try {     usbmanager = (usbmanager) context.getsystemservice(context.usb_service);     (final usbdevice device : usbmanager.getdevicelist().values()) {         if (device.getvendorid() == this.vendorid && device.getproductid() == this.productid) {              context.registerreceiver(receiver, new intentfilter(action_usb_permission));             usbmanager.requestpermission(device,                     pendingintent.getbroadcast(context, 0, new intent(action_usb_permission), 0));             break;         }     } } catch (exception e) { } return this; }  public usbdeviceconnection waitconnection(int timeout) { try {     thread.sleep(10, 0);     syncobj.wait(timeout); } catch (interruptedexception e) { } return getconnection(); } 
  2. straightforward loop

    public usbconnector startconnection(context context) {  broadcastreceiver receiver = new broadcastreceiver() {     public void onreceive(context context, intent intent) {         string action = intent.getaction();         if (action_usb_permission.equals(action)) {             synchronized (this) {                 if (intent.getbooleanextra(usbmanager.extra_permission_granted, false)) {                     usbdevice device = intent.getparcelableextra(usbmanager.extra_device);                     if (device != null) {                         if (device.getvendorid() == vendorid && device.getproductid() == productid) {                             connection = usbmanager.opendevice(device);                             connecteddevice = device;                         }                     }                 }                 permissionrequested = false;             }         }     } };  try {     permissionrequested = false;     usbmanager = (usbmanager) context.getsystemservice(context.usb_service);     (final usbdevice device : 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(context, 0, new intent(action_usb_permission), 0));             break;         }     } } catch (exception e) { } return this; }  public usbdeviceconnection waitconnection(int timeout) { int waited = timeout; while (permissionrequested && waited > 0) {     try {         thread.sleep(10, 0);     } catch (interruptedexception e) {     }     waited -= 10; }  return getconnection(); } 

so in both cases, according logs, waitconnection() method (that called consumer after startconnection()) seems block execution (i gave timeout 10 seconds, , blocked 10 seconds), , right after it's completed, broadcastreceiver gets message. appears requestpermission() not async (as thought is), in case, how possible startconnection() exits , before message received? , how can wait broadcastreceiver message? if don't use waitconnection() method, how consumer should know moment when can start checking connection availability?

"and right after it's completed, broadcastreceiver gets message"

the onreceived callback, default, called on main thread. sounds calling waitconnection() on main thread well. since waitconnection() blocks, main thread cannot process additional messages until waitconnection() returns. means onreceived not called until waitconnection() times out.

it bad idea block main thread. read here

instead, have onreceive launch new activity whatever need once usb permission. may or may not best solution you, regardless, key here never block main thread.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -