I am new to Kotlin and Android. I need to write an app that finds a particular peripheral, connects to it and writes to a characteristic.
I can find it, and I am connecting to it and finding services.
However, I can not seem to discover an characteristics at all.
Code:
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
super.onServicesDiscovered(gatt, status)
mClientActionListener.log("** Made it to onServicesDiscovered")
if (status != BluetoothGatt.GATT_SUCCESS) {
mClientActionListener.log("Device service discovery unsuccessful, status $status")
return
}
mClientActionListener.log("BLE Service status: $status. What we are looking for: $gatt")
val matchingCharacteristics = bluetoothUtils.findCharacteristics(gatt)
...
fun findCharacteristics(bluetoothGatt: BluetoothGatt): List<BluetoothGattCharacteristic> {
Log.i(TAG_BLEUTIL, "** Made it to findCharacteristics")
val matchingCharacteristics = ArrayList<BluetoothGattCharacteristic>()
val serviceList = bluetoothGatt.services
Log.i(TAG_BLEUTIL, "** serviceList: $serviceList")
val characteristics = bluetoothGatt.readCharacteristic(serviceList[1].characteristics[0])
Log.i(TAG_GATT_CALLBACK, "** Characteristics: $characteristics")
val service = findService(serviceList) ?: return matchingCharacteristics
val characteristicList = service.characteristics
Log.i(TAG_BLEUTIL, "characterList: $characteristicList")
for (characteristic in characteristicList) {
matchingCharacteristics.add(characteristic)
}
Log.i(TAG_BLEUTIL, "Characteristics: $matchingCharacteristics")
return matchingCharacteristics
}
Logs from a typical run:
... D/PairState: Device004
... I/PairState: 00:23:A7:FD:2B:B1
... D/BluetoothAdapter: isLeEnabled(): ON
... I/PairState: ** Connecting to VQpa004
... I/BleService: Connecting to 00:23:A7:FD:2B:B1D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=00:23:A7:FD:2B:B1
... I/BleService: onConnectionStateChange newState: 2
... D/BleGattServerCallback: onConnectionStateChange newState: 2
... I/BleService: Connected to device 00:23:A7:FD:2B:B1
... D/BleGattServerCallback: BluetoothProfile.STATE_CONNECTED: 0
... D/BluetoothGatt: discoverServices() - device: 00:23:A7:FD:2B:B1
... D/BluetoothGatt: onConnectionUpdated() - Device=00:23:A7:FD:2B:B1 interval=6 latency=0 timeout=500 status=0
... D/BluetoothGatt: onSearchComplete() = Device=00:23:A7:FD:2B:B1 Status=0
... I/BleService: ** Made it to onServicesDiscovered
... I/BleService: BLE Service status: 0. What we are looking for: android.bluetooth.BluetoothGatt@9917c65
... I/BluetoothUtils: ** Made it to findCharacteristics
... I/BluetoothUtils: ** serviceList: [android.bluetooth.BluetoothGattService@1f6ed3a, android.bluetooth.BluetoothGattService@746ddeb, android.bluetooth.BluetoothGattService@f8c4748]
... I/BleGattServerCallback: ** Characteristics: false
... I/BluetoothUtils: ** Made it to findService
... D/BluetoothGatt: onConnectionUpdated() - Device=00:23:A7:FD:2B:B1 interval=36 latency=0 timeout=500 status=0
From what I am understanding from the docs as well as some other (Java oriented) posts on SO this code should be working. But I’m out of ideas.
Thanks for the help. I’m glad to offer more code if that helps.
Thanks