如何从android中的BLE设备获取实际值?

Posted

技术标签:

【中文标题】如何从android中的BLE设备获取实际值?【英文标题】:How to get actual values from BLE device in android? 【发布时间】:2018-09-11 11:57:44 【问题描述】:

在获得血压服务的所有特征后,我无法获得数值(收缩压、舒张压和脉搏(uuid:00002a35-0000- 1000-8000-00805f9b34fb) 而且我对所有这些特征和描述符都没有理解。因此,请任何人帮助我解决 BLE 设备中的这个问题。

【问题讨论】:

【参考方案1】:

你问的问题太笼统了。您可能需要通过此链接了解 android BLE 流程。

See this tutorial on BLE

一个GATT数据库实现一个或多个profiles(Heart Rate, Audio etc),每个profile由一个或多个services组成,每个服务由一个或多个characteristics组成。

只是为了让您一目了然,characteristics 是属性,您可以在其中定义要用于读取或写入的属性。

descriptors 是您写入数据以启用/发送数据的对象。

UUID 用于发送和接收数据的内容在客户端和服务器之间应该相同。

【讨论】:

谢谢你的回复,实际上,我得到的是制造商名称、设备名称等与设备相关的值,或者是永远不会改变但无法动态的值血压测量值。 您必须了解他们正在写入数据的哪些特征,您必须阅读它。您必须使用硬件/设备的数据表。【参考方案2】:

尝试解析扫描记录,你可能会在那里找到一些信息

Kotlin 代码

    enum class EBLE 

    EBLE_ZERO, // Zero element
    EBLE_FLAGS, //«Flags»   Bluetooth Core Specification:
    EBLE_16BitUUIDInc, //«Incomplete List of 16-bit Service Class UUIDs»    Bluetooth Core Specification:
    EBLE_16BitUUIDCom, //«Complete List of 16-bit Service Class UUIDs»  Bluetooth Core Specification:
    EBLE_32BitUUIDInc,//«Incomplete List of 32-bit Service Class UUIDs» Bluetooth Core Specification:
    EBLE_32BitUUIDCom,//«Complete List of 32-bit Service Class UUIDs»   Bluetooth Core Specification:
    EBLE_128BitUUIDInc,//«Incomplete List of 128-bit Service Class UUIDs»   Bluetooth Core Specification:
    EBLE_128BitUUIDCom,//«Complete List of 128-bit Service Class UUIDs» Bluetooth Core Specification:
    EBLE_SHORTNAME,//«Shortened Local Name» Bluetooth Core Specification:
    EBLE_LOCALNAME,//«Complete Local Name»  Bluetooth Core Specification:
    EBLE_TXPOWERLEVEL,//«Tx Power Level»    Bluetooth Core Specification:
    EBLE_DEVICECLASS,//«Class of Device»    Bluetooth Core Specification:
    EBLE_SIMPLEPAIRHASH,//«Simple Pairing Hash C»   Bluetooth Core Specification:​«Simple Pairing Hash C-192»   ​Core Specification Supplement, Part A, section 1.6
    EBLE_SIMPLEPAIRRAND,//«Simple Pairing Randomizer R» Bluetooth Core Specification:​«Simple Pairing Randomizer R-192» ​Core Specification Supplement, Part A, section 1.6
    EBLE_DEVICEID,//«Device ID» Device ID Profile v1.3 or later,«Security Manager TK Value» Bluetooth Core Specification:
    EBLE_SECURITYMANAGER,//«Security Manager Out of Band Flags» Bluetooth Core Specification:
    EBLE_SLAVEINTERVALRA,//«Slave Connection Interval Range»    Bluetooth Core Specification:
    EBLE_16BitSSUUID,//«List of 16-bit Service Solicitation UUIDs»  Bluetooth Core Specification:
    EBLE_128BitSSUUID, //«List of 128-bit Service Solicitation UUIDs»   Bluetooth Core Specification:
    EBLE_SERVICEDATA,//«Service Data»   Bluetooth Core Specification:​«Service Data - 16-bit UUID»  ​Core Specification Supplement, Part A, section 1.11
    EBLE_PTADDRESS,//«Public Target Address»    Bluetooth Core Specification:
    EBLE_RTADDRESS,//«Random Target Address»    Bluetooth Core Specification:
    EBLE_APPEARANCE,//«Appearance»  Bluetooth Core Specification:
    EBLE_DEVADDRESS,//«​LE Bluetooth Device Address»    ​Core Specification Supplement, Part A, section 1.16
    EBLE_LEROLE,//«​LE Role»    ​Core Specification Supplement, Part A, section 1.17
    EBLE_PAIRINGHASH,//«​Simple Pairing Hash C-256» ​Core Specification Supplement, Part A, section 1.6
    EBLE_PAIRINGRAND,//«​Simple Pairing Randomizer R-256»   ​Core Specification Supplement, Part A, section 1.6
    EBLE_32BitSSUUID,//​«List of 32-bit Service Solicitation UUIDs» ​Core Specification Supplement, Part A, section 1.10
    EBLE_32BitSERDATA,//​«Service Data - 32-bit UUID»   ​Core Specification Supplement, Part A, section 1.11
    EBLE_128BitSERDATA,//​«Service Data - 128-bit UUID» ​Core Specification Supplement, Part A, section 1.11
    EBLE_SECCONCONF,//​«​LE Secure Connections Confirmation Value»  ​Core Specification Supplement Part A, Section 1.6
    EBLE_SECCONRAND,//​​«​LE Secure Connections Random Value»   ​Core Specification Supplement Part A, Section 1.6​
    EBLE_3DINFDATA, //​​«3D Information Data»   ​3D Synchronization Profile, v1.0 or later
    EBLE_MANDATA; //«Manufacturer Specific Data»    Bluetooth Core Specification:

    companion object 
        private val map = EBLE.values()
        fun fromInt(type: Int) = if (type > 0) map[type] else EBLE_MANDATA

        fun getDistance(rssi: Int, txPower: Int) = 
            /*
             * RSSI = TxPower - 10 * n * lg(d)
             * n = 2 (in free space)
             *
             * d = 10 ^ ((TxPower - RSSI) / (10 * n))
            */

             Math.pow(10.0, (txPower.toDouble() - rssi) / (10 * 2))
        
        /*
     BLE Scan record parsing
    */
    fun ParseRecord(scanRecord: ByteArray): Map<EBLE, ByteArray> 
        val ret = HashMap<EBLE, ByteArray>()
        var index = 0
        while (index < scanRecord.size) 
            val length = scanRecord[index++].toInt()
            //Zero value indicates that we are done with the record now
            if (length == 0) break

            val type = scanRecord[index].toInt()
            //if the type is zero, then we are pass the significant section of the data,
            // and we are thud done
            if (type == 0) break

            Arrays.copyOfRange(scanRecord, index + 1, index + length)?.let 
                ret[EBLE.fromInt(type)] = it //HexUtil.formatHexString(it)
            

            index += length
        

        return ret
    
    

【讨论】:

感谢您的回复,但我对此感到非常沮丧。

以上是关于如何从android中的BLE设备获取实际值?的主要内容,如果未能解决你的问题,请参考以下文章

Android 从 BLE 设备获取温度读数

Android 低功耗蓝牙(Ble) 开发总结

如何在 Android 中获取当前连接的 BLE 外围设备

Android BLE 无法从设备接收 Gatt 特性通知

Android蓝牙BLE我可以修改啥连接的配置参数

如何使用Android蓝牙开发