bluetooth lowenergy - Android BLE BluetoothGatt object remains connected and triggers onCharacteristicChanged() even after a call to disconnect() -
i trying disconnect characteristic notifications exiting app. here how doing in exitcleanup() function:
if (btgatt != null && mwritechar != null) { boolean b=btgatt.setcharacteristicnotification(mwritechar, false); log.w("appinfo", "exiting , unsubscribing: " + b); }
the log shows: exiting , unsubscribing: true
. far good. attempt disconnect gatt object altogether using following:
if (btgatt != null && btmanager!=null && btmanager.getconnectionstate(btdevice, bluetoothprofile.gatt) != bluetoothprofile.state_disconnected ) { //making sure gatt , bt manager still //also making sure connection state not disconnected btgatt.disconnect(); btgatt.close(); log.w( "appinfo", "finishing. connection state=" + btmanager.getconnectionstate(btdevice, bluetoothprofile.gatt) ); }
this things weird. log displays following: finishing. connection state=2
, indicating bluetoothdevice still connected.
this problem, because when app finishes , destroys assets, bluetoothgattcallback still continues receive notifications behind scenes. first throws following nullpointerexception:
04-25 22:49:54.392 17280-17293/com.myapp.appinfo d/bluetoothgatt﹕ onclientconnectionstate() - status=0 clientif=8 device=54:4a:16:26:a1:b5 04-25 22:49:54.392 17280-17293/com.myapp.appinfo w/bluetoothgatt﹕ unhandled exception in callback java.lang.nullpointerexception @ android.bluetooth.bluetoothgatt$1.onclientconnectionstate(bluetoothgatt.java:168) @ android.bluetooth.ibluetoothgattcallback$stub.ontransact(ibluetoothgattcallback.java:71) @ android.os.binder.exectransact(binder.java:404) @ dalvik.system.nativestart.run(native method) 04-25 22:49:54.402 17280-17280/com.myapp.appinfo d/bluetoothmanager﹕ getconnectionstate()
and continues publish onnotify() calls trigger oncharacteristicchanged() calls after app has terminated awile ago:
d/bluetoothgatt﹕ onnotify() - device=54:4a:16:26:a1:b5 uuid=0000ffe1-0000-1000-8000-00805f9b34fb
any advice on how disconnect gatt characteristic notifications when exiting app?
it seems can't call these method together. disconnection callback might arrive later close() method performed.
you can add mgatt.close(); onconnectionstatechange callback close connection.
Comments
Post a Comment