使用 Plugable USB-BT4LE adapter 或者 CSR 4.0
点击(此处)折叠或打开
-
hciconfig #查看设备存在. sudo hciconfig hci0 up
-
-
正常产出
-
hci0: Type: BR/EDR Bus: UART
-
BD Address: B8:27:EB:23:E2:A4 ACL MTU: 1021:8 SCO MTU: 64:1
-
UP RUNNING
-
RX bytes:1987 acl:0 sco:0 events:91 errors:0
- TX bytes:1647 acl:0 sco:0 commands:57 errors:0
点击(此处)折叠或打开
-
sudo hcitool lescan
-
-
正常产出
-
LE Scan ...
-
B0:B4:48:ED:44:C3 (unknown)
- B0:B4:48:ED:44:C3 CC2650 SensorTag
点击(此处)折叠或打开
-
gatttool -I -b MAC-address
-
例如
-
$ gatttool -I -b B0:B4:48:ED:44:C3
-
[B0:B4:48:ED:44:C3][LE]> connect
-
Attempting to connect to B0:B4:48:ED:44:C3
-
Connection successful
- [B0:B4:48:ED:44:C3][LE]>
点击(此处)折叠或打开
-
[B0:B4:48:ED:44:C3][LE]> char-read-uuid 00002a00-0000-1000-8000-00805f9b34fb
- handle: 0x0003 value: 53 65 6e 73 6f 72 54 61 67 20 32 2e 30
点击(此处)折叠或打开
-
[B0:B4:48:ED:44:C3][LE]> characteristics 1 ffff f000aa72-0451-4000-b000-000000000000
-
handle: 0x0046, char properties: 0x0a, char value handle: 0x0047, uuid: f000aa72-0451-4000-b000-000000000000 //得到句柄为 0x47
-
[B0:B4:48:ED:44:C3][LE]> char-write-req 47 01 //通过句柄设置内容.
- Characteristic value was written successfully
使用Python操作 BLE.
点击(此处)折叠或打开
-
from bluepy import btle
-
import time
-
import binascii
-
- //链接设备
-
print "Connecting..."
-
dev = btle.Peripheral("B0:B4:48:BF:C9:83")
-
-
print "Services..."
-
for svc in dev.services:
-
print str(svc)
-
-
//链接 UUID
-
lightSensor = btle.UUID("f000aa70-0451-4000-b000-000000000000")
-
-
lightService = dev.getServiceByUUID(lightSensor)
-
for ch in lightService.getCharacteristics():
-
print str(ch)
-
- //设置 UUID属性
-
uuidConfig = btle.UUID("f000aa72-0451-4000-b000-000000000000")
-
lightSensorConfig = lightService.getCharacteristics(uuidConfig)[0]
-
# Enable the sensor
-
lightSensorConfig.write(bytes("\x01"))
-
-
time.sleep(1.0) # Allow sensor to stabilise
-
-
uuidValue = btle.UUID("f000aa71-0451-4000-b000-000000000000")
-
lightSensorValue = lightService.getCharacteristics(uuidValue)[0]
-
# Read the sensor
-
val = lightSensorValue.read()
- print "Light sensor raw value", binascii.b2a_hex(val)
==================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
************************************************************************************************************
==================================================================
参考
hcitool: linux 蓝牙操作的瑞士军*刀工具. 通过HCI 来操作蓝牙设备, 扫描或者命令(数据)读写BT/BLE.
gatttool: 访问蓝牙设备的 services.
点击(此处)折叠或打开
-
hcitool dev ### 查询本机设备.
-
-
sudo hcitool lescan ### 执行 BLE 扫描
-
-
链接设备
-
sudo gatttool -b <BLE Address> -l ### -l 代表打开交互式对话.
-
-
在交互窗口中执行
-
connect ### 链接
-
primary ### 打印所有的UUIDs, 即所谓的 services
-
### 输出类似:
-
attr handle: 0x0001, end grp handle: 0x0007 uuid: 00001800-0000-1000-8000-00805f9b34fb
-
attr handle: 0x0008, end grp handle: 0x000b uuid: 00001801-0000-1000-8000-00805f9b34fb
-
attr handle: 0x000c, end grp handle: 0xffff uuid: 0000180f-0000-1000-8000-00805f9b34fb
-
-
char-desc ### 获取所有的句柄 handles,
-
输出类似于 handle: 0x0001, uuid: 2800
-
handle是由bluez确定的, 和某个特定的特征 characteristic(UUID) 绑定的.
-
-
char-read-hnd <handle> ### 从handle读取数据.
-
输出类似于 Characteristic value/descriptor: 64
-
返回的数值为 16 进制的内容.
-
- 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