3

我正在尝试从我的应用程序中的具体 BLE 特征中读取值,但目前我尝试从所需特征中读取值,onCharacteristicRead 函数似乎没有被触发。

首先,我使用 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")
        }
    }

如您所见,我使用了一个 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", "-----------------------------" )
}

所有这些过程最终返回给我下一个结果

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

这是完全正确的(它向我显示设备上的每个服务以及该服务中的每个特征),但是当我尝试从 00002a53-0000-1000-8000-00805f9b34fb 中正确报告的characteristicRS 中读取值时,我的问题就出现了返回终端,但重点是(据我所知),在我打电话的那一刻

mBluetoothGatt!!.readCharacteristic(characteristic)

功能

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")
        }
    }

应该被触发并向我返回特征中的值,但毕竟没有输入。我忘记了什么?

PD:数据被声明为字节数组

4

1 回答 1

1

获得所有特征后,您需要启用读取、写入或通知权限。

启用后,如果需要,您将需要写入描述符值。

或者您可以直接传递对从 ble 设备获取数据有用的命令,然后您将能够接收数据。

这些权限是必要的。

**已编辑

@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);
}
于 2019-08-05T10:57:18.590 回答