android - BLE - i am not able to read 2 characteristic one of temperature service and another of Battery service at same time -
this code discovering services , characteristic , setting descriptor both temperature , battery characteristic.
in starting discovering services temperature , battery. then,i discover characterstic each temperature , battery service , write descriptor both.
when run code,the call going oncharactersticchanged , got temperature result. but,call not going on oncharactersticread battery
(bluetoothgattservice service : services) { log.e("asd service discoverd", service.getuuid().tostring()); // check service should temperature service or battery service if (service.getuuid().equals(bt_thermo_service) || service.getuuid().equals(bt_battery_service)) { log.e("asd service discoverd", service.getuuid().tostring()); list<bluetoothgattcharacteristic> characteristics = service.getcharacteristics(); // // create compartor // sort list cpmparor // addd in queue // read same (bluetoothgattcharacteristic characteristic : characteristics) { log.e("asd charac discoverd:", characteristic.getuuid().tostring()); if (characteristic.getuuid().equals(bt_real_time_temperature_charteristics) || characteristic.getuuid().equals(bt_batery_level_characteristics)) { log.e("asd charac discoverd:", characteristic.getuuid().tostring()); arraylist.add(characteristic); // check if characterstic realtime temperature measurement characterstic or battery level characterstic if (characteristic.getuuid().equals(bt_real_time_temperature_charteristics)) { //indicate ble send temperature data each time when new data value found //notify ble device send data gatt.setcharacteristicnotification(characteristic, true); (bluetoothgattdescriptor descriptor : characteristic.getdescriptors()) { descriptor.setvalue(bluetoothgattdescriptor.enable_indication_value); gatt.writedescriptor(descriptor); } } else if (characteristic.getuuid().equals(bt_batery_level_characteristics)) { //notify ble device send data gatt.readcharacteristic(characteristic); (bluetoothgattdescriptor descriptor : characteristic.getdescriptors()) { descriptor.setvalue(bluetoothgattdescriptor.enable_notification_value); gatt.writedescriptor(descriptor); } } } } }
these gattcallback methods.
@override public void oncharacteristicread(bluetoothgatt gatt, bluetoothgattcharacteristic characteristic, int status) { super.oncharacteristicread(gatt, characteristic, status); log.e("charvalu", "" + characteristic); if (characteristic.getuuid().equals(bt_batery_level_characteristics)) { byte b[] = characteristic.getvalue(); if (b.length != 0) { log.e("battery", integer.tostring(b.length)); // bytebuffer batterybuffer = bytebuffer.wrap(b); // long batterystatus = batterybuffer.getlong(); // log.e("battery","" + batterystatus); } } } @override public void oncharacteristicchanged(bluetoothgatt gatt, bluetoothgattcharacteristic characteristic) { super.oncharacteristicchanged(gatt, characteristic); log.e("inside char", "" + characteristic); if (characteristic != null) { log.e("on change char", characteristic.getuuid().tostring()); if (characteristic.getuuid().equals(bt_real_time_temperature_charteristics)) { log.e("on change inside", characteristic.getuuid().tostring()); //temperature data comes in byte array of size 12 byte b[] = characteristic.getvalue(); if (b.length != 0) { //check header , tail of data packet if (b[0] == 0x7c && b[11] == 0x7d) { //temp reading stored in 7 , 8 byte bytebuffer tempbuffer = bytebuffer.wrap(b, 7, 2); tempbuffer.order(byteorder.little_endian); short temp = tempbuffer.getshort(); final float ftemp = (float) (temp / 100.0); log.e("sunittemp", float.tostring(ftemp)); runonuithread(new thread() { @override public void run() { super.run(); workinglistener.unstablereading(new ivythermoreading(ftemp)); } }); } } } } }
unfortunately sending read , write requests @ once synchronously not work since android allows 1 pending gatt operation @ time. must somehow enqueue work , continue sending request once callback of previous request arrives.
Comments
Post a Comment