您好,登錄后才能下訂單哦!
小編給大家分享一下基于python如何實現藍牙通信,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
基于python實現藍牙通信代碼實例
linux下安裝
sudo apt-get install python-pip libglib2.0-dev sudo pip install bluepy
官方示例
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
藍牙通信模塊pybluez的使用
選擇藍牙通信對象
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" )
查詢設備服務
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("")
通過RFCOMM方式進行通信
采用類似于socket編程模型的方式進行藍牙通信的編程
1.服務器端程序
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. 客戶端程序
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()
通過L2CAP方式進行通信
L2CAP的sockets方式幾乎等同于RFCOMM的sockets方式,唯一的不同是通過L2CAP的方式,并且端口是0x1001到0x8FFF之間的奇數端口。默認的連接可以傳送的可靠報文是672個字節。
1.服務器端程序
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.客戶端程序
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大小
l2cap_sock = bluetooth.BluetoothSocket( bluetooth.L2CAP ) # connect the socket bluetooth.set_l2cap_mtu( l2cap_sock, 65535 )
以上是“基于python如何實現藍牙通信”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。