<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Python3環(huán)境,樹(shù)莓派使用bluepy與BLE設(shè)備通信

      掃描設(shè)備

      創(chuàng)建一個(gè)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)

       

      掃描設(shè)備并打印

       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

      通過(guò)Peripheral來(lái)獲取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)

       

      通過(guò)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)

       

      訂閱與通知

      創(chuàng)建一個(gè)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))

      在創(chuàng)建Peripheral時(shí),指定

       1 p_device.withDelegate(NotifyDelegate(p_device)) 

      查找descriptor,并設(shè)定其值為1.設(shè)定值時(shí)注意大小端 bytes([1, 0])。注意,直接寫characteristic是錯(cuò)誤的,一定要找到characteristic下的UUID為0x2902的descriptor。部分藍(lán)牙設(shè)備的UUID可能不規(guī)范,通過(guò)UUID查找可能存在重復(fù),建議getDescriptors篩選好對(duì)應(yīng)范圍。

      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

      posted @ 2021-03-09 15:37  天命小豬  閱讀(1953)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 叙永县| 久久久精品2019中文字幕之3 | 日韩人妻精品中文字幕| 亚洲欧美日韩在线码| 日本伊人色综合网| 亚洲成a人片在线视频| 亚洲国产性夜夜综合| 少妇粉嫩小泬喷水视频www| 四虎永久在线高清免费看| 日韩亚洲精品中文字幕| 亚洲精品无码日韩国产不卡av| 玩弄放荡人妻少妇系列| 国产在线播放专区av| 亚洲欧美日韩精品久久亚洲区| 色偷偷亚洲女人天堂观看| 乱人伦中文字幕成人网站在线| 贵南县| 在线观看中文字幕国产码| 国产成人8X人网站视频| 九九热在线视频观看这里只有精品| 亚洲综合av一区二区三区| 国产女同一区二区在线| 中国国产免费毛卡片| 马山县| 一区二区三区精品不卡| 暖暖 免费 高清 日本 在线观看5| 国产午夜亚洲精品一区| 日韩精品一区二区都可以| 黑人巨大精品欧美一区二区| 亚洲大老师中文字幕久热| 综合偷自拍亚洲乱中文字幕| 忘忧草在线社区www中国中文 | 国产精品亚洲二区亚瑟| 午夜福利国产精品小视频| 国产旡码高清一区二区三区| 男女性高爱潮免费网站| 人人妻人人插视频| 最新中文字幕国产精品| 最新av中文字幕无码专区| 国产一区二区高潮视频| 欧美奶涨边摸边做爰视频|