android - Unhandled exception in callback -
i developing android app need connect ble medical device( here blood pressure) , transfer measured bp. upgrading code .i.e., have bluetooth 2.0 code connects bp device successfully, writes , reads values device successfully.
but in ble, able write value (here bp device requires specific value written starting measuring of values) , when trying enable notifications getting null pointer exception.
code writecharacteristic method:
public boolean writecharacteristic(bluetoothgatt mbluetoothgatt) throws interruptedexception { if (mbluetoothadapter == null ){ log.w(tag, "bluetoothadapter not initialized"); return false; } if (mbluetoothgatt == null ){ log.w(tag, "bluetoothgatt not initialized"); return false; } //bluetoothgattservice service = mbluetoothgatt.getservice(config_descriptor); bluetoothgattservice service = mbluetoothgatt.getservice(my_uuid); if (service == null){ log.e(tag, "service not found!"); return false; } bluetoothgattcharacteristic characteristic = service.getcharacteristic(my_uuid_characteristic); if (characteristic == null){ log.e(tag, "characteristic not found!"); return false; } //int counter = 3; boolean status = false; byte[] value = {(byte)0x0a}; characteristic.setvalue(value); //enable local notifications mbluetoothgatt.setcharacteristicnotification(characteristic, true); //enabled remote notifications bluetoothgattdescriptor desc = characteristic.getdescriptor(config_descriptor); desc.setvalue(bluetoothgattdescriptor.enable_notification_value); mbluetoothgatt.writedescriptor(desc); status = mbluetoothgatt.writecharacteristic(characteristic); log.e(tag, "service :"+status); return status; }
log:-
11-01 23:39:32.289 1220-1255/com.example.sagarch.bluetooth_test_4 d/bluetoothgatt﹕ setcharacteristicnotification() - uuid: 0000fff2-0000-1000-8000-00805f9b34fb enable: true 11-01 23:39:32.289 1220-1220/com.example.sagarch.bluetooth_test_4 e/viewrootimpl﹕ senduseractionevent() mview == null 11-01 23:39:32.289 1220-1255/com.example.sagarch.bluetooth_test_4 w/bluetoothgatt﹕ unhandled exception in callback java.lang.nullpointerexception @ com.example.sagarch.bluetooth_test_4.pollingtest$3.writecharacteristic(pollingtest.java:451) @ com.example.sagarch.bluetooth_test_4.pollingtest$3.onservicesdiscovered(pollingtest.java:359) @ android.bluetooth.bluetoothgatt$1.onsearchcomplete(bluetoothgatt.java:299) @ android.bluetooth.ibluetoothgattcallback$stub.ontransact(ibluetoothgattcallback.java:215) @ android.os.binder.exectransact(binder.java:404) @ dalvik.system.nativestart.run(native method) 11-01 23:39:32.369 1220-1220/com.example.sagarch.bluetooth_test_4 d/progressbar﹕ updatedrawablebounds: left = 0 11-01 23:39:32.369 1220-1220/com.example.sagarch.bluetooth_test_4 d/progressbar﹕ updatedrawablebounds: top = 0 11-01 23:39:32.369 1220-1220/com.example.sagarch.bluetooth_test_4 d/progressbar﹕ updatedrawablebounds: right = 48 11-01 23:39:32.369 1220-1220/com.example.sagarch.bluetooth_test_4 d/progressbar﹕ updatedrawablebounds: bottom = 48 11-01 23:39:43.239 1220-1255/com.example.sagarch.bluetooth_test_4 d/bluetoothgatt﹕ onclientconnectionstate() - status=0 clientif=5 device=8c:de:52:2c:65:b2 11-01 23:39:43.239 1220-1255/com.example.sagarch.bluetooth_test_4 d/tag﹕ connection state change: 0 -> disconnected 11-01 23:39:43.239 1220-1255/com.example.sagarch.bluetooth_test_4 d/tag﹕ connection state change: 0 -> disconnected 11-01 23:39:43.239 1220-1255/com.example.sagarch.bluetooth_test_4 w/bluetoothgatt﹕ unhandled exception in callback android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views. @ android.view.viewrootimpl.checkthread(viewrootimpl.java:7099) @ android.view.viewrootimpl.requestlayout(viewrootimpl.java:1076) @ android.view.view.requestlayout(view.java:17449) @ android.view.view.setflags(view.java:9749) @ android.view.view.setvisibility(view.java:6298) @ android.app.dialog.hide(dialog.java:302) @ com.example.sagarch.bluetooth_test_4.pollingtest$3.onconnectionstatechange(pollingtest.java:322) @ android.bluetooth.bluetoothgatt$1.onclientconnectionstate(bluetoothgatt.java:172) @ android.bluetooth.ibluetoothgattcallback$stub.ontransact(ibluetoothgattcallback.java:71) @ android.os.binder.exectransact(binder.java:404) @ dalvik.system.nativestart.run(native method) 11-01 23:41:32.579 1220-1220/com.example.sagarch.bluetooth_test_4 d/bluetoothadapter﹕ stoplescan() 11-01 23:41:32.589 1220-1220/com.example.sagarch.bluetooth_test_4 d/bluetoothgatt﹕ close() 11-01 23:41:32.589 1220-1220/com.example.sagarch.bluetooth_test_4 d/bluetoothgatt﹕ unregisterapp() - mclientif=5
i trying enable notifications public uuid config_descriptor = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb");
. there problem uuid or there else wrong code. please me. getting nullpointer exception
thank people.
edit:
writecharacterisitic called onservicediscovered()
public void onservicesdiscovered(bluetoothgatt gatt, int status) { //mhandler.sendmessage(message.obtain(null, msg_progress, "enabling sensors...")); /* * services discovered, going reset our state machine , start * working through sensors need enable */ if (status == bluetoothgatt.gatt_success) { try { serviceslist = gatt.getservices(); (bluetoothgattservice service : serviceslist) { list<bluetoothgattcharacteristic> characteristics = service.getcharacteristics(); } writecharacteristic(gatt); } catch (interruptedexception e) { e.printstacktrace(); } } else { log.w(tag, "onservicesdiscovered received: " + status); } }
change code one:
public void onservicesdiscovered(bluetoothgatt gatt, int status) { //mhandler.sendmessage(message.obtain(null, msg_progress, "enabling sensors...")); /* * services discovered, going reset our state machine , start * working through sensors need enable */ if (status == bluetoothgatt.gatt_success) { try { serviceslist = gatt.getservices(); (bluetoothgattservice service : serviceslist) { list<bluetoothgattcharacteristic> characteristics = service.getcharacteristics(); } runonuithread(new runnable() { @override public void run() { writecharacteristic(gatt); } }); } catch (interruptedexception e) { e.printstacktrace(); } } else { log.w(tag, "onservicesdiscovered received: " + status); } }
Comments
Post a Comment