I Try to devlope android app to read from cycling sensor ,
if anyone can help plz . its connect to the sensor but cant read the characteristic.
i dont know how to read the characterestic value . its a speed sensor sor i want to be notify or read (i dont know the exact its read or notify)
///////////////////////////LOGCAT////////////////////////////
onScanResult() - ScanResult{mDevice=F7:88:0B:34:04:5F
onScanResult: F7:88:0B:34:04:5F:35007-2
connect() - device: F7:88:0B:34:04:5F, auto: false
registerApp()
registerApp() - UUID=b71dffd9-b87e-4ac9-9985-28ad9934745b
onClientRegistered() - status=0 clientIf=6
onClientConnectionState() - status=0 clientIf=6 device=F7:88:0B:34:04:5F
discoverServices() - device: F7:88:0B:34:04:5
onSearchComplete() = Device=F7:88:0B:34:04:5F Status=0
//////////////////////LIST ALL SERVISES/////////////////////
setCharacteristicNotification() - uuid: 00002a5b-0000-1000-8000-00805f9b34fb enable: true
this is the code ;
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
ScanningEnd = false;
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
List<BluetoothGattService> services = gatt.getServices();
Log.e("onServicesDiscovered", "Services count: "+services.size());
for (BluetoothGattService service : gatt.getServices()) {
Log.d(TAG, "Found Service " + service.getUuid().toString());
for(BluetoothGattCharacteristic mcharacteristic :service.getCharacteristics())
{
Log.d(TAG, "Found Characteristic " + mcharacteristic.getUuid().toString());}}
characteristicNotifi = gatt.getService(NOTIF_SERVICE)
.getCharacteristic(NOTIF_CHARACTERESTIC);
gatt.setCharacteristicNotification(characteristicNotifi, true);
gatt.readCharacteristic(characteristicNotifi);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
if (characteristic.equals(characteristicNotifi)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "value changed", Toast.LENGTH_LONG);
}});}}};
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
}
};
}
.
See this answer for how to subscribe to notifications correctly using Android.
You need to do two steps to activate notifications on both the Android phone and the remote BLE device:
gatt.setCharacteristicNotification(characteristicNotifi, true);
// 0x2902 org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor descriptor = characteristicNotifi.getDescriptor(uuid);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
These steps are also described in the Android BLE Guide, but the second step is a little bit obscured, unfortunately.
This should be enough to get a callback on your onCharacteristicChanged
.