android - Capture bluetooth upon paired state -


i have listviewwhich display list of paired bluetooth devices. wanna capture state inorder update paired listview whenever new request pair device gets accepted.

so far have tried listening bluetoothdevice.action_acl_connected no avail.

 protected void oncreate(bundle savedinstancestate) {         ...         intentfilter filter1 = new intentfilter(bluetoothdevice.action_acl_connected);         this.registerreceiver(mreceiver, filter1);      }   //the broadcastreceiver listens bluetooth broadcasts     private final broadcastreceiver mreceiver = new broadcastreceiver() {         @override         public void onreceive(context context, intent intent) {             string action = intent.getaction();             bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device);              if (bluetoothdevice.action_acl_connected.equals(action)) {                 getpaireddevices(); // paired devices , update listview              } } 

first, scan pair , unpair device, can code

public void scandevices(){     intentfilter filter = new intentfilter();      filter.addaction(bluetoothdevice.action_found);     filter.addaction(bluetoothadapter.action_state_changed);     filter.addaction(bluetoothadapter.action_discovery_finished);      activity.registerreceiver(mreceiverscan, filter);     bluetoothadapter.startdiscovery(); }  public void pair(bluetoothdevice device){     activity.registerreceiver(mpairreceiver, new intentfilter(bluetoothdevice.action_bond_state_changed));     devicepair=device;     try {         method method = device.getclass().getmethod("createbond", (class[]) null);         method.invoke(device, (object[]) null);     } catch (exception e) {         if(discoverycallback!=null)             discoverycallback.onerror(e.getmessage());     } }  public void unpair(bluetoothdevice device) {     devicepair=device;     try {         method method = device.getclass().getmethod("removebond", (class[]) null);         method.invoke(device, (object[]) null);     } catch (exception e) {         if(discoverycallback!=null)             discoverycallback.onerror(e.getmessage());     } }  private broadcastreceiver mreceiverscan = new broadcastreceiver() {     @override     public void onreceive(context context, intent intent) {         string action = intent.getaction();          switch (action) {             case bluetoothadapter.action_state_changed:                 final int state = intent.getintextra(bluetoothadapter.extra_state, bluetoothadapter.error);                 if (state == bluetoothadapter.state_off) {                     if (discoverycallback != null)                         discoverycallback.onerror("bluetooth turned off");                 }                 break;             case bluetoothadapter.action_discovery_finished:                 context.unregisterreceiver(mreceiverscan);                 if (discoverycallback != null)                     discoverycallback.onfinish();                 break;             case bluetoothdevice.action_found:                 bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device);                 if (discoverycallback != null)                     discoverycallback.ondevice(device);                 break;         }     } };  private final broadcastreceiver mpairreceiver = new broadcastreceiver() {     public void onreceive(context context, intent intent) {         string action = intent.getaction();          if (bluetoothdevice.action_bond_state_changed.equals(action)) {             final int state = intent.getintextra(bluetoothdevice.extra_bond_state, bluetoothdevice.error);             final int prevstate = intent.getintextra(bluetoothdevice.extra_previous_bond_state, bluetoothdevice.error);              if (state == bluetoothdevice.bond_bonded && prevstate == bluetoothdevice.bond_bonding) {                 if(discoverycallback!=null)                     discoverycallback.onpair(devicepair);             } else if (state == bluetoothdevice.bond_none && prevstate == bluetoothdevice.bond_bonded){                 if(discoverycallback!=null)                     discoverycallback.onunpair(devicepair);             }         }     } }; 

finally, can add these device list as

public list<bluetoothdevice> getpaireddevices(){     list<bluetoothdevice> devices = new arraylist<>();     (bluetoothdevice bluedevice : bluetoothadapter.getbondeddevices()) {         devices.add(bluedevice);     }     return devices; } 

don't forget add permission in androidmanifet.xml file

<uses-permission android:name="android.permission.bluetooth" /> <uses-permission android:name="android.permission.bluetooth_admin" /> 

this library used

https://github.com/omaflak/bluetooth-library

and sample app use above lib.

https://github.com/omaflak/bluetooth-android


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -