It uses a 2 way RFCOMM communication - you can send messages to and from 2 devices, 1 being the server which waits for connections, 1 being the client which makes a connection.
Install the library
sudo apt-get install python3-dbus sudo pip3 install bluedot
Pairing
The 2 devices you which want to communicate between will need to be paired, the Blue Dot documentation describes how to pair 2 raspberry pi's which might be useful.
Simple Client / Server Example
Lets create a simple example, a server which waits for connections and when it receives data it echo's it back to the client.
Create a new Python program and save it as btserver.py:
from bluedot.btcomm import BluetoothServer
from signal import pause
def data_received(data):
print(data)
s.send(data)
s = BluetoothServer(data_received)
pause()
Create a 2nd program and save it as btclient.py:
from bluedot.btcomm import BluetoothClient from signal import pause def data_received(data): print(data) c = BluetoothClient("nameofyourserver", data_received) c.send("helloworld") pause()
Adapter
There is also a useful API for accessing the Bluetooth adapter allowing you to get its current status, power it on/off, make it discoverable or find the devices its paired with.
from bluedot.btcomm import BluetoothAdapter a = BluetoothAdapter() print("Powered = {}".format(a.powered)) print(a.paired_devices) a.allow_pairing()
Documentation
There is comprehensive documentation for the btcomm library, which describes the API and how to use it.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.