android - Not able to read a characteristic value from BLE device -
i need write , read characteristic value android ble device. i'm able write not read. here how i'm writing :
public void writecustomcharacteristic(int value) { if (mbluetoothadapter == null || mbluetoothgatt == null) { log.w(tag, "bluetoothadapter not initialized"); return; } /*check if service available on device*/ bluetoothgattservice mcustomservice = mbluetoothgatt.getservice(uuid.fromstring("<my service uuid>")); if(mcustomservice == null){ log.w(tag, "custom ble service not found"); return; } /*get read characteristic service*/ bluetoothgattcharacteristic mwritecharacteristic = mcustomservice.getcharacteristic(uuid.fromstring("<my characterstic uuid>")); mwritecharacteristic.setwritetype(bluetoothgattcharacteristic.write_type_no_response); mwritecharacteristic.setvalue(value,bluetoothgattcharacteristic.format_uint8,0); if(!mbluetoothgatt.writecharacteristic(mwritecharacteristic)){ log.w(tag, "failed write characteristic"); }else{ log.w(tag, "success write characteristic"); } }
and operation success if use
mwritecharacteristic.setwritetype(bluetoothgattcharacteristic.write_type_no_response);
if use other write types rather write_type_no_response
not triggering oncharacteristicwrite()
callback method.
here how i'm reading characteristic :
private boolean readcharacteristics(int groupposition,int childposition){ try{ if (mgattcharacteristics != null) { final bluetoothgattcharacteristic characteristic = mgattcharacteristics.get(2).get(2); final int charaprop = characteristic.getproperties(); if ((charaprop | bluetoothgattcharacteristic.property_read) > 0) { // if there active notification on characteristic, clear // first doesn't update data field on user interface. if (mnotifycharacteristic != null) { mbluetoothleservice.setcharacteristicnotification(mnotifycharacteristic, false); mnotifycharacteristic = null; } mbluetoothleservice.readcharacteristic(characteristic); } if ((charaprop | bluetoothgattcharacteristic.property_notify) > 0) { mnotifycharacteristic = characteristic; mbluetoothleservice.setcharacteristicnotification(characteristic, true); } waitsometime(100); if ((charaprop | bluetoothgattcharacteristic.property_indicate) > 0) { mnotifycharacteristic = characteristic; mbluetoothleservice.setcharacteristicindication(characteristic, true); } waitsometime(100); if ((charaprop | bluetoothgattcharacteristic.property_write) > 0) { byte[] value = characteristic.getvalue(); //this being null if (mnotifycharacteristic != null) { mbluetoothleservice.setcharacteristicnotification(mnotifycharacteristic, true); mnotifycharacteristic = null; } mbluetoothleservice.writecharacteristic(characteristic, value); if(value!=null&&value.length>0) { toast.maketext(devicecontrolactivity.this,"success reading data",toast.length_long).show(); }else{ toast.maketext(devicecontrolactivity.this,"error reading data",toast.length_long).show(); } } /* if ((charaprop | bluetoothgattcharacteristic.property_indicate) > 0) { mnotifycharacteristic = characteristic; //characteristic.setwritetype(); mbluetoothleservice.setcharacteristicindication( characteristic, true); byte[] value=characteristic.getvalue(); }*/ return true; } }catch (exception e){ e.printstacktrace(); } return false; }
characteristic.getvalue()
returns null. i'm going wrong?
Comments
Post a Comment