M5Stack

iBeacon Sample

#include <M5Atom.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEBeacon.h>

// MAMORI の UUID を模擬
#define BEACON_UUID "b9407f30-f5f8-466e-aff9-25556b57fe6e"
#define BEACON_MAJOR 0xBE49
#define BEACON_MINOR 0xF747
#define BEACON_TX_POWER -65

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(BEACON_MAJOR);
  oBeacon.setMinor(BEACON_MINOR);
  oBeacon.setSignalPower(BEACON_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);
  
}

CRGB dispColor(uint8_t r, uint8_t g, uint8_t b) {
  return (CRGB)(( r << 16) | (g << 8) | b);
}
void setup() {
  M5.begin(true, false, true);
  BLEDevice::init("");
  M5.dis.drawpix(0, dispColor(0, 0, 0));
  pAdvertising = BLEDevice::getAdvertising();
  setBeacon();
}

void loop() {
  pAdvertising->start();
  M5.dis.drawpix(0, dispColor(0, 0, 255));
  delay(100);
  pAdvertising->stop();
  M5.dis.drawpix(0, dispColor(0, 0, 0));
  delay(900);
}
トップ   一覧 検索 最終更新   ヘルプ   最終更新のRSS