初めてのBLE (5) micro:bitでペリフェラル②Arduinoのセントラル
前回、micro:bitをBLEの温度計にしました。このペリフェラルに、セントラルがつなぎにくると、数字の1を表示し、接続がなくなると×の表示になります。
●Arduino BLEのセントラル
セントラル機能で利用しているNano 33 IoTで、サンプル・スケッチPeripheral Exploreを動かし、micro:bitに接続する実験をします。
loop()を次のように変更します。BLEを探すと複数見つかるので、ここではlocalName() == "BBC micro:bit [gavag]"としてmicro:bitボードを発見し、その後2秒待ってから、各種データを読み出します。"BBC micro:bit [gavag]"は、サンプル・スケッチのscanで見つかった名前です。 [gavag]は製品によって異なるようです。
void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();
delay(2000);
if (peripheral.localName() == "BBC micro:bit [gavag]") {
Serial.print("\nFound ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
// stop scanning
BLE.stopScan();
explorerPeripheral(peripheral);
// peripheral disconnected, we are done
while (1) {
// do nothing
}
}
// }
}
実行しました。最後のUUIDが温度計です。Value=0x19は25℃です。