Created
April 22, 2018 10:54
-
-
Save KazuyukiEguchi/ff039d6e377ab60e3285a51ca9ddb9b2 to your computer and use it in GitHub Desktop.
M5STACK(ESP32)でiBeaconを受信してみる ref: https://qiita.com/KazuyukiEguchi/items/159e628ab9f7fcc74541
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// M5STACK(ESP32)でiBeaconを受信してみる | |
// 2018/04/22 Programed by Kazuyuki Eguchi | |
#include <BLEDevice.h> | |
#include <BLEUtils.h> | |
#include <BLEScan.h> | |
#include <BLEAdvertisedDevice.h> | |
int scanTime = 10; // スキャン完了までの秒数 | |
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { | |
void onResult(BLEAdvertisedDevice advertisedDevice) { | |
char uuid[60]; | |
BLEAddress addr = advertisedDevice.getAddress(); | |
int rssi = advertisedDevice.getRSSI(); | |
std::string data = advertisedDevice.getManufacturerData(); | |
if(data.length() == 25) | |
{ | |
if((data[0] == 0x4c) && (data[1] == 0x00) && (data[2] == 0x02) && (data[3] == 0x15)) { | |
sprintf(uuid,"%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X" | |
,data[4],data[5],data[6],data[7],data[8],data[9],data[10],data[11],data[12],data[13] | |
,data[14],data[15],data[16],data[17],data[18],data[19]); | |
int major = (int)(((data[20]&0xff) << 8) + (data[21] & 0xff)); | |
int minor = (int)(((data[22]&0xff) << 8) + (data[23] & 0xff)); | |
signed char power = (signed char)(data[24]&0xff); | |
Serial.printf("addr=%s rssi=%d uuid=%s,major=%d,minor=%d,power=%d\n",addr.toString().c_str(),rssi,uuid,major,minor,power); | |
} | |
} | |
} | |
}; | |
void setup() { | |
Serial.begin(115200); | |
} | |
void loop() { | |
BLEDevice::init(""); | |
BLEScan* pBLEScan = BLEDevice::getScan(); | |
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(),true); // 重複を許さない場合は、falseに変更のこと。 | |
pBLEScan->setActiveScan(true); | |
BLEScanResults foundDevices = pBLEScan->start(scanTime); | |
Serial.println("Scan done!"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment