简体   繁体   English

从具体的 BLE 特性 Kotlin 读取值

[英]Read values from concrete BLE characteristic Kotlin

I'm trying to read values from a concrete BLE characteristic in my app but at the moment I try to read values from the desired characteristic the onCharacteristicRead function seems like not being triggered.我正在尝试从我的应用程序中的具体 BLE 特征中读取值,但目前我尝试从所需的特征中读取值,但 onCharacteristicRead 函数似乎没有被触发。

In very first place i use onServicesDiscovered in order to get every service from the device:首先,我使用 onServicesDiscovered 来从设备获取每项服务:

       override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {

        Log.e("BluetoothLeService", "onServiceDiscovered()")
        if(status == BluetoothGatt.GATT_SUCCESS){
            Log.i(TAG, "**ACTION_SERVICE_DISCOVERED** $status")
            var gattServices: List<BluetoothGattService> = mBluetoothGatt!!.services
            Log.e("onServiceDiscovered", "Services count: ${gattServices.size}")
            for(gattService in gattServices){
                var serviceUUID = gattService.uuid.toString()
                Log.e("OnServicesDiscovered", "Service uuid: $serviceUUID" )
                readCharacteristic(gattService)

            }
            Log.e("OnServicesDiscovered", "---------------------------" )

            broadcastUpdate( "com.np.lekotlin.ACTION_GATT_SERVICES_DISCOVERED")

        } else {
            //Service discovery failed so log a warning
            Log.i(TAG, "onServicesDiscovered received: $status")
        }
    }

As you can see I use a for loop in order to achieve every service name and characteristic associated with every service with the readCharacteristic function如您所见,我使用 for 循环来实现与每个服务相关联的每个服务名称和特征与 readCharacteristic 函数

   fun readCharacteristic(service: BluetoothGattService) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized")
        return
    }
    var gattCharacteristic: List<BluetoothGattCharacteristic> = service.characteristics
    Log.i(TAG, "**LEO LAS ${gattCharacteristic.count()} CARACTERISTICAS DE $service**")


    for(characteristic in gattCharacteristic){
        Log.e("OnServicesDiscovered", "Service characteristic: ${characteristic.uuid}" )
        if(characteristic.uuid == characteristicRS){
            mBluetoothGatt!!.setCharacteristicNotification(characteristic, true)
            mBluetoothGatt!!.readCharacteristic(characteristic)
        }
    }
    Log.e("OnServicesDiscovered", "-----------------------------" )
}

All this process finally returns me the next result所有这些过程最终返回给我下一个结果

E/BluetoothLeService: onServiceDiscovered()
I/PSoCCapSenseLedService: **ACTION_SERVICE_DISCOVERED** 0
E/onServiceDiscovered: Services count: 4
E/OnServicesDiscovered: Service uuid: 00001800-0000-1000-8000-00805f9b34fb
I/PSoCCapSenseLedService: **LEO LAS 3 CARACTERISTICAS DE android.bluetooth.BluetoothGattService@1b31ffb*
E/OnServicesDiscovered: Service characteristic: 00002a00-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: Service characteristic: 00002a01-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: Service characteristic: 00002a04-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: -----------------------------
E/OnServicesDiscovered: Service uuid: 00001801-0000-1000-8000-00805f9b34fb
I/PSoCCapSenseLedService: **LEO LAS 1 CARACTERISTICAS DE android.bluetooth.BluetoothGattService@4254818**
E/OnServicesDiscovered: Service characteristic: 00002a05-0000-1000-8000-00805f9b34fb
-----------------------------
E/OnServicesDiscovered: Service uuid: 00001814-0000-1000-8000-00805f9b34fb
I/PSoCCapSenseLedService: **LEO LAS 4 CARACTERISTICAS DE android.bluetooth.BluetoothGattService@a673271**
E/OnServicesDiscovered: Service characteristic: 00002a53-0000-1000-8000-00805f9b34fb
Service characteristic: 00002a54-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: Service characteristic: 00002a5d-0000-1000-8000-00805f9b34fb
Service characteristic: 00002a55-0000-1000-8000-00805f9b34fb
-----------------------------
E/OnServicesDiscovered: Service uuid: 0000180a-0000-1000-8000-00805f9b34fb
I/PSoCCapSenseLedService: **LEO LAS 3 CARACTERISTICAS DE android.bluetooth.BluetoothGattService@3b51856**
E/OnServicesDiscovered: Service characteristic: 00002a29-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: Service characteristic: 00002a24-0000-1000-8000-00805f9b34fb
Service characteristic: 00002a27-0000-1000-8000-00805f9b34fb
E/OnServicesDiscovered: -----------------------------
---------------------------
D/BluetoothGatt: onConnectionUpdated() - Device=00:A0:50:00:00:0F interval=36 latency=0 timeout=500 status=0

Which is completely correct (it displays me every service on the device and every characteristic inside this service), but my problem comes when I try to read values from characteristicRS which is 00002a53-0000-1000-8000-00805f9b34fb, which is correctly reported in the return terminal, but the point is (according to my knowledge) than, in the moment I call这是完全正确的(它向我显示设备上的每个服务以及该服务中的每个特征),但是当我尝试从 00002a53-0000-1000-8000-00805f9b34fb 中正确报告的characteristicRS 读取值时出现了问题返回终端,但重点是(据我所知)比我打电话的那一刻

mBluetoothGatt!!.readCharacteristic(characteristic)

The function功能

override fun onCharacteristicRead(
        gatt: BluetoothGatt,
        characteristic: BluetoothGattCharacteristic,
        status: Int
    ) {
        Log.i(TAG, "HAGO COSAS")
        if (status == BluetoothGatt.GATT_SUCCESS) {
            //See if the read was successful
            Log.i(TAG, "**ACTION_DATA_READ** ${characteristic.uuid}")
            broadcastUpdate("com.np.lekotlin.ACTION_DATA_AVAILABLE"/*, characteristic*/)                 //Go broadcast an intent with the characteristic data
            data = characteristic.value
            Log.e("onCharacteristicRead", "Datos:")
            Log.e("onCharacteristicRead", "$data")

        } else {
            Log.i(TAG, "ACTION_DATA_READ: Error$status")
        }
    }

Should be triggered and returning me the values from the characteristic, but is not even entering after all.应该被触发并向我返回特征中的值,但毕竟甚至没有进入。 What am I forgetting?我忘记了什么?

PD: data is declared as a ByetArray PD:数据被声明为一个 ByetArray

After getting all the characteristics, you will need to enable the read, write or notify permission.获得所有特征后,您需要启用读取、写入或通知权限。

Once you enable that, you will need to write the descriptor value if required so.启用它后,如果需要,您将需要写入描述符值。

Or you can directly pass the command which is useful for getting the data from your ble device and you will be able to receive the data after than.或者您可以直接传递有助于从您的 ble 设备获取数据的命令,之后您将能够接收数据。

These permissions are necessary.这些权限是必需的。

**EDITED **编辑

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status){
   BluetoothGattCharacteristic characteristic = gatt.getService(SERVICE_UUID)
getCharacteristic(CHAR_UUID);
gatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = 
characteristic.getDescriptor(CHARACTERISTIC_CONFIG_UUID);

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM