一、升级安装蓝牙相关软件包
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get install pi-bluetooth bluez bluez-firmware blueman
二、添加pi用户到蓝牙组
sudo usermod-G bluetooth -a pi
三、重启sudo reboot
查看自己的蓝牙设备
hciconfig
设备打开
sudo hciconfig hci0 up
设备关闭
sudo hciconfig hci0 down
列出蓝牙设备
sudo hcitool lescan
57:4D:70:F9:11:92 (unknown)
C6:05:04:03:F1:BF (unknown)
C6:05:04:03:F1:BF ClinkBlood
5B:B4:3D:DD:C1:CE (unknown)
5B:B4:3D:DD:C1:CE (unknown)
61:1A:D6:26:12:ED (unknown)
61:1A:D6:26:12:ED (unknown)
44:0E:5A:35:6A:A0 (unknown)
44:0E:5A:35:6A:A0 (unknown)
64:09:80:6F:18:84 (unknown)
查看蓝牙设备信息
sudo hcitool info C6:05:04:03:F1:BF
连接蓝牙设备
gatttool -b C6:05:04:03:F1:BF -I
[C6:05:04:03:F1:BF][LE]> connect
Attempting to connect to C6:05:04:03:F1:BF
Connection successful
[C6:05:04:03:F1:BF][LE]> primary
attr handle: 0x0001, end grp handle: 0x0009 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x000c, end grp handle: 0x000f uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle: 0x0010, end grp handle: 0x0016 uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle: 0x0017, end grp handle: 0x001e uuid: 00005970-6d75-4749-5053-676e6f6c7553
attr handle: 0x0020, end grp handle: 0x0029 uuid: 0000fc00-0000-1000-8000-00805f9b34fb
[C6:05:04:03:F1:BF][LE]> characteristics
handle: 0x0002, char properties: 0x02, char value handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, char properties: 0x02, char value handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0006, char properties: 0x0a, char value handle: 0x0007, uuid: 00002a02-0000-1000-8000-00805f9b34fb
handle: 0x0008, char properties: 0x02, char value handle: 0x0009, uuid: 00002a04-0000-1000-8000-00805f9b34fb
handle: 0x000d, char properties: 0x22, char value handle: 0x000e, uuid: 00002a05-0000-1000-8000-00805f9b34fb
handle: 0x0011, char properties: 0x02, char value handle: 0x0012, uuid: 00002a29-0000-1000-8000-00805f9b34fb
handle: 0x0013, char properties: 0x02, char value handle: 0x0014, uuid: 00002a24-0000-1000-8000-00805f9b34fb
handle: 0x0015, char properties: 0x02, char value handle: 0x0016, uuid: 00002a23-0000-1000-8000-00805f9b34fb
handle: 0x0018, char properties: 0x0a, char value handle: 0x0019, uuid: 01005970-6d75-4749-5053-676e6f6c7553
handle: 0x001a, char properties: 0x12, char value handle: 0x001b, uuid: 02005970-6d75-4749-5053-676e6f6c7553
handle: 0x001d, char properties: 0x0a, char value handle: 0x001e, uuid: 03005970-6d75-4749-5053-676e6f6c7553
handle: 0x0021, char properties: 0x08, char value handle: 0x0022, uuid: 0000fca0-0000-1000-8000-00805f9b34fb
handle: 0x0024, char properties: 0x10, char value handle: 0x0025, uuid: 0000fca1-0000-1000-8000-00805f9b34fb
handle: 0x0028, char properties: 0x08, char value handle: 0x0029, uuid: 0000fca2-0000-1000-8000-00805f9b34fb
[C6:05:04:03:F1:BF][LE]> char-read-hnd 0x001b
Characteristic value/descriptor: fe fd 40 01 6c 0a
最后写个脚本:
import os
import time
import sys
while True:
time.sleep(1)
out=sys.stdout
process = os.popen("sudo gatttool -b C6:05:04:03:F1:BF --char-read -a 0x001b")
output = process.read()
out.write("\n%s" % (output))
out.flush()
process.close