技術はメシのタネ

底辺プログラマで技術の向上を目指しているけれどやりたい事が無くて困っている

iOSでiBeaconを探す、AndroidでiBeaconを探す

ちょっとメモなので親切で無いのはご勘弁を。
AndroidiOSの両方で、iBeaconのアドバタイズ・パケットを受信して領域検知を行うアプリを作るとする。

肝心要の領域検知を開始する開始の仕方。

iOSの場合。

var myLocationManager:CLLocationManager = CLLocationManager()
var myBeaconRegion:CLBeaconRegion!

myBeaconRegion = CLBeaconRegion(proximityUUID: uuid)
myLocationManager.startMonitoringForRegion(myBeaconRegion)

Androidの場合。
APIだけで申し訳ないが、ちょっと時間が無いのでご勘弁を。

public boolean startLeScan(UUID[] serviceUuids, LeScanCallback callback)

ここで注目したいのが、UUID。

iOSの場合、CLBeaconRegionの引数のUUIDは、proximity UUID
実際のアドバタイズ・パケットに含まれているもの。

一方、Androidの場合、startLeScanの引数のUUIDはService UUID
GATTサーバーに登録済みのプロファイルを引っ掛けるもの……だと思う、たぶん。

もし特定のperipheralのみを探す場合, 代わりにstartLeScan(UUID[], BluetoothAdapter.LeScanCallback)を使うことができる. アプリケーションがサポートしているGATT serviceのUUIDのarrayを提供すればよい.
Yukiの枝折


同じ128bitのUUIDであっても、2つのUUIDのもたらす効果は全く別ってわけ。
まあ本家本元だけあってiOSの方が断然使いやすくできているのは仕方ないとして、Androidの紛らわしさったら無い。

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
        ...
        }
};
.
.
.
mBluetoothAdapter.startLeScan(mLeScanCallback);


この様に、Androidでは、引数1つのstartLeScan()を用いてBLEの検出をコールバックで待ち受けて、scanRecordを解析するまでそれがiBeacon形式のアドバタイズ・パケットかどうかはわからない。

……っていうの、本当かなあって疑ってる。
今ひとつBLEとかGATTのことがよく理解できていないのであれなんだけども。

サービスがこうで、
Bluetooth Developer Portal サービス

proximityのプロファイルがこうなわけでしょ。
Home > GATT Specifications > Profiles > Profile Viewer

<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright 2011 Bluetooth SIG, Inc. All rights reserved. -->
<Profile xsi:noNamespaceSchemaLocation="http://schemas.bluetooth.org/Documents/profile.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    type="org.bluetooth.profile.proximity"
    name="Proximity">
    <InformativeText>
        <Abstract>
            The Proximity profile enables proximity monitoring between two devices.
        </Abstract>
        <Summary>
            The Proximity profile defines the behavior when a device moves away from a peer device so that the connection is dropped or the path loss increases above a preset level, causing an immediate alert. This alert can be used to notify the user that the devices have become separated. As a consequence of this alert, a device may take further action, for example to lock one of the devices so that it is no longer usable.
        <p>
            The Proximity profile can also be used to define the behavior when the two devices come closer together such that a connection is made or the path loss decreases below a preset level.
        </p>
        </Summary>
    </InformativeText>
    <Role name="Proximity Reporter">
        <Service type="org.bluetooth.service.link_loss">
            <Declaration>Primary</Declaration>
            <Requirement>Mandatory</Requirement>
        </Service>
        <Service type="org.bluetooth.service.immediate_alert">
            <Declaration>Primary</Declaration>
            <Requirement>Optional</Requirement>
        </Service>
        <Service type="org.bluetooth.service.tx_power">
            <Declaration>Primary</Declaration>
            <Requirement>Optional</Requirement>
        </Service>
        <Conditional condition="if_any_supported_all_must_be_supported">
            <Type>org.bluetooth.service.immediate_alert</Type>
            <Type>org.bluetooth.service.tx_power</Type>
        </Conditional>
    </Role>
    <Role name="Proximity Monitor">
        <Client type="org.bluetooth.service.link_loss">
            <Requirement>Mandatory</Requirement>
        </Client>
        <Client type="org.bluetooth.service.immediate_alert">
            <Requirement>Mandatory</Requirement>
        </Client>
        <Client type="org.bluetooth.service.tx_power">
            <Requirement>Mandatory</Requirement>
        </Client>
    </Role>
</Profile>

ビーコン側がたぶんProximity Reporterだと思うんだけど、0000XXXX-0000-1000-8000-00805f9b34fbのXXXXに、

  • org.bluetooth.service.link_loss……0x1803

入れてやってUUID.fromString(String)したのをstartLeScan()に渡してやれば、GATTサーバに登録済みのサービス(BLE)を無制限にscanするんじゃなくて、iBeacon(proximity)だけ拾うんじゃ無いかなあ……と思う次第。

追記

ダメでした。
そっかー、proximityじゃないのか……。
噂のAndroid用iBeaconライブラリAltBeaconのソースを確認してみたけど、やっぱり引数1つのstartLeScan()を使っている。
ソース

こういう質問も出てるけど、今のところ回答なし。
Service UUIDはわかりませーん。