gpt4 book ai didi

android - 从具体的 BLE 特性 Kotlin 中读取值

转载 作者:太空狗 更新时间:2023-10-29 14:35:15 26 4
gpt4 key购买 nike

我正在尝试从我的应用程序中的具体 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:数据声明为 ByetArray

最佳答案

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

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

或者你可以直接传递命令,这对于从你的蓝牙设备获取数据很有用,然后你就可以接收数据了。

这些权限是必需的。

**已编辑

@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);
}

关于android - 从具体的 BLE 特性 Kotlin 中读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57357019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com