Notification Data from sensor device BLE Andorid application

Hello,

How to use the nordic android ble library, I have implemented it but only once when on the road can receive data. From the BLE device every minute sends data.

I tried to read data through the nrf connect application the results are as follows:


Do the read characteristic and disable notification symbols have an effect like in https://github.com/NordicSemiconductor/Android-nRF-Connect/tree/main/documentation

my code snippet is as follows:

public class MyBleManager extends BleManager {
private static final String TAG = MyBleManager.class.getSimpleName();

private BluetoothGattCharacteristic targetCharacteristic;

public MyBleManager(@NonNull Context context) {
super(context);
}

@NonNull
@Override
protected BleManagerGattCallback getGattCallback() {
return new MyBleManagerGattCallback();
}

private class MyBleManagerGattCallback extends BleManagerGattCallback {
@Override
protected void initialize() {
super.initialize();
Log.d(TAG, "Initializing BLE connection...");
requestMtu(50)
.done((device -> Log.d(TAG, "initialize: " + device.getAddress())))
.fail(((device, status) -> Log.d(TAG, "initialize: " + device + " :" + status)))
.enqueue();
if (targetCharacteristic != null) {

setNotificationCallback(targetCharacteristic)
.with((device, data) -> {
String value = data.getStringValue(0);
Log.d(TAG, "onDataReceived: " + value);
});

enableNotifications(targetCharacteristic)
.done(device -> Log.d(TAG, "Notifications enabled on: " + targetCharacteristic.getUuid()))
.fail((device, status) -> Log.e(TAG, "Failed to enable notifications, status: " + status))
.enqueue();

readCharacteristic(targetCharacteristic)
.with((device, data) -> {
byte[] dataValue = data.getValue();
Log.d(TAG, "initialize data: " + data.toString());
})
.fail((device, status) -> Log.e(TAG, "Failed to read characteristic, status: " + status))
.enqueue();
} else {
Log.w(TAG, "Target characteristic is null, cannot initialize notifications.");
}
}

@Override
protected boolean isRequiredServiceSupported(@NonNull BluetoothGatt gatt) {
Log.d(TAG, "Discovering services...");
List<BluetoothGattService> services = gatt.getServices();

for (BluetoothGattService service : services) {
Log.d(TAG, "Found service: " + service.getUuid());
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
Log.d(TAG, "Found characteristic: " + characteristic.getUuid());

if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
Log.d(TAG, "Characteristic supports READ: " + characteristic.getUuid());
}
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
Log.d(TAG, "Characteristic supports NOTIFY: " + characteristic.getUuid());
targetCharacteristic = characteristic;
}
}
}

boolean isSupported = targetCharacteristic != null;
Log.d(TAG, "Required service supported: " + isSupported);
return isSupported;
}

@Override
protected void onServicesInvalidated() {
Log.d(TAG, "Services invalidated. Cleaning up...");
targetCharacteristic = null;
}

}

int toInt(byte b1, byte b2) {
return ((b1 & 0xFF) << 8) | (b2 & 0xFF);
}

float toFloat(byte b1, byte b2, float scaleFactor) {
int intValue = toInt(b1, b2);
return intValue / scaleFactor;
}
}

For more details, see https://github.com/0d3ng/LemsNordic.

Thank you very much for your response

Related