ESP32: GATT通信

スケッチ例を参考にしました。

プログラム

```text:Server_multiconnect.ino
// ---------------------------------------------------------------
/*
	Server_multiconnect.ino

					Sep/21/2024
*/
// ---------------------------------------------------------------
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks : public BLEServerCallbacks {
	void onConnect(BLEServer *pServer) {
		deviceConnected = true;
		BLEDevice::startAdvertising();
	};

	void onDisconnect(BLEServer *pServer) {
		deviceConnected = false;
	}
};

// ---------------------------------------------------------------
void setup() {
	Serial.begin(115200);

	// Create the BLE Device
	BLEDevice::init("M5CORE2-TEST");

	// Create the BLE Server
	pServer = BLEDevice::createServer();
	pServer->setCallbacks(new MyServerCallbacks());

	// Create the BLE Service
	BLEService *pService = pServer->createService(SERVICE_UUID);

	// Create a BLE Characteristic
	pCharacteristic = pService->createCharacteristic(
		CHARACTERISTIC_UUID,
		BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE
	);

	// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
	// Create a BLE Descriptor
	pCharacteristic->addDescriptor(new BLE2902());

	// Start the service
	pService->start();

	// Start advertising
	BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
	pAdvertising->addServiceUUID(SERVICE_UUID);
	pAdvertising->setScanResponse(false);
	pAdvertising->setMinPreferred(0x0);	// set value to 0x00 to not advertise this parameter
	BLEDevice::startAdvertising();
	Serial.println("Waiting a client connection to notify...");
}

// ---------------------------------------------------------------
void loop() {
	// notify changed value
	if (deviceConnected) {
		pCharacteristic->setValue((uint8_t *)&value, 4);
		pCharacteristic->notify();
		value += 200;
	Serial.print("value = ");
	Serial.println(value);
		delay(2000);
	}
	// disconnecting
	if (!deviceConnected && oldDeviceConnected) {
		delay(500);									 // give the bluetooth stack the chance to get things ready
		pServer->startAdvertising();	// restart advertising
		Serial.println("start advertising");
		oldDeviceConnected = deviceConnected;
	}
	// connecting
	if (deviceConnected && !oldDeviceConnected) {
		// do stuff here on connecting
		oldDeviceConnected = deviceConnected;
	}
}
// ---------------------------------------------------------------
```

## Arduino IDE
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/0b5e32b3-bbac-4acb-c674-99de02d3b7e5.png)

## Android の nRF Connect で接続
![nrf_connect_sep22.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/72af5591-2123-a354-1b9e-3f8c0b2a2bec.png)

## Android の LightBlue で接続
![LightBlue_sep22.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/c6a4663b-ceec-171f-2344-ff518889f9d8.png)

>READ AGAIN をクリックすると値が読めます。