SoFunction
Updated on 2024-11-16

Example of bluetooth communication code based on python

This article introduces the realization of bluetooth communication code based on python examples, the text through the sample code is very detailed, for everyone's learning or work has a certain reference learning value, you can refer to the following friends

Installation and examples

Installation under linux

sudo apt-get install python-pip libglib2.0-dev
sudo pip install bluepy

official example

import btle
class MyDelegate():
	def __init__(self, params):
	.__init__(self)#..
	.initialise here
def handleNotification(self, cHandle,
	data): #...perhaps check cHandle#...process 'data'
#
Initialisation-- -- -- -
p = (address)
(MyDelegate(params))
# Setup to turn notifications on, .#svc =
	(service_uuid)# ch =
	(char_uuid)[0]# ch
	.write(setup_data)
# Main loop-- -- -- --
while True:
	if (1.0): #
	handleNotification() was called
continue
print "Waiting..."#
Perhaps do something
else here

Use of the Bluetooth communication module pybluez

Selecting 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 Equipment Services

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("")

Communication via RFCOMM method

Programming Bluetooth communication using a socket-like programming model

1. Server-side program

import bluetooth
server_sock = (
	)
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. Client program

import bluetooth
bd_addr = "01:23:45:67:89:AB"
port = 1
sock = (
	)
((bd_addr, port))
("hello!!")
()

Communication via L2CAP method

The L2CAP sockets approach is almost equivalent to the RFCOMM sockets approach, the only difference being that it is via L2CAP and the ports are odd numbers between 0x1001 and 0x8FFF. The default reliable message that can be transmitted over the connection is 672 bytes.

1. Server-side program

import bluetooth
server_sock = (
	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. Client program

import bluetooth
sock=(bluetooth.L2CAP)
bd_addr = "01:23:45:67:89:AB"
port = 0x1001
((bd_addr, port))
("hello!!")
()

Adjusting the MTU size

l2cap_sock = ( bluetooth.L2CAP )
# connect the socket
bluetooth.set_l2cap_mtu( l2cap_sock, 65535 )

This is the whole content of this article.