基于python實(shí)現(xiàn)藍(lán)牙通信代碼實(shí)例
這篇文章主要介紹了基于python實(shí)現(xiàn)藍(lán)牙通信代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(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" )
查詢(xún)?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("")
通過(guò)RFCOMM方式進(jìn)行通信
采用類(lèi)似于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. 客戶(hù)端程序
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()
通過(guò)L2CAP方式進(jìn)行通信
L2CAP的sockets方式幾乎等同于RFCOMM的sockets方式,唯一的不同是通過(guò)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.客戶(hù)端程序
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)文章
PyQt5使用mimeData實(shí)現(xiàn)拖拽事件教程示例解析下
這篇文章主要為大家介紹了PyQt5使用mimeData實(shí)現(xiàn)拖拽事件的教程示例解析,系列文章詳見(jiàn)文中跳轉(zhuǎn)鏈接,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
使用urllib庫(kù)的urlretrieve()方法下載網(wǎng)絡(luò)文件到本地的方法
今天小編就為大家分享一篇使用urllib庫(kù)的urlretrieve()方法下載網(wǎng)絡(luò)文件到本地的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python 使用 prettytable 庫(kù)打印表格美化輸出功能
這篇文章主要介紹了Python 使用 prettytable 庫(kù)打印表格美化輸出功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Python爬蟲(chóng)谷歌Chrome F12抓包過(guò)程原理解析
這篇文章主要介紹了Python爬蟲(chóng)谷歌Chrome F12抓包過(guò)程原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Python數(shù)據(jù)分析中Groupby用法之通過(guò)字典或Series進(jìn)行分組的實(shí)例
下面小編就為大家分享一篇Python數(shù)據(jù)分析中Groupby用法之通過(guò)字典或Series進(jìn)行分組的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
flask操作數(shù)據(jù)庫(kù)插件Flask-SQLAlchemy的使用
Python?中最廣泛使用的ORM框架是SQLAlchemy,它是一個(gè)很強(qiáng)大的關(guān)系型數(shù)據(jù)庫(kù)框架,本文就來(lái)介紹一下flask操作數(shù)據(jù)庫(kù)插件Flask-SQLAlchemy的使用,感興趣的可以了解一下2023-09-09
python3.7安裝matplotlib失敗問(wèn)題的完美解決方法
由于學(xué)習(xí)需要安裝matplotlib庫(kù),閱讀網(wǎng)上教程后一直出現(xiàn)各種各樣的錯(cuò)誤,下面這篇文章主要給大家介紹了關(guān)于python3.7安裝matplotlib失敗問(wèn)題的完美解決方法,需要的朋友可以參考下2022-07-07
python實(shí)現(xiàn)登錄與注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)登錄與注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Python PEP8代碼規(guī)范常見(jiàn)問(wèn)題以及解決方案
PEP8是Python的代碼規(guī)范,本文總結(jié)了常見(jiàn)的PEP8代碼規(guī)范問(wèn)題及解決方法,幫助開(kāi)發(fā)者編寫(xiě)規(guī)范的代碼2024-11-11

