Reputation: 1
The problem is "how can my code dont trigger the OnCharacteristicChanged...i set the notification to true ma the only result that i have is this:
Connected to GATT server.
Service : android.bluetooth.BluetoothGattService@bf8aa7
Qui entra, service non nullo
Found characteristic: 00005303-0000-0041-4c50-574953450000
Characteristic supports notifications.
Found characteristic: 00005302-0000-0041-4c50-574953450000
Characteristic does NOT support notifications or indications.
Characteristic for notifications found and notifications set.
Descriptor for notification written successfully.
onDescriptorWrite :Sucess
Disconnected from GATT server, status: 8
this is my code:
public CompletableFuture<Void> connect(String deviceAddress)
{
CompletableFuture<Void> future = new CompletableFuture<>();
BluetoothManager bluetoothManager = getSystemService(BluetoothManager.class);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
device = bluetoothAdapter.getRemoteDevice(deviceAddress);
gatt = device.connectGatt(null, false, new BluetoothGattCallback()
{
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
{
if (newState == BluetoothGatt.STATE_CONNECTED) {
BA.Log("Connected to GATT server.");
gatt.discoverServices();
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
BA.Log("Disconnected from GATT server, status: " + status);
future.complete(null);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService service = gatt.getService(UUID.fromString(SERVICE_UUID));
BA.Log("Service : " + service);
//attualmente stampa Service : android.bluetooth.BluetoothGattService@bf8aa7 con 00005303-0000-0041-4c50-574953450000
//attualmente stampa Service : android.bluetooth.BluetoothGattService@ad02a43 con 00002902-0000-1000-8000-00805f9b34fb
if (service != null)
{
BA.Log("Qui entra, service non nullo");
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID_NOTIFY));
if (characteristic != null)
{
gatt.setCharacteristicNotification(characteristic, true);
startReadBlock = true;
BA.Log("Characteristic for notifications found and notifications set.");
//Before onCharacteristicChanged is called you had to enable notification. Someting like:
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
BA.Log("Descriptor for notification written successfully.");
}else
{
BA.Log("Notification characteristic not found.");
}
}
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
{
byte[] data = characteristic.getValue();
BA.Log("Received HandleRX: " + bytesToHex(data));
if (startReadBlock)
{
BA.Log("START READDATABLOCK");
readDataBlock(data);
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
BA.Log("Characteristic written successfully.");
} else {
BA.Log("Error writing characteristic: " + status);
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
BA.Log("onDescriptorWrite :" + ((status == BluetoothGatt.GATT_SUCCESS) ? "Sucess" : "false"));
}
});
return future;
}
i stay for hour to add log for trying to step with code but nothing....are days that i am in this problem
Upvotes: 0
Views: 45