- Bluetooth Smart Ready：支持传统蓝牙模式和LE模式
- Bluetooth Smart：仅支持LE模式
新一代的手机（iPhone 4S+，SG3+），笔记本电脑，平板电脑等，都已经配备了蓝牙4.0，即“Smart Ready”设备。另一方面，Beacons仅支持低功耗协议（电池更耐用），即“Bluetooth Smart”设备。而一些旧设备，比如外设、汽车系统、旧手机等，通常只支持传统蓝牙设备。
BLE的最大亮点就在于低功耗。例如，一些beacon设备仅靠一枚纽扣电池就能够持续2年传送一个信号，而这种电池一般是不可拆卸的，因此你只需在beacon停止工作后进行替换。传统蓝牙和LE蓝牙使用的是相同的波段（2.4 GHz – 2.4835 GHz）。BLE协议的传输速率较低，因此并不适用于大量数据的传送，而仅适用于定位和简单通信。在覆盖范围方面，传统蓝牙和LE蓝牙的都可达到100米。
How does BLE communication work?
BLE communication consists of two main parts: advertising and connecting.
Advertising is a one-way discovery mechanism. Devices which want to be discovered can transmit packets of data in intervals from 20 ms to 10 seconds. The shorter the interval, the shorter the battery life, but the faster the device can be discovered. The packets can be up to 47 bytes in length and consist of:
- 1 byte preamble
- 4 byte access address
- 2-39 bytes advertising channel PDU
- 3 bytes CRC
For advertisement communication channels, the access address is always
0x8E89BED6. For data channels, it is different for each connection.
搜索到设备后，就可以建立连接。然后，就可以读取BLE设备中提供的服务及其特性，这一过程也被看作是对关贸总协定（GATT）的实现。每个特性都会提供一些可供读取或修改的数值。例如，一个智能恒温器可以提供一个读取当前温度或湿度的服务（作为该服务的特性），以及另一个用于设置所需温度的服务和特性。然而，由于beacon设备不使用连接模式，所以我会跳过一些细节。如果你想了解更多关于连接BLE设备的内容，Apple’s Core Bluetooth guide（苹果核心蓝牙指南）可以提供参考，尽管你不是iOS开发者也可以选择阅读。想要获取更具技术性的文章，可以参考EE times（BLE介绍，充分利用BLE广告模式）。
02 01 06 1A FF 4C 00 02 15 B9 40 7F 30 F5 F8 46 6E AF F9 25 55 6B 57 FE 6D 00 49 00 0A C5
The data above already has the preamble, fixed access address, advertisement PDU header and MAC address removed; it is only the advertisement data – 30 bytes, so it fits nicely in the 31 byte limit.
What makes a BLE advertisement an iBeacon one? The format is fixed by Apple. To break it down (see also SO):
1 2 3 4 5
02 01 06 1A FF 4C 00 02 15: iBeacon prefix (fixed except for 3rd byte - flags) B9 40 7F 30 F5 F8 46 6E AF F9 25 55 6B 57 FE 6D: proximity UUID (here: Estimote’s fixed UUID) 00 49: major 00 0A: minor C5: 2’s complement of measured TX power
What follows is that if you want to experiment with beacons, you don’t really need any special devices. If you have a newer phone (e.g. iPhone 4S+, SG3+) or a Bluetooth 4 laptop (e.g. Retina MacBook), you can turn any of these devices into an iBeacon transmitter and receiver. For iPhones, see for example the “Locate iB” app in AppStore. For MacOS, see here. And of course, you can use Raspberry Pi as a beacon as well.
Breaking down the iBeacon format
Apart from the mostly fixed iBeacon prefix (
02 01 ... 15), what is the meaning of the other components?
The proximity UUID (
B9 ... 6D in our example), is an identifier which should be used to distinguish your beacons from others. If, for example, beacons where used to present special offers to customers in a chain of stores, all beacons belonging to the chain would have the same proximity UUID. The dedicated iPhone application for that chain would scan in the background for beacons with the given UUID.
The major number (2 bytes, here: 0x0049, so 73) is used to group a related set of beacons. For example, all beacons in a store will have the same major number. That way the application will know in which specific store the customer is.
The minor number (again 2 bytes, here: 0x000A, so 10) is used to identify individual beacons. Each beacon in a store will have a different minor number, so that you know where the customer is exactly.
The final field, TX power, is used to determine how close you are to a beacon. This can be presented either as rough information (immediate/far/out of range) or as a more precise measurement in meters (you can convert to feet of course ;) ). How is it done?
The TX power (here: 0xC5 = 197, 2’s complement = 256-197 = -59 dBm) is the strength of the signal measured at 1 meter from the device (RSSI – Received Signal Strength Indication). As the strength of the signal decreases predictably as we get further, knowing the RSSI at 1 meter, and the current RSSI (we get that information together with the received signal), it is possible to calculate the difference. iOS has this built-in, for other platforms, it needs to be hand-coded, see this SO answer for a specific algorithm.
Obstacles such as furniture, people or communication congestion can weaken the signal. Hence the distance is only an estimate.
iOS comes with some additional integration with iBeacons. Your app can receive notifications when a beacon comes into range – but not only when the app is in the foreground, also when it is in the background! An app can subscribe to region enter/exit events, so that it is partially woken up even if it isn’t running. In response to such events, the app can send e.g. a local push notification, prompting the user to open the app and see the in-store promotion (which can be for example fetched from the internet), or other relevant content.
More precisely, when the phone isn’t active, iOS goes into a low-power monitoring mode: only iBeacon region enter/exit events are detected. When the phone and app are active, you can enter ranging mode, which enables you to detect the signal strength and estimate the distance more precisely.
Note that it can take some time for your phone to detect a beacon. Firstly, the beacons transmit the advertisements from time to time. Secondly, if your phone isn’t active, it monitors for bluetooth signals only sometimes as well. For a beacon to be detected, these two intervals must overlap. In practice, it can take up to 15 minutes to detect a beacon.
For a step-by-step guide to writing an iOS iBeacon application, see here. Beacon manufacturers also often provide dedicated SDKs which help in writing beacon-enabled applications. See for example Estimote’s iOS SDK and Android one.
How can I get some beacons?
Beacons are currently a scarce resource; you often have to wait a couple of weeks to get some; but certainly availability will become better and better.
Hence the fastest option is to build a “softbeacon”: turn your iPhone/Android/MacBook/other laptop/RaspberryPi into one (as described above).
Your second option is to try and order some beacons:
- pre-order Estimote beacons; 3 for $99
- Kontakt beacons come in a couple of packages; 4 for $99, 10 for $279
- RaspberryPi kits from RadiusNetworks: 1 for $99
- RedBearLab offers BLE shields for Arduino for $30
- Bleu sells USB-iBeacon dongles. 1 for $40, 5 for $150
iBeacons isn’t the only proximity BLE-based technology. Qualcomm is developing its own beacons, Gimbal, together with iOS and Android SDKs. They will offer a similar feature set, however the format of the BLE advertisement may be different. My developer kit is on its way, so I haven’t tested them yet, but the beacons certainly look interesting – especially because of the pricing – $5/basic beacon.