0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32: GATT の再接続のテスト

Last updated at Posted at 2024-10-10

こちらと同じことを行いました。
ESP32を使ったBLE実装時の再接続について

ArduinoIDE 2.3.3 を使いました。

プログラム

gatt_advertise.ino
// ---------------------------------------------------------------------
//	gatt_advertise.ino
//
//						Oct/10/2024
// ---------------------------------------------------------------------

#define	PROGRAM	"gatt_advertise.ino"
#define VERSION	"Oct/06/2024 PM 13:25"

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

#define DEVICE_NAME         "DEV-HAT"
#define SERVICE_UUID        "cf2a980a-f58a-49ba-bc44-8ef5e1cb2dd3"

int icount = 0;
// ---------------------------------------------------------------------
// Serverのコールバックで接続に対する処理を行う
class ServerCallbacks : public BLEServerCallbacks {
	// 接続時に呼び出される
	void onConnect(BLEServer* pServer) {
		Serial.println("*** onConnect ***");
		// 接続されたらAdvertisingを停止する
		BLEDevice::stopAdvertising();
	}

// ---------------------------------------------------------------------
	// 切断された時に呼び出される
	void onDisconnect(BLEServer* pServer) {
		Serial.println("*** onDisconnect ***");
		// 再接続のためにもう一度Advertisingする
		BLEDevice::startAdvertising();
	}
};

// ---------------------------------------------------------------------
void setup() {
	Serial.begin(115200);
	while (! Serial) { delay(1); }
	delay(300);
	Serial.println();
	Serial.println("*** setup *** start ***");
	Serial.println(PROGRAM);
	Serial.println(VERSION);

	// BLEの初期化
	BLEDevice::init(DEVICE_NAME);
	BLEServer* pServer = BLEDevice::createServer();
	pServer->setCallbacks(new ServerCallbacks());
	BLEService* pService = pServer->createService(SERVICE_UUID);
	
	pService->start();

	// Scanに引っかかるようにアドバタイジングする
	BLEAdvertising* pAdvertinsing = BLEDevice::getAdvertising();
	pAdvertinsing->addServiceUUID(SERVICE_UUID);
	pAdvertinsing->setScanResponse(true);
	// iPhoneの接続問題を解決するため
	pAdvertinsing->setMinPreferred(0x06); 
	pAdvertinsing->setMinPreferred(0x12);
	BLEDevice::startAdvertising();

	Serial.println("*** setup *** end ***");
}

// ---------------------------------------------------------------------
void loop() {
	icount++;

	if ((icount % 10) == 0)
		{
		Serial.print(VERSION);
		Serial.print(" -- ");
		Serial.println(icount);
		}

	delay(1000);
}

// ---------------------------------------------------------------------

UUID

こちらのページで作成しました。
Online UUID Generator

実行結果

image.png

*** setup *** start ***
gatt_advertise.ino
Oct/06/2024 PM 13:25
*** setup *** end ***
Oct/06/2024 PM 13:25 -- 10
Oct/06/2024 PM 13:25 -- 20
*** onConnect ***
Oct/06/2024 PM 13:25 -- 30
*** onDisconnect ***
Oct/06/2024 PM 13:25 -- 40
*** onConnect ***
Oct/06/2024 PM 13:25 -- 50
Oct/06/2024 PM 13:25 -- 60
Oct/06/2024 PM 13:25 -- 70
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?