[BlueZ] 2、使用bluetoothctl搜索、連接、配對、讀寫、使能notify藍牙低功耗設備

来源:https://www.cnblogs.com/zjutlitao/archive/2018/09/05/9589661.html
-Advertisement-
Play Games

星期三, 05. 九月 2018 02:03上午 beautifulzzzz ![][ bar] 1、前言 上一篇講瞭如何編譯安裝BlueZ 5,本篇主要在於玩BlueZ,用命令行去操作BLE設備: [ [BlueZ] 1、Download install and use the BlueZ and ...


星期三, 05. 九月 2018 02:03上午 - beautifulzzzz

1、前言

上一篇講瞭如何編譯安裝BlueZ-5,本篇主要在於玩BlueZ,用命令行去操作BLE設備:

2、gatttool —— 老工具趟坑

剛開始跟著 Get Started with Bluetooth Low Energy on Linux 操作gatttool,發現坑太多(主要原因是工具老了):

採用sudo gatttool -b 4D:69:98:0E:91:5E -I去連接
發現會報錯:Error: connect error: Connection refused (111)
最終參考LINK-11發現需要加random選項(#1)

➜  ~  sudo gatttool -b 4D:69:98:0E:91:5E -I
[4D:69:98:0E:91:5E][LE]> connect
Attempting to connect to 4D:69:98:0E:91:5E
Error: connect error: Connection refused (111)
[4D:69:98:0E:91:5E][LE]> exit
➜  ~  sudo gatttool  -t random  -b 4D:69:98:0E:91:5E -I
[4D:69:98:0E:91:5E][LE]> connect
Attempting to connect to 4D:69:98:0E:91:5E
Connection successful
[4D:69:98:0E:91:5E][LE]> 
(gatttool:3104): GLib-WARNING **: Invalid file descriptor.

過一回會10S自動斷開,網上說這個工具老了,不建議用了(#2):

There are new tools to use with GATT, bluetoothctl/bluetoothd is the preferred since with that you have GAP, etc, 
but if want to use a stand alone tool then I suggest you use btgatt-client.

3、bluetoothctl——NB的新工具

命令行進入bluetoothctl操作環境(#6)

bluetoothctl

我在手機上用lightblue模擬一個BLE設備ty_prod,之後對其service進行修改,調用scan on進行搜索還是老的,
最終發現要先用remove移除之前的設備,之後再scan就會出現[NEW] Device 72:3B:E1:81:4E:4F ty_prod設備
註: 用lightblue模擬的設備的MAC不是固定的
註: 我發現在lightblue中無論怎麼模擬BLE設備,一旦被連上搜索到的service都是IPone的

[bluetooth]# devices
Device 28:ED:6A:A0:26:B7 ty_prod
Device 58:71:33:00:00:24 Bluetooth Keyboard
Device 00:1A:7D:DA:71:0A SHEN-PC
Device 94:87:E0:B3:AC:6F Mi Phone
[bluetooth]# remove 28:ED:6A:A0:26:B7 
...
[bluetooth]# scan on
Discovery started
[NEW] Device 72:3B:E1:81:4E:4F ty_prod
[bluetooth]# scan off
...
Discovery stopped
[bluetooth]# connect 72:3B:E1:81:4E:4F
Attempting to connect to 72:3B:E1:81:4E:4F
[CHG] Device 72:3B:E1:81:4E:4F Connected: yes
Connection successful
[ty_prod]

索性就用IPhone自帶的服務做測試了~

[ty_prod]# info
Device 28:ED:6A:A0:26:B7 (public)
    Name: tuya_mdev_test
    Alias: tuya_mdev_test
    Appearance: 0x0040
    Icon: phone
    Paired: yes
    Trusted: no
    Blocked: no
    Connected: yes
    LegacyPairing: no
    UUID: Fax                       (00001111-0000-1000-8000-00805f9b34fb)
    UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
    UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
    UUID: Current Time Service      (00001805-0000-1000-8000-00805f9b34fb)
    UUID: Device Information        (0000180a-0000-1000-8000-00805f9b34fb)
    UUID: Battery Service           (0000180f-0000-1000-8000-00805f9b34fb)
    UUID: Vendor specific           (7905f431-b5ce-4e99-a40f-4b1e122d00d0)
    UUID: Vendor specific           (89d3502b-0f36-433a-8ef4-c502ad55f8dc)
    UUID: Vendor specific           (9fa480e0-4967-4542-9390-d343dc5d04ae)
    UUID: Vendor specific           (d0611e78-bbb4-4591-a5f8-487910ae4366)
[CHG] Device 28:ED:6A:A0:26:B7 ServicesResolved: no
[CHG] Device 28:ED:6A:A0:26:B7 Connected: no

我們用Current Time Service,列出所有attributes操作如下:

[tuya_mdev_test]# menu gatt
[tuya_mdev_test]# list-attributes 28:ED:6A:A0:26:B7
...
Primary Service
    /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041
    00001805-0000-1000-8000-00805f9b34fb
    Current Time Service
Characteristic
    /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0045
    00002a0f-0000-1000-8000-00805f9b34fb
    Local Time Information
Characteristic
    /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0042
    00002a2b-0000-1000-8000-00805f9b34fb
    Current Time
Descriptor
    /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0042/desc0044
    00002902-0000-1000-8000-00805f9b34fb
    Client Characteristic Configuration
...

上面Current Time Service對應的服務如下圖:

我們選擇Current Time進行操作UUID:0x2A2B

[ty_prod]# select-attribute /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0042
[tuya_mdev_test:/service0041/char0042]# read
Attempting to read /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0042
[CHG] Attribute /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041/char0042 Value:
  e2 07 09 05 01 24 11 03 f1 02                    .....$....      
  e2 07 09 05 01 24 11 03 f1 02                    .....$.... 
[tuya_mdev_test:/service0041/char0042]# attribute-info
Characteristic - Current Time
    UUID: 00002a2b-0000-1000-8000-00805f9b34fb
    Service: /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0041
    Value:
  e2 07 09 05 01 2e 01 03 f5 02                    ..........      
    Notifying: yes
    Flags: read
    Flags: notify

讀出結果大致意思應該是:2018-9/5-1:36:17 周三

讀取一下0x180A的Device Information:

[tuya_mdev_test:/service0006/char0007]# select-attribute /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0047/char004a
[tuya_mdev_test:/service0047/char004a]# attribute-info
Characteristic - Model Number String
    UUID: 00002a24-0000-1000-8000-00805f9b34fb
    Service: /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0047
    Flags: read
[tuya_mdev_test:/service0047/char004a]# read
Attempting to read /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0047/char004a
[CHG] Attribute /org/bluez/hci0/dev_47_B1_26_C1_81_18/service0047/char004a Value:
  69 50 68 6f 6e 65 36 2c 32                       iPhone6,2       
  69 50 68 6f 6e 65 36 2c 32                       iPhone6,2    

當然寫、使能notify也很簡單,看help即可。最後斷開連接、並退出!!!

[tuya_mdev_test:/service0047/char004a]# disconnect 28:ED:6A:A0:26:B7
Attempting to disconnect from 28:ED:6A:A0:26:B7
[CHG] Device 28:ED:6A:A0:26:B7 ServicesResolved: no
Successful disconnected
[CHG] Device 28:ED:6A:A0:26:B7 Connected: no
[bluetooth]# quit

[1].Cannot connect to BLE device on Raspberry Pi
[2].Invalid file descriptor gatttool of bluez 5.32
[3].Get Started with Bluetooth Low Energy on Linux
[4].Reverse Engineering a Bluetooth Low Energy Light Bulb
[5].Doing Bluetooth Low Energy on Linux
[6].Tutorial: BLE Pairing the Raspberry Pi 3 Model B with Hexiwear

@beautifulzzzz
智能硬體、物聯網,熱愛技術,關註產品
博客:http://blog.beautifulzzzz.com
園友交流群:414948975

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近在做一個使用基於.net mvc 實現前後臺傳輸Json的實例。網上找了一些資料。發現在開發的時候,許多的數據交互都是以Json格式傳輸的。其中涉及序列化對象的使用的有DataContractJsonSerializer,JavaScriptSerializer和Json.net即Newtons ...
  • 大家可能在編碼中或多或少的使用過out的ref,但是是否註意過他兩的詳細用法以及區別? 本文想介紹下詳細介紹下out參數,ref參數以及一般值參數。 值參數 在使用參數時,把一個值傳遞給函數使用的一個變數。在函數中對此變數的任何修改都不影響函數調用中指定的參數。如下麵的函數,是使函數是使傳遞過來的參 ...
  • LitJson.dll下載地址 密碼:1znp 前一段時間一直糾結unity連接資料庫請求數據,浪費了不少時間。後來改用http請求,順利拿到數據,然後就著手於解析數據,就有了這篇文章 如果大家看不懂,這裡有一個視頻講的還是相當詳細的 ...
  • 1 DateTime beginTime = DateTime.Now.Date; 2 Console.WriteLine(beginTime); 3 DateTime endTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, Dat ...
  • 用 .net core 寫的 滑動+點擊漢字的驗證碼,代碼比較簡單就不做說明瞭。 github地址 https://github.com/wangchengqun/NetCoreVerificationCode ...
  • 前言 我想,有一部分程式員應該是在二三線城市的,雖然不知道占比,但想來應該不在少數。 我是這部分人群中的一份子。 我們這群人,面對的客戶,大多是國內中小企業,或者政府的小部門。這類客戶的特點是,資金有限,人力有限。 什麼意思呢?就是你如果敢給他安一臺Linux伺服器,客戶的信息員和測試員會把你堵在牆 ...
  • 第1章 CentOS7的使用 1.1 為什麼要使用CentOS7版本 CentOS7是在CentOS6基礎上發佈的新版本,與之前的版本相比,主要的更新包括: 1、內核更新到3.10.0 2、支持Linux容器 3、LVM快照支持ext4和XFS 4、轉用systemd、firewalld和GRUB2 ...
  • 版權聲明:本文為博主原創文章,未經允許,不得轉載。 head head 命令可以將一段文本的開頭一部分輸出到標準輸出。 head命令既可以處理文本文件也可以處理標準輸入。 基本應用 處理文本文件: 當有多個文件輸入時,head會順序處理並輸出: 處理標準輸入: 當沒有其他參數時,head預設輸出前1 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...