0

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);
              }
        
          };
    
        }

.
Adam DOGLA
  • 23
  • 2
  • Pleaase hare what you've done so far. Stackoverflow is here to help but not to create the whole application for you. – Kozmotronik Nov 14 '22 at 06:23
  • Have you tried following the [Android BLE Guide](https://developer.android.com/guide/topics/connectivity/bluetooth/transfer-ble-data#java)? – Michael Kotzjan Nov 14 '22 at 09:54
  • thx for answer .i try to make this code by by Ariel Malka --> https://stackoverflow.com/questions/64215063/android-ble-readcharacteristic-is-not-working and this is the code: – Adam DOGLA Nov 14 '22 at 10:47
  • @AdamDOGLA the code Ariel used is not working as he stated in his question. Please try to follow the Android guide I linked above – Michael Kotzjan Nov 15 '22 at 08:48
  • @Michael Kotzjanhi .thx for your answer. i edit the question im able to connect to the sensor and enable the notify but i dont know how to read the value plz some solution. – Adam DOGLA Nov 15 '22 at 13:20

1 Answers1

0

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:

  1. Enable notification handling on your Android phone:
gatt.setCharacteristicNotification(characteristicNotifi, true);
  1. Enable notifications on your BLE device for every characteristic you want:
// 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.

Michael Kotzjan
  • 2,093
  • 2
  • 14
  • 23