diff --git a/libraries/BLE/examples/BLE_Broadcast/BLE_Broadcast.ino b/libraries/BLE/examples/BLE_Broadcast/BLE_Broadcast.ino new file mode 100644 index 00000000000..923105d2bb8 --- /dev/null +++ b/libraries/BLE/examples/BLE_Broadcast/BLE_Broadcast.ino @@ -0,0 +1,46 @@ +#include +#include +#include + +// Function Declaration +void setup_ble(void); + +// Create Data +struct IMUData{ + float accelX; + float accelY; + float accelZ; + float gyroX; + float gyroY; + float gyroZ; +}; + +void setup(void) { + // Begin Serial + Serial.begin(115200); + + // M5Unified configuration + auto cfg = M5.config(); + cfg.internal_imu = true; // Enable internal IMU + M5.begin(cfg); + + // Setup BLE + setup_ble(); +} + +void loop(void) { + Serial.println("Hello World\n"); + delay(100); +} + +// Function to Setup BLE +void setup_ble(void) { + BLEDevice::init("M5Capsule"); + BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); + pAdvertising->setScanResponse(true); + pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue + pAdvertising->setMinPreferred(0x12); + BLEDevice::startAdvertising(); +} + + diff --git a/libraries/BLE/examples/BLE_Notify/BLE_Notify.ino b/libraries/BLE/examples/BLE_Notify/BLE_Notify.ino new file mode 100644 index 00000000000..27b5c3d22f6 --- /dev/null +++ b/libraries/BLE/examples/BLE_Notify/BLE_Notify.ino @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include + +#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID +#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" +#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" + +#define ble_dev "M5" + +void ble_scan(void); +void connection(void); +static void notifyCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify); + +// BLE Service +BLEUUID serviceUUID(SERVICE_UUID); +BLEUUID serviceRX(CHARACTERISTIC_UUID_RX); +BLEUUID serviceTX(CHARACTERISTIC_UUID_TX); + +// BLE Callback +int scanTime = 5; //In seconds +BLEScan *pBLEScan; +BLEAdvertisedDevice *device = NULL; +BLERemoteCharacteristic *pRemoteCharacteristic; +BLEClient *pClient; +BLERemoteService *pRemoteService; + +// BLE Logic +bool tryConnecting = false; +bool connected = false; +std::string dev_name; + +// Advertise Callback +class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { + void onResult(BLEAdvertisedDevice advertisedDevice) { + dev_name = advertisedDevice.getName().c_str(); + if ((sizeof(dev_name) > 0) && (dev_name.find(ble_dev) != std::string::npos)){ + device = new BLEAdvertisedDevice(advertisedDevice); + Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str()); + advertisedDevice.getScan()->stop(); + tryConnecting = true; + } + } +}; + +// Connect / Disconnect Callback +class MyClientCallback : public BLEClientCallbacks { + void onConnect(BLEClient *pclient) { + Serial.println("onConnect"); + } + + void onDisconnect(BLEClient *pclient) { + connected = false; + Serial.println("onDisconnect"); + } +}; + +// Function to Initialize Device +void setup(void) { + Serial.begin(115200); + BLEDevice::init("T-SIMCAM"); +} + +// Function to Loop Device +void loop(void) { + if (!connected){ + ble_scan(); + } + if (tryConnecting){ + connection(); + } + delay(100); +} + +// Function to Scan BLE device +void ble_scan(void){ + 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 + + while (1) { + Serial.println("Scanning"); + pBLEScan->start(scanTime, false); + delay(100); + pBLEScan->clearResults(); + if (device != NULL){ + break; + } + } +} + +// Function to Start BLE Connection +void connection(){ + pClient = BLEDevice::createClient(); + pClient->setClientCallbacks(new MyClientCallback()); + pClient->connect(device); + pClient->setMTU(517); + + // Obtain a reference to the service we are after in the remote BLE server. + pRemoteService = pClient->getService(serviceUUID); + if (pRemoteService == nullptr) { + Serial.print("Failed to find our service UUID: "); + Serial.println(serviceUUID.toString().c_str()); + pClient->disconnect(); + return; + } + + // Obtain a reference to the characteristic in the service of the remote BLE server. + pRemoteCharacteristic = pRemoteService->getCharacteristic(serviceTX); + if (pRemoteCharacteristic == nullptr) { + Serial.print("Failed to find our characteristic UUID: "); + Serial.println(serviceTX.toString().c_str()); + pClient->disconnect(); + return; + } + + // Start Notify + if (pRemoteCharacteristic->canNotify()) { + pRemoteCharacteristic->registerForNotify(notifyCallback); + } else { + Serial.println("Cannot Notify"); + } + + connected = true; + tryConnecting = false; +} + +// Notify Callback +static void notifyCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify) { + Serial.write(pData, length); + Serial.println(); +} diff --git a/libraries/BLE/examples/BLE_Publish/BLE_Publish.ino b/libraries/BLE/examples/BLE_Publish/BLE_Publish.ino new file mode 100644 index 00000000000..e3315f25676 --- /dev/null +++ b/libraries/BLE/examples/BLE_Publish/BLE_Publish.ino @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include +#include "esp_bt.h" + +#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID +#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" +#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" + +BLEServer *pServer = NULL; +BLECharacteristic *pTxCharacteristic; +bool deviceConnected = false; +bool oldDeviceConnected = false; + +uint8_t uuid[6]; +char addr[18]; + +typedef struct { + float ax; + float ay; + float az; + float gx; + float gy; + float gz; +} IMUData; + +IMUData data; +uint8_t connection_count = 0; + +class MyServerCallbacks : public BLEServerCallbacks { + void onConnect(BLEServer *pServer) { + deviceConnected = true; + connection_count ++; + Serial.print("Connected : "); + Serial.println(connection_count); + if (connection_count < 2){ + Serial.println("Keep Advertising"); + BLEDevice::startAdvertising(); + } + }; + + void onDisconnect(BLEServer *pServer) { + BLEDevice::startAdvertising(); + connection_count --; + Serial.print("Disconnected : "); + Serial.println(connection_count); + if (connection_count == 0){ + Serial.println("Reset All"); + deviceConnected = false; + } + }; +}; + +class MyCallbacks : public BLECharacteristicCallbacks { + void onWrite(BLECharacteristic *pCharacteristic) { + std::string rxValue = pCharacteristic->getValue(); + + if (rxValue.length() > 0) { + Serial.println("*********"); + Serial.print("Received Value: "); + for (int i = 0; i < rxValue.length(); i++) { + Serial.print(rxValue[i]); + } + + Serial.println(); + Serial.println("*********"); + } + } +}; + +void setup(){ + M5.begin(); + M5.Imu.Init(); + Serial.begin(115200); + + esp_read_mac(uuid, ESP_MAC_BT); + snprintf(addr, sizeof(addr), "%02X:%02X:%02X:%02X:%02X:%02X", + uuid[0], + uuid[1], + uuid[2], + uuid[3], + uuid[4], + uuid[5] + ); + + // Create the BLE Device + BLEDevice::init("AIS M5StickC"); + + // 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 + pTxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY); + pTxCharacteristic->addDescriptor(new BLE2902()); + + BLECharacteristic *pRxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE); + pRxCharacteristic->setCallbacks(new MyCallbacks()); + + // 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() { + if (deviceConnected) { + char buf[96]; + M5.Imu.getAccelData(&data.ax, &data.ay, &data.az); + M5.Imu.getGyroData(&data.gx, &data.gy, &data.gz); + + snprintf(buf, sizeof(buf), + "{\"ax\":%.2f," + "\"ay\":%.2f," + "\"az\":%.2f," + "\"gx\":%.2f," + "\"gy\":%.2f," + "\"gz\":%.2f}", + data.ax, data.ay, data.az, data.gx, data.gy, data.gz + ); + + // pTxCharacteristic->setValue(&txValue, 1); + pTxCharacteristic->setValue(buf); + pTxCharacteristic->notify(); + delay(10); // bluetooth stack will go into congestion, if too many packets are sent + + Serial.println(addr); + } + + // 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) { + oldDeviceConnected = deviceConnected; + } +} diff --git a/libraries/BLE/examples/Scan/Scan.ino b/libraries/BLE/examples/Scan/Scan.ino index 6b8a1fa6f48..dd1b3845299 100644 --- a/libraries/BLE/examples/Scan/Scan.ino +++ b/libraries/BLE/examples/Scan/Scan.ino @@ -31,9 +31,9 @@ void setup() { void loop() { // put your main code here, to run repeatedly: - BLEScanResults *foundDevices = pBLEScan->start(scanTime, false); + BLEScanResults foundDevices = pBLEScan->start(scanTime, false); Serial.print("Devices found: "); - Serial.println(foundDevices->getCount()); + Serial.println(foundDevices.getCount()); Serial.println("Scan done!"); pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory delay(2000);