与Adafruit库形成错误形成的十六进制UUID字符串

时间:2016-06-21 12:01:59

标签: python uuid

问题

我正在尝试使用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)

2 个答案:

答案 0 :(得分:0)

此代码取决于D-Bus,特别是&#39;属性&#39;由它提供。我想说问题出在这里:

var passInput = element(by.id('Passwd')); passInput.sendKeys('test'); https://github.com/adafruit/Adafruit_Python_BluefruitLE/blob/master/Adafruit_BluefruitLE/bluez_dbus/device.py#L131

可能你要么没有正确安装D-Bus,要么就是另一个问题(链接到它,配置,版本问题等)。当您尝试获取&#39; UUIDs&#39;属性,你得到无效的字符串,然后在这里感觉到uuid.UUID():

uuids = self._props.Get(_INTERFACE, 'UUIDs')

你得到了你所看到的错误。我会打印出return [uuid.UUID(str(x)) for x in uuids]数组的值以进行调试,看看它中元素的值是什么,然后相应地继续。

祝你好运!

答案 1 :(得分:0)

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

正确的方法是阅读有关this的更多内容并配置您的adafruit蓝牙。

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

if len(hex) != 32:
    raise ValueError('badly formed hexadecimal UUID string')
/usr/lib64/python2.7/uuid.py问题中的

来自“十六进制”参数的len需要配置为像 128位 6e400001b5a3f393e0a9e50e24dcca9e而不是 16位 0x1800

要查看您需要配置GATT的哪些参数,只需输入

print "this is value of hex", hex
在普遍评论的/usr/lib64/python2.7/uuid.py声明之前的if