解码十六进制返回(某些)意外值

时间:2017-02-11 16:04:13

标签: python bluetooth micropython

我正在使用Pymakr从SiPy板执行蓝牙扫描。解码返回的广告时,控制台会返回意外的值。

from network import Bluetooth
bluetooth = Bluetooth()

bluetooth.start_scan(30)
while bluetooth.isscanning():
    adv = bluetooth.get_adv()
    if adv:
         print(adv)
         print(adv[4].decode())

广告返回:

(mac=b'\xd0O~\x07\xc0.', addr_type=0, adv_type=0, rssi=-53, data=b'\x02\x01\x1a\x0b\xffL\x00\t\x06\x03\x04\xc0\xa8\x01!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')

解码'数据'在[4]我得到:

STX SOH SUB VT ÿL

我很难理解最后两个角色。 为何重音?为什么资本L?这部分数据字符串是什么:

\xffL

对我来说看起来很糟糕。

我正在使用MicroPython,所以我的解决方法有限。

1 个答案:

答案 0 :(得分:0)

我读过一些非完整的micropython文档,并且误解了我的输出。输出是原始二进制,而不是十六进制....

print(mac)  #is the rawbinary equal to adv[0]
mac = binascii.hexlify(mac) #apparently supported in firmware
print(mac.decode()) #makes wonders

修复了问题。

现在输出到控制台:

(mac=b'fUD3"\x11', addr_type=1, adv_type=0, rssi=-27, data=b'\x02\x01\x06\x06\x088MHzL\x04\xff\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
b'fUD3"\x11'
665544332211