python3藍芽通訊 python實現藍芽通訊

2021-10-25 19:42:28 字數 3412 閱讀 8125

安裝和示例

linux下安裝

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

sudo pip install bluepy

官方示例

importbtleclassmydelegate(btle.defaultdelegate):def __init__(self, params):

btle.defaultdelegate.__init__(self)#... initialise here

defhandlenotification(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 --------

whiletrue:if p.waitfornotifications(1.0):#handlenotification() was called

continue

print "waiting..."

#perhaps do something else here

藍芽通訊模組pybluez的使用

選擇藍芽通訊物件

importbluetooth

target_name= "my device"target_address=none

nearby_devices=bluetooth.discover_devices()for bdaddr innearby_devices:if target_name ==bluetooth.lookup_name( bdaddr ):

target_address=bdaddrbreak

if target_address is notnone:print("found target bluetooth device with address", target_address)else:print("could not find target bluetooth device nearby")

查詢裝置服務

importbluetooth

nearby_devices= bluetooth.discover_devices(lookup_names=true)for addr, name innearby_devices:print("%s - %s" %(addr, name))

services= bluetooth.find_service(address=addr)for svc inservices: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("")

通過rfcomm方式進行通訊

採用類似於socket程式設計模型的方式進行藍芽通訊的程式設計

1.伺服器端程式

importbluetooth

server_sock=bluetooth.bluetoothsocket( bluetooth.rfcomm )

port= 1server_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. 客戶端程式

importbluetooth

bd_addr= "01:23:45:67:89:ab"port= 1sock=bluetooth.bluetoothsocket( bluetooth.rfcomm )

sock.connect((bd_addr, port))

sock.send("hello!!")

sock.close()

通過l2cap方式進行通訊

l2cap的sockets方式幾乎等同於rfcomm的sockets方式,唯一的不同是通過l2cap的方式,並且埠是0x1001到0x8fff之間的奇數埠。預設的連線可以傳送的可靠報文是672個位元組。

1.伺服器端程式

importbluetooth

server_sock=bluetooth.bluetoothsocket( bluetooth.l2cap )

port= 0x1001server_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.客戶端程式

importbluetooth

sock=bluetooth.bluetoothsocket(bluetooth.l2cap)

bd_addr= "01:23:45:67:89:ab"port= 0x1001sock.connect((bd_addr, port))

sock.send("hello!!")

sock.close()

調整mtu大小

l2cap_sock =bluetooth.bluetoothsocket( bluetooth.l2cap )#connect the socketbluetooth.set_l2cap_mtu( l2cap_sock,65535 )

refer:

Mac上同時安裝python3和python2

系統 mac 已安裝環境 anaconda3,python3 由於在平時的專案中,經常由於各種python庫的版本不匹配的問題,導致python2和python3的執行環境無法相容。因此我們可以在上述已安裝環境的基礎上,再安裝python2。具體步驟非常簡單,參考下面。先檢查當前環境為python3...

python藍芽通訊 python實現藍芽通訊

安裝和示例 linux下安裝 sudo apt get install python pip libglib2.0 dev sudo pip install bluepy 官方示例 importbtleclassmydelegate btle.defaultdelegate def init sel...

python藍芽通訊 python實現藍芽通訊

安裝和示例 linux下安裝 sudo apt get install python pip libglib2.0 dev sudo pip install bluepy 官方示例 import btle class mydelegate btle.defaultdelegate def init ...