Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
接触確認アプリが有効になっているかを調べるアプリです。数十センチ程度まで近づくと素早く点滅します。M5 Atom 用です。https://twitter.com/ksasao/status/1274385507565178885 参照。Apache 2.0ライセンスです。
#include <M5Atom.h>
#include <BLEDevice.h>
// Contact Tracing Bluetooth Specification (Apple/Google)
// https://blog.google/documents/58/Contact_Tracing_-_Bluetooth_Specification_v1.1_RYGZbKW.pdf
const char* uuid = "0000fd6f-0000-1000-8000-00805f9b34fb";
bool found = false;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if(advertisedDevice.haveServiceUUID()){
if(strncmp(advertisedDevice.getServiceUUID().toString().c_str(),uuid, 36) == 0){
int rssi = advertisedDevice.getRSSI();
Serial.print("RSSI: ");
Serial.println(rssi);
Serial.print("ADDR: ");
Serial.println(advertisedDevice.getAddress().toString().c_str());
if(rssi > -40){
found = true;
Serial.println("Found!");
}
}
}
}
};
void setup() {
M5.begin(true, false, true);
BLEDevice::init("");
}
void set_led(uint8_t r, uint8_t g, uint8_t b){
int color = (g << 16) | (r << 8) | b;
M5.dis.drawpix(0, color);
delay(30);
}
void blink_led(uint8_t r, uint8_t g, uint8_t b, int duration, int count){
for(int i=0; i< count; i++){
set_led(r,g,b);
delay(duration>>1);
set_led(0,0,0);
delay(duration>>1);
}
}
void loop(){
set_led(0,0,255);
found = false;
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->start(1, false);
if(found){
blink_led(255,255,255,300,3);
}else{
set_led(0,0,0);
delay(300);
}
M5.update();
}
@ksasao

This comment has been minimized.

Copy link
Owner Author

@ksasao ksasao commented Jun 21, 2020

周囲に接触確認アプリを入れている人がいるかどうかを調べたい場合は、rssi > -40 の部分を適当に修正してください。自分の持っているデバイスに反応しないようにするには、rssi > -70 && rssi < -55 とかにするといいと思います。自分の BLE アドレスも頻繁に変わるので決め打ちできないため。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.