BLE操作在Linux下的操作

9380阅读 0评论2019-09-16 iibull
分类:其他平台

参考  
使用 Plugable USB-BT4LE adapter 或者  CSR  4.0

点击(此处)折叠或打开

  1. hciconfig #查看设备存在.   sudo hciconfig hci0 up

  2. 正常产出
  3. hci0:    Type: BR/EDR Bus: UART
  4.     BD Address: B8:27:EB:23:E2:A4 ACL MTU: 1021:8 SCO MTU: 64:1
  5.     UP RUNNING
  6.     RX bytes:1987 acl:0 sco:0 events:91 errors:0
  7.     TX bytes:1647 acl:0 sco:0 commands:57 errors:0
扫描

点击(此处)折叠或打开

  1. sudo hcitool lescan

  2. 正常产出
  3. LE Scan ...
  4.  B0:B4:48:ED:44:C3 (unknown)
  5.  B0:B4:48:ED:44:C3 CC2650 SensorTag
连接 设备

点击(此处)折叠或打开

  1. gatttool -I -b MAC-address
  2. 例如
  3. $ gatttool -I -b B0:B4:48:ED:44:C3
  4.  [B0:B4:48:ED:44:C3][LE]> connect
  5.  Attempting to connect to B0:B4:48:ED:44:C3
  6.  Connection successful
  7.  [B0:B4:48:ED:44:C3][LE]>
通过 UUID读取内容

点击(此处)折叠或打开

  1. [B0:B4:48:ED:44:C3][LE]> char-read-uuid 00002a00-0000-1000-8000-00805f9b34fb
  2.  handle: 0x0003      value: 53 65 6e 73 6f 72 54 61 67 20 32 2e 30
通过UUID设置. 需要先得到 handle, 然后设置. 

点击(此处)折叠或打开

  1. [B0:B4:48:ED:44:C3][LE]> characteristics 1 ffff f000aa72-0451-4000-b000-000000000000
  2.  handle: 0x0046, char properties: 0x0a, char value handle: 0x0047, uuid: f000aa72-0451-4000-b000-000000000000  //得到句柄为 0x47
  3.  [B0:B4:48:ED:44:C3][LE]> char-write-req 47 01  //通过句柄设置内容.
  4.  Characteristic value was written successfully

使用Python操作 BLE. 

点击(此处)折叠或打开

  1. from bluepy import btle
  2. import time
  3. import binascii
  4.  
  5. //链接设备
  6.  print "Connecting..."
  7.  dev = btle.Peripheral("B0:B4:48:BF:C9:83")
  8.  
  9.  print "Services..."
  10.  for svc in dev.services:
  11.      print str(svc)

  12. //链接 UUID
  13. lightSensor = btle.UUID("f000aa70-0451-4000-b000-000000000000")
  14.  
  15.  lightService = dev.getServiceByUUID(lightSensor
  16.  for ch in lightService.getCharacteristics():  
  17.      print str(ch)

  18. //设置 UUID属性
  19. uuidConfig = btle.UUID("f000aa72-0451-4000-b000-000000000000")
  20.  lightSensorConfig = lightService.getCharacteristics(uuidConfig)[0]
  21.  # Enable the sensor
  22.  lightSensorConfig.write(bytes("\x01"))
  23.  
  24.  time.sleep(1.0) # Allow sensor to stabilise
  25.  
  26.  uuidValue = btle.UUID("f000aa71-0451-4000-b000-000000000000")
  27.  lightSensorValue = lightService.getCharacteristics(uuidValue)[0]
  28.  # Read the sensor
  29.  val = lightSensorValue.read()
  30.  print "Light sensor raw value", binascii.b2a_hex(val)

==================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
************************************************************************************************************
==================================================================
参考  

hcitool: linux 蓝牙操作的瑞士军*刀工具. 通过HCI 来操作蓝牙设备,  扫描或者命令(数据)读写BT/BLE.
gatttool: 访问蓝牙设备的 services. 

点击(此处)折叠或打开

  1. hcitool dev ### 查询本机设备.

  2. sudo hcitool lescan ### 执行 BLE 扫描

  3. 链接设备
  4. sudo gatttool -b <BLE Address> -l ### -l 代表打开交互式对话.

  5. 在交互窗口中执行
  6. connect ### 链接
  7. primary ### 打印所有的UUIDs, 即所谓的 services
  8. ### 输出类似:
  9. attr handle: 0x0001, end grp handle: 0x0007 uuid: 00001800-0000-1000-8000-00805f9b34fb
  10. attr handle: 0x0008, end grp handle: 0x000b uuid: 00001801-0000-1000-8000-00805f9b34fb
  11. attr handle: 0x000c, end grp handle: 0xffff uuid: 0000180f-0000-1000-8000-00805f9b34fb

  12. char-desc ### 获取所有的句柄 handles,
  13. 输出类似于 handle: 0x0001, uuid: 2800
  14. handle是由bluez确定的, 和某个特定的特征 characteristic(UUID) 绑定的.

  15. char-read-hnd <handle> ### 从handle读取数据.
  16. 输出类似于 Characteristic value/descriptor: 64
  17. 返回的数值为 16 进制的内容.

  18. char-write-req <handle> <data> ### 写句柄

==================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
************************************************************************************************************
通过 BLUEZ 的 HCI 接口直接操作  APIs
http://dev.ti.com/tirex/content/simplelink_cc2640r2_sdk_1_35_00_33/docs/ble5stack/ble_user_guide/html/doxygen/group___h_c_i.html 

其中 HCI_LE_*** 的部分为 BLE操作的APIs
例如   
HCI_LE_CreateConnCmd 

上一篇:BLE linux 开发基本要义入门
下一篇:WINE 搭建常用工具