A simple Python API for Bluetooth D-Bus calls. Allows easy pairing, connecting and scanning. Also provides a TCP-to-RFCOMM socket bridge for data transfer.
python-dbus
tcpbridge
The package was tested with Python 2.7
pip install bluetool
or clone and run make install
-
Bluetooth:
list:
[{"name": Name, "mac_address": MAC-address}, ... ]
Methods of class Bluetooth:
start_scanning(timeout)
:scan
in backgroundscan(timeout)
get_devices_to_pair()
, returns listget_available_devices()
, returns listget_paired_devices()
, returns listget_connected_devices()
, returns listmake_discoverable()
, returns boolstart_pairing(address)
:pair
in backgroundpair(address)
, returns boolconnect(address)
, returns booldisconnect(address)
, returns booltrust(address)
, returns boolremove(address)
, returns bool
-
BluetoothServer:
- Step1: Use
run()
to create SPP - Step2: Connect the bluetooth device
- Step3: TCPServer is available for connection
Use
shutdown()
to shutdown server. - Step1: Use
- Scanning
from bluetool import Bluetooth
bluetooth = Bluetooth()
bluetooth.scan()
devices = bluetooth.get_available_devices()
print(devices)
- Using the RFCOMM-TCP Bridge
import signal
from bluetool import BluetoothServer
def handler(signum, frame):
server.shutdown()
tcp_port = 8100
server = BluetoothServer(tcp_port)
signal.signal(signal.SIGINT, handler)
signal.signal(signal.SIGTERM, handler)
server.run()
This package was written by Aleksandr Aleksandrov working at Emlid.
The bluetool was originally written for the Emlid Reach RTK receiver, but we decided to open source it, as there is no easy Python API for BT pairing/connecting. Feel free to add issues and submit pull requests.