Help us understand the problem. What is going on with this article?

自分のスマホが出力しているであろうCOCOAのBLEを見てみる

先日リリースされた接触確認アプリのCOCOA。
仕組みとしてはBLEを使っているはずなので、仕様さえわかれば受信してみることは可能なはず。

ということで、「自分のスマホが出力しているであろうCOCOAのBLEを見てみたい」ということを目標に試してみる

仕様

UUIDは[0xFD6F]
飛んでいるBLEを受信して、このID(10進だと64879)のものを拾えばいいはず。

なのだが、なんだか上手くいかない。。

似たようなことをやっている人を発見

そんな中Facebookのグループで出てたスケッチをまねして作ってみたのが以下。
※サンプルにしたスケッチがM5ATOM用だったので、表示などをM5StickC用にしてます。

#include <M5StickC.h>
#include <BLEDevice.h>

int scanTime = 5; //In seconds
BLEScan* pBLEScan;

// 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;
int deviceNum = 0;
int nearDeviceNum = 0;

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("  >> ADDR: ");
                Serial.print(advertisedDevice.getAddress().toString().c_str());
                Serial.print(" , ");
                Serial.print("RSSI: ");
                Serial.print(rssi);
                deviceNum++;
                if(rssi > -69){
                    found = true;
                    Serial.print(" >> Near! << ");
                    nearDeviceNum++;
                }
                Serial.println("");
            }
        }
    }
};

void setup() {
    M5.begin(true, false, true);
    M5.Lcd.setRotation( 1 );
    M5.Lcd.fillScreen(BLACK);
//    Serial.begin(115200);
    Serial.println("Scanning...");
    BLEDevice::init("");
    pBLEScan = BLEDevice::getScan(); //create new scan
    pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
    pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
    pBLEScan->setInterval(100);
    pBLEScan->setWindow(99); // less or equal setInterval value

    M5.Lcd.setTextSize(2);
}

void loop(){

    M5.update();

    Serial.println("Loop Start.");
    found = false;
    deviceNum = 0;
    nearDeviceNum = 0;
    BLEScanResults foundDevices = pBLEScan->start(scanTime, false); 

    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setTextColor(WHITE);
    M5.Lcd.setCursor(30, 20);

    M5.Lcd.print("Found : ");
    M5.Lcd.println(deviceNum);
    Serial.print(" - FoundDevice : ");
    Serial.println(deviceNum);

    if(nearDeviceNum > 0){
      M5.Lcd.setTextColor(YELLOW);
    }
    M5.Lcd.setCursor(30, 50);
    M5.Lcd.print("Near : ");
    M5.Lcd.println(nearDeviceNum);
    Serial.print(" - NearDevice : ");
    Serial.println(nearDeviceNum);


    pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
    Serial.println("Loop End.");
    delay(1000);

}

これでM5StickCで近く(1mちょいぐらい)の距離にあるCOCOAが入っているスマホの件数がわかる。

こんな感じ。

IMG_0036.jpg
IMG_0037.jpg
IMG_0038.jpg

これで何ができるわけではないが、BLEを身近に扱うことができる一歩にはなった気がする。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした