override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.d(TAG,"success")
if (ActivityCompat.checkSelfPermission(this@MainActivity, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { }
mGatt?.discoverServices()
}
else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.d(TAG,"fale")
}
}
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
when(status){
BluetoothGatt.GATT_SUCCESS -> {
if (ActivityCompat.checkSelfPermission(this@MainActivity, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { }
for (service in gatt.services) {
for (characteristic in service.getCharacteristics()) {
if(hasProperty(characteristic.properties, BluetoothGattCharacteristic.PROPERTY_READ)) {
Log.d(TAG + "Read ", characteristic.uuid.toString())
gatt.readCharacteristic(characteristic)
}
if( hasProperty(characteristic.properties, BluetoothGattCharacteristic.PROPERTY_NOTIFY)) {
Log.d(TAG + "NOTIFY ", characteristic.uuid.toString())
gatt.setCharacteristicNotification(characteristic, true)
val descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"))
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
gatt.writeDescriptor(descriptor)
}
if( hasProperty(characteristic.properties, BluetoothGattCharacteristic.PROPERTY_WRITE)){
Log.d(TAG + "Write ", characteristic.uuid.toString())
gatt.writeCharacteristic(characteristic)
}
}
}
}
}
}
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, value: ByteArray) {
super.onCharacteristicChanged(gatt, characteristic, value)
Log.d(TAG,"onCharacteristicChanged")
}
In onConnectionStateChange, success log is output normally
Called normally from onServicesDiscovered to GATT_SUCCESS.
But onCharacteristicChanged is not called.
Which part is wrong or need to be added?
In the nRF Connect app, the value is normally displayed with the same Bluetooth module.