RedBearLabのBLE Nano 2(nRF52832)でiBeaconを発信してみた。
とりあえず規格最短の20msで発信しまくる。
開発環境
macOS Sierra 10.12.4でArduinoを使う。
手順
Boards ManagerにBLE_Nano2を追加しないといけないので注意。
Preferences->Setting->Additional Boards Manager URLsにhttps://redbearlab.github.io/arduino/package_redbear_nRF5x_beta_index.json
を追加。
Tools->Boards:->Boards Managerで「RedBear nRF52832 Boards」をインストール。
Tools->Boards:からBLE_Nano2を選択。
BLE_Nano2を選択するとFile->ExamplesにExamples for BLE_Nano2が出現する。
詳しくはここ。
ソースコード
とりあえずひたすらビーコンを出すだけのプログラム。
# include <nRF5x_BLE_API.h>
BLEDevice ble;
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
ble.init();
ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
ble.accumulateAdvertisingPayload(GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)"LLLL", 4);
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.setAdvertisingInterval(20);
ble.setAdvertisingTimeout(0);
ble.startAdvertising();
}
void loop() {
}
iPhoneで受信する際の注意
iPhoneで受信する際はCoreBluetoothを使い接続せずにScanResultだけ見ることで最短の20msで検知できる。
しかし、RSSIしか見れない。
CoreLocationだと1sに一度になってしまうがuuid, major, minorなどを見ることができる。
iPhone側の処理は気が向いたら書きます。