android - onCharacteristicWrite() is being called, but it doesn't always write -


i have custom piece of hardware bluetooth low energy chip. have set array 500 u32s such array[n] == n. i'm working on android app can connect device, request length of array, , request datapoints in array 1 @ time.

the android app appears working fine. connects device, requests length, , continues request next piece of data after previous piece received. however, partway through array(anywhere 2 450 elements in - appears inconsistent), write command, , make way oncharacteristicwrite(), never receives response. have ble peripheral hooked coolterm, , never receives command. here snippets code , logs:

bleservice:

 private final bluetoothgattcallback blegattcallback = new bluetoothgattcallback() {      @override     public void oncharacteristicread(bluetoothgatt gatt, bluetoothgattcharacteristic characteristic, int status) {         super.oncharacteristicread(gatt, characteristic, status);         log.d("oncharacteristicread", bytearrtohex(characteristic.getvalue()));     }      @override     public void oncharacteristicwrite(bluetoothgatt gatt, bluetoothgattcharacteristic characteristic, int status) {         if(status != bluetoothgatt.gatt_success){             log.d("oncharacteristicwrite", "failed write, retrying");             gatt.writecharacteristic(characteristic);         }         log.d("oncharacteristicwrite", bytearrtohex(characteristic.getvalue()));         super.oncharacteristicwrite(gatt, characteristic, status);     }      @override     public void oncharacteristicchanged(bluetoothgatt gatt, bluetoothgattcharacteristic characteristic) {         super.oncharacteristicchanged(gatt, characteristic);         log.d("oncharacteristicchanged", bytearrtohex(characteristic.getvalue()));         broadcastupdate(action_data_available, characteristic);     }  }; 

i have omitted unnecessary parts of callback related descriptor writes, connection state changes, etc. when data broadcasted, received in portion of mainactivity:

private broadcastreceiver messagereceiver = new broadcastreceiver() {     @override     public void onreceive(context context, intent intent) {         string receiveduuid = intent.getstringextra("uuid");         byte[] data = intent.getbytearrayextra("data");         log.d("messagereceiver", "received intent in mainactivity uuid " + receiveduuid.tostring());         if(receiveduuid.equals(read_len_uuid.tostring()) && currentlyreading) {             datapoints = new arraylist<long>();             numberofdatapoints = 0;             numberofdatapoints |= (data[0] & 0xff);             numberofdatapoints |= (data[1] & 0xff) << 8;             numberofdatapoints |= (data[2] & 0xff) << 16;             numberofdatapoints |= (data[3] & 0xff) << 24;             log.d("receiver test:", "number of datapoints = " + numberofdatapoints);              if(numberofdatapoints > 0) {                 bleservice.requestdatapoint(0);             }         } else if (receiveduuid.equals(read_datapoint_uuid.tostring()) && currentlyreading){             long message = 0;             message |= (data[0] & 0xff);             message |= (data[1] & 0xff) << 8;             message |= (data[2] & 0xff) << 16;             message |= (data[3] & 0xff) << 24;              log.d("datapoint recieved", "index " + datapoints.size() + " = " + message);             datapoints.add(message);              if(datapoints.size() < numberofdatapoints){                 bleservice.requestdatapoint(datapoints.size());             }         }     } }; 

the code calls writecharacteristic:

public void requestdatapoint(int index){     log.d("requestdatapoint", "requested datapoint @ " + index);      bluetoothgattcharacteristic commandchar = this.gattservice.getcharacteristic(write_uuid);     byte[] request = new byte[3];      // command - 2 = index     request[0] = (byte) (2 & 0xff);      // index     request[1] = (byte) ((index) & 0xff);     request[2] = (byte) ((index >> 8) & 0xff);     commandchar.setvalue(request);     blegatt.writecharacteristic(commandchar); } 

i'm there isn't issue sending commands quickly. it's incredibly slow, did on purpose more test things before moving on next portion of project.

an snippet 1 of debug logs:

08-23 12:08:18.470 16753-16753/sethp.datalogcollector d/requestdatapoint: requested datapoint @ 49 08-23 12:08:18.570 16753-16765/sethp.datalogcollector d/oncharacteristicwrite: 02 31 00  08-23 12:08:18.570 16753-16765/sethp.datalogcollector d/oncharacteristicchanged: 31 00 00 00  08-23 12:08:18.570 16753-16765/sethp.datalogcollector d/bleservice: characteristic found. uuid: 00020000-5f5f-4a49-4847-464544434241 08-23 12:08:18.575 16753-16753/sethp.datalogcollector d/messagereceiver: received intent in mainactivity uuid 00020000-5f5f-4a49-4847-464544434241 08-23 12:08:18.575 16753-16753/sethp.datalogcollector d/datapoint recieved: index 49 = 49 08-23 12:08:18.575 16753-16753/sethp.datalogcollector d/requestdatapoint: requested datapoint @ 50 08-23 12:05:55.585 16753-16765/sethp.datalogcollector d/oncharacteristicwrite: 02 32 00  08-23 12:05:55.585 16753-16765/sethp.datalogcollector d/oncharacteristicchanged: 32 00 00 00  08-23 12:05:55.585 16753-16765/sethp.datalogcollector d/bleservice: characteristic found. uuid: 00020000-5f5f-4a49-4847-464544434241 08-23 12:05:55.585 16753-16753/sethp.datalogcollector d/messagereceiver: received intent in mainactivity uuid 00020000-5f5f-4a49-4847-464544434241 08-23 12:05:55.590 16753-16753/sethp.datalogcollector d/datapoint recieved: index 50 = 50 08-23 12:05:55.590 16753-16753/sethp.datalogcollector d/requestdatapoint: requested datapoint @ 51 08-23 12:05:55.680 16753-16845/sethp.datalogcollector d/oncharacteristicwrite: 02 33 00  08-23 12:05:55.685 16753-16764/sethp.datalogcollector d/oncharacteristicchanged: 33 00 00 00  08-23 12:05:55.685 16753-16764/sethp.datalogcollector d/bleservice: characteristic found. uuid: 00020000-5f5f-4a49-4847-464544434241 08-23 12:05:55.685 16753-16753/sethp.datalogcollector d/messagereceiver: received intent in mainactivity uuid 00020000-5f5f-4a49-4847-464544434241 08-23 12:05:55.685 16753-16753/sethp.datalogcollector d/datapoint recieved: index 51 = 51 08-23 12:05:55.685 16753-16753/sethp.datalogcollector d/requestdatapoint: requested datapoint @ 52 08-23 12:05:55.785 16753-16765/sethp.datalogcollector d/oncharacteristicchanged: 34 00 00 00  08-23 12:05:55.785 16753-16765/sethp.datalogcollector d/bleservice: characteristic found. uuid: 00020000-5f5f-4a49-4847-464544434241 08-23 12:05:55.785 16753-16753/sethp.datalogcollector d/messagereceiver: received intent in mainactivity uuid 00020000-5f5f-4a49-4847-464544434241 08-23 12:05:55.785 16753-16753/sethp.datalogcollector d/datapoint recieved: index 52 = 52 08-23 12:05:55.785 16753-16753/sethp.datalogcollector d/requestdatapoint: requested datapoint @ 53 08-23 12:05:55.790 16753-16765/sethp.datalogcollector d/oncharacteristicwrite: 02 35 00  

and corresponding coolterm log snippet:

command: 02 index: 0031 command = 2 datapoint @ 49 = 49 attempting send 49  command: 02 index: 0032 command = 2 datapoint @ 50 = 50 attempting send 50  command: 02 index: 0033 command = 2 datapoint @ 51 = 51 attempting send 51  command: 02 index: 0034 command = 2 datapoint @ 52 = 52 attempting send 52 

note in peripheral log, doesn't appear receive request datapoint 53. reference, first hex byte in oncharacteristicwrite debug command. command 02 means requesting datapoint @ index of whatever next 2 bytes contain.

i have noticed in android log, there isn't oncharacteristicwrite log requesting datapoint 51. seem happens every time right before stops getting data, i'm not sure if significant or if it's issue log buffer.

i have run quite few tests trying notice patterns, , have noticed seems more datapoints when device not connected debug cable. thought @ point maybe have issue asynchronous interrupting callback, don't know doing that. have thoughts why doesn't seem write data after oncharacteristicwrite called?

thanks

edit:

i followed emil's suggestion , turned on bluetooth logging. played around wireshark , figured out going on. tried out app again, , ran index 102 until stopped, @ point disconnected device. dug through packets in wireshark, , found device did receive data 102, did not send request 103. double checked android log, , log statement inside oncharacteristicwrite said sent command 02 67 00, request 103. so, appears oncharacteristicwrite being called, characteristic not being written.

after more staring , thinking, quite either 1) oncharacteristicwrite being called improperly, because data never being written, or 2) somehow, asynchronous interrupting , stopping transmitting. have no idea doing this.

final edit:

even though, far understand spec, oncharacteristicwrite supposed called when reliable, successful write in progress, decided check return value writecharacteristic. should have checked hours ago. , know, returning false on last request.

i think fact oncharacteristicwrite called though return false bug. i've read safe use oncharacteristicwrite call write next chunk of data. either wrong, or screwy going on here. either way, guess it's pretty idea check function call return values.

now think see it. notification arrives before onwritecharacteristic callback. since issue next write in notification callback, previous write still pending. must make sure onwritecharacteristic has been called before issue next write.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -