文档结构  
翻译进度:36%     翻译赏金:0 元 (?)    ¥ 我要打赏
参与翻译: Judy (5)

iBeacons无疑是最近的一个热门话题,通过这种技术,可以实现室内定位,让你的手机知道你处在信标(beacon)范围内。这种技术的应用十分广泛:在停车场内它可以帮助你轻松找到你的汽车,在商场内它可以根据定位为你提供优惠券和相关的服务,还有很多我们现在无法想象的强大功能。

Will iBeacons become the lighthouses of our times?

现在有很多文章是关于“什么是iBeacons?”以及“我们可以利用它做什么?”,但站在技术层面看,它的工作原理是什么呢? 它的基础技术是低功耗蓝牙技术(BLE),那么……

什么是低功耗蓝牙技术(BLE)?

低功耗蓝牙技术(BLE,官方网站维基百科)是2010年底发布的蓝牙4.0技术的一部分,它的前身是2006年发布的诺基亚Wibree技术,但现在已被合并到蓝牙技术。它不同于传统蓝牙的协议,并且使用设备也不是向后兼容,因此,现在市面上主要存在以下三种设备:

第 1 段(可获 1.95 积分)
  • Bluetooth:仅支持传统蓝牙模式
  • Bluetooth Smart Ready:支持传统蓝牙模式和LE模式
  • Bluetooth Smart:仅支持LE模式

LogoBluetoothSmartReady

新一代的手机(iPhone 4S+,SG3+),笔记本电脑,平板电脑等,都已经配备了蓝牙4.0,即“Smart Ready”设备。另一方面,Beacons仅支持低功耗协议(电池更耐用),即“Bluetooth Smart”设备。而一些旧设备,比如外设、汽车系统、旧手机等,通常只支持传统蓝牙设备。

第 2 段(可获 1.09 积分)

BLE的最大亮点就在于低功耗。例如,一些beacon设备仅靠一枚纽扣电池就能够持续2年传送一个信号,而这种电池一般是不可拆卸的,因此你只需在beacon停止工作后进行替换。传统蓝牙和LE蓝牙使用的是相同的波段(2.4 GHz – 2.4835 GHz)。BLE协议的传输速率较低,因此并不适用于大量数据的传送,而仅适用于定位和简单通信。在覆盖范围方面,传统蓝牙和LE蓝牙的都可达到100米。

第 3 段(可获 1.26 积分)

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

bluetooth le packet

For advertisement communication channels, the access address is always 0x8E89BED6. For data channels, it is different for each connection.

第 4 段(可获 1.29 积分)

交替的PDU数据也有它自己的标头(2字节,内容包括有效载荷数据的大小和类型,设备是否支持连接等)和实际载荷数据(最多37字节)。

最后,有效载荷的前6字节是该设备的MAC地址,而实际载荷数据最多31字节。

BLE设备可以在不可连接的广告模式(所有信息都包含在广告中)下运行,但设备也可以允许进行连接(通常情况下)。

搜索到设备后,就可以建立连接。然后,就可以读取BLE设备中提供的服务及其特性,这一过程也被看作是对关贸总协定(GATT)的实现。每个特性都会提供一些可供读取或修改的数值。例如,一个智能恒温器可以提供一个读取当前温度或湿度的服务(作为该服务的特性),以及另一个用于设置所需温度的服务和特性。然而,由于beacon设备不使用连接模式,所以我会跳过一些细节。如果你想了解更多关于连接BLE设备的内容,Apple’s Core Bluetooth guide(苹果核心蓝牙指南)可以提供参考,尽管你不是iOS开发者也可以选择阅读。想要获取更具技术性的文章,可以参考EE times(BLE介绍充分利用BLE广告模式)。

第 5 段(可获 2.88 积分)

Beacon设备如何使用BLE?

Beacon设备只使用了广告信道,“beacon”正如它字面上的意思(信标、灯塔)一样,它们会在一定的时间间隔内传送数据包,而这些数据可以被智能手机等设备获取。因此iBeacons只是单纯使用BLE广告模式,并在此基础上对iOS提供一定支持。

如果你试图拦截iBeacon的一个广告数据包,比如说是来自Estimote beacon的数据包,你会看到以下的数据:

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

(要截获以上数据,如果你使用的是OSX设备,可以下载额外的XCode,它里面包含了蓝牙扫描仪和数据包记录器;如果你使用的是Windows设备,请点击这里

第 6 段(可获 1.28 积分)

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

bluetooth le ibeacon packet

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.

第 7 段(可获 1.69 积分)

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.

第 8 段(可获 1.63 积分)

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.

Measuring distance

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.

第 9 段(可获 2.18 积分)

Obstacles such as furniture, people or communication congestion can weaken the signal. Hence the distance is only an estimate.

iOS integration

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.

第 10 段(可获 1.49 积分)

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.

第 11 段(可获 1.41 积分)

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:

第 12 段(可获 1.24 积分)
  • 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

Alternatives

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.

第 13 段(可获 1.4 积分)

What’s next?

Now the only thing left to do is to develop some beacon-enabled applications! For this purpose, keep SoftwareMill in mind: we are always on the lookout for interesting projects just waiting to be developed :).

sml_mainlogo_rgb

第 14 段(可获 0.46 积分)

文章评论