首先先了解一下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

SDP

https://www.cnblogs.com/hzl6255/p/3826558.html

树莓派 - 蓝牙 (1) 试试Beacon的更多相关文章

  1. 树莓派开发笔记(十一):蓝牙的使用,BlueZ协议(双树莓探测rssi并通过蓝牙互传获取的rssi信号强度)

    若该文为原创文章,转载请注明原文出处本文章博客地址:https://blog.csdn.net/qq21497936/article/details/110940484长期持续带来更多项目与技术分享, ...

  2. Java 学习文章汇总

    目前JAVA可以说是产业界和学术界最热门的语言,许多人都很急切想把JAVA学好. 但学习是需要步骤的,除非像电影中演的那样,能够把需要的专业技巧下载到脑海:主角只花了几秒下载资料,就马上具备飞行员的技 ...

  3. Linux 应用笔记

    Linux 应用笔记 Linux 应用笔记 小书匠 Raspberry Pi 常用命令 CentOs Raspberry Ubuntu python 实用教程 Vim 权限问题 内存分配 shell ...

  4. 在树莓派3B、Ubuntu 18.04关闭板载Wifi、蓝牙

    树莓派没有传统的BIOS设置界面,文件/boot/firmware/config.txt就相当一个BIOS设置.这里是config.txt的详细文档:https://github.com/raspbe ...

  5. 【树莓派】【转】树莓派3装Android 6.0,支持Wi-Fi和蓝牙

    树莓派3装Android 6.0,支持Wi-Fi和蓝牙 相信对于许多树莓派初学者(包括我)来说,Android系统的确是一个不错的选择.但国内这方面资源稀缺,经本人FQ苦寻,找到了老外的树莓派Andr ...

  6. 蓝牙Beacon广播数据包格式以及解析

    目录 1. 获取原始蓝牙广播包 2. 安装WireShark软件 3. 分析Beacon广播包数据 3.1 第一个数据包格式 3.2 第二个数据包格式 3.3 Android程序开发中的蓝牙广播包 4 ...

  7. 树莓派二:apt-get出错、蓝牙、汉化、输入法

    用apt-get install一个软件的时候出现了一个错误: E: Encountered a section with no Package: header E: Problem with Mer ...

  8. 让你少走弯路的搭建树莓派的Net与NodeJS运行环境

      树莓派是当前最火的嵌入计算平台没有之一,树莓派可以给我们无数的想象,树莓派的高性能.低功耗.低成本.可扩展性(最新的树莓派原生支持WIFI和蓝牙,这功能太赞了)深受大家的喜爱.虽然树莓派到目前为止 ...

  9. Beacon浅析

    作者:hongbosun 一.Beacon简介 Beacon是基于BLE技术实现的物理设备.BLE(全称Bluetooth Low Energy)是蓝牙4.0技术规范的一部分.它起源于Nokia的Wi ...

随机推荐

  1. 黑客攻防技术宝典web实战篇:攻击应用程序架构习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 假设受攻击的应用程序使用两台不同的服务器:一台应用程序服务器和一台数据库服务器.已经发现一 ...

  2. CGI、ASP、PHP、JSP、 ASP.NET网站开发语言比较

    一.主流网站开发语言的简介及优缺点. 现在主流的网站开发语言主要包括cgi.asp.php.asp.net.jsp等. HTML:当然这是网页最基本的语言,每一个服务器语言都需要它的支持. (一)  ...

  3. AtCoder Grand Contest 016 E - Poor Turkeys

    题目传送门:https://agc016.contest.atcoder.jp/tasks/agc016_e 题目大意: 有\(N\)只火鸡,现有\(M\)个人,每个人指定了两只火鸡\(x,y\),每 ...

  4. codeforces 149D Coloring Brackets (区间DP + dfs)

    题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红 ...

  5. 18.3.1获得Class对象

    package d18_3_1; /** * Java中的java.lang.Class,简单理解就是为每个java对象的类型标识的类, * 虚拟机使用运行时类型信息选择正确的执行方法,用来保存这些运 ...

  6. 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering

    http://codeforces.com/contest/742/problem/E 跪着看题解后才会的. 对于任何一对BF[i]和GF[i] 连接了一条边后,那么他们和隔壁都是不会有边相连的了,这 ...

  7. Windows API函数大全三

    7. API之位图.图标和光栅运算函数 BitBlt 将一幅位图从一个设备场景复制到另一个 CopyIcon 制作指定图标或鼠标指针的一个副本.这个副本从属于发出调用的应用程序 CopyImage 复 ...

  8. java 自定义注解,并使用示例

    场景: 对需要校验  手机验证码和短信验证码的controller方法添加 自定义的注解 @CheckType 1. 定义注解 /** * 需要短信.验证码验证方法上的注解 * date: 2018年 ...

  9. 系统设计摘录CAP

    系统架构设计理论与原则 这里主要介绍几种常见的架构设计理论和原则,常见于大中型互联系统架构设计. (一).CAP理论 1.什么是CAP 所谓CAP,即一致性(Consistency).可用性(Avai ...

  10. PMP项目管理学习笔记(11)——范围管理之定义范围

    定义范围过程组 定义范围包含将项目分解为团队成员要完成的具体工作之前你需要知道的所有一切. 输入:需求文档.项目章程.组织过程资产 工具:辅助工作室.产品分析.代理方案识别.专家判断 辅助工作室: 与 ...