简体   繁体   English

如何从 Android 中的 BLE 设备读取数据?

[英]How does one read data from a BLE device in Android?

My mobile phone app is supposed to be the central device and the BLE kit is supposed to be the peripheral device.我的手机应用程序应该是中央设备,BLE 套件应该是外围设备。 I have managed to write to Kit, as in send data.我已经设法写信给 Kit,就像发送数据一样。 The kit itself according to it documentation has this:根据它的文档,套件本身具有: 在此处输入图像描述

According to the documentation: The Rx (receive) and Tx (transmit) Characteristics are named from the point of view of the Peripheral BGX device.根据文档: Rx(接收)和 Tx(发送)特性是从 Peripheral BGX 设备的角度命名的。

To send a string to be received by the Peripheral serial interface, write to the Rx Characteristic.要发送要由外设串行接口接收的字符串,请写入 Rx 特性。

To read a string transmitted from the Peripheral serial interface, enable Notifications or Indications from the Tx Characteristic then wait for subsequent Notification or Indication Events to occur.要读取从外围串行接口传输的字符串,请从 Tx 特性启用通知或指示,然后等待后续通知或指示事件发生。

So how exactly am I supposed to Read data from the device mentioned if the characteristic for receiving data doesn't have READ as one of its properties?那么,如果接收数据的特性没有 READ 作为其属性之一,我应该如何从提到的设备中读取数据呢?

When I sent data as in writing data to the device I used the writeCharacteristic function.当我在向设备写入数据时发送数据时,我使用了writeCharacteristic function

  fun write(message:String){
    val bytes = BigInteger(message.replace("\\s".toRegex(), ""), 16).toByteArray()
    Timber.i("Bytes value ---> ${bytes.toHexString()}")
    val device = getBleDevice()
    val characteristicRX = getBleCharacteristic()
    writeCharacteristic(device, characteristicRX, bytes)
}

Then I would call this function to send:然后我会调用这个 function 来发送:

fun sendMessage(message:String){
    Timber.i("Check if isConnected = true --> ${isConnected.value}")
    if(isConnected.value == true){
        write(message)
    }else{
       Timber.e("Make sure that you connected and paired with the desired device.")
    }
}

So how do I go on about receiving data instead from the BLE device?那么我如何 go 关于接收数据而不是从 BLE 设备? Wouldn't the readCharacteristic function come into play here? readCharacteristic function 不会在这里发挥作用吗? I ask this because the code I am working on was original designed to exchange data using classical Bluetooth and I was tasked with converting it into BLE instead.我问这个是因为我正在处理的代码最初是为使用经典蓝牙交换数据而设计的,而我的任务是将其转换为 BLE。 But when I used a serial monitor to see the bytes being sent I found that the buttons that are supposed to trigger receiving data are instead sending it to the Kit.但是当我使用串行监视器查看正在发送的字节时,我发现应该触发接收数据的按钮正在将其发送到套件。 This caught my attention as I haven't started working on the reading mechanism and the screenshot I posted here also has me puzzled, as I have thought that the TX characteristic would be a readable one not writeable.这引起了我的注意,因为我还没有开始研究读取机制,我在这里发布的屏幕截图也让我感到困惑,因为我认为TX特性将是一个可读不可写的特性。

In my app each parameter has a code.在我的应用程序中,每个参数都有一个代码。 If it's a write command it looks like this:如果它是一个写命令,它看起来像这样:

  enum class WriteCommandCodes(val value: String) {
    TOOL_ADDRESS("08 00 00 00 20 30 04 27"),
    RPM_THRESHOLD("08 00 00 00 20 30 04 13"),
    BACKLASH_1("08 00 00 00 20 30 04 22"),
    BACKLASH_2("08 00 00 00 20 30 04 23"),
    DELAY("08 00 00 00 20 30 04 20"),

    BATTERY1_CAPACITY("08 00 00 00 20 30 0F"),
    BATTERY2_CAPACITY("08 00 00 00 20 30 10")}

the payload that is created later on has both the parameter's code and the data being sent.稍后创建的有效负载具有参数的代码和正在发送的数据。

for the Read parameters I have this:对于读取参数,我有这个:

 enum class ReadRequestCodes(val value: String) {
    KEY_ADDRESS("08 00 00 00 20 30 05 11 00 00 00 00 00"),
    TOOL_ADDRESS("08 00 00 00 20 30 05 27 00 00 00 00 00"),
    RPM_THRESHOLD("08 00 00 00 20 30 05 13 00 00 00 00 00"),
    BACKLASH("08 00 00 00 20 30 05 22 00 00 00 00 00"),

    POWER_SRC_TYPE("08 00 00 00 20 30 05 26 00 00 00 00 00"),
    BATTERY1_PERCENTAGE("08 00 00 00 20 30 11 00 00 00 00 00 00"),
    BATTERY2_PERCENTAGE("08 00 00 00 20 30 12 00 00 00 00 00 00")}

The same is supposed to happen only difference is I'll be receiving this time around.应该会发生同样的情况,唯一不同的是我这次会收到。

Yes you override the default onCharacteristicChanged method and implement what you want to do with the new data.是的,您覆盖默认的 onCharacteristicChanged 方法并实现您想要对新数据执行的操作。 You can see someone doing something similar here你可以看到有人在这里做类似的事情

BLE receiving GATT notifications from a characteristic BLE 从特性接收 GATT 通知

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

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