LoginSignup
6
6

More than 3 years have passed since last update.

M5StickC を iBeacon にする

Posted at

IMG_0062.jpeg

iBeaconが足りなく、テスト用にM5StackCをiBeaconとして代用した方法を紹介します。

困っていた点

  • 沢山のビーコンがあり、どれが該当のMajor, Minorのビーコンが分からない。
  • iBeaconの数が足りない。

改善したこと

  • UUID, Major, Minorをディスプレイに表示するようにしました。

iBeaconソースコード

#include <M5StickC.h>
#include "BLEDevice.h"
#include "BLEUtils.h"
#include "BLEBeacon.h"

#define BEACON_UUID           "4d6fc88b-be75-6698-da48-6866a36ec78e"

#define MAJOR   1000
#define MINOR   1000

#define TX_POWER   -65

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

BLEAdvertising *pAdvertising;

std::string setUUID(){
  std::string org = BEACON_UUID;
  std::string dst = "";
  if( org.length() != 36 ){
    return "error";
  }
  dst  = org[34];
  dst += org[35];
  dst += org[32];
  dst += org[33];
  dst += org[30];
  dst += org[31];
  dst += org[28];
  dst += org[29];
  dst += org[8];
  dst += org[26];
  dst += org[27];
  dst += org[24];
  dst += org[25];
  dst += org[23];
  dst += org[21];
  dst += org[22];
  dst += org[19];
  dst += org[20];
  dst += org[18];
  dst += org[16];
  dst += org[17];
  dst += org[14];
  dst += org[15];
  dst += org[13];
  dst += org[11];
  dst += org[12];
  dst += org[9];
  dst += org[10];
  dst += org[6];
  dst += org[7];
  dst += org[4];
  dst += org[5];
  dst += org[2];
  dst += org[3];
  dst += org[0];
  dst += org[1];
  return dst;
}

void setBeacon() {

  BLEBeacon oBeacon = BLEBeacon();
  oBeacon.setManufacturerId(0x4C00);
  oBeacon.setProximityUUID(BLEUUID(setUUID()));
  oBeacon.setMajor(MAJOR);
  oBeacon.setMinor(MINOR);
  oBeacon.setSignalPower(TX_POWER);
  BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
  BLEAdvertisementData oScanResponseData = BLEAdvertisementData();

  oAdvertisementData.setFlags(0x04);

  std::string strServiceData = "";

  strServiceData += (char)26;     // Len
  strServiceData += (char)0xFF;   // Type
  strServiceData += oBeacon.getData(); 
  oAdvertisementData.addData(strServiceData);

  pAdvertising->setAdvertisementData(oAdvertisementData);
  pAdvertising->setScanResponseData(oScanResponseData);

}
#define LED 10

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(M5_BUTTON_HOME, INPUT_PULLUP);
  digitalWrite(LED, HIGH);
  M5.begin();
  M5.Lcd.setRotation(1);
  M5.Lcd.fillScreen(BLACK);

  M5.Lcd.setCursor(2, 0, 2);
  M5.Lcd.printf("UUID: %s",BEACON_UUID);
  M5.Lcd.setCursor(2, 40, 2);
  M5.Lcd.printf("Major %d Minor %d",MAJOR ,MINOR);  

  Serial.begin(115200);
  BLEDevice::init("");
  pAdvertising = BLEDevice::getAdvertising();

  setBeacon();
}

void loop() {  
  pAdvertising->start();
  digitalWrite(LED, LOW);  
  Serial.println("Advertizing started...");
  delay(100);
  Serial.println("Advertizing stop...");
  pAdvertising->stop();
  digitalWrite(LED, HIGH);  
  delay(900);
}

ユーザーごとに変更する変数

#define BEACON_UUID           "4d6fc88b-be75-6698-da48-6866a36ec78e"
#define MAJOR   1000
#define MINOR   1234

悩んだこと

  • UUIDの指定が後ろから、文字列を詰めて指定する点です。

Tips

MacOS Catalinaでは、いろいろと問題がありますので、下記の記事を参考にしてください。
https://qiita.com/SamAkada/items/fa32d6072a832a10cd84

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6