How Read characteristic value using Android BLE (omron) in Android

79 views Asked by At

Anyone help me. bluetooth connection success and onCharacteristicRead received values but it's convert time not coming correct values. here what issue ? conversion anything having issue?

            deviceListView.setOnItemClickListener { _, _, position, _ ->
                val selectedDevice = discoveredDevices[position]
               bluetoothDevice = bluetoothAdapter!!.getRemoteDevice(selectedDevice.address)
                str_deviceaddress=selectedDevice.address
                // Connect to GATT server
                mBluetoothGatt = bluetoothDevice.connectGatt(this, false, gattCallback)

            }
        }
    }


private val gattCallback: BluetoothGattCallback = object : BluetoothGattCallback() {

    override fun onConnectionStateChange(
        gatt: BluetoothGatt,
        status: Int,
        newState: Int
    ) {
        if (newState == BluetoothGatt.STATE_CONNECTED) {
            Log.i(TAG, "Connected to GATT server.")
            gatt.discoverServices()
        } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
            Log.i(TAG, "Disconnected from GATT server.")
        }
    }
    override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
   
            Log.i(TAG, "Services discovered successfully.")
            val desiredServiceUUID: UUID  // Type annotation
            if (str_deviceaddress.contentEquals("DC:A2:24:A1:DE:DA")) {
                desiredServiceUUID = UUID.fromString("0000fe4a-0000-1000-8000-00805f9b34fb")
            } else {
                desiredServiceUUID = UUID.fromString("00001810-0000-1000-8000-00805f9b34fb")
            }


            val service: BluetoothGattService? = gatt.getService(desiredServiceUUID)

                if (service != null) {
                    // Iterate through the available characteristics of the service
                    for (characteristic in service.characteristics) {
                        Log.i(TAG, "Characteristic UUID: ${characteristic.uuid}")
                        val desiredCharacteristicUUID: UUID  // Type annotation
                        if (str_deviceaddress.contentEquals("DC:A2:24:A1:DE:DA")) {
                            desiredCharacteristicUUID =
                            UUID.fromString("b305b680-aee7-11e1-a730-0002a5d5c51b")
                        } else {
                            desiredCharacteristicUUID =
                                UUID.fromString("00002a35-0000-1000-8000-00805f9b34fb")
                        }


                        if (characteristic.uuid == desiredCharacteristicUUID) {
                            Log.i(TAG, "Desired Characteristic UUID: $desiredCharacteristicUUID")

                                        val characteristic: BluetoothGattCharacteristic? =
                service?.getCharacteristic(desiredCharacteristicUUID)
                        Log.i(TAG, "characteristicZ: $characteristic")
                            if (characteristic != null) {
                                 gatt.readCharacteristic(characteristic)

                            } else {
                                Log.e(TAG, "Characteristic with UUID $desiredCharacteristicUUID not found.")
                            }
                           // gatt.readCharacteristic(characteristic)
                       }
                    }
                } 
        } else {
            Log.e(TAG, "Failed to discover services. Status: $status")
        }
    }

    override fun onCharacteristicRead(
        gatt: BluetoothGatt,
        characteristic: BluetoothGattCharacteristic,
        status: Int
    ) {

        Log.i(TAG, "onCharacteristicRead successfully."+status)
    if (status == BluetoothGatt.GATT_SUCCESS) {

        val isPaired: Boolean = bluetoothDevice.bondState == BluetoothDevice.BOND_BONDED



        if (isPaired) {
            // Device is paired
            println("The device is paired.")
        } else {
            // Device is not paired
            println("The device is not paired.")
        }
Log.e("characteristic",""+characteristic.properties)


        Log.e("chservice",""+characteristic.service.type)
            Log.e("chserviceff",""+characteristic.descriptors)
            val value = characteristic.value // This is the byte array containing the value
            Log.i(TAG, "characteristic value: $value")
            // Convert the byte array to a string or any other data type as needed


        val byteArray = characteristic.value
        Log.i(TAG, "Raw byte values: ${byteArray.joinToString(", ")}")
      
        }
    }
 
}


}

here I received onCharacteristicRead function inside characteristic value: [B@8add8e2 . how it's convert ? please anyone help me. received value convert time come wrong values. Here Print the raw byte values and their hexadecimal representation. here service discovered also success and Connected to GATT server also success, onCharacteristicRead function also calling

logcat

0

There are 0 answers