Skip to main content

Python interface for BaBLE

Project description

baBLE - Python interface

This is the cross-platform Python interface to communicate with the platform-dependent baBLE bridge, to use native Bluetooth Low Energy in your projects.

Usage

import bable_interface  # Import the library
import time

bable = bable_interface.BaBLEInterface()  # Create a BaBLEInterface object to send commands
connections = {}

def on_notification_received(success, result, failure_reason):
    print("Notification received:", result, failure_reason)


def on_connected(success, result, failure_reason):
    print("Connected callback called", result, failure_reason)
    if not success:
        bable.cancel_connection()
    else:
        connections[result['address']] = result
        char = bable_interface.Characteristic(uuid='1234', handle='0x000a', value_handle='0x000b')
        # Command can be forced to be asynchronous by setting `sync` to False (if command is sync by default)
        # It is often useful if you want to call a command into a callback function (because they must not be blocking)
        bable.set_notification(True, result['connection_handle'], char, on_notification_received=on_notification_received, sync=False)


def on_unexpected_disconnection(success, result, failure_reason):
    print("Device has been unexpectedly disconnected", result, failure_reason)


def on_device_found(success, result, failure_reason):
    print("Device found:", result, failure_reason)
    if result['address'] == "11:22:33:44:55:66":
        print("Connecting...")
        # You can precise timeout duration (meaning the time before an exception is raised if no response has been received)
        # by setting the `timeout` parameter with the value you want (in seconds)
        bable.connect(result['address'], result['address_type'], on_connected, on_unexpected_disconnection, timeout=5.0)


def on_disconnected(success, result, failure_reason):
    print("Disconnected callback called", result, failure_reason)


def on_error(status, message):
    print("Error received:", status, message)
    bable.stop(sync=False)


# Start the bable interface (meaning starting the threads and the subprocess needed to make it work)
# You can restrict baBLE to only one controller by setting the controller_id (if you want to use multiple controllers)
bable.start(on_error=on_error, controller_id=1)

# Commands that could take a long time are asynchronous by default: result is sent by calling a callback function.
# If an error occures, callback function will be called with `success` parameter as False and failure_reason as the error
bable.start_scan(on_device_found, timeout=1)

time.sleep(10)  # We can sleep here without blocking the interface (because it is running in another thread)

try:
    print("Disconnecting...")
    # Command can be forced to be synchronous by setting `sync` to True (if command is async by default)
    bable.disconnect(connections['11:22:33:44:55:66'], sync=True)  # Will block until disconnected
    print("Disconnected")
except Exception as e:  # Synchronous commands raise exception on error
    print("Error while disconnecting:", e)

# When we have finished, we have to stop the bable interface to cleanly terminate the threads and the subprocess.
bable.stop()
print("End")

Installation

Currently, only Linux is available (Windows and Mac coming soon...)

From Pypi

It is recommended to install it from Pypi to have a ready-to-go package with a pre-compiled bridge in it.

pip install bable-interface

This technique will work for for x86_64, i686 and armv7l architecture.

From source

You can also build it from source but you'll have to compile the bridge by yourself (cf platforms/ to see how to build your bridge from source).

$ git clone https://github.com/iotile/baBLE.git
$ cd baBLE/interfaces/python

# Here you can either copy your compiled bridge to the bin folder to add it to your package...
$ cp <path_to_your_bridge_exe> ./bable_interface/bin

# ... or you can add the folder where the bridge executable is to your PATH (and add use_path=True to BaBLEInterface.start())
$ export PATH=<path_to_your_bridge_exe_folder>:$PATH

$ python setup.py install

"bable" script

After installing the Python bable interface, you'll have access to the bable command from your shell. It could be useful for 2 things:

How to use it without sudo

By default, on Linux, you need to have root rights to communicate with the Bluetooth subkernel. But if you don't want to execute your program as sudo, you can simply set the capabilities of the bridge executable. The simplest way to do so is simply to run:

$ bable --set-cap

This will simply run the following command, that you can obviously run by yourself, especially if you want to use a different bable bridge:

$ sudo setcap cap_net_raw,cap_net_admin+eip <path_to_your_bridge_exe>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bable-interface-1.2.1.tar.gz (835.7 kB view details)

Uploaded Source

Built Distributions

bable_interface-1.2.1-py3-none-linux_armv7l.whl (866.8 kB view details)

Uploaded Python 3

bable_interface-1.2.1-py2-none-linux_armv7l.whl (866.8 kB view details)

Uploaded Python 2

File details

Details for the file bable-interface-1.2.1.tar.gz.

File metadata

  • Download URL: bable-interface-1.2.1.tar.gz
  • Upload date:
  • Size: 835.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/2.7.14

File hashes

Hashes for bable-interface-1.2.1.tar.gz
Algorithm Hash digest
SHA256 79051b232c1fa11d036d0aa2b17d0cf77ded854e6dfa5adaeeb4f2bdb0e5e854
MD5 82a45f20aaffb7b48322afa44a265330
BLAKE2b-256 2bf2a1bddb4a628dbb3765a3d9c1c409dd648aec9093043a3eda1d661106f0fc

See more details on using hashes here.

File details

Details for the file bable_interface-1.2.1-py3-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: bable_interface-1.2.1-py3-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.3

File hashes

Hashes for bable_interface-1.2.1-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d7aad5aa7d9d832b61f28a42a841dc5a1ce87f430210195c3c0cb45e431cb3d6
MD5 0996a34d8679d81e7d01fb44028dbf89
BLAKE2b-256 fe41448160026a4895bdb38b794259365d95a35cb04d3f6918be4d080aaab31e

See more details on using hashes here.

File details

Details for the file bable_interface-1.2.1-py3-none-manylinux1_i686.whl.

File metadata

  • Download URL: bable_interface-1.2.1-py3-none-manylinux1_i686.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.3

File hashes

Hashes for bable_interface-1.2.1-py3-none-manylinux1_i686.whl
Algorithm Hash digest
SHA256 69ae0e9d9ab28ebf16b625b6017e37dcbc7bdabbbf1d3de72d745ec1fb5a760d
MD5 b741fceaa73076e7d2132983b440c686
BLAKE2b-256 7211d799e56df8ace8012e00ad99190f12e49e721d5e6dd4589d49ec1d9b3188

See more details on using hashes here.

File details

Details for the file bable_interface-1.2.1-py3-none-linux_armv7l.whl.

File metadata

  • Download URL: bable_interface-1.2.1-py3-none-linux_armv7l.whl
  • Upload date:
  • Size: 866.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.3

File hashes

Hashes for bable_interface-1.2.1-py3-none-linux_armv7l.whl
Algorithm Hash digest
SHA256 af928db6f500ab12d1f73ab96a3303e59fcaf4b8f3feb30e9e53a662b1192bd0
MD5 61043b42ee7be5d5aee0496a9de2b281
BLAKE2b-256 227318d06aec7d9ab6a31b1f81001b4f7fd4827961fa590455edeb83a15ece4e

See more details on using hashes here.

File details

Details for the file bable_interface-1.2.1-py2-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: bable_interface-1.2.1-py2-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/2.7.14

File hashes

Hashes for bable_interface-1.2.1-py2-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1e6b8b4d47905ca34efedaf0296b58c56e651679cf3e13b563e8b3ebd5b1e631
MD5 998ff08846c77c6624090a75e1bd157c
BLAKE2b-256 963a5c7b47dcc412c8b98683d1e406702a90eef9ad5439be1856854b541e2ed6

See more details on using hashes here.

File details

Details for the file bable_interface-1.2.1-py2-none-manylinux1_i686.whl.

File metadata

  • Download URL: bable_interface-1.2.1-py2-none-manylinux1_i686.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/2.7.14

File hashes

Hashes for bable_interface-1.2.1-py2-none-manylinux1_i686.whl
Algorithm Hash digest
SHA256 aac692d9875c18a6f188f0ea9a1bdd521271c68eb3732f8e4575671f4e05a617
MD5 fee27a4ff404b810b7b5d4754f349919
BLAKE2b-256 409204eeb0817162f532e3cb309b86e5d3e341dad7ecac8a7a252465e2effa09

See more details on using hashes here.

File details

Details for the file bable_interface-1.2.1-py2-none-linux_armv7l.whl.

File metadata

  • Download URL: bable_interface-1.2.1-py2-none-linux_armv7l.whl
  • Upload date:
  • Size: 866.8 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/2.7.14

File hashes

Hashes for bable_interface-1.2.1-py2-none-linux_armv7l.whl
Algorithm Hash digest
SHA256 3351df5f19f9fbb49bb542e25027b632fc426d82fed046d9041373d3abb119a2
MD5 d20fba15f30226541fe47d4893d2b60b
BLAKE2b-256 171f92bfb38d14b186363fc3dc78552b6ab0f5e0201cc7a8bcb950afd0847ac8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page