Giter Site home page Giter Site logo

adafruit_python_bluefruitle's Introduction

THIS LIBRARY IS DEPRECATED. USE THIS NEW LIBRARY INSTEAD:

https://github.com/adafruit/Adafruit_Blinka_bleio

Adafruit Python BluefruitLE

Python library to simplify access to Bluefruit LE (Bluetooth low energy) devices and services on Linux and Mac OSX. Hides all the platform-specific BLE code (using BlueZ on Linux and CoreBluetooth on Mac OSX) and provides a simpler syncronous BLE API which is great for scripts and other automated tasks using BLE. For example you can create a program to read and write data (like sensor readings) with a Bluefruit LE device in UART mode.

See more details from the guide here: https://learn.adafruit.com/bluefruit-le-python-library/overview

Note this library provides a Python wrapper for BLE Central Mode, meaning it can initiate connections and data exchanges with other BLE Peripherals. It does not allow you to emulate a BLE Peripheral via Python, or provide Python-based access to BLE peripherals connected to your system.

Be aware that this library is early in development! The examples show how to detect and interact with BLE UART devices which are the primary target of the library. More advanced features like getting advertisement data or interacting with other BLE services might not work or be currently supported. In particular BLE support for desktop operating systems is still somewhat buggy and spotty with support. The library has been developed and primarily tested on Python 2.7.x, but if there are issues with Python 3 please raise them so support can be added.

Supported Platforms

The library supports the following platforms:

  • Linux, using the latest BlueZ 5.33 release. The library is implemented using BlueZ's experimental DBus bindings for access from languages such as Python.
  • Mac OSX, using Apple's CoreBluetooth library. The library depends on PyObjC which Apple includes with their Python version on OSX. Note that if you're using a different Python, like the one provided by Homebrew, be sure to install PyObjC for that version of Python.

What about Windows support?

The library currently does not support Windows since BLE support is limited on that platform. Windows 7 does not support BLE at all. Windows 8 does not have BLE device search and pairing APIs. Windows 10 appears to have a more capable BLE API but it has not yet been invesigated to see if it can be supported by this library yet.

Linux & Raspberry Pi Requirements

On Linux (like with a Raspberry Pi) you'll need to compile and install the latest version of BlueZ, currently version 5.33, to gain access to the Bluetooth LE API it exposes. It's important to use this more recent version of BlueZ as the BLE APIs are still in development and a bit experimental.

Warning: Be careful compiling and installing a later version of BlueZ on top of an existing version if you use a Linux desktop OS like Ubuntu, Debian, etc. You might cause an issue with the new BlueZ version conflicting with the distribution's older BlueZ installation and wreaking havoc with package dependencies. Ubuntu in particular can get into a very bad state since core components depend on BlueZ. Look for a properly packaged BlueZ 5.33 release for your distribution, or install into a virtual machine that can protect your desktop machine from conflicts.

The steps below describe how to install BlueZ 5.33 on a Raspberry Pi running its Raspbian operating system. In a terminal on the Raspberry Pi run:

sudo apt-get update
sudo apt-get -y install libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.33.tar.gz
tar xvfz bluez-5.33.tar.gz
cd bluez-5.33
./configure --disable-systemd
make
sudo make install
sudo cp ./src/bluetoothd /usr/local/bin/

Finally you'll need to make sure the bluetoothd daemon runs at boot and is run with the --experimental flag to enable all the BLE APIs. To do this edit the /etc/rc.local file and add the following line before the exit 0 at the end:

/usr/local/bin/bluetoothd --experimental &

Save the changed file and reboot the Pi. Then verify using the command ps aux | grep bluetoothd that the bluetoothd daemon is running.

Mac OSX Requirements

On Mac OSX you do not need to install any dependencies to start using the library (the PyObjC library should be installed already by Apple).

Installation

Once the requirements above have been met the library can be installed by running the following command inside its root directory:

sudo python setup.py install

or if you are behind a proxy and want the setup to use your environment variables:

sudo -E python setup.py install

This will install the library so it can be used by any script on your system.

Alternatively you can run sudo python setup.py develop to have the library installed in develop mode where changes to the code (like doing a git pull) will immediately take effect without a reinstall.

After the library is installed examine the examples folder to see some examples of usage:

  • list_uarts.py - This example will print out any BLE UART devices that can be found and is a simple example of searching for devices.
  • uart_service.py - This example will connect to the first BLE UART device it finds, send the string 'Hello World!' and then wait 60 seconds to receive a reply back. The example uses a simple syncronous BLE UART service implementation to send and receive data with the UART device.
  • device_info.py - This example will connect to the first BLE UART device it finds and print out details from its device info service. Note this example only works on Mac OSX! Unfortunately a bug / design issue in the current BlueZ API prevents access to the device information service.
  • low_level.py - This is a lower-level example that interacts with the services and characteristics of a BLE device directly. Just like the uart_service.py example this will connect to the first found UART device, send a string, and then print out messages that are received for one minute.

To run an example be sure to run as the root user on Linux using sudo, for example to run the uart_service.py example:

sudo python uart_service.py

On Mac OSX the sudo prefix to run as root is not necessary.

adafruit_python_bluefruitle's People

Contributors

brennen avatar dgboy2000 avatar dhalbert avatar iantruslove avatar joshbode avatar ladyada avatar microbuilder avatar nneonneo avatar splitframe avatar tdicola avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adafruit_python_bluefruitle's Issues

UART.find_devices() returns empty list

In the same configuration, find_device() returns the first CPB correctly. What I want to be able to do is to run this in a classroom full of CPBs where each student names theirs differently. And so the version running on their computer/RPi gets the correct device by checking the name.

MacOS 10.15.2
Python 3.7.4

Can't reconnect to device after disconnecting from it.

Is there a way to reconnect after disconnecting? I'm running into an issue where I connect to a device, start discovery, create an instance of the service, disconnect, but then if I call connect on it again it fails to connect.

uart_service.py throws an error on Raspberry Pi 3

  • Raspberry Pi 3

  • Python 2.7

  • Error message you are receiving, including any Python exception traces:

File "uart_service.py", line 85, in
ble.run_mainloop_with(main)
File "build/bdist.linux-armz7l/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 118, in _user_tread_main
File "uart_service.py", line 59, in main
uart = UART(device)
File "build/bdist.linux-arm7l/egg/Adafruit_BluefruitLE/services/uart.py", line 48, in init
AttributeError: 'NoneType' object has no attribute 'find_characteristic'

  • List the steps to reproduce the problem below (if possible attach code or commands
    to run):

Adafruit Bluefruit 32u4 Feather running bleuart_datamode
Run sudo python uart_service from the examples folder.
Upon "Discovering Services", this error results.

General question, what thread is CoreBluetooth running on?

^ title.

In the documentation for run_mainloop_with():

        If you are writing a GUI application that has a main loop (a GTK glib main loop
        on Linux, or a Cocoa main loop on OSX) then you don't need to call this
        function.

So in my application, I didn't need to call run_mainloop_with to work, and I simply initialized it with

 self.ble = Adafruit_BluefruitLE.get_provider()
 self.ble.initialize()

in my main thread...however what thread is CoreBluetooth (the underlying wrapped library) using? I thought CoreBluetooth needed to be run on the main thread for its callbacks to work?

How to run bluetooth in event loop?

Any pointers on how to run this library in an event loop? In the run_main_loop_with() signature, it says If you are writing a GUI application that has a main loop (a GTK glib main loop on Linux, or a Cocoa main loop on OSX) then you don't need to call this function.

So I'm subclassing my class Example to the Thread class. Then I'm calling run() to initialize the bluetooth provider with self.ble.initialize(), then I'm retrieving adapters with adapter = self.ble.get_default_adapter() but that fails.

class Arduino(threading.Thread):
    '''Arduino object

    '''

    def __init__(self, is_active=True,
                 uart_service_uuid=UART_SERVICE_UUID,
                 rotate_table_char_uuid=ROTATE_TABLE_CHAR_UUID,
                 table_position_char_uuid=TABLE_POSITION_CHAR_UUID,
                 is_table_rotating_char_uuid=IS_TABLE_ROTATING_CHAR_UUID):
        super(Arduino, self).__init__()
        self.is_active = is_active
        self.__uart_service_uuid = uart_service_uuid
        self.__rotate_table_char = None
        self.__rotate_table_char_uuid = rotate_table_char_uuid
        self.__table_position_char_uuid = table_position_char_uuid
        self.__is_table_rotating_char_uuid = is_table_rotating_char_uuid

        self.device = None

        # Services notifications
        self.is_rotating = False
        self.table_position = 0

        # Get the BLE provider for the current platform.
        self.ble = Adafruit_BluefruitLE.get_provider()
        self.ble.initialize()
        print(dir(self.ble))
        # Start the mainloop to process BLE events, and run the provided function in a background thread.
        # self.ble.run_mainloop_with(self.run)

    def run(self):

        self.ble.clear_cached_data()
        adapter = self.ble.get_default_adapter() # FAILS TO GRAB THE ADAPTER

Python 3 installation failure

Summary: Build error on Python 3 because of raise statement syntax

  • Platform/operating system (i.e. Raspberry Pi with Raspbian operating system,
    Windows 32-bit, Windows 64-bit, Mac OSX 64-bit, etc.):

OSX 10.10

  • Python version (run python -version or python3 -version): 3.5
  • Error message you are receiving, including any Python exception traces:

byte-compiling build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py to provider.cpython-34.pyc
File "build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 103
raise self._exception[1], None, self._exception[2]
^
SyntaxError: invalid syntax

byte-compiling build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/config.py to config.cpython-34.pyc
byte-compiling build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/corebluetooth/init.py to init.cpython-34.pyc
byte-compiling build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/corebluetooth/adapter.py to adapter.cpython-34.pyc
byte-compiling build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/corebluetooth/device.py to device.cpython-34.pyc
byte-compiling build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/corebluetooth/gatt.py to gatt.cpython-34.pyc
byte-compiling build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/corebluetooth/metadata.py to metadata.cpython-34.pyc
byte-compiling build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/corebluetooth/objc_helpers.py to objc_helpers.cpython-34.pyc
byte-compiling build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/corebluetooth/provider.py to provider.cpython-34.pyc
File "build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/corebluetooth/provider.py", line 290
raise exec_info[1], None, exec_info[2]
^
SyntaxError: invalid syntax

byte-compiling build/bdist.macosx-10.5-x86_64/egg/Adafruit_BluefruitLE/interfaces/init.py to init.cpython-34.pyc

  • List the steps to reproduce the problem below (if possible attach code or commands
    to run):

%sudo python setup.py install

Linux Performance Issues due to Repeated Polling of ObjectManager GetManagedObjects()

I'm working on an application that requires repeated connection, discovery, some minor activity and disconnection.

Every 4-5 seconds or so, the stack locks up for 15 or so more seconds. Host in question is a Raspberry Pi 1.

My debugging traced this back to GetManagedObjects() being slow to respond and sometimes locking up.

From what I can tell reading the DBus docs on ObjectManager, they never intended for GetManagedObjects() to be called repeatedly -- rather, it is to enable a quick initial sync of the entire state of the endpoint, and then changes are to be communicated by InterfacesAdded and InterfacesRemoved events.

In device.discover() for example, the code calls GetManagedObjects() every second until the endpoints it is looking for appear. In one sample of my connection through disconnection cycle, there were 13 calls, 5 of which during discover(). As other device enter the system and end up in the device tree, the performance would degrade even faster.

The least invasive change for a big benefit would be to maintain the complete dict returned by GetManagedObjects() inside of the provider, updating it on Interface* events. For a full and optimized BLE implementation maybe it should be implemented at the Adapter or Device level using the AddWatch mechanism mentioned in the docs.

You guys have implemented the most complete Python BLE library, and out of everything out there it is really the only one to implement it properly for Linux/Bluez i.e. using DBus to bluetoothd and not helper c wrappers. This could (should) be morphed into the standard Python BLE implementation.

I may take a crack at a proof of concept implementation in the provider.

Cheers

Refactor timeout behavior

Lots of functions take timeouts, make one global spot to set the default timeout and then allow overrides per function. Also look at using concurrent.futures to let people write async code if they desire (but make the default syncronous blocking code for simplicity).

very slow response

I'm trying to log data from a notify characteristic that publishes a 4 byte number 15 times per second.

The library does kind of work, but:

-there is a ~10 second delay before initially receiving a notification after subscribing
-notifications arrive erratically in bunches of 2 or 3 about every second - so I guess most must be discarded

I'm using OSX sierra on a mac book pro.

I've seen similar performance in linux.

Without looking into it deeply, is this a limitation of desktop BLE libraries or in the implementation?

Raspberry Pi Connection Error

  • Platform/operating system (i.e. Raspberry Pi with Raspbian operating system,
    Windows 32-bit, Windows 64-bit, Mac OSX 64-bit, etc.): Raspbian running on A Raspberry Pi 3
  • Python version (run python -version or python3 -version): 2.7.9
  • Error message you are receiving, including any Python exception traces:
Traceback (most recent call last):
  File "device_info.py", line 89, in <module>
    ble.run_mainloop_with(main)
  File "build/bdist.linux-armv7l/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 118, in _user_thread_main
  File "device_info.py", line 66, in main
    dis = DeviceInformation(device)
  File "build/bdist.linux-armv7l/egg/Adafruit_BluefruitLE/services/device_information.py", line 58, in __init__
AttributeError: 'NoneType' object has no attribute 'find_characteristic'

followed by the inability to connect to the device again.

Reproduce the Issue.

  1. Raspberry Pi 3 running Raspbian
  2. nRF51822 powered circuit board
  3. Using a board that has never connected to the Raspberry Pi, go to the examples folder and run python device_info.py (will crash with above stack trace).
  4. Attempt to connect to it again using python low_level.py. It won't connect.

Note: This will run on a mac, but not on a Raspberry Pi 3.

device.rssi gives error: No such property 'RSSI'

  • Platform/operating system (i.e. Raspberry Pi with Raspbian operating system,
    Windows 32-bit, Windows 64-bit, Mac OSX 64-bit, etc.): Linux raspberrypi 4.19.97-v7+ #1294 SMP Thu Jan 30 13:15:58 GMT 2020 armv7l GNU/Linux

  • Python version (run python -version or python3 -version): Python 2.7.16

  • Error message you are receiving, including any Python exception traces: Traceback (most recent call last):
    File "getRSSI.py", line 80, in
    ble.run_mainloop_with(main)
    File "build/bdist.linux-armv7l/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 105, in run_mainloop_with
    File "build/bdist.linux-armv7l/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 120, in _user_thread_main
    File "getRSSI.py", line 63, in main
    rssi = device.rssi
    File "build/bdist.linux-armv7l/egg/Adafruit_BluefruitLE/bluez_dbus/device.py", line 161, in rssi
    File "/usr/local/lib/python2.7/dist-packages/dbus/proxies.py", line 147, in call
    **keywords)
    File "/usr/local/lib/python2.7/dist-packages/dbus/connection.py", line 653, in call_blocking
    message, timeout)
    dbus.exceptions.DBusException: org.freedesktop.DBus.Error.InvalidArgs: No such property 'RSSI'

  • List the steps to reproduce the problem below (if possible attach code or commands
    to run):

Program is an adaptation of the low_level.py example:


import Adafruit_BluefruitLE
from Adafruit_BluefruitLE.services import UART
import uuid
import time

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...')
UART.disconnect_devices()

# 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 = UART.find_device()
    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:
    print('Getting device info...')
    # device.is_connected and device.name work successfully
    connection = device.is_connected
    name = device.name

print ('Connection: ')
print (connection)
print ('Name: ')
print (name)
try:
rssi = device.rssi #error occurs here
print ('RSSI: ')
print (rssi)
finally:
pass
finally:
print('Disconnecting...')
# Make sure device is disconnected on exit.

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)

Get service data from advertising data on Mac OSX.

Hi,

I am making code to receive and parse data from the advertising packet of ble device.
For now, I can add some code to make this lib get the data of service from advertising packet(no need connect to device) on linux platform, base on bus provider method: _print_tree().
The question is how can I achieve this on Mac OSX with bluetooth core ?

Thanks in advance!

Issues in macOS 10.15 Catalina

Hello there,
Thanks for the library!
Unfortunately, the examples don't seem to run on macOS 10.15
Python: 3.6.4

As soon as a UART capable device is found (i.e I plug the BLE UART Friend in) python crashes.
All the examples throw the same class Assertion error:

** 2019-11-15 23:53:03.304 Python[798:9684] *** Terminating app due to uncaught exception 'OC_PythonException', reason: '<class 'AssertionError'>: <memory at 0x115582348>'
*** First throw call stack:
(
0 CoreFoundation 0x00000001000eed63 __exceptionPreprocess + 250
1 libobjc.A.dylib 0x0000000100ac8bd4 objc_exception_throw + 48
2 CoreFoundation 0x000000010010930c -[NSException raise] + 9
3 _objc.cpython-36m-darwin.so 0x000000010269ea5e PyObjCErr_ToObjCWithGILState + 46
4 _objc.cpython-36m-darwin.so 0x00000001026649c0 -[OC_PythonObject forwardInvocation:] + 736
5 CoreFoundation 0x00000001000952c5 forwarding + 829
6 CoreFoundation 0x0000000100094ef8 _CF_forwarding_prep_0 + 120
7 CoreBluetooth 0x000000010ebbf31b -[CBCentralManager handlePeripheralDiscovered:] + 1577
8 CoreBluetooth 0x000000010ebc0967 -[CBCentralManager handleMsg:args:] + 641
9 CoreBluetooth 0x000000010ebb866a -[CBManager xpcConnectionDidReceiveMsg:args:] + 177
10 CoreBluetooth 0x000000010ebb85ac __30-[CBXpcConnection _handleMsg:]_block_invoke + 50
11 libdispatch.dylib 0x0000000101070553 _dispatch_call_block_and_release + 12
12 libdispatch.dylib 0x00000001010714de _dispatch_client_callout + 8
13 libdispatch.dylib 0x0000000101076a9e _dispatch_lane_serial_drain + 597
14 libdispatch.dylib 0x0000000101077455 _dispatch_lane_invoke + 414
15 libdispatch.dylib 0x000000010107ca91 _dispatch_main_queue_callback_4CF + 783
16 CoreFoundation 0x000000010003e290 CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9
17 CoreFoundation 0x000000010003d9da __CFRunLoopRun + 2370
18 CoreFoundation 0x000000010003ce13 CFRunLoopRunSpecific + 499
19 Foundation 0x000000010381bcad -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 212
20 _objc.cpython-36m-darwin.so 0x000000010269d787 ffi_call_unix64 + 79
21 ??? 0x00000001127984f8 0x0 + 4604921080
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6 **

I'd appreciate any support, Cheers!

ble.find_device name argument on Linux

  • OS: Ubuntu 16.04 Desktop
  • Python Version: 2.7
  • Bluez Version: 5.37-0ubuntu5.1

When using the commands ble.find_device or find_devices, I get an error if I include name. If I omit name, it finds my device and everything works.

This code:

device = ble.find_device(service_uuids=[UART_SERVICE_UUID], timeout_sec=1, name="MyCustomName")

Gives me this error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/mypath/src/uart/controller.py", line 90, in run
    ble.run_mainloop_with(setup)
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 105, in run_mainloop_with
    raise_(self._exception[1], None, self._exception[2])
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 120, in _user_thread_main
    self._return_code = target()
  File "/mypath/src/uart/controller.py", line 41, in setup
    device = ble.find_device(service_uuids=[UART_SERVICE_UUID], timeout_sec=1, name="MyCustomName")
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/interfaces/provider.py", line 135, in find_device
    found = self.find_devices(service_uuids, name)
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/interfaces/provider.py", line 114, in find_devices
    if device.name == name:
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/device.py", line 150, in name
    return self._props.Get(_INTERFACE, 'Name')
  File "/home/user/.local/lib/python2.7/site-packages/dbus/proxies.py", line 145, in __call__
    **keywords)
  File "/home/user/.local/lib/python2.7/site-packages/dbus/connection.py", line 651, in call_blocking
    message, timeout)
DBusException: org.freedesktop.DBus.Error.InvalidArgs: No such property 'Name'

If I remove the name, it eventually finds my device and boots successfully.

Possibly related, I can't use the DeviceInformation function.

This code:

device = ble.find_device(service_uuids=[UART_SERVICE_UUID], timeout_sec=1)
print DeviceInformation(device))

Results in this error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/mypath/src/uart/controller.py", line 95, in run
    ble.run_mainloop_with(setup)
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 105, in run_mainloop_with
    raise_(self._exception[1], None, self._exception[2])
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 120, in _user_thread_main
    self._return_code = target()
  File "/mypath/src/uart/controller.py", line 50, in setup
    print DeviceInformation(device)
  File "build/bdist.linux-x86_64/egg/Adafruit_BluefruitLE/services/device_information.py", line 58, in __init__
    self._manufacturer = self._dis.find_characteristic(MANUFACTURER_CHAR_UUID)
AttributeError: 'NoneType' object has no attribute 'find_characteristic'

I know next to nothing about any of this, but looking at my D-Bus through DFeet, I can find my device under /org/bluez/hci0/my-mac-addr/ and its Properties have a Name key which has the name I'm expecting to find.

Ultimately, what I'm trying to do is try to ensure that a host machine only connects to a specific Bluefruit. In a perfect world, I'd put the MAC address in a config file. In an even more perfect world, I'd ensure that this Bluefruit can't connect to another host machine. If there's a better way to do this that works around this problem, I'm happy to go that route.

Appreciate your help with this! Thank you for this fantastic library and product.

Download error when installed on Mac OS 10.11

I cloned the git repository and installed using the directions provided, but I ran into multiple errors:

$ sudo python setup.py install
running install
Checking .pth file support in /Library/Python/2.7/site-packages/
/usr/bin/python -E -c pass
TEST PASSED: /Library/Python/2.7/site-packages/ appears to support .pth files
running bdist_egg
running egg_info
creating Adafruit_BluefruitLE.egg-info
writing requirements to Adafruit_BluefruitLE.egg-info/requires.txt
writing Adafruit_BluefruitLE.egg-info/PKG-INFO
writing top-level names to Adafruit_BluefruitLE.egg-info/top_level.txt
writing dependency_links to Adafruit_BluefruitLE.egg-info/dependency_links.txt
writing manifest file 'Adafruit_BluefruitLE.egg-info/SOURCES.txt'
reading manifest file 'Adafruit_BluefruitLE.egg-info/SOURCES.txt'
writing manifest file 'Adafruit_BluefruitLE.egg-info/SOURCES.txt'
installing library code to build/bdist.macosx-10.11-intel/egg
running install_lib
running build_py
creating build
creating build/lib
creating build/lib/Adafruit_BluefruitLE
copying Adafruit_BluefruitLE/init.py -> build/lib/Adafruit_BluefruitLE
copying Adafruit_BluefruitLE/config.py -> build/lib/Adafruit_BluefruitLE
copying Adafruit_BluefruitLE/platform.py -> build/lib/Adafruit_BluefruitLE
creating build/lib/Adafruit_BluefruitLE/bluez_dbus
copying Adafruit_BluefruitLE/bluez_dbus/init.py -> build/lib/Adafruit_BluefruitLE/bluez_dbus
copying Adafruit_BluefruitLE/bluez_dbus/adapter.py -> build/lib/Adafruit_BluefruitLE/bluez_dbus
copying Adafruit_BluefruitLE/bluez_dbus/device.py -> build/lib/Adafruit_BluefruitLE/bluez_dbus
copying Adafruit_BluefruitLE/bluez_dbus/gatt.py -> build/lib/Adafruit_BluefruitLE/bluez_dbus
copying Adafruit_BluefruitLE/bluez_dbus/provider.py -> build/lib/Adafruit_BluefruitLE/bluez_dbus
creating build/lib/Adafruit_BluefruitLE/corebluetooth
copying Adafruit_BluefruitLE/corebluetooth/init.py -> build/lib/Adafruit_BluefruitLE/corebluetooth
copying Adafruit_BluefruitLE/corebluetooth/adapter.py -> build/lib/Adafruit_BluefruitLE/corebluetooth
copying Adafruit_BluefruitLE/corebluetooth/device.py -> build/lib/Adafruit_BluefruitLE/corebluetooth
copying Adafruit_BluefruitLE/corebluetooth/gatt.py -> build/lib/Adafruit_BluefruitLE/corebluetooth
copying Adafruit_BluefruitLE/corebluetooth/metadata.py -> build/lib/Adafruit_BluefruitLE/corebluetooth
copying Adafruit_BluefruitLE/corebluetooth/objc_helpers.py -> build/lib/Adafruit_BluefruitLE/corebluetooth
copying Adafruit_BluefruitLE/corebluetooth/provider.py -> build/lib/Adafruit_BluefruitLE/corebluetooth
creating build/lib/Adafruit_BluefruitLE/interfaces
copying Adafruit_BluefruitLE/interfaces/init.py -> build/lib/Adafruit_BluefruitLE/interfaces
copying Adafruit_BluefruitLE/interfaces/adapter.py -> build/lib/Adafruit_BluefruitLE/interfaces
copying Adafruit_BluefruitLE/interfaces/device.py -> build/lib/Adafruit_BluefruitLE/interfaces
copying Adafruit_BluefruitLE/interfaces/gatt.py -> build/lib/Adafruit_BluefruitLE/interfaces
copying Adafruit_BluefruitLE/interfaces/provider.py -> build/lib/Adafruit_BluefruitLE/interfaces
creating build/lib/Adafruit_BluefruitLE/services
copying Adafruit_BluefruitLE/services/init.py -> build/lib/Adafruit_BluefruitLE/services
copying Adafruit_BluefruitLE/services/colorific.py -> build/lib/Adafruit_BluefruitLE/services
copying Adafruit_BluefruitLE/services/device_information.py -> build/lib/Adafruit_BluefruitLE/services
copying Adafruit_BluefruitLE/services/servicebase.py -> build/lib/Adafruit_BluefruitLE/services
copying Adafruit_BluefruitLE/services/uart.py -> build/lib/Adafruit_BluefruitLE/services
creating build/bdist.macosx-10.11-intel
creating build/bdist.macosx-10.11-intel/egg
creating build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE
copying build/lib/Adafruit_BluefruitLE/init.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE
creating build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus
copying build/lib/Adafruit_BluefruitLE/bluez_dbus/init.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus
copying build/lib/Adafruit_BluefruitLE/bluez_dbus/adapter.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus
copying build/lib/Adafruit_BluefruitLE/bluez_dbus/device.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus
copying build/lib/Adafruit_BluefruitLE/bluez_dbus/gatt.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus
copying build/lib/Adafruit_BluefruitLE/bluez_dbus/provider.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus
copying build/lib/Adafruit_BluefruitLE/config.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE
creating build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth
copying build/lib/Adafruit_BluefruitLE/corebluetooth/init.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth
copying build/lib/Adafruit_BluefruitLE/corebluetooth/adapter.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth
copying build/lib/Adafruit_BluefruitLE/corebluetooth/device.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth
copying build/lib/Adafruit_BluefruitLE/corebluetooth/gatt.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth
copying build/lib/Adafruit_BluefruitLE/corebluetooth/metadata.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth
copying build/lib/Adafruit_BluefruitLE/corebluetooth/objc_helpers.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth
copying build/lib/Adafruit_BluefruitLE/corebluetooth/provider.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth
creating build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces
copying build/lib/Adafruit_BluefruitLE/interfaces/init.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces
copying build/lib/Adafruit_BluefruitLE/interfaces/adapter.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces
copying build/lib/Adafruit_BluefruitLE/interfaces/device.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces
copying build/lib/Adafruit_BluefruitLE/interfaces/gatt.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces
copying build/lib/Adafruit_BluefruitLE/interfaces/provider.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces
copying build/lib/Adafruit_BluefruitLE/platform.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE
creating build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services
copying build/lib/Adafruit_BluefruitLE/services/init.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services
copying build/lib/Adafruit_BluefruitLE/services/colorific.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services
copying build/lib/Adafruit_BluefruitLE/services/device_information.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services
copying build/lib/Adafruit_BluefruitLE/services/servicebase.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services
copying build/lib/Adafruit_BluefruitLE/services/uart.py -> build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/init.py to init.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus/init.py to init.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus/adapter.py to adapter.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus/device.py to device.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus/gatt.py to gatt.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py to provider.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/config.py to config.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/init.py to init.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/adapter.py to adapter.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/device.py to device.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/gatt.py to gatt.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/metadata.py to metadata.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/objc_helpers.py to objc_helpers.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/provider.py to provider.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces/init.py to init.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces/adapter.py to adapter.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces/device.py to device.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces/gatt.py to gatt.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/interfaces/provider.py to provider.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/platform.py to platform.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services/init.py to init.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services/colorific.py to colorific.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services/device_information.py to device_information.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services/servicebase.py to servicebase.pyc
byte-compiling build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/services/uart.py to uart.pyc
creating build/bdist.macosx-10.11-intel/egg/EGG-INFO
copying Adafruit_BluefruitLE.egg-info/PKG-INFO -> build/bdist.macosx-10.11-intel/egg/EGG-INFO
copying Adafruit_BluefruitLE.egg-info/SOURCES.txt -> build/bdist.macosx-10.11-intel/egg/EGG-INFO
copying Adafruit_BluefruitLE.egg-info/dependency_links.txt -> build/bdist.macosx-10.11-intel/egg/EGG-INFO
copying Adafruit_BluefruitLE.egg-info/requires.txt -> build/bdist.macosx-10.11-intel/egg/EGG-INFO
copying Adafruit_BluefruitLE.egg-info/top_level.txt -> build/bdist.macosx-10.11-intel/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/Adafruit_BluefruitLE-0.9.10-py2.7.egg' and adding 'build/bdist.macosx-10.11-intel/egg' to it
removing 'build/bdist.macosx-10.11-intel/egg' (and everything under it)
Processing Adafruit_BluefruitLE-0.9.10-py2.7.egg
Copying Adafruit_BluefruitLE-0.9.10-py2.7.egg to /Library/Python/2.7/site-packages
Adding Adafruit-BluefruitLE 0.9.10 to easy-install.pth file

Installed /Library/Python/2.7/site-packages/Adafruit_BluefruitLE-0.9.10-py2.7.egg
Processing dependencies for Adafruit-BluefruitLE==0.9.10
Searching for pyobjc-framework-CoreBluetooth
Reading https://pypi.python.org/simple/pyobjc-framework-CoreBluetooth/
Download error on https://pypi.python.org/simple/pyobjc-framework-CoreBluetooth/: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) -- Some packages may not be found!
Couldn't find index page for 'pyobjc-framework-CoreBluetooth' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
Download error on https://pypi.python.org/simple/: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) -- Some packages may not be found!
No local packages or download links found for pyobjc-framework-CoreBluetooth
error: Could not find suitable distribution for Requirement.parse('pyobjc-framework-CoreBluetooth')

trouble running examples on mac OS

I am new to BLE and Bluetooth. I am not able to run the examples. They never output anything useful? They never seem to find any peripherals

(macBLETest) $ python Adafruit_Python_BluefruitLE/examples/list_uarts.py 
Using adapter: Default Adapter
Searching for UART devices...
Press Ctrl-C to quit (will take ~30 seconds on OSX).
^C(macBLETest) $ 

To try and debug this. I used the example from the blog post https://punchthrough.com/bluetooth-low-energy-peripheral-testing/ . It shows how to use their iOS Lightblue to create a virtual device that their BluefruitLE test python scripts is able to read and write to.

for unknown reasons, the BluefruitLE example do not seem to find the virtual device or any of my real devices.

My env

  • mac os Mojave 10.14.6 (18G2022)
  • Python 3.7.5

Installation process

the version in pypi seems out of date. It rasies NSExceptions

(base) $ conda activate macBLETest
(macBLETest) $ pwd
~/macBLETest/Adafruit_Python_BluefruitLE
(macBLETest) $ git clone https://github.com/adafruit/Adafruit_Python_BluefruitLE.git
(macBLETest) $ python setup.py install
(macBLETest) $ pip install pyobjc

Kind regards

Andy

Occurrd Error with connected USB Serial device

  • Platform/operating system
    Mac OSX 64-Bit
    macOS High Sierra 10.13.1

  • Python version
    python3.6.0

  • Error message you are receiving, including any Python exception traces:

2017-12-04 13:43:00.872 python[691:5388] *** Terminating app due to uncaught exception 'OC_PythonException', reason: '<class 'ValueError'>: bytes is not a 16-char string'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff2d1d82fb __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x00007fff53eefc76 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff2d1d8249 -[NSException raise] + 9
	3   _objc.cpython-36m-darwin.so         0x000000010b2b75ae PyObjCErr_ToObjCWithGILState + 46
	4   _objc.cpython-36m-darwin.so         0x000000010b2e4692 -[OC_PythonObject forwardInvocation:] + 898
	5   CoreFoundation                      0x00007fff2d14e3ac ___forwarding___ + 748
	6   CoreFoundation                      0x00007fff2d14e038 _CF_forwarding_prep_0 + 120
	7   CoreBluetooth                       0x00007fff2cc8a94c -[CBCentralManager handlePeripheralDiscovered:] + 1163
	8   CoreBluetooth                       0x00007fff2cc8b624 -[CBCentralManager handleMsg:args:] + 543
	9   CoreBluetooth                       0x00007fff2cc998ae __30-[CBXpcConnection _handleMsg:]_block_invoke + 53
	10  libdispatch.dylib                   0x00007fff54aac6cd _dispatch_call_block_and_release + 12
	11  libdispatch.dylib                   0x00007fff54aa4e88 _dispatch_client_callout + 8
	12  libdispatch.dylib                   0x00007fff54ab9356 _dispatch_queue_serial_drain + 635
	13  libdispatch.dylib                   0x00007fff54aac239 _dispatch_queue_invoke + 373
	14  libdispatch.dylib                   0x00007fff54ab02fd _dispatch_main_queue_callback_4CF + 776
	15  CoreFoundation                      0x00007fff2d1909c9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
	16  CoreFoundation                      0x00007fff2d152c4a __CFRunLoopRun + 2586
	17  CoreFoundation                      0x00007fff2d151fa3 CFRunLoopRunSpecific + 483
	18  Foundation                          0x00007fff2f2023f6 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 277
	19  _objc.cpython-36m-darwin.so         0x000000010b2f3c1f ffi_call_unix64 + 79
	20  ???                                 0x00007ffee5810400 0x0 + 140732748858368
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

  • List the steps to reproduce the problem below (if possible attach code or commands
    to run):
    It error was occurred in all of example program in this library under connected Arduino via USB.
    Its'not occured with USB memory.
    If you can fix it,I'll be glad.
    Thank you for your develop this library.

Switch between BLE adapters

in provider.py function get_default_adapter returns first adapter by default.

The problem is if there are multiple adapters, or in my case USB-BLE key with Bluetooth and BLE functionality.
In this case, first adapter (Bluetooth) is selected by default, and there is no option to switch to BLE.

Exceptions are platform specific

  • Platform/operating system (i.e. Raspberry Pi with Raspbian operating system,
    Windows 32-bit, Windows 64-bit, Mac OSX 64-bit, etc.): Ubuntu 18

  • Python version (run python -version or python3 -version): 3.5

  • Error message you are receiving, including any Python exception traces: dbus.exceptions.DBusException not handled by library

  • List the steps to reproduce the problem below (if possible attach code or commands
    to run):

adapter = ble.get_default_adapter()
adapter.power_on()
adapter.start_scan()
... find devices
adapter.stop_scan() <--- Sometimes raises a dbus.exceptions.DBusException

dbus exceptions should not make it to the application interface as are platform (linux) specific right? They should be converted to a library specific or generic exception type. Now my application exception handling code needs to import and handle dbus exception types.

Thanks for the handly library, still the best option around IMO...

TypeError exception on Device.disconnect() for Sphero BB-8 on OS X

Built off from one of the example scripts to connect with a Sphero BB-8 toy, list its advertised BLE services, and disconnect. On calling disconnect(), I see this error:

Traceback (most recent call last):
  File "<my project dir>/src/test.py", line 58, in <module>
    provider.run_mainloop_with(main)
  File "build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/provider.py", line 265, in run_mainloop_with
  File "/Library/Python/2.7/site-packages/PyObjCTools/AppHelper.py", line 182, in runConsoleEventLoop
    if not runLoop.runMode_beforeDate_(mode, nextfire):
  File "/Library/Python/2.7/site-packages/PyObjCTools/AppHelper.py", line 41, in call_
    func(*args, **kwargs)
  File "build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/provider.py", line 274, in _user_thread_main
  File "<my project dir>/src/test.py", line 53, in main
    bb8.disconnect()
  File "build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/device.py", line 73, in disconnect
  File "build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/gatt.py", line 59, in list_characteristics
  File "build/bdist.macosx-10.11-intel/egg/Adafruit_BluefruitLE/corebluetooth/metadata.py", line 63, in get_all
TypeError: 'NoneType' object is not iterable

My test scripts are here: https://gist.github.com/wolfteeth/9baf0133c582fc08e518

I'm not sure if I'm doing something wrong, or if there's something unusual about the device that the library wasn't ready for.

Incompatibility with BlueZ version >= 5.38

There is a known incompatible with BlueZ versions greater than 5.37, since the API was changed in 5.38 to fully conform to D-Bus specs.

This results in a device.find_characteristic(uuid) call failing because paths = self._props.Get(_SERVICE_INTERFACE, 'Characteristics') in bluez_dbus/gatt.py throws an exception with the Characteristics property doesn't exist.

The fix is relatively simple (although I'd ended up writing almost a complete wrapper for Bluez D-Bus by the time I figured it out), the GATT Characteristics are now objects in the tree below the Service objects, rather than being properties.

Patch: gatt_fix_characteristics_bluez_5.38.patch.txt

This patch could be edited to retain compatibility with pre-5.38 versions of BlueZ, it would take a version check in the setup process and alternate code paths. Happy to add those if it means the patch can be merged. In the meantime, this issue should help anyone coming fresh to this library and not wanting to revert to an older BlueZ.

hcitool lescan shows BLE device but python list_uarts.py does not

Just starting to learn about working with Bluetooth BLE devices and I am having an issue.

hcitool lescan shows BLE device but python list_uarts.py does not (see below):

hciconfig example:

pi@rasp01 ~/bluefruitle/Adafruit_Python_BluefruitLE/examples $ hciconfig
hci0: Type: BR/EDR Bus: USB
BD Address: 5C:F3:70:6C:3A:DC ACL MTU: 1021:8 SCO MTU: 64:1
UP RUNNING
RX bytes:2591 acl:0 sco:0 events:164 errors:0
TX bytes:1522 acl:0 sco:0 commands:128 errors:0

hcitool lescan example:

pi@rasp01 ~/bluefruitle/Adafruit_Python_BluefruitLE/examples $ sudo hcitool lescan
LE Scan ...
74:DA:EA:AC:96:68 (unknown)
74:DA:EA:AC:96:68 HMSensor

python list_uarts.py example:

pi@rasp01 ~/bluefruitle/Adafruit_Python_BluefruitLE/examples $ sudo python list_uarts.py
Using adapter: BlueZ 5.33
Searching for UART devices...
Press Ctrl-C to quit (will take ~30 seconds on OSX).
(nothing shows up here after waiting for 30 to 40 seconds so I use Ctrl-C to end process)

P.S. I am using the following BLE device from Amazon/Diymall.
http://www.amazon.com/Diymall%C2%AE-Bluetooth-Near-field-Positioning-Acquisition/dp/B00O2UDZU6

org.freedesktop.DBus.Error.LimitsExceeded: Connection ":1.26" is not allowed to add more match rules

Thank you for opening an issue on an Adafruit Python library repository. To
improve the speed of resolution please review the following guidelines and
common troubleshooting steps below before creating the issue:

  • Do not use GitHub issues for troubleshooting projects and issues. Instead use
    the forums at http://forums.adafruit.com to ask questions and troubleshoot why
    something isn't working as expected. In many cases the problem is a common issue
    that you will more quickly receive help from the forum community. GitHub issues
    are meant for known defects in the code. If you don't know if there is a defect
    in the code then start with troubleshooting on the forum first.

  • If following a tutorial or guide be sure you didn't miss a step. Carefully
    check all of the steps and commands to run have been followed. Consult the
    forum if you're unsure or have questions about steps in a guide/tutorial.

  • For Python/Raspberry Pi projects check these very common issues to ensure they don't apply:

    • If you are receiving an ImportError: No module named... error then a
      library the code depends on is not installed. Check the tutorial/guide or
      README to ensure you have installed the necessary libraries. Usually the
      missing library can be installed with the pip tool, but check the tutorial/guide
      for the exact command.

    • Be sure you are supplying adequate power to the board. Check the specs of
      your board and power in an external power supply. In many cases just
      plugging a board into your computer is not enough to power it and other
      peripherals.

    • Double check all soldering joints and connections. Flakey connections
      cause many mysterious problems. See the guide to excellent soldering for examples of good solder joints.

If you're sure this issue is a defect in the code and checked the steps above
please fill in the following fields to provide enough troubleshooting information.
You may delete the guideline and text above to just leave the following details:

  • Platform/operating system (i.e. Raspberry Pi with Raspbian operating system,
    Windows 32-bit, Windows 64-bit, Mac OSX 64-bit, etc.): RPI Raspbian

  • Python version (run python -version or python3 -version): 3.5.3

  • Error message you are receiving, including any Python exception traces: Traceback (most recent call last):
    File "/home/pi/Adafruit_Python_BluefruitLE/examples/scan_test.py", line 56, in
    ble.run_mainloop_with(main)
    File "/home/pi/Adafruit_Python_BluefruitLE/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 105, in run_mainloop_with
    raise_(self._exception[1], None, self._exception[2])
    File "/home/pi/Adafruit_Python_BluefruitLE/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 120, in _user_thread_main
    self._return_code = target()
    File "/home/pi/Adafruit_Python_BluefruitLE/examples/scan_test.py", line 40, in main
    for device in set(ble.find_devices()):
    File "/home/pi/Adafruit_Python_BluefruitLE/Adafruit_BluefruitLE/interfaces/provider.py", line 109, in find_devices
    devices = self.list_devices()
    File "/home/pi/Adafruit_Python_BluefruitLE/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 170, in list_devices
    return map(BluezDevice, self._get_objects('org.bluez.Device1'))
    File "/home/pi/Adafruit_Python_BluefruitLE/Adafruit_BluefruitLE/bluez_dbus/device.py", line 53, in init
    self._props.connect_to_signal('PropertiesChanged', self._prop_changed)
    File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 544, in connect_to_signal
    dbus_interface, **keywords)
    File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 369, in connect_to_signal
    **keywords)
    File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 157, in add_signal_receiver
    watch = self.watch_name_owner(bus_name, callback)
    File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 373, in watch_name_owner
    return NameOwnerWatch(self, bus_name, callback)
    File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 80, in init
    arg0=bus_name)
    File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 160, in add_signal_receiver
    self.add_match_string(str(match))
    File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 398, in add_match_string
    BUS_DAEMON_IFACE, 'AddMatch', 's', (rule,))
    File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
    message, timeout)
    dbus.exceptions.DBusException: org.freedesktop.DBus.Error.LimitsExceeded: Connection ":1.26" is not allowed to add more match rules (increase limits in configuration file if required)
    Error in atexit._run_exitfuncs:
    Traceback (most recent call last):
    File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
    File "/home/pi/Adafruit_Python_BluefruitLE/Adafruit_BluefruitLE/bluez_dbus/adapter.py", line 76, in stop_scan
    self._adapter.StopDiscovery()
    File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in call
    **keywords)
    File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
    message, timeout)
    DBusException: org.bluez.Error.Failed: No discovery started
    Error in sys.exitfunc:
    Traceback (most recent call last):
    File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
    File "/home/pi/Adafruit_Python_BluefruitLE/Adafruit_BluefruitLE/bluez_dbus/adapter.py", line 76, in stop_scan
    self._adapter.StopDiscovery()
    File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in call
    **keywords)
    File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
    message, timeout)
    dbus.exceptions.DBusException: org.bluez.Error.Failed: No discovery started

  • List the steps to reproduce the problem below (if possible attach code or commands
    to run):

Search for BLE Device devices and list all that are found.

Author: Tony DiCola

import atexit
import time

import Adafruit_BluefruitLE

interval = 5

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()

# Start scanning with the bluetooth adapter.
adapter.start_scan()
# Use atexit.register to call the adapter stop_scan function before quiting.
# This is good practice for calling cleanup code in this main function as
# a try/finally block might not be called since this is a background thread.
atexit.register(adapter.stop_scan)
print('Searching for BLE devices...')
# Enter a loop and print out whenever a new Device device is found.
lastTime = time.time()
while True:
    if time.time() - lastTime >= interval:
        adapter.stop_scan()
        for device in set(ble.find_devices()):
            print('Found Device: {0}'.format(device.id))
    
        print('new check')
        ble.clear_cached_data()
        lastTime = time.time()
        adapter.start_scan()

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)

OC_PythonException/NSException on example script

Hello,

I'm trying to get AdaFruit_Python_BluefruitLE working under macOS 10.3.1, Python 3.6.3 (brew install python3) and I'm getting the following error when trying the examples:

$ python3 examples/device_info.py
Using adapter: Default Adapter
Disconnecting any connected UART devices...
Searching for UART device...
2017-12-02 14:57:07.554 Python[97493:18996787] *** Terminating app due to uncaught exception 'OC_PythonException', reason: '<class 'AssertionError'>: <memory at 0x10d04fd08>'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fff4c7d62fb __exceptionPreprocess + 171
        1   libobjc.A.dylib                     0x00007fff73143c76 objc_exception_throw + 48
        2   CoreFoundation                      0x00007fff4c7d6249 -[NSException raise] + 9
        3   _objc.cpython-36m-darwin.so         0x000000010b89779e PyObjCErr_ToObjCWithGILState + 46
        4   _objc.cpython-36m-darwin.so         0x000000010b8ce832 -[OC_PythonObject forwardInvocation:] + 898
        5   CoreFoundation                      0x00007fff4c74c3ac ___forwarding___ + 748
        6   CoreFoundation                      0x00007fff4c74c038 _CF_forwarding_prep_0 + 120
        7   CoreBluetooth                       0x00007fff4c28894c -[CBCentralManager handlePeripheralDiscovered:] + 1163
        8   CoreBluetooth                       0x00007fff4c289624 -[CBCentralManager handleMsg:args:] + 543
        9   CoreBluetooth                       0x00007fff4c2978ae __30-[CBXpcConnection _handleMsg:]_block_invoke + 53
        10  libdispatch.dylib                   0x00007fff73d006cd _dispatch_call_block_and_release + 12
        11  libdispatch.dylib                   0x00007fff73cf8e88 _dispatch_client_callout + 8
        12  libdispatch.dylib                   0x00007fff73d0d356 _dispatch_queue_serial_drain + 635
        13  libdispatch.dylib                   0x00007fff73d00239 _dispatch_queue_invoke + 373
        14  libdispatch.dylib                   0x00007fff73d042fd _dispatch_main_queue_callback_4CF + 776
        15  CoreFoundation                      0x00007fff4c78e9c9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
        16  CoreFoundation                      0x00007fff4c750c4a __CFRunLoopRun + 2586
        17  CoreFoundation                      0x00007fff4c74ffa3 CFRunLoopRunSpecific + 483
        18  Foundation                          0x00007fff4e8003f6 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 277
        19  _objc.cpython-36m-darwin.so         0x000000010b8d6aaf ffi_call_unix64 + 79
        20  ???                                 0x00007ffee5087290 0x0 + 140732740956816
)
libc++abi.dylib: terminating with uncaught exception of type NSException

It might be a PyObjC issues, but has anyone seen/solved this?

$ pip3 freeze | grep pyobjc
pyobjc==4.0.1
pyobjc-core==4.0.1
pyobjc-framework-Accounts==4.0.1
pyobjc-framework-AddressBook==4.0.1
pyobjc-framework-AppleScriptKit==4.0.1
pyobjc-framework-AppleScriptObjC==4.0.1
pyobjc-framework-ApplicationServices==4.0.1
pyobjc-framework-Automator==4.0.1
pyobjc-framework-AVFoundation==4.0.1
pyobjc-framework-AVKit==4.0.1
pyobjc-framework-CalendarStore==4.0.1
pyobjc-framework-CFNetwork==4.0.1
pyobjc-framework-CloudKit==4.0.1
pyobjc-framework-Cocoa==4.0.1
pyobjc-framework-Collaboration==4.0.1
pyobjc-framework-ColorSync==4.0.1
pyobjc-framework-Contacts==4.0.1
pyobjc-framework-ContactsUI==4.0.1
pyobjc-framework-CoreBluetooth==4.0.1
pyobjc-framework-CoreData==4.0.1
pyobjc-framework-CoreLocation==4.0.1
pyobjc-framework-CoreML==4.0.1
pyobjc-framework-CoreServices==4.0.1
pyobjc-framework-CoreSpotlight==4.0.1
pyobjc-framework-CoreText==4.0.1
pyobjc-framework-CoreWLAN==4.0.1
pyobjc-framework-CryptoTokenKit==4.0.1
pyobjc-framework-DictionaryServices==4.0.1
pyobjc-framework-DiskArbitration==4.0.1
pyobjc-framework-EventKit==4.0.1
pyobjc-framework-ExceptionHandling==4.0.1
pyobjc-framework-ExternalAccessory==4.0.1
pyobjc-framework-FinderSync==4.0.1
pyobjc-framework-FSEvents==4.0.1
pyobjc-framework-GameCenter==4.0.1
pyobjc-framework-GameController==4.0.1
pyobjc-framework-GameKit==4.0.1
pyobjc-framework-GameplayKit==4.0.1
pyobjc-framework-ImageCaptureCore==4.0.1
pyobjc-framework-IMServicePlugIn==4.0.1
pyobjc-framework-InputMethodKit==4.0.1
pyobjc-framework-InstallerPlugins==4.0.1
pyobjc-framework-InstantMessage==4.0.1
pyobjc-framework-Intents==4.0.1
pyobjc-framework-IOSurface==4.0.1
pyobjc-framework-iTunesLibrary==4.0.1
pyobjc-framework-LatentSemanticMapping==4.0.1
pyobjc-framework-LaunchServices==4.0.1
pyobjc-framework-LocalAuthentication==4.0.1
pyobjc-framework-MapKit==4.0.1
pyobjc-framework-MediaAccessibility==4.0.1
pyobjc-framework-MediaLibrary==4.0.1
pyobjc-framework-MediaPlayer==4.0.1
pyobjc-framework-ModelIO==4.0.1
pyobjc-framework-MultipeerConnectivity==4.0.1
pyobjc-framework-NetFS==4.0.1
pyobjc-framework-NetworkExtension==4.0.1
pyobjc-framework-NotificationCenter==4.0.1
pyobjc-framework-OpenDirectory==4.0.1
pyobjc-framework-Photos==4.0.1
pyobjc-framework-PhotosUI==4.0.1
pyobjc-framework-PreferencePanes==4.0.1
pyobjc-framework-PubSub==4.0.1
pyobjc-framework-QTKit==4.0.1
pyobjc-framework-Quartz==4.0.1
pyobjc-framework-SafariServices==4.0.1
pyobjc-framework-SceneKit==4.0.1
pyobjc-framework-ScreenSaver==4.0.1
pyobjc-framework-ScriptingBridge==4.0.1
pyobjc-framework-SearchKit==4.0.1
pyobjc-framework-ServiceManagement==4.0.1
pyobjc-framework-Social==4.0.1
pyobjc-framework-SpriteKit==4.0.1
pyobjc-framework-StoreKit==4.0.1
pyobjc-framework-SyncServices==4.0.1
pyobjc-framework-SystemConfiguration==4.0.1
pyobjc-framework-Vision==4.0.1
pyobjc-framework-WebKit==4.0.1

Cheers,
Josh

AttributeError: 'NoneType' object has no attribute 'power_on'

Hi I am trying to run any of your examples but I keep getting the following error no matter what example I try. Can you please explain what is going on?

root@beaglebone:~/Adafruit_Python_BluefruitLE/examples# python low_level.py
Traceback (most recent call last):
File "low_level.py", line 106, in
ble.run_mainloop_with(main)
File "build/bdist.linux-armv7l/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 105, in run_mainloop_with
File "build/bdist.linux-armv7l/egg/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 120, in _user_thread_main
File "low_level.py", line 38, in main
adapter.power_on()
AttributeError: 'NoneType' object has no attribute 'power_on'

dbus.exceptions.DBusException No such property 'Characteristics'

I get a Exception when running the example script. Same here!

me@mypc:~/Adafruit_Python_BluefruitLE-master/examples$ python3 uart_service.py 
Using adapter: Yoga
Disconnecting any connected UART devices...
Searching for UART device...
Connecting to device...
Discovering services...
Traceback (most recent call last):
  File "uart_service.py", line 85, in <module>
    ble.run_mainloop_with(main)
  File "/home/pascal/.local/lib/python3.6/site-packages/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 105, in run_mainloop_with
    raise_(self._exception[1], None, self._exception[2])
  File "/home/pascal/.local/lib/python3.6/site-packages/future/utils/__init__.py", line 416, in raise_
    raise exc
  File "/home/pascal/.local/lib/python3.6/site-packages/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 120, in _user_thread_main
    self._return_code = target()
  File "uart_service.py", line 59, in main
    uart = UART(device)
  File "/home/pascal/.local/lib/python3.6/site-packages/Adafruit_BluefruitLE/services/uart.py", line 50, in __init__
    self._tx = self._uart.find_characteristic(TX_CHAR_UUID)
  File "/home/pascal/.local/lib/python3.6/site-packages/Adafruit_BluefruitLE/interfaces/gatt.py", line 48, in find_characteristic
    for char in self.list_characteristics():
  File "/home/pascal/.local/lib/python3.6/site-packages/Adafruit_BluefruitLE/bluez_dbus/gatt.py", line 56, in list_characteristics
    paths = self._props.Get(_SERVICE_INTERFACE, 'Characteristics')
  File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 145, in __call__
    **keywords)
  File "/usr/lib/python3/dist-packages/dbus/connection.py", line 651, in call_blocking
    message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.InvalidArgs: No such property 'Characteristics'

I installed this module with pip3 install --user Adafruit-BluefruitLE

Any help? Maybe just updating the pypi package

Ctrl-C doesn't interrupt list_uarts.py on OSX 10.9.5

list_uarts.py never exits even after Ctrl-C is typed. The "while True" loop in list_uarts continues to run once per second and the "except KeyboardInterrupt" clause in corebluetooth/provider.py is never executed. Sending other signals to the process (e.g. HUP, TERM) cause it to exit, but not SIGINT. This is with the standard Apple supplied python 2.7.5 that comes with OSX 10.9.5.

no module named 'dbus'

  • Platform/operating system (i.e. Raspberry Pi with Raspbian operating system,
    Windows 32-bit, Windows 64-bit, Mac OSX 64-bit, etc.):
$ uname -a
Linux Ridl3y 4.19.27-gentoo-r1 #1 SMP Tue Mar 19 20:10:14 EDT 2019 x86_64 Intel(R) Core(TM) i7-7600U CPU @ 2.80GHz GenuineIntel GNU/Linux
  • Python version (run python -version or python3 -version):
$ python3 --version
Python 3.6.5
  • Error message you are receiving, including any Python exception traces:
$ python3 list_uarts.py 
Traceback (most recent call last):
  File "list_uarts.py", line 11, in <module>
    ble = Adafruit_BluefruitLE.get_provider()
  File "/home/msirabella/Documents/projects/blueduck/venv/lib/python3.6/site-packages/Adafruit_BluefruitLE/platform.py", line 38, in get_provider
    from .bluez_dbus.provider import BluezProvider
  File "/home/msirabella/Documents/projects/blueduck/venv/lib/python3.6/site-packages/Adafruit_BluefruitLE/bluez_dbus/provider.py", line 29, in <module>
    import dbus
ModuleNotFoundError: No module named 'dbus'
  • List the steps to reproduce the problem below (if possible attach code or commands
    to run):
    • install library in virtual environment
      $ pip install git+http://github.com/adafruit/Adafruit_Python_BluefruitLE.git
    • clone repository
      $ git clone [email protected]:adafruit/Adafruit_Python_BluefruitLE.git
    • run example
      $ python3 Adafruit_Python_BluefruitLE/examples/list_uarts.py

$ pip install dbus-python fixed this issue

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.