从具体的 BLE 特性 Kotlin 中读取值
Read values from concrete BLE characteristic Kotlin
我正在尝试从我的应用程序中的具体 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", "-----------------------------" )
}
所有这些过程最终 return 给我下一个结果
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 中读取值时,我的问题就来了,在 return 终端中正确报告,但重点是(据我所知),而不是在我调用
的那一刻
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")
}
}
应该被触发并 return 从特征中获取值,但毕竟还没有进入。
我忘记了什么?
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);
}
我正在尝试从我的应用程序中的具体 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", "-----------------------------" )
}
所有这些过程最终 return 给我下一个结果
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 中读取值时,我的问题就来了,在 return 终端中正确报告,但重点是(据我所知),而不是在我调用
的那一刻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")
}
}
应该被触发并 return 从特征中获取值,但毕竟还没有进入。 我忘记了什么?
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);
}