0
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.

Yujin
  • 1

1 Answers1

0

The documentation for the onCharacteristicChanged callback states:

Callback triggered as a result of a remote characteristic notification. Note that the value within the characteristic object may have changed since receiving the remote characteristic notification, so check the parameter value for the value at the time of notification.

This means that the characteristic you want this callback to work on must allow notifications and notifications need to be activated. This is a question that handles the activation of BLE notifications:

Enabling Bluetooth characteristic Notification in Android (Bluetooth Low Energy ) Not Working

Michael Kotzjan
  • 2,093
  • 2
  • 14
  • 23