首先先了解一下bluez, 以及常用的tools.
- hcitool、bluetoothctl等工具,可以進行BLE設備的掃描、連接、配對、廣播等操作;
- hcitool可以發送HCI command,設置BLE的廣播數據;
- gatttool可以在GATT層面,完成GATT profile的連接、service attribute的讀寫等操作;
bluez
安裝
sudo apt-get install bluez
hciconfig
查看藍牙設備
pi@raspberrypi:~ $ hciconfig
hci0: Type: Primary Bus: UART
BD Address: B8:27:EB:26:23:F1 ACL MTU: 1021:8 SCO MTU: 64:1
UP RUNNING
RX bytes:822 acl:0 sco:0 events:57 errors:0
TX bytes:4231 acl:0 sco:0 commands:57 errors:0
所有命令如下
pi@raspberrypi:~ $ hciconfig --help
hciconfig - HCI device configuration utility
Usage:
hciconfig
hciconfig [-a] hciX [command ...]
Commands:
up Open and initialize HCI device
down Close HCI device
reset Reset HCI device
rstat Reset statistic counters
auth Enable Authentication
noauth Disable Authentication
encrypt Enable Encryption
noencrypt Disable Encryption
piscan Enable Page and Inquiry scan
noscan Disable scan
iscan Enable Inquiry scan
pscan Enable Page scan
ptype [type] Get/Set default packet type lm [mode] Get/Set default link mode lp [policy] Get/Set default link policy name [name] Get/Set local name class [class] Get/Set class of device voice [voice] Get/Set voice setting iac [iac] Get/Set inquiry access code inqtpl [level] Get/Set inquiry transmit power level inqmode [mode] Get/Set inquiry mode inqdata [data] Get/Set inquiry data inqtype [type] Get/Set inquiry scan type inqparms [win:int] Get/Set inquiry scan window and interval pageparms [win:int] Get/Set page scan window and interval pageto [to] Get/Set page timeout afhmode [mode] Get/Set AFH mode sspmode [mode] Get/Set Simple Pairing Mode aclmtu <mtu:pkt> Set ACL MTU and number of packets scomtu <mtu:pkt> Set SCO MTU and number of packets delkey <bdaddr> Delete link key from the device oobdata Get local OOB data commands Display supported commands features Display device features version Display version information revision Display revision information block <bdaddr> Add a device to the blacklist unblock <bdaddr> Remove a device from the blacklist lerandaddr <bdaddr> Set LE Random Address leadv [type] Enable LE advertising 0 - Connectable undirected advertising (default) 3 - Non connectable undirected advertising noleadv Disable LE advertising lestates Display the supported LE states
hcitool
pi@raspberrypi:~ $ hcitool --help
hcitool - HCI Tool ver 5.43
Usage:
hcitool [options] <command> [command parameters]
Options:
--help Display help
-i dev HCI device
Commands:
dev Display local devices
inq Inquire remote devices
scan Scan for remote devices
name Get name from remote device
info Get information from remote device
spinq Start periodic inquiry
epinq Exit periodic inquiry
cmd Submit arbitrary HCI commands
con Display active connections
cc Create connection to remote device
dc Disconnect from remote device
sr Switch master/slave role
cpt Change connection packet type
rssi Display connection RSSI
lq Display link quality
tpl Display transmit power level
afh Display AFH channel map
lp Set/display link policy settings
lst Set/display link supervision timeout
auth Request authentication
enc Set connection encryption
key Change connection link key
clkoff Read clock offset
clock Read local or remote clock
lescan Start LE scan
leinfo Get LE remote information
lewladd Add device to LE White List
lewlrm Remove device from LE White List
lewlsz Read size of LE White List
lewlclr Clear LE White List
lerladd Add device to LE Resolving List
lerlrm Remove device from LE Resolving List
lerlclr Clear LE Resolving List
lerlsz Read size of LE Resolving List
lerlon Enable LE Address Resolution
lerloff Disable LE Address Resolution
lecc Create a LE Connection
ledc Disconnect a LE Connection
lecup LE Connection Update
For more information on the usage of each command use:
hcitool <command> --help
bluetoothctl
打開bluetoochctl工具: bluetoothctl
pi@raspberrypi:/etc/systemd/system $ bluetoothctl
[NEW] Controller B8:27:EB:26:23:F1 raspberrypi [default]
掃描: scan on
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:26:23:F1 Discovering: yes
[NEW] Device 00:26:5E:DD:78:E6 00-26-5E-DD-78-E6
列出掃描到的設備:devices
[bluetooth]# devices
Device 00:26:5E:DD:78:E6 00-26-5E-DD-78-E6
退出:exit
[bluetooth]# exit
[DEL] Controller B8:27:EB:26:23:F1 raspberrypi [default]
pi@raspberrypi:/etc/systemd/system $
所有的命令如下:
[bluetooth]# help
Available commands:
list List available controllers
show [ctrl] Controller information
select <ctrl> Select default controller
devices List available devices
paired-devices List paired devices
power <on/off> Set controller power
pairable <on/off> Set controller pairable mode
discoverable <on/off> Set controller discoverable mode
agent <on/off/capability> Enable/disable agent with given capability
default-agent Set agent as the default one
advertise <on/off/type> Enable/disable advertising with given type
set-advertise-uuids [uuid1 uuid2 ...] Set advertise uuids
set-advertise-service [uuid][data=[xx xx ...] Set advertise service data
set-advertise-manufacturer [id][data=[xx xx ...] Set advertise manufacturer data
set-advertise-tx-power <on/off> Enable/disable TX power to be advertised
set-scan-filter-uuids [uuid1 uuid2 ...] Set scan filter uuids
set-scan-filter-rssi [rssi] Set scan filter rssi, and clears pathloss
set-scan-filter-pathloss [pathloss] Set scan filter pathloss, and clears rssi
set-scan-filter-transport [transport] Set scan filter transport
set-scan-filter-clear Clears discovery filter.
scan <on/off> Scan for devices
info [dev] Device information
pair [dev] Pair with device
trust [dev] Trust device
untrust [dev] Untrust device
block [dev] Block device
unblock [dev] Unblock device
remove <dev> Remove device
connect <dev> Connect device
disconnect [dev] Disconnect device
list-attributes [dev] List attributes
set-alias <alias> Set device alias
select-attribute <attribute> Select attribute
attribute-info [attribute] Select attribute
read Read attribute value
write <data=[xx xx ...]> Write attribute value
notify <on/off> Notify attribute value
register-profile <UUID ...> Register profile to connect
unregister-profile Unregister profile
version Display version
quit Quit program
RaspberryPi 發送beacon
蘋果在BLE的基礎上推出了iBeacon協議。iBeacon使用了BLE的廣播部分,但不建立連接。一個遵守iBeacon協議的外設稱為Beacon。Beacon會廣播自己的身份信息和發射信號的強度。中心設備接收到廣播之后,除了可以獲知Beacon的身份之外,還能通過信號的衰減算出自己與Beacon的距離。
pi@raspberrypi:~ $ sudo hciconfig hci0 noscan
pi@raspberrypi:~ $ sudo hciconfig hci0 leadv 3
pi@raspberrypi:~ $ sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 63 6F 3F 8F 64 91 4B EE 95 F7 D8 CC 64 A8 63 B5 00 01 00 02 C5
< HCI Command: ogf 0x08, ocf 0x0008, plen 31
1E 02 01 1A 1A FF 4C 00 02 15 63 6F 3F 8F 64 91 4B EE 95 F7
D8 CC 64 A8 63 B5 00 01 00 02 C5
> HCI Event: 0x0e plen 4
01 08 20 00
使用Android APP測試Beacon功能
iBeacon & Eddystone Scanner Google開發的,可以從Google Play下載運行(地址為:https://play.google.com/store/apps/details?id=de.flurp.beaconscanner.app&hl=zh_CN)。
如果你的Android設備不能訪問Google Play,也可以通過在線APK下載網站(https://apkpure.com/)將APK下載到電腦后安裝。
測試結果
拿着手機走幾步看看:
使用結束后,可以用下面命令來恢復掃描和停止廣播:
sudo hciconfig hci0 piscan # 恢復掃描
sudo hciconfig hci0 noleadv # 停止廣播
Reference
藍牙串口
https://www.raspberrypi.org/forums/viewtopic.php?p=919420
https://blog.csdn.net/wanyeye/article/details/52909869?fps=1&locationNum=10
使用Beacon:
https://www.cnblogs.com/vamei/p/6753531.html
http://www.wowotech.net/bluetooth/eddystone_test.html
gatttool hcitool
https://blog.csdn.net/talkxin/article/details/50610984
https://blog.csdn.net/Chuck_lin/article/details/78846165
SPP
https://baike.baidu.com/item/%E8%93%9D%E7%89%99%E4%B8%B2%E5%8F%A3/1415870?fr=aladdin