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

2021-10-13 13:27:48 字數 3522 閱讀 3839

安裝和示例

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:

an introduction to bluetooth programming

python之藍芽通訊模組pybluez學習筆記

以上就是python實現藍芽通訊的全部內容。

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

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

Python 藍芽通訊模組pybluez Win7

今天學習windows下的藍芽控制,安裝pybluez pip install pybluez安裝很順利,沒有遇見網上關於sdk之類的問題,可能是我的機子已經安裝了vs各執行庫的原因吧 使用庫只需要import bluetooth即可,但我這裡出現了 atributeerror attribute ...

Android藍芽通訊(三) 藍芽通訊的實現

在前兩篇部落格中大致介紹了藍芽相關的基礎知識,不了解的朋友可以檢視前兩篇部落格 android藍芽通訊 一 藍芽功能的相關許可權 android藍芽通訊 二 藍芽的相關操作 在本篇部落格中我將介紹如何實現兩個藍芽裝置之間的通訊,藍芽的通訊類似於socket的通訊,在藍芽通訊中沒有絕對的裝置充當ser...