Python3環境,樹莓派使用bluepy與BLE設備通信


掃描設備

創建一個ScanDelegate

1 class ScanDelegate(DefaultDelegate):
2     def __init__(self):
3         DefaultDelegate.__init__(self)
4 
5     def handleDiscovery(self, dev, isNewDev, isNewData):
6         if isNewDev:
7             print("Discovered device", dev.addr)
8         elif isNewData:
9             print("Received new data from", dev.addr)

 

掃描設備並打印

 1 scanner = Scanner().withDelegate(ScanDelegate())
 2 devices = scanner.scan(10.0)
 3 creyond_devices = []
 4 for dev in devices:
 5     print("Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi))
 6     for (adtype, desc, value) in dev.getScanData():
 7         if value == "CreYond":
 8             creyond_devices.append(dev)
 9         print("  %s = %s" % (desc, value))
10 
11 print(creyond_devices)

 

獲取Services與Characteristics

通過Peripheral來獲取Services

1 c_device = creyond_devices[0]
2 p_device = Peripheral(c_device)
3 p_device.withDelegate(NotifyDelegate(p_device))
4 services = p_device.getServices()
5 
6 # displays all services
7 for service in services:
8     print(service)

 

通過UUID獲取指定Service  

1 service_uuid = UUID("00000010-3354-4d64-6e6f-XXXXXXX3534a")
2 c_service = p_device.getServiceByUUID(service_uuid)
3 print(c_service)

 

獲取Service下的Characteristics

1 characteristics = c_service.getCharacteristics()
2 # displays all characteristics
3 for char in characteristics:
4     print(char)

 

訂閱與通知

創建一個NotifyDelegate

1 class NotifyDelegate(DefaultDelegate):
2     # Constructor (run once on startup)
3     def __init__(self, params):
4         DefaultDelegate.__init__(self)
5 
6     # func is caled on notifications
7     def handleNotification(self, cHandle, data):
8         print("Notification from Handle: 0x" + format(cHandle,'02X') )
9         print(hexlify(data))

在創建Peripheral時,指定

 1 p_device.withDelegate(NotifyDelegate(p_device)) 

查找descriptor,並設定其值為1.設定值時注意大小端 bytes([1, 0])。注意,直接寫characteristic是錯誤的,一定要找到characteristic下的UUID為0x2902的descriptor。部分藍牙設備的UUID可能不規范,通過UUID查找可能存在重復,建議getDescriptors篩選好對應范圍。

1 hEcg=notify_char.getHandle()
2 for descriptor in p_device.getDescriptors(hEcg,c_service.hndEnd):
3     if (descriptor.uuid==0x2902):
4         print(f'Client Characteristic Configuration found at handle 0x{format(descriptor.handle,"02X")}')
5         hEcgCCC=descriptor.handle
6 
7 p_device.writeCharacteristic(hEcgCCC,bytes([1, 0]))

等待顯示:

1 while True:
2     if p_device.waitForNotifications(1.0):
3         # handleNotification() was called
4         continue
5 
6     print("Waiting... Waited more than one sec for notification")

 

完整代碼:

 1 # %%
 2 from bluepy.btle import Scanner, DefaultDelegate,UUID, Peripheral
 3 from binascii import hexlify
 4 import struct
 5 # %%
 6 class ScanDelegate(DefaultDelegate):
 7     def __init__(self):
 8         DefaultDelegate.__init__(self)
 9 
10     def handleDiscovery(self, dev, isNewDev, isNewData):
11         if isNewDev:
12             print("Discovered device", dev.addr)
13         elif isNewData:
14             print("Received new data from", dev.addr)
15 
16 
17 class NotifyDelegate(DefaultDelegate):
18     # Constructor (run once on startup)
19     def __init__(self, params):
20         DefaultDelegate.__init__(self)
21 
22     # func is caled on notifications
23     def handleNotification(self, cHandle, data):
24         print("Notification from Handle: 0x" + format(cHandle,'02X') )
25         print(hexlify(data))
26 
27 
28 # %%
29 scanner = Scanner().withDelegate(ScanDelegate())
30 devices = scanner.scan(10.0)
31 creyond_devices = []
32 for dev in devices:
33     print("Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi))
34     for (adtype, desc, value) in dev.getScanData():
35         if value == "CreYond":
36             creyond_devices.append(dev)
37         print("  %s = %s" % (desc, value))
38 
39 print(creyond_devices)
40 # %% get services
41 # the first device name CreYond
42 c_device = creyond_devices[0]
43 p_device = Peripheral(c_device)
44 p_device.withDelegate(NotifyDelegate(p_device))
45 services = p_device.getServices()
46 
47 # displays all services
48 for service in services:
49     print(service)
50 # %% get specified service
51 service_uuid = UUID("00000010-3354-4d64-6e6f-xxxxxxxxx534a")
52 c_service = p_device.getServiceByUUID(service_uuid)
53 print(c_service)
54 
55 # %%
56 characteristics = c_service.getCharacteristics()
57 # displays all characteristics
58 for char in characteristics:
59     print(char)
60 # %% 
61 notify_char = characteristics[0]
62 
63 #%%
64 hEcg=notify_char.getHandle()
65 for descriptor in p_device.getDescriptors(hEcg,c_service.hndEnd):
66     if (descriptor.uuid==0x2902):
67         print(f'Client Characteristic Configuration found at handle 0x{format(descriptor.handle,"02X")}')
68         hEcgCCC=descriptor.handle
69 
70 p_device.writeCharacteristic(hEcgCCC,bytes([1, 0]))
71 
72 #%%
73 tmp_data=p_device.readCharacteristic(0x11)
74 print(tmp_data)
75 # %%
76 while True:
77     if p_device.waitForNotifications(1.0):
78         # handleNotification() was called
79         continue
80 
81     print("Waiting... Waited more than one sec for notification")
82 
83 # %%
84 p_device.disconnect()
85 
86 # %%

 

 

參考鏈接:

【Bluetooth LE】Bluez中Bluetoothctl指令詳解(連接iPhone為例)

bluepy官方Demo

RPi Bluetooth LE

github demo


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM