Python Ble Library Mac

RASPBERRY PI/PI A+, B+, 2, 3PROGRAMMING/PYTHONINTERNET OF THINGS - IOT/BLUEFRUIT / BLE
  • Nov 01, 2018  Bluetooth GATT SDK for Python. Contribute to getsenic/gatt-python development by creating an account on GitHub. Bluetooth GATT SDK for Python. Contribute to getsenic/gatt-python development by creating an account on GitHub. Currently Linux is the only platform supported by this library. Unlike other libraries this GATT SDK is based directly.
  • Dec 03, 2018  Python module for interfacing with BLE devices through Bluez.
  • Jul 18, 2019  BlueST SDK. BlueST SDK is a multi-platform library (Android, iOS, and Linux supported) that allows easy access to the data exported by a Bluetooth Low Energy (BLE) device that implements the BlueST Protocol.Documentation. Documentation can be found here. This version of the SDK is compatible with Python 3.5 and runs on a Linux system.

So you've got a Bluefruit LE device that's ready to be the next awesome wireless IoT gadget, but how do you actually talk to it from your computer? The Bluefruit LE Python library is just what you need to write code that reads and writes data with a Bluefruit LE device! This Python library allows you to write simple code to talk to a Bluefruit LE UART from a Mac OSX computer or Linux machine, like a Raspberry Pi. This library is great for logging sensor data, controlling your device, and much more through the wireless magic of Bluetooth low energy!

Library

To use this library you'll need to be running a Mac OSX or Linux machine with a Bluetooth 4.0 low energy adapter. Sorry Windows is not yet supported because Bluetooth low energy support is still a little bit too new to the platform (only the very latest Windows 10 release exposes enough BLE APIs to completely control a device). In the future Windows support might be added, but for now stick with a Linux machine, like a Raspberry Pi, or a Mac OSX desktop/laptop.

Python Bluetooth LE (Low Energy) and GATT Library. The primary API for users of this library is provided by pygatt.BLEBackend and pygatt.BLEDevice.After initializing an instance of the preferred backend (available implementations are found in pygatt.backends, use the BLEBackend.connect method to connect to a device and get an instance of BLEDevice. This Python library allows you to write simple code to talk to a Bluefruit LE UART from a Mac OSX computer or Linux machine, like a Raspberry Pi. This library is great for logging sensor data, controlling your device, and much more through the wireless magic of Bluetooth low energy!

You'll also want to be somewhat familiar with the Python programming language and Bluetooth low energy. The Hitchhiker's Guide to Python has a great learning python section with links to books and free resources for learning the language. For Bluetooth low energy be sure to read this excellent intro guide and even consider picking up a book on the topic.

When you're ready continue on to learn about what hardware you'll need to use the Bluefruit LE Python library.

This guide was first published on Aug 18, 2015. It was lastupdated on Aug 18, 2015.This page (Overview) was last updated on Apr 10, 2020.

COMMUNICATION WITH BLUETOOTH

The source code of all examples can be downloaded from here.


Data transmission with Bluetooth

Python Ble Library Mac Os

For data exchange between two Raspberry Pi's or a Raspberry Pi and a computer or smartphone, the Bluetooth protocol is often a good choice. It may be simpler than using TCP/ IP, because no TCP-network (WLAN, router) is needed. Some application proposals are:

  • The Raspberry Pi carries out data acquisition with sensors and continuously reports the measured data to a PC (remote sensing)
  • A moving robot (rover) using an embedded Raspberry Pi is controlled by a remote console (remote-guidance)
  • The Raspberry Pi receives control data from an external device, e.g. a smartphone, that acts like an external keyboard
  • Two (or more) Raspberry Pi's share common tasks

The latest version of the NOOPs distribution supports the built-in Bluetooth chip of the Raspberry model 3 and of the Raspberry ZeroW as well as most USB Bluetooth dongles on the Raspberry Pi model 2. The current version of the RaspiBrick firmware downloaded from here is based on this operating system version. On startup, Bluetooth is activated and placed in Discovery Mode. An external device recognizes the Raspberry Pi with the Bluetooth friendly name raspberrypi and can pair without authentication. On the other hand, the Raspberry Pi can connect itself to a visible external Bluetooth device without pairing.

As with TCP/IP, data transmission is performed using the client/server technology. It is important to know that the communication partners, the server and client are not symmetric. First the server program has to be started and only then a client can establish a connection.
Each Bluetooth device has a Bluetooth-friendly name and a unique 48-bit Bluetooth Mac address in the hexadecimal form nn: nn: nn: nn: nn: nn (n is a hex digit 0.9, A, B, C, D, E, F), which is fixed in the Bluetooth hardware. (In addition to authentication, pairing is used to determine the MAC address and store it together with the Bluetooth name.)
A Bluetooth server provides its services at start-up via a service name. An external device can perform a Bluetooth search to find the server with a particular service name and determine both the Bluetooth name and the Bluetooth-Mac address. Python Bluetooth programming is greatly simplified when using our user-friendly libraries. They are event-driven and similarly usable under standard Python (for the Raspberry Pi and PCs with Python2.7), as in TigerJython. A stream based Bluetooth library for Java SE and Android
is also available.

Python 2.7 Bluetooth library (inkl Doc), Doc online
TigerJython Bluetooth library (included in TigerJython distribution), Doc online
Java SE Bluetooth library Java + Doc, Doc online
Android Bluetooth is part of JDroidLib

Python For Mac Download

Experiment 1: Bluetooth Echo Client/Server

Aim:
On the Raspberry Pi runs a Bluetooth server waiting for a connecting client. After the connection of a PC client, the client sends the numbers 0 to 100 to the server. The received numbers are sent back unmodified (like an echo).

Program at the Raspberry Pi:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

Remarks:
The code is extremely simple: When BTServer is instantiated, the service name and the callback function are specified. In the callback, status changes are written out and the received message is sent back. Messages are always strings which are supplemented by a character 0 (zero-terminated 8-bit ASCII strings). The terminator character is removed transparently at the receiver.
In the client program, an instance of BTClient is created, specifying the callback function. Subsequently, the client performs a Bluetooth search with the given service name, which is limited to a maximum duration of 20 s. If the search is successful, a tuple is returned that contains the Bluetooth Mac address of the server and the Bluetooth channel. With this information, the client uses connect () to perform a connection attempt, which is limited to a interval of 20 s. Then it sends the numbers as a string and waits in a while loop to receive the echo.

Program:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

Remarks:
The output window of the clients shows:

Performing search for service name EchoServer
Got server info (u'B8:27:EB:04:A6:7E', 1)
Connecting (u'B8:27:EB:04:A6:7E', 1)
Connected (u'B8:27:EB:04:A6:7E', 1)
Message 0
Message 1
..

Instead of searching with the given Bluetooth service name, the Bluetooth server name can be used:

client.findServer('raspberrypi', 20)

If the Bluetooth MAC address is known (e.g. by a previous Bluetooth search result), the serverInfo can be hard-coded:

serverInfo = ('B8:27:EB:04:A6:7E', 1)
connect(serverInfo, 20)

Mac user library folder missing. This greatly decreases the connection delay.

Python Bluetooth Ble

Experiment 2: Remote-Guided Rover

Python Mac Address

Aim:
Via an H-bridge the Raspberry Pi controls two motors of a moving robot (see chapter DC motors). The rover that runs a Bluetooth server receives commands forward, backward, left, right, and stop from a Bluetooth client that runs on a PC, smartphone, or another Raspberry Pi. In our setup the H-bridge is wired to GPIO pins 32, 36 for the left motor, and 38 and 40 for the right motor.

Program at the Raspberry Pi:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

Remarks:
In the callback onStateChanged(), the corresponding function is called upon receipt of a command. The remote control can be performed by a PC running a program in any programming language, such as TigerJython, showing a dialog box to send the commands with buttons clicks.

Program:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

Remarks:
Once a Bluetooth search has been made, the Bluetooth MAC address is displayed in the title bar of the dialog and you can 'hard-wire' it in the program code. Instead of
serverInfo = client.findService (serviceName, 30)
we write for our rover:
serverInfo = ('B8: 27: EB: 05: 5A: F8', 1)
and the connection is established in less time.
It is useful to run the program automatically when the Raspberry Pi is booting (by just naming it autostart.py) and to display certain status information of the rover with LEDs or better on a connected display (7-segment or OLED).

Python Ble Library Mac Os

Experiment 3: Speed control with Android app

Aim:
In order to change the speed of the motors, the motors are supplied with a PWM signal via the H-bridge and remote-controlled with an Android app.
The same GPIO pins are used as in the previous program.

Program at the Raspberry Pi:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

Remarks:
Negative speeds correspond to a backward movement. It is ensured that the speeds are always in the range -100 . 100.
The Android app uses the JDroidLib framework, which also contains a Bluetooth library. However, this library is not event-controlled, but more classically based on streams. The app can be modified and re-built on our online compiler, eliminating the need for a local Android development system. Before starting the program, the smartphone must be paired with the Raspberry Pi.

Open source in Online-Compiler.
Create QR code to download Android app to your smartphone

Program:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

Remarks:
More information about the development of Android apps with the framework JDroidLib can be found here.
Of course, a control program can also be written with Java SE on a PC. It is advantageous to use the Bluetooth library developed by us, which is based on BlueCove (however, BlueCove is no longer compatible with the latest MacOS). More information can be found here.