This repository has been archived by the owner on Mar 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble.ino
50 lines (39 loc) · 1.67 KB
/
ble.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
static void notifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) {
// No idea what this does
Serial.print("Notify callback for characteristic ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
/**
Called for each advertising BLE server.
*/
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
// Check if the BLE device is advertising its UUID
if(advertisedDevice.haveServiceUUID()) {
// If so, get the UUID
pServerUUID = new BLEUUID(advertisedDevice.getServiceUUID());
Serial.println( "Device has a UUID");
// Compare it with the UUID we're looking for
for (int i = 0; i < (sizeof(known_UUIDs) / sizeof(known_UUIDs[0])); i++) {
if (strcmp(pServerUUID->toString().c_str(), known_UUIDs[i].c_str()) == 0) {
Serial.println("Device with a matching UUID found");
device_found = true;
// No need to continue scanning
Serial.println("Stopping BLE scan");
advertisedDevice.getScan()->stop();
}
}
}
}
}; // MyAdvertisedDeviceCallbacks
void BLE_setup() {
BLEDevice::init("");
pClient = BLEDevice::createClient();
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
}