Hello kai, thanks for directing me here from the Play Store app page.
This particular BLE device I'm working on uses a Telit chipset. This simplified version of your Play Store app does not seem to include the profile for these, however your app on the Play Store works flawlessly.
private static final UUID BLUETOOTH_LE_TELIT_SERVICE = UUID.fromString("0000fefb-0000-1000-8000-00805f9b34fb");
private static final UUID TELIT_DATA_RX = UUID.fromString("00000001-0000-1000-8000-008025000000");
private static final UUID TELIT_DATA_TX = UUID.fromString("00000002-0000-1000-8000-008025000000");
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(TAG, "servicesDiscovered, status " + status);
if (canceled) {
return;
}
writePending = false;
for (BluetoothGattService gattService : gatt.getServices()) {
UUID uuid = gattService.getUuid();
if (uuid.equals(BLUETOOTH_LE_CC254X_SERVICE)) {
Log.d(TAG, "service cc254x uart");
//for(BluetoothGattCharacteristic characteristic : gattService.getCharacteristics())
// Log.d(TAG, "characteristic "+characteristic.getUuid());
readCharacteristic = gattService.getCharacteristic(BLUETOOTH_LE_CC254X_CHAR_RW);
writeCharacteristic = gattService.getCharacteristic(BLUETOOTH_LE_CC254X_CHAR_RW);
} else if (uuid.equals(BLUETOOTH_LE_NRF_SERVICE)) {
Log.d(TAG, "service nrf uart");
//for(BluetoothGattCharacteristic characteristic : gattService.getCharacteristics())
// Log.d(TAG, "characteristic "+characteristic.getUuid());
BluetoothGattCharacteristic rw2 = gattService.getCharacteristic(BLUETOOTH_LE_NRF_CHAR_RW2);
BluetoothGattCharacteristic rw3 = gattService.getCharacteristic(BLUETOOTH_LE_NRF_CHAR_RW3);
if (rw2 != null && rw3 != null) {
int rw2prop = rw2.getProperties();
int rw3prop = rw3.getProperties();
boolean rw2write = (rw2prop & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0;
boolean rw3write = (rw3prop & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0;
Log.d(TAG, "characteristic properties " + rw2prop + "/" + rw3prop);
if (rw2write && rw3write) {
onSerialConnectError(new IOException("multiple write characteristics (" + rw2prop + "/" + rw3prop + ")"));
return;
} else if (rw2write) { // some devices use this ...
writeCharacteristic = rw2;
readCharacteristic = rw3;
} else if (rw3write) { // ... and other devices use this characteristic
writeCharacteristic = rw3;
readCharacteristic = rw2;
} else {
onSerialConnectError(new IOException("no write characteristic (" + rw2prop + "/" + rw3prop));
return;
}
}
} else if (uuid.equals(BLUETOOTH_LE_TELIT_SERVICE)) {
Log.d(TAG, "service cc254x uart");
//for(BluetoothGattCharacteristic characteristic : gattService.getCharacteristics())
// Log.d(TAG, "characteristic "+characteristic.getUuid());
readCharacteristic = gattService.getCharacteristic(TELIT_DATA_RX);
writeCharacteristic = gattService.getCharacteristic(TELIT_DATA_TX);
}
}
if (readCharacteristic == null || writeCharacteristic == null) {
for (BluetoothGattService gattService : gatt.getServices()) {
Log.d(TAG, "service " + gattService.getUuid());
}
onSerialConnectError(new IOException("no serial profile found"));
return;
}
int writeProperties = writeCharacteristic.getProperties();
if ((writeProperties & (BluetoothGattCharacteristic.PROPERTY_WRITE + // Microbit,HM10-clone have WRITE
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) == 0) { // HM10,TI uart have only WRITE_NO_RESPONSE
onSerialConnectError(new IOException("write characteristic not writable"));
return;
}
if (!gatt.setCharacteristicNotification(readCharacteristic, true)) {
onSerialConnectError(new IOException("no notification for read characteristic"));
return;
}
BluetoothGattDescriptor readDescriptor = readCharacteristic.getDescriptor(BLUETOOTH_LE_CCCD);
if (readDescriptor == null) {
onSerialConnectError(new IOException("no CCCD descriptor for read characteristic"));
return;
}
int readProperties = readCharacteristic.getProperties();
if ((readProperties & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
Log.d(TAG, "enable read indication");
readDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
} else if ((readProperties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
Log.d(TAG, "enable read notification");
readDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else {
onSerialConnectError(new IOException("no indication/notification for read characteristic (" + readProperties + ")"));
return;
}
Log.d(TAG, "writing read characterictic descriptor");
if (!gatt.writeDescriptor(readDescriptor)) {
onSerialConnectError(new IOException("read characteristic CCCD descriptor not writable"));
}
Log.d(TAG, "writing read characteristic descriptor");
// continues asynchronously in onDescriptorWrite()
}
EDIT: Nevermind. I figured out what the "credits" characteristic is for.
For anyone reading this, the credits are needed for sending/receiving data. If you don't send enough credits on the characteristic with UUID:
private void trySend(BluetoothGattCharacteristic characteristic, int nCredits) {
boolean bResult = characteristic.setValue(nCredits, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
Log.v("ble", "trySendCredits setValue(): " + bResult);
bResult = btGatt.writeCharacteristic(characteristic);
Log.v("ble", "trySendCredits writeCharacteristic(): " + bResult);
}
Then nothing will work.