基于python實(shí)現(xiàn)藍(lán)牙通信代碼實(shí)例
這篇文章主要介紹了基于python實(shí)現(xiàn)藍(lán)牙通信代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
安裝和示例
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
藍(lán)牙通信模塊pybluez的使用
選擇藍(lán)牙通信對(duì)象
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" )
查詢?cè)O(shè)備服務(wù)
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方式進(jìn)行通信
采用類似于socket編程模型的方式進(jìn)行藍(lán)牙通信的編程
1.服務(wù)器端程序
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方式進(jìn)行通信
L2CAP的sockets方式幾乎等同于RFCOMM的sockets方式,唯一的不同是通過L2CAP的方式,并且端口是0x1001到0x8FFF之間的奇數(shù)端口。默認(rèn)的連接可以傳送的可靠報(bào)文是672個(gè)字節(jié)。
1.服務(wù)器端程序
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()
調(diào)整MTU大小
l2cap_sock = bluetooth.BluetoothSocket( bluetooth.L2CAP ) # connect the socket bluetooth.set_l2cap_mtu( l2cap_sock, 65535 )
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python plt.imshow函數(shù)及其參數(shù)使用
plt.imshow()是Matplotlib庫中的一個(gè)函數(shù),主要用于顯示圖像或矩陣數(shù)據(jù),本文主要介紹了Python plt.imshow函數(shù)及其參數(shù)使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
20行python代碼實(shí)現(xiàn)人臉識(shí)別
這篇文章主要介紹了python人臉識(shí)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python 訪問限制 private public的詳細(xì)介紹
在一個(gè)模塊中,我們可能會(huì)定義很多函數(shù)和變量。這篇文章主要介紹了Python 訪問限制 private public的詳細(xì)介紹,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
python判斷所輸入的任意一個(gè)正整數(shù)是否為素?cái)?shù)的兩種方法
今天小編就為大家分享一篇python判斷所輸入的任意一個(gè)正整數(shù)是否為素?cái)?shù)的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06
OpenCV實(shí)戰(zhàn)之OpenCV中的顏色空間
這篇文章主要介紹了OpenCV實(shí)戰(zhàn)之OpenCV中的顏色空間,解計(jì)算機(jī)視覺中常用的色彩空間,并將其用于基于顏色分割。我們還將用C?++和Python共享演示代碼,下文詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-04-04
Kmeans聚類算法python sklearn用戶畫像教程
這篇文章主要介紹了Kmeans聚類算法python sklearn用戶畫像教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
如何使用Django Admin管理后臺(tái)導(dǎo)入CSV
這篇文章主要介紹了如何使用Django Admin管理后臺(tái)導(dǎo)入CSV,幫助大家更好的理解和使用django框架,感興趣的朋友可以了解下2020-11-11

