Installation and examples
Under linux installation
sudo apt-get install python-pip libglib2.0-dev sudo pip install bluepy
Examples official
import btle class MyDelegate(btle.DefaultDelegate): def __init__(self, params): btle.DefaultDelegate.__init__(self) # ... initialise here def handleNotification(self, cHandle, data): # ... perhaps check cHandle # ... process 'data' # Initialisation ------- p = btle.Peripheral( address ) p.setDelegate( MyDelegate(params) ) # Setup to turn notifications on, e.g. # svc = p.getServiceByUUID( service_uuid ) # ch = svc.getCharacteristics( char_uuid )[0] # ch.write( setup_data ) # Main loop -------- while True: if p.waitForNotifications(1.0): # handleNotification() was called continue print "Waiting..." # Perhaps do something else here
Use of a Bluetooth communication module pybluez
Select the Bluetooth communication objects
import bluetooth target_name = "My Device" target_address = None nearby_devices = bluetooth.discover_devices() for bdaddr in nearby_devices: if target_name == bluetooth.lookup_name( bdaddr ): target_address = bdaddr break if target_address is not None: print("found target bluetooth device with address ", target_address) else: print("could not find target bluetooth device nearby")
Inquiry service equipment
import bluetooth nearby_devices = bluetooth.discover_devices(lookup_names=True) for addr, name in nearby_devices: print(" %s - %s" % (addr, name)) services = bluetooth.find_service(address=addr) for svc in services: print("Service Name: %s" % svc["name"]) print(" Host: %s" % svc["host"]) print(" Description: %s" % svc["description"]) print(" Provided By: %s" % svc["provider"]) print(" Protocol: %s" % svc["protocol"]) print(" channel/PSM: %s" % svc["port"]) print(" svc classes: %s "% svc["service-classes"]) print(" profiles: %s "% svc["profiles"]) print(" service id: %s "% svc["service-id"]) print("")
Communicate by way RFCOMM
By way of socket programming model similar to Bluetooth communication for programming
1. Server-side program
import bluetooth server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM ) port = 1 server_sock.bind(("",port)) server_sock.listen(1) client_sock,address = server_sock.accept() print "Accepted connection from ",address data = client_sock.recv(1024) print "received [%s]" % data client_sock.close() server_sock.close()
2. The client program
import bluetooth bd_addr = "01:23:45:67:89:AB" port = 1 sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM ) sock.connect((bd_addr, port)) sock.send("hello!!") sock.close()
Communicate by way L2CAP
The L2CAP RFCOMM sockets manner almost equivalent to the sockets embodiment, the only difference is the way by L2CAP, odd port and the port is between 0x1001 to 0x8FFF. The default connection may be reliably transmitted packet is 672 bytes.
1. Server-side program
import bluetooth server_sock=bluetooth.BluetoothSocket( bluetooth.L2CAP ) port = 0x1001 server_sock.bind(("",port)) server_sock.listen(1) client_sock,address = server_sock.accept() print "Accepted connection from ",address data = client_sock.recv(1024) print "received [%s]" % data client_sock.close() server_sock.close()
2. The client program
import bluetooth sock=bluetooth.BluetoothSocket(bluetooth.L2CAP) bd_addr = "01:23:45:67:89:AB" port = 0x1001 sock.connect((bd_addr, port)) sock.send("hello!!") sock.close()
MTU size adjustment
l2cap_sock = bluetooth.BluetoothSocket( bluetooth.L2CAP ) # connect the socket bluetooth.set_l2cap_mtu( l2cap_sock, 65535 )
refer:
An Introduction to Bluetooth Programming
Python's Bluetooth communication module pybluez study notes