Linux 端藍芽調試__Linux

來源:互聯網
上載者:User
Linux 端藍芽調試

@(嵌入式)[BLE, ble]

畢業大半年一直從事Ble外設開發,發現linux上可以使用bluez進行藍芽開發,更加便捷的是,有一個python模組對bluez介面進行封裝,叫bluepy,給開發測試帶來更多便捷。 擷取 & 安裝

開發平台 ubunt 14.04

$ sudo apt-get install git build-essential libglib2.0-dev$ git clone https://github.com/IanHarvey/bluepy.git$ cd bluepy$ python setup.py build$ python setup.py install

個人測試指令碼庫mybluepy, 沒怎麼寫過python,供參考。 使用 hcitool & gatttool

安裝了bluez協議棧後提供的工具,簡單操作如下。 查看藍芽pc的裝置

hcitool dev
掃描ble
hcitool lescan
設定gatttool
 gatttool  -b 76:66:44:33:22:72 -I

-I : 進入互動模式

在gatttool下,
開始串連裝置 : connect
輸入 : help查看其他指令

輸入characteristics 查看特徵值資訊,結果大概如下

handle: 0x0022, char properties: 0x1a, char value handle: 0x0023, uuid: 0000a802-0000-1000-8000-00805f9b34fb

如上, handle 0x0023 寫資料, 向0x0023+1 寫 0x0100,設定監聽,寫0取消監聽 bluepy 指令碼

模組文檔
提供一個簡單的例子:

#!/usr/bin/pythonfrom __future__ import print_functionimport sysimport binasciiimport btleimport osble_conn = Noneclass MyDelegate(btle.DefaultDelegate):    def __init__(self, conn):        btle.DefaultDelegate.__init__(self)        self.conn = conn    def handleNotification(self, cHandle, data):        data = binascii.b2a_hex(data)        print("Notification:", str(cHandle), " data ", data)    def handleDiscovery(self, dev, isNewDev, isNewData):        if isNewDev:            pass        elif isNewData:            print("\nDiscovery:", "MAC:", dev.addr, " Rssi ", str(dev.rssi))def ble_connect(devAddr):    global ble_conn    if not devAddr is None and ble_conn is None:        ble_conn = btle.Peripheral(devAddr, btle.ADDR_TYPE_PUBLIC)        ble_conn.setDelegate(MyDelegate(ble_conn))        print("connected")def ble_disconnect():    global ble_conn    ble_conn = None    print("disconnected")if __name__ == '__main__':    ble_mac == "66:55:44:33:22:11"    # scan     scanner = btle.Scanner().withDelegate(MyDelegate(None))    timeout = 10.0    devices = scanner.scan(timeout)    for dev in devices:        if dev.addr == ble_mac:            print("\nDiscovery:", "MAC:", dev.addr, " Rssi ", str(dev.rssi))            for (adtype, desc, value) in dev.getScanData():                print ("  %s(0x%x) = %s" % (desc, int(adtype), value))            break    # connect      ble_connect(ble_mac)    # write , set listen    snd_content_str = """\x01\x00"""    ble_conn.writeCharacteristic(handle, snd_content_str)    # wait notification      ble_conn.waitForNotifications(2.0)    # disconnect     ble_disconnect()
參考 get start with bluez
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.