蓝牙 LE OTS OLCP 计数对象命令

问题描述

如何从适用于 Kotlin Android 的 OTS 蓝牙 LE 中的对象数量命令 (0x07) OLCP 中获得答案?

private val channel = Channel<BluetoothResult>()

private val mGattCallback = object : BluetoothGattCallback() {

    override fun onCharacteristicRead(gatt: BluetoothGatt?,characteristic: BluetoothGattCharacteristic?,status: Int) {
        super.onCharacteristicRead(gatt,characteristic,status)
        if (gatt !=null && characteristic != null) {
            channel.offer(BluetoothResult(characteristic.uuid,characteristic.value,status))
        }
    }

    override fun onCharacteristicWrite(gatt: BluetoothGatt?,status: Int) {
        super.onCharacteristicWrite(gatt,status)
        if (characteristic != null) {
                channel.offer(BluetoothResult(characteristic.uuid,status))
            }
        }

    override fun onDescriptorWrite(gatt: BluetoothGatt?,descriptor: BluetoothGattDescriptor?,status: Int) {
        super.onDescriptorWrite(gatt,descriptor,status)
        if (descriptor != null) {
            channel.offer(BluetoothResult(descriptor.uuid,descriptor.value,status))
        }
    }
}

private suspend fun enableIndication(characteristic: BluetoothGattCharacteristic): Boolean {
    val property = characteristic.properties
    if (property and PROPERTY_INDICATE == 0) {
        Log.w(TAG,"Indication prohibited")
        return false
    }
    val descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG))
    val result = writeDescriptor(descriptor,BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)
    return if (result != null) result.status == BluetoothGatt.GATT_SUCCESS else false
}

private suspend fun commandOlcp(characteristic: BluetoothGattCharacteristic,value: ByteArray): Boolean {
    return writeCharacteristic(characteristic,value)?.value.contentEquals(value)
}

private fun objectTransferService(gattServices: List<BluetoothGattService>?): BluetoothGattService? {
    gattServices?.forEach { service ->
        if (BulatGattAttributes.lookup(service.uuid.toString(),"") == "Object Transfer")
            return service
    }
    return null
}

private fun objectListControlPoint(gattService: BluetoothGattService?): BluetoothGattCharacteristic? {
    gattService?.characteristics?.forEach { characteristic ->
        if (BulatGattAttributes.lookup(characteristic.uuid.toString(),"") == "Object list control point")
            return characteristic
    }
    return null
}

@ExperimentalUnsignedTypes
suspend fun numberObjects(gattServices: List<BluetoothGattService>?): String? {
    val objectTransferService = objectTransferService(gattServices)
    val objectListControlPoint = objectListControlPoint(objectTransferService) ?: return null
    if (!enableIndication(objectListControlPoint)) {
        Log.w(TAG,"The command indicate is not understood")
        return null
    }
    if (!commandOlcp(objectListControlPoint,COMMAND_COUNT)) {
        Log.w(TAG,"The command count is not understood")
        return null
    }
    if (!commandOlcp(objectListControlPoint,COMMAND_RESPONSE)) {
        Log.w(TAG,"The command response is not understood")
        return null
    }
    return null
}

private suspend fun waitForResult(uuid: UUID): BluetoothResult {
    return withTimeoutOrNull(TimeUnit.SECONDS.toMillis(3)) {
        var bluetoothResult: BluetoothResult = channel.receive()
        while (bluetoothResult.uuid != uuid) {
            bluetoothResult = channel.receive()
        }
        bluetoothResult
    } ?: run {
        throw TimeoutException("Bluetooth timeout")
    }
}

companion object {
    @ExperimentalUnsignedTypes
    val COMMAND_COUNT = ubyteArrayOf(0x07U).toByteArray()
    @ExperimentalUnsignedTypes
    val COMMAND_RESPONSE = ubyteArrayOf(0x70U,0x07U).toByteArray()
}

通信算法应该是什么?如果我发送 0x07 命令 (writeCharacteristic),我会收到 0x07 回显响应 (onCharacteristicWrite)。如果我仍然发送 0x70 追击,我也会收到 0x70 的回声。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)