Reference connection: http://www.codes51.com/article/detail_4021799.html
Problem description recently developed on Android BLE read/write Data
However, writing data to the device is smooth, but it is not acceptable to receive data from the device.
/** * Enables or disables notification on a give characteristic. * * @param characteristic characteristic to act on. * @param enabled If True, enable notification. False otherwise. * /public void setcharacteristicnotification (bluetoothgattcharacteristic characteristic, Boolean enabled) { if (Mbluetoothadapter = = NULL | | mbluetoothgatt = = NULL) { LOG.W (TAG, "Bluetoothadapter not Initialized");
return; } Mbluetoothgatt.setcharacteristicnotification (characteristic, enabled); }
The above-mentioned is not receiving data, the main is to add less mbluetoothgatt.writedescriptor (descriptor);
The following is the modified code
/** * Enables or disables notification on a give characteristic. * * @param characteristic characteristic to act on. * @param enabled IF True, enable notification. False otherwise. */public void setcharacteristicnotification (bluetoothgattcharacteristic characteristic, Boolean enabled) {if (Mbluetoothadapter = = NULL | | mbluetoothgatt = = NULL) {LOG.W (TAG, "Bluetoothadapter not initialized"); Return }//mbluetoothgatt.setcharacteristicnotification (characteristic, enabled);Boolean isenablenotification = mbluetoothgatt.setcharacteristicnotification (characteristic, enabled); if (isenablenotification) {list<bluetoothgattdescriptor> descriptorlist = characteristic.getdescriptors ( ); if (descriptorlist! = null && descriptorlist.size () > 0) {for (Bluetoothgattdescriptor descriptor : descriptorlist) {descriptor.setvalue (bluetoothgattdescriptor.enable_notification_value); Mbluetoothgatt.writedescriptor (descriptor); } } }}
If that's not the case, it's another possibility. The characteristic UUID setting is incorrect
You can look at the Boolean isenablenotification = Mbluetoothgatt.setcharacteristicnotification (characteristic, enabled);
Whether the return is false or true,
In addition, we will write an article about the Bluetooth data read and write
The above describes the Android BLE setcharacteristicnotification receive no data, including the content, I hope to be interested in Android development friends helpful.
This article site link: http://www.codes51.com/article/detail_4021799.html
Android BLE setcharacteristicnotification receive no data