【问题标题】:badly formed hexadecimal UUID string with Adafruit library带有 Adafruit 库的格式错误的十六进制 UUID 字符串
【发布时间】:2016-09-29 01:08:13
【问题描述】:

问题

我正在尝试使用 Bluefruit LE Friend 加密狗 (Adafruit)。

他们提供了一个python library 来与之通信。

不幸的是,他们提供的示例不起作用:

user@server# python ../../low_level.py 
Using adapter: user-VirtualBox
Disconnecting any connected UART devices...
Searching for UART device...
Connecting to device...
Discovering services...
Traceback (most recent call last):
  File "../../low_level.py", line 106, in <module>
    ble.run_mainloop_with(main)
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 118, in _user_thread_main
  File "../../low_level.py", line 70, in main
    device.discover([UART_SERVICE_UUID], [TX_CHAR_UUID, RX_CHAR_UUID])
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/device.py", line 106, in discover
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/device.py", line 137, in advertised
  File "/usr/lib/python2.7/uuid.py", line 134, in __init__
    raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string

我尝试了什么

我在/usr/lib/python2.7/uuid.py 中添加了一行,用于在引发异常时打印 uuid 值;我得到值1800。我找不到这个值是从哪里来的。

我找不到文件build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/device.py 来调试那个文件……


这是代码,来自their GitHub repo:

# Example of low level interaction with a BLE UART device that has an RX and TX
# characteristic for receiving and sending data.  This doesn't use any service
# implementation and instead just manipulates the services and characteristics
# on a device.  See the uart_service.py example for a simpler UART service
# example that uses a high level service implementation.
# Author: Tony DiCola
import logging
import time
import uuid

import Adafruit_BluefruitLE


# Enable debug output.
#logging.basicConfig(level=logging.DEBUG)

# Define service and characteristic UUIDs used by the UART service.
UART_SERVICE_UUID = uuid.UUID('6E400001-B5A3-F393-E0A9-E50E24DCCA9E')
TX_CHAR_UUID      = uuid.UUID('6E400002-B5A3-F393-E0A9-E50E24DCCA9E')
RX_CHAR_UUID      = uuid.UUID('6E400003-B5A3-F393-E0A9-E50E24DCCA9E')

# Get the BLE provider for the current platform.
ble = Adafruit_BluefruitLE.get_provider()


# Main function implements the program logic so it can run in a background
# thread.  Most platforms require the main thread to handle GUI events and other
# asyncronous events like BLE actions.  All of the threading logic is taken care
# of automatically though and you just need to provide a main function that uses
# the BLE provider.
def main():
    # Clear any cached data because both bluez and CoreBluetooth have issues with
    # caching data and it going stale.
    ble.clear_cached_data()

    # Get the first available BLE network adapter and make sure it's powered on.
    adapter = ble.get_default_adapter()
    adapter.power_on()
    print('Using adapter: {0}'.format(adapter.name))

    # Disconnect any currently connected UART devices.  Good for cleaning up and
    # starting from a fresh state.
    print('Disconnecting any connected UART devices...')
    ble.disconnect_devices([UART_SERVICE_UUID])

    # Scan for UART devices.
    print('Searching for UART device...')
    try:
        adapter.start_scan()
        # Search for the first UART device found (will time out after 60 seconds
        # but you can specify an optional timeout_sec parameter to change it).
        device = ble.find_device(service_uuids=[UART_SERVICE_UUID])
        if device is None:
            raise RuntimeError('Failed to find UART device!')
    finally:
        # Make sure scanning is stopped before exiting.
        adapter.stop_scan()

    print('Connecting to device...')
    device.connect()  # Will time out after 60 seconds, specify timeout_sec parameter
                      # to change the timeout.

    # Once connected do everything else in a try/finally to make sure the device
    # is disconnected when done.
    try:
        # Wait for service discovery to complete for at least the specified
        # service and characteristic UUID lists.  Will time out after 60 seconds
        # (specify timeout_sec parameter to override).
        print('Discovering services...')
        device.discover([UART_SERVICE_UUID], [TX_CHAR_UUID, RX_CHAR_UUID])

        # Find the UART service and its characteristics.
        uart = device.find_service(UART_SERVICE_UUID)
        rx = uart.find_characteristic(RX_CHAR_UUID)
        tx = uart.find_characteristic(TX_CHAR_UUID)

        # Write a string to the TX characteristic.
        print('Sending message to device...')
        tx.write_value('Hello world!\r\n')

        # Function to receive RX characteristic changes.  Note that this will
        # be called on a different thread so be careful to make sure state that
        # the function changes is thread safe.  Use Queue or other thread-safe
        # primitives to send data to other threads.
        def received(data):
            print('Received: {0}'.format(data))

        # Turn on notification of RX characteristics using the callback above.
        print('Subscribing to RX characteristic changes...')
        rx.start_notify(received)

        # Now just wait for 30 seconds to receive data.
        print('Waiting 60 seconds to receive data from the device...')
        time.sleep(60)
    finally:
        # Make sure device is disconnected on exit.
        device.disconnect()


# Initialize the BLE system.  MUST be called before other BLE calls!
ble.initialize()

# Start the mainloop to process BLE events, and run the provided function in
# a background thread.  When the provided main function stops running, returns
# an integer status code, or throws an error the program will exit.
ble.run_mainloop_with(main)

【问题讨论】:

  • 0x1800 是org.bluetooth.service.generic_access 的 GATT 标识符是巧合吗?

标签: python uuid


【解决方案1】:

此代码取决于 D-Bus,特别是它提供的“属性”。我想说问题出在这里:

uuids = self._props.Get(_INTERFACE, 'UUIDs') https://github.com/adafruit/Adafruit_Python_BluefruitLE/blob/master/Adafruit_BluefruitLE/bluez_dbus/device.py#L131

可能您没有正确安装 D-Bus,或者存在其他问题(链接到它、配置、版本问题等)。当您尝试获取 'UUIDs' 属性时,您会得到无效的字符串,然后在此处理解为 uuid.UUID():

return [uuid.UUID(str(x)) for x in uuids]

你会得到你所看到的错误。我会打印出uuids 数组的值以进行调试,看看其中元素的值是什么,然后进行相应的处理。

祝你好运!

【讨论】:

  • 我修改了文件/usr/lib/python2.7/uuid.py 以在捕获到异常时打印 UUID 值。有错误的是1800,但我不知道它来自哪里。 (我找不到文件device.py 进行调试!)。
  • D-Bus 似乎安装正确,我安装了installation page 中提到的包。 (libusb-dev, libdbus-1-dev & python-dbus)
【解决方案2】:

你有两种方法可以解决它。

正确的方式阅读更多关于this 并配置您的 adafruit 蓝牙。

或者不好的方式你可以评论这个if语句检查

if len(hex) != 32:
    raise ValueError('badly formed hexadecimal UUID string')

/usr/lib64/python2.7/uuid.py 的问题是来自“hex”参数的 len 需要像这样配置 128 位 6e400001b5a3f393e0a9e50e24dcca9e 而不是 16 位@987654326 @。

要查看你需要配置哪些 GATT 参数,只需放一个

print "this is value of hex", hex

在/usr/lib64/python2.7/uuid.py 之前评论过的if 声明之前。

【讨论】:

    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2015-12-03
    • 2016-11-22
    相关资源
    最近更新 更多