### setStoreLTK() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Example of how to implement the storeLTK callback. ```cpp int saveLTK(uint8_t* address, uint8_t* ltk) { EEPROM.put(100, address); // 6 bytes EEPROM.put(106, ltk); // 16 bytes return 1; } BLE.setStoreLTK(saveLTK); ``` -------------------------------- ### setGetLTK() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Example of how to implement the getLTK callback. ```cpp int retrieveLTK(uint8_t* address, uint8_t* ltk) { // Look up address in EEPROM and copy LTK if found uint8_t storedAddr[6]; EEPROM.get(100, storedAddr); if (memcmp(address, storedAddr, 6) == 0) { EEPROM.get(106, ltk); // 16-byte LTK return 1; } return 0; // Not found } BLE.setGetLTK(retrieveLTK); ``` -------------------------------- ### setDisplayCode() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Example of how to implement the displayCode callback. ```cpp void showPairingCode(uint32_t code) { Serial.print("Pairing code: "); Serial.println(code); // 6 digits, e.g., 123456 } BLE.setDisplayCode(showPairingCode); ``` -------------------------------- ### advertise() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Starts advertising the configured services and data. Must call addService() and advertising configuration methods before calling this. ```cpp BLE.setLocalName("MyPeripheral"); BLE.setAdvertisedService(service); service.addCharacteristic(characteristic); BLE.addService(service); BLE.advertise(); ``` -------------------------------- ### scan() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Starts scanning for advertising peripherals. ```cpp BLE.scan(); void loop() { BLEDevice peripheral = BLE.available(); if (peripheral) { // Process discovered device } } ``` -------------------------------- ### setStoreIRK() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Example of how to implement the storeIRK callback. ```cpp int saveIRK(uint8_t* address, uint8_t* irk) { // Store 6-byte address and 16-byte IRK in non-volatile memory EEPROM.put(0, address); EEPROM.put(6, irk); return 1; } BLE.setStoreIRK(saveIRK); ``` -------------------------------- ### setGetIRKs() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Example of how to implement the getIRKs callback. ```cpp int getStoredIRKs(uint8_t* nIRKs, uint8_t** BDAddrType, uint8_t*** BDAddrs, uint8_t*** IRKs) { // Retrieve all stored IRKs from non-volatile memory *nIRKs = 2; // 2 IRKs stored *BDAddrType = storedAddrTypes; *BDAddrs = storedAddresses; *IRKs = storedIRKs; return 1; } BLE.setGetIRKs(getStoredIRKs); ``` -------------------------------- ### advertise() Failure Handling Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of how to handle a failure when starting BLE advertising. ```cpp BLEService service("180F"); BLE.addService(service); if (!BLE.advertise()) { Serial.println("Failed to start advertising!"); // Check that services are configured before advertising } ``` -------------------------------- ### BLEDeviceEventHandler Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/types.md Example usage of BLEDeviceEventHandler. ```cpp void onDeviceConnected(BLEDevice device) { Serial.print("Connected to: "); Serial.println(device.address()); } BLE.setEventHandler(BLEConnected, onDeviceConnected); ``` -------------------------------- ### BLE Global Instance Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/types.md Example of initializing the BLE global instance. ```cpp #include void setup() { if (!BLE.begin()) { Serial.println("Failed to initialize BLE!"); } } ``` -------------------------------- ### discoverAttributes() example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bledevice.md Example of connecting to a peripheral and discovering its attributes. ```cpp if (peripheral.connect()) { if (peripheral.discoverAttributes()) { Serial.println("Services discovered"); } } ``` -------------------------------- ### scan() — Failure Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of how to handle a failure when starting a scan. ```cpp if (!BLE.scan()) { Serial.println("Failed to start scan!"); // Ensure BLE.begin() was called; ensure previous scan stopped } ``` -------------------------------- ### Usage Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bleadvertising-data.md Example demonstrating how to create and set custom advertising data. ```cpp #include void setup() { BLE.begin(); // Create custom advertising data BLEAdvertisingData advData; advData.setLocalName("MyDevice"); advData.setAdvertisedServiceUuid("180F"); // Battery Service uint8_t mfgData[] = {0x42, 0x43}; advData.setManufacturerData(0x004C, mfgData, 2); BLE.setAdvertisingData(advData); BLE.advertise(); } ``` -------------------------------- ### advertise() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Starts broadcasting the configured advertising packet. ```cpp int BLE.advertise() ``` ```cpp BLE.setLocalName("MyPeripheral"); BLE.setAdvertisedService(service); service.addCharacteristic(characteristic); BLE.addService(service); BLE.advertise(); ``` -------------------------------- ### advertisementData() example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bledevice.md Example of copying advertisement data into a buffer. ```cpp uint8_t advData[31]; int len = peripheral.advertisementData(advData, sizeof(advData)); ``` -------------------------------- ### scan() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Starts scanning for advertising peripherals. ```cpp int BLE.scan(bool withDuplicates = false) ``` ```cpp BLE.scan(); // Report each device once BLE.scan(true); // Report all advertisements (useful for RSSI monitoring) ``` -------------------------------- ### begin() Initialization Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Initializes the Bluetooth Low Energy module and prepares it for operation. ```cpp #include void setup() { Serial.begin(9600); if (!BLE.begin()) { Serial.println("Starting BLE module failed!"); while (1); } Serial.println("BLE initialized"); } ``` -------------------------------- ### Example: Creating a BLEService with UUID Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bleservice.md Example of creating a BLEService with a specified UUID. ```cpp BLEService batteryService("180F"); // Battery Service ``` -------------------------------- ### advertisedServiceUuid() example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bledevice.md Example of iterating through and printing advertised service UUIDs. ```cpp for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) { Serial.println(peripheral.advertisedServiceUuid(i)); } ``` -------------------------------- ### debug() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Enables debug output to a serial stream. ```cpp Serial.begin(115200); BLE.debug(Serial); ``` -------------------------------- ### Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blecharacteristic.md Example of creating a characteristic and accessing its UUID. ```cpp BLECharacteristic charBatteryLevel("2A19", BLERead | BLENotify, 1); Serial.println(charBatteryLevel.uuid()); // "2A19" ``` -------------------------------- ### BLE.begin() — Failure Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of how to handle a failure in BLE.begin(). ```cpp if (!BLE.begin()) { Serial.println("Failed to initialize BLE!"); // Do not proceed; halt or retry while (1); // Halt } ``` -------------------------------- ### manufacturerData() example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bledevice.md Example of copying manufacturer data into a buffer. ```cpp uint8_t mfgData[31]; int len = peripheral.manufacturerData(mfgData, sizeof(mfgData)); ``` -------------------------------- ### Initialize and Advertise BLE Service Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Initializes the BLE module, sets up a service, and starts advertising. Ensure BLE.begin() is called before other BLE functions. ```arduino BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service // ... // begin initialization if (!BLE.begin()) { Serial.println("starting Bluetooth® Low Energy module failed!"); while (1); } BLE.setAdvertisedService(ledService); // ... // start advertising BLE.advertise(); ``` -------------------------------- ### read() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blecharacteristic.md Example of reading a characteristic value from a remote device in central mode. ```cpp BLECharacteristic remoteChar = peripheral.characteristic("2A19"); if (remoteChar.canRead() && remoteChar.read()) { uint8_t value; remoteChar.readValue(value); Serial.println(value); } ``` -------------------------------- ### Return Values and Error Handling Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/README.md Example demonstrating how to check return values for common BLE operations. ```cpp if (!BLE.begin()) { // BLE init failed } if (!peripheral.connect()) { // Connection failed } int bytes = characteristic.writeValue(data, length); if (bytes != length) { // Write incomplete or failed } ``` -------------------------------- ### Initialize BLE and Scan for Peripherals Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Initializes the BLE module and starts scanning for nearby peripherals. Ensure BLE.begin() is called before any other BLE operations. ```arduino // begin initialization if (!BLE.begin()) { Serial.println("starting Bluetooth® Low Energy module failed!"); while (1); } Serial.println("BLE Central scan"); // start scanning for peripheral BLE.scan(); ``` -------------------------------- ### Example: Accessing Service UUID Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bleservice.md Example of accessing the UUID of a BLEService. ```cpp BLEService service("180F"); Serial.println(service.uuid()); // "180F" ``` -------------------------------- ### setBinaryConfirmPairing() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Example demonstrating how to register a callback function for binary pairing confirmation. The callback function `confirmPairing` is defined to return `true`, indicating confirmation. This function is then registered with `BLE.setBinaryConfirmPairing()`. ```cpp bool confirmPairing() { // Implement your confirmation logic (e.g., button check) return true; } BLE.setBinaryConfirmPairing(confirmPairing); ``` -------------------------------- ### setDisplayCode() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Example demonstrating how to register a callback function to display a pairing confirmation code to the user. The callback function `showCode` is defined to print the received 6-digit confirmation code to the serial monitor. This function is then registered with `BLE.setDisplayCode()`. ```cpp void showCode(uint32_t code) { Serial.print("Pairing code: "); Serial.println(code); } BLE.setDisplayCode(showCode); ``` -------------------------------- ### BLE.advertise() Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Starts advertising. This function makes the BLE device discoverable by other devices. ```APIDOC ## BLE.advertise() ### Description Start advertising. ### Method `BLE.advertise()` ### Parameters None ### Returns - 1 on success - 0 on failure ### Request Example ```arduino BLE.advertise(); ``` ``` -------------------------------- ### BLEUnsignedCharCharacteristic Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristics.md Example of creating a BLEUnsignedCharCharacteristic. ```cpp BLEUnsignedCharCharacteristic batteryLevel("2A19", BLERead | BLENotify); ``` -------------------------------- ### broadcast() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blecharacteristic.md Example of how to broadcast a characteristic value in peripheral mode. ```cpp characteristic.writeValue(newValue); characteristic.broadcast(); ``` -------------------------------- ### Verify Preconditions Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of checking the state of a peripheral and characteristic before attempting to read. ```cpp // Check state before operations if (peripheral && peripheral.connected()) { BLECharacteristic char = peripheral.characteristic("2A19"); if (char && char.canRead()) { char.read(); } } ``` -------------------------------- ### Initialize BLE and Scan for Peripherals Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Initializes the BLE module and starts scanning for nearby peripheral devices. Ensure BLE.begin() is called before any other BLE operations. ```arduino // begin initialization if (!BLE.begin()) { Serial.println("starting Bluetooth® Low Energy module failed!"); while (1); } Serial.println("BLE Central scan"); // start scanning for peripheral BLE.scan(); BLEDevice peripheral = BLE.available(); if (peripheral) { // ... Serial.println("Connecting ..."); if (peripheral.connect()) { Serial.println("Connected"); } else { Serial.println("Failed to connect!"); return; } // discover peripheral attributes Serial.println("Discovering attributes ..."); if (peripheral.discoverAttributes()) { Serial.println("Attributes discovered"); } else { Serial.println("Attribute discovery failed!"); peripheral.disconnect(); return; } // ... } ``` -------------------------------- ### Check Return Values - Initialization and Advertising Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Examples demonstrating the importance of checking return values for BLE.begin() and BLE.advertise(). ```cpp // Always check method return values if (!BLE.begin()) { // Handle initialization failure } if (!BLE.advertise()) { // Handle advertising failure } ``` -------------------------------- ### Battery Level Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristics.md Example of setting up and using a BLEUnsignedCharCharacteristic for battery level monitoring. ```cpp #include BLEService batteryService("180F"); BLEUnsignedCharCharacteristic batteryLevel("2A19", BLERead | BLENotify); void setup() { BLE.begin(); BLE.setLocalName("BatteryMonitor"); BLE.setAdvertisedService(batteryService); batteryService.addCharacteristic(batteryLevel); BLE.addService(batteryService); batteryLevel.writeValue(100); BLE.advertise(); } void loop() { int newLevel = analogRead(A0) / 10; // 0-100 if (newLevel != batteryLevel.value()) { batteryLevel.writeValue(newLevel); } } ``` -------------------------------- ### Handle Connection Events Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/README.md Example of setting up event handlers for BLE connection and disconnection. ```cpp void onConnected(BLEDevice device) { Serial.print("Connected to: "); Serial.println(device.address()); } void onDisconnected(BLEDevice device) { Serial.println("Disconnected"); } void setup() { BLE.begin(); BLE.setEventHandler(BLEConnected, onConnected); BLE.setEventHandler(BLEDisconnected, onDisconnected); } ``` -------------------------------- ### addDescriptor() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blecharacteristic.md Example of adding a descriptor to a characteristic before service registration. ```cpp BLECharacteristic characteristic("2A19", BLERead | BLENotify, 1); BLEDescriptor descriptor("2902", (uint8_t*)&cccdValue, 2); characteristic.addDescriptor(descriptor); ``` -------------------------------- ### pairable() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Checks if the device is configured to accept pairings. ```cpp virtual bool pairable() ``` -------------------------------- ### discoverService() Failure Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of handling a failed service discovery. ```cpp if (peripheral.discoverService("180F")) { BLEService service = peripheral.service("180F"); Serial.println("Service discovered"); } else { Serial.println("Service not found!"); } ``` -------------------------------- ### connect() Retry Strategy Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of implementing a retry strategy for BLE connections. ```cpp int maxRetries = 3; for (int i = 0; i < maxRetries; i++) { if (peripheral.connect()) { Serial.println("Connected!"); break; } delay(500); } ``` -------------------------------- ### setBinaryConfirmPairing() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Registers a callback to confirm or reject pairing with binary response. The callback returns true to confirm and false to reject. ```cpp bool confirmPairingRequest() { // Check a physical button or other authorization source if (buttonPressed()) { return true; // User approved } return false; // User rejected } BLE.setBinaryConfirmPairing(confirmPairingRequest); ``` -------------------------------- ### discoverService() Fallback Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of manually discovering a specific service when discoverAttributes() fails. ```cpp // Instead of discoverAttributes(), discover specific service if (peripheral.discoverService("180F")) { Serial.println("Battery service found"); } ``` -------------------------------- ### setEventHandler() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Registers a callback for device-level events such as connection, disconnection, and discovery. ```cpp void onConnected(BLEDevice device) { Serial.println("Device connected!"); } BLE.setEventHandler(BLEConnected, onConnected); ``` -------------------------------- ### Example: Checking Service Validity Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bleservice.md Example of checking if a BLEService is valid using the operator bool(). ```cpp BLEService service = peripheral.service("180F"); if (service) { Serial.println("Service found"); } ``` -------------------------------- ### BLEFloatCharacteristic Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristics.md Example of creating and initializing a BLEFloatCharacteristic. ```cpp BLEFloatCharacteristic temperature("2A1C", BLERead | BLENotify); ``` -------------------------------- ### Start BLE Advertising Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Starts the Bluetooth® Low Energy advertising process. This function should be called after initialization and configuration of services and parameters. ```arduino // begin initialization if (!BLE.begin()) { Serial.println("starting Bluetooth® Low Energy module failed!"); while (1); } // ... BLE.advertise(); // ... ``` -------------------------------- ### addService() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Registers a service and its characteristics for local access. Must be called before advertise(). ```cpp BLEService myService("180F"); BLE.addService(myService); ``` -------------------------------- ### Constructor Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristic.md Examples of creating BLETypedCharacteristic instances for built-in types and a custom struct. ```cpp // Built-in types BLETypedCharacteristic sensorReading("1234", BLERead | BLENotify); BLETypedCharacteristic temperature("5678", BLERead | BLENotify); // Custom struct struct SensorData { uint16_t temperature; uint16_t humidity; }; BLETypedCharacteristic sensor("ABCD", BLERead); ``` -------------------------------- ### debug() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Enables debug output to a specified serial stream. ```cpp virtual void debug(Stream& stream) ``` ```cpp BLE.debug(Serial); ``` -------------------------------- ### Enable Security (Pairing) Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/README.md Example of enabling security features and setting up pairing callbacks. ```cpp void setup() { BLE.begin(); BLE.setPairable(YES); BLE.setStoreIRK(saveIRK); BLE.setGetIRKs(getIRKs); BLE.setStoreLTK(saveLTK); BLE.setGetLTK(getLTK); BLE.setDisplayCode(showPairingCode); BLE.setBinaryConfirmPairing(userConfirmsPairing); } ``` -------------------------------- ### Usage Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blestring-characteristic.md Example demonstrating how to create and use a BLEStringCharacteristic within a BLE service. ```cpp #include void setup() { Serial.begin(9600); while (!Serial); BLE.begin(); // Create a service with string characteristic BLEService deviceInfoService("180A"); BLEStringCharacteristic manufacturer("2A29", BLERead, 32); manufacturer.writeValue(String("Arduino")); deviceInfoService.addCharacteristic(manufacturer); BLE.addService(deviceInfoService); BLE.setLocalName("MyDevice"); BLE.advertise(); } void loop() { BLEDevice central = BLE.central(); if (central) { while (central.connected()) { if (manufacturer.written()) { Serial.println("Manufacturer string:"); Serial.println(manufacturer.value()); } } } } ``` -------------------------------- ### Temperature Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristics.md Example of using a BLEFloatCharacteristic to read and write temperature values. ```cpp BLEService environmentService("181A"); BLEFloatCharacteristic temperature("2A1F", BLERead | BLENotify); void setup() { BLE.begin(); environmentService.addCharacteristic(temperature); BLE.addService(environmentService); temperature.writeValue(22.5); } void loop() { float currentTemp = readTemperature(); temperature.writeValue(currentTemp); } ``` -------------------------------- ### setPairable() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Configures the pairing mode of the device. ```cpp virtual void setPairable(uint8_t pairable) ``` ```cpp BLE.setPairable(YES); // Enable pairing ``` -------------------------------- ### Example: Retrieving a Characteristic by UUID Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bleservice.md Example of retrieving a BLECharacteristic from a BLEService using its UUID. ```cpp BLECharacteristic batteryLevel = service.characteristic("2A19"); if (batteryLevel) { Serial.println(batteryLevel.value()); } ``` -------------------------------- ### connect() Failure Handling Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of handling a connection failure when trying to connect to a BLE peripheral. ```cpp BLEDevice peripheral = BLE.available(); if (peripheral) { if (!peripheral.connect()) { Serial.println("Connection failed!"); // Retry or continue scanning } } ``` -------------------------------- ### setSupervisionTimeout() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Sets the connection supervision timeout. ```cpp virtual void setSupervisionTimeout(uint16_t supervisionTimeout) ``` -------------------------------- ### BLECharacteristicEventHandler Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/types.md Example usage of BLECharacteristicEventHandler. ```cpp void onCharacteristicWritten(BLEDevice device, BLECharacteristic characteristic) { uint8_t value; characteristic.readValue(value); Serial.println(value); } characteristic.setEventHandler(BLEWritten, onCharacteristicWritten); ``` -------------------------------- ### Create a Custom Characteristic Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/README.md Example of creating a custom BLE service and characteristic. ```cpp // 16-bit temperature in 0.01°C units BLETypedCharacteristic temperature("2A1F", BLERead | BLENotify); BLEService envService("181A"); envService.addCharacteristic(temperature); BLE.addService(envService); // Update periodically int16_t tempValue = (int16_t)(22.5 * 100); // 22.50°C temperature.writeValue(tempValue); ``` -------------------------------- ### address() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Returns the Bluetooth MAC address of the local device. ```cpp String addr = BLE.address(); Serial.println(addr); // "AA:BB:CC:DD:EE:FF" ``` -------------------------------- ### setStoreIRK() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Example demonstrating how to register a callback function to persist Identity Resolving Keys (IRKs) during pairing. The callback function `saveIRK` is defined to store the provided address and IRK, returning 1 on success. This function is then registered with `BLE.setStoreIRK()`. ```cpp int saveIRK(uint8_t* address, uint8_t* irk) { // Store 6-byte address and 16-byte IRK in persistent memory return 1; } BLE.setStoreIRK(saveIRK); ``` -------------------------------- ### Log Detailed Information on Failure Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of logging detailed information when an operation like discoverAttributes() fails. ```cpp if (!peripheral.discoverAttributes()) { Serial.print("Discovery failed for: "); Serial.println(peripheral.address()); Serial.print("RSSI: "); Serial.println(peripheral.rssi()); } ``` -------------------------------- ### BLELocalDevice::begin() Initialization Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Initializes the Bluetooth Low Energy hardware and establishes connection to the BLE radio module. ```cpp int BLE.begin() ``` ```cpp void setup() { if (!BLE.begin()) { Serial.println("Starting BLE module failed!"); while (1); // Halt } Serial.println("BLE initialized successfully"); } ``` -------------------------------- ### Initialize Battery Level Characteristic Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Example of creating an unsigned char characteristic for battery level with read and notify properties. ```arduino // Bluetooth® Low Energy Battery Level Characteristic BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes ``` -------------------------------- ### discoverAttributes() Failure Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of handling a failed attribute discovery and subsequent disconnect. ```cpp if (peripheral.connect()) { if (!peripheral.discoverAttributes()) { Serial.println("Discovery failed!"); peripheral.disconnect(); // Try again or use manual discovery } } ``` -------------------------------- ### setConnectionInterval() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Sets the preferred connection interval range. ```cpp virtual void setConnectionInterval(uint16_t minimumConnectionInterval, uint16_t maximumConnectionInterval) ``` -------------------------------- ### setFlags() — Flags Already Set Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of how to handle the case where flags are already set. ```cpp // Flags are often set implicitly; check return before calling if (!BLE.setFlags(BLEFlagsGeneralDiscoverable)) { Serial.println("Could not set flags (already set or no space)"); } ``` -------------------------------- ### available() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md In central mode (scanning), returns the next discovered peripheral device. Returns an empty BLEDevice if no new device is available. ```cpp void loop() { BLEDevice peripheral = BLE.available(); if (peripheral) { Serial.println(peripheral.localName()); } } ``` -------------------------------- ### Implement Retry Logic Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of implementing retry logic with exponential backoff for connection attempts. ```cpp // Retry with exponential backoff int retries = 0; while (!peripheral.connect() && retries < 5) { retries++; delay(100 * retries); // 100ms, 200ms, 300ms... } ``` -------------------------------- ### paired() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Checks if the device has successfully paired with a remote device. ```cpp virtual bool paired() ``` -------------------------------- ### setAdvertisingInterval() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Sets the advertising broadcast interval in milliseconds. ```cpp virtual void setAdvertisingInterval(uint16_t advertisingInterval) ``` -------------------------------- ### Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blecharacteristic.md Example of accessing individual bytes of the characteristic value. ```cpp BLECharacteristic charValue("1234", BLERead, 4); uint8_t firstByte = charValue[0]; ``` -------------------------------- ### Start BLE Scan Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Initiates a scan for advertising Bluetooth Low Energy devices. Optionally, set 'withDuplicates' to true to include duplicate advertisements. ```arduino BLE.scan(); ``` -------------------------------- ### BLE.scanForName Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Starts scanning for Bluetooth Low Energy devices advertising with a specific name. ```APIDOC ## BLE.scanForName(name) ## BLE.scanForName(name, withDuplicates) ### Description Starts scanning for Bluetooth® Low Energy devices that are advertising with a particular (local) name. ### Method `BLE.scanForName` ### Parameters #### Path Parameters - **name** (String) - Required - The local name of the device to filter for. - **withDuplicates** (boolean) - Optional - If `true`, advertisements received more than once will not be filtered. Defaults to `false`. ### Request Example ```arduino BLE.scanForName("LED"); ``` ### Response #### Success Response (200) - **1** on success - **0** on failure #### Response Example (No response body) ``` -------------------------------- ### BLEUnsignedIntCharacteristic Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristics.md Example of creating a BLEUnsignedIntCharacteristic. ```cpp BLEUnsignedIntCharacteristic timestamp("2A08", BLERead | BLENotify); ``` -------------------------------- ### setAppearance() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Sets the device appearance code (used by centrals to select icons/categories). ```cpp BLE.setAppearance(0x03C1); // Generic Heart Rate Sensor ``` -------------------------------- ### Subscribe to Notifications Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/README.md Example of subscribing to characteristic notifications and handling updates. ```cpp BLECharacteristic temp = peripheral.characteristic("2A1F"); if (temp && temp.canSubscribe()) { temp.subscribe(); while (peripheral.connected()) { if (temp.valueUpdated()) { float value; // Read float value Serial.println(value); } } temp.unsubscribe(); } ``` -------------------------------- ### scanForName() Failure Handling Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of how to handle a failure when scanning for a BLE device by name. ```cpp #if !BLE.scanForName("MyDevice") Serial.println("Scan for name failed!"); #endif ``` -------------------------------- ### Initialize BLE and Scan for Peripherals Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Initializes the BLE module and begins scanning for available peripheral devices. ```arduino // begin initialization if (!BLE.begin()) { Serial.println("starting Bluetooth® Low Energy module failed!"); while (1); } Serial.println("BLE Central scan"); // start scanning for peripheral BLE.scan(); BLEDevice peripheral = BLE.available(); if (peripheral) { // ... } ``` -------------------------------- ### BLEUnsignedShortCharacteristic Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristics.md Example of creating a BLEUnsignedShortCharacteristic. ```cpp BLEUnsignedShortCharacteristic appearance("2A01", BLERead); ``` -------------------------------- ### setTimeout() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Sets the default timeout for polling and BLE operations. ```cpp virtual void setTimeout(unsigned long timeout) ``` -------------------------------- ### BLEBoolCharacteristic Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristics.md Example of creating a BLEBoolCharacteristic. ```cpp BLEBoolCharacteristic powerOn("2A9D", BLERead | BLEWrite); ``` -------------------------------- ### scanForAddress() Failure Handling Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of how to handle a failure when scanning for a BLE device by MAC address. ```cpp #if !BLE.scanForAddress("AA:BB:CC:DD:EE:FF") Serial.println("Scan for address failed!"); #endif ``` -------------------------------- ### scanForUuid() Failure Handling Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of how to handle a failure when scanning for a BLE device by UUID. ```cpp #if !BLE.scanForUuid("180F") Serial.println("Scan for UUID failed!"); #endif ``` -------------------------------- ### subscribe() Failure Handling Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of handling a subscription failure for a remote characteristic. ```cpp BLECharacteristic remoteChar = peripheral.characteristic("2A19"); if (remoteChar.canSubscribe()) { if (!remoteChar.subscribe()) { Serial.println("Subscription failed!"); // May already be subscribed; check state } } ``` -------------------------------- ### writeValueBE() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristic.md Example of writing a value in big-endian byte order. ```cpp uint32_t ipAddress = 0xC0A80001; // 192.168.0.1 characteristic.writeValueBE(ipAddress); // Stores as C0, A8, 00, 01 ``` -------------------------------- ### BLEService() Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Create a new Bluetooth Low Energy service. ```APIDOC ## BLEService() ### Description Create a new Bluetooth Low Energy service. ### Syntax BLEService(uuid) ### Parameters #### Request Body - **uuid** (String) - Required - 16-bit or 128-bit UUID in String format. ### Returns - **BLEService** - A new BLEService instance. ``` -------------------------------- ### writeValueLE() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristic.md Example of writing a value in little-endian byte order. ```cpp uint16_t data = 0x1234; characteristic.writeValueLE(data); // Stores as 0x34, 0x12 ``` -------------------------------- ### Example: Adding a Characteristic to a Service Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bleservice.md Example of adding a BLECharacteristic to a BLEService. ```cpp BLEService batteryService("180F"); BLEUnsignedCharCharacteristic batteryLevel("2A19", BLERead | BLENotify); batteryService.addCharacteristic(batteryLevel); ``` -------------------------------- ### Timestamp Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristics.md Example of using a BLEUnsignedIntCharacteristic to write a timestamp. ```cpp BLEUnsignedIntCharacteristic currentTime("2A0F", BLERead | BLENotify); void loop() { unsigned long now = millis(); currentTime.writeValue((uint32_t)(now / 1000)); } ``` -------------------------------- ### BLE Global Instance Usage Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/README.md Demonstrates the usage of the global BLE instance for common operations like initialization, configuration, and starting BLE modes. ```cpp extern BLELocalDevice& BLE; BLE.begin(); // Initialize BLE.setLocalName("..."); // Configure BLE.advertise(); // Start peripheral mode BLE.scan(); // Start central mode BLE.available(); // Get discovered devices BLE.central(); // Get connected device ``` -------------------------------- ### valueBE() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristic.md Example of reading the characteristic value interpreting bytes as big-endian. ```cpp uint16_t netValue = characteristic.valueBE(); ``` -------------------------------- ### scanForName() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Scans for peripherals with a specific advertised name. ```cpp int BLE.scanForName(String name, bool withDuplicates = false) ``` ```cpp BLE.scanForName("MyPeripheral"); ``` -------------------------------- ### valueLE() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristic.md Example of reading the characteristic value interpreting bytes as little-endian. ```cpp uint16_t data = characteristic.valueLE(); ``` -------------------------------- ### Initialize BLE Device Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Initializes the BLE module. Returns 1 on success and 0 on failure. ```arduino // begin initialization if (!BLE.begin()) { Serial.println("starting Bluetooth® Low Energy module failed!"); while (1); } ``` -------------------------------- ### writeValue() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristic.md Example of writing a typed value to a characteristic using writeValue(). ```cpp BLETypedCharacteristic timestamp("0000180A-0000-1000-8000-00805f9b34fb", BLERead | BLENotify); uint32_t now = millis() / 1000; timestamp.writeValue(now); ``` -------------------------------- ### value() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bletyped-characteristic.md Example of reading the current characteristic value as type T using value(). ```cpp BLETypedCharacteristic counter("1234", BLERead); uint16_t current = counter.value(); Serial.println(current); ``` -------------------------------- ### BLEService() Constructor Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bleservice.md Default constructor creates an empty service. ```cpp BLEService() ``` -------------------------------- ### Initialize BLEDescriptor Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Create a new BLEDescriptor instance with a UUID and initial value. ```arduino BLEDescriptor millisLabelDescriptor("2901", "millis"); ``` -------------------------------- ### BLE Class Initialization and Control Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Methods for initializing, stopping, and polling the Bluetooth Low Energy module. ```APIDOC ## BLE.begin() ### Description Initializes the Bluetooth® Low Energy device. ### Method `BLE.begin()` ### Endpoint N/A (Library function) ### Parameters None ### Returns - 1 on success - 0 on failure ### Request Example ```arduino if (!BLE.begin()) { Serial.println("starting Bluetooth® Low Energy module failed!"); while (1); } ``` ## BLE.end() ### Description Stops the Bluetooth® Low Energy device. ### Method `BLE.end()` ### Endpoint N/A (Library function) ### Parameters None ### Returns Nothing ### Request Example ```arduino BLE.end(); ``` ## BLE.poll() ### Description Poll for Bluetooth® Low Energy radio events and handle them. ### Method `BLE.poll()` or `BLE.poll(timeout)` ### Endpoint N/A (Library function) ### Parameters #### Query Parameters - **timeout** (int) - Optional - Timeout in ms, to wait for event. Defaults to 0 ms if not specified. ### Returns Nothing ### Request Example ```arduino BLE.poll(); ``` ``` -------------------------------- ### setStoreLTK() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Example demonstrating how to register a callback function to persist Long-Term Keys (LTKs) during pairing. The callback function `saveLTK` is defined to store the provided address and LTK, returning 1 on success. This function is then registered with `BLE.setStoreLTK()`. ```cpp int saveLTK(uint8_t* address, uint8_t* ltk) { // Store 6-byte address and 16-byte LTK in persistent memory return 1; } BLE.setStoreLTK(saveLTK); ``` -------------------------------- ### pairable() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Checks the current pairing mode. ```cpp bool BLE.pairable() ``` -------------------------------- ### noDebug() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Disables debug output. ```cpp virtual void noDebug() ``` -------------------------------- ### Pairable Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/types.md Device pairing mode configuration. ```cpp enum Pairable { NO = 0, YES = 1, ONCE = 2, }; ``` -------------------------------- ### Get BLE RSSI Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Queries the signal strength of the connected device. ```arduino if (BLE.connected()) { Serial.print("RSSI = "); Serial.println(BLE.rssi()); } ``` -------------------------------- ### Constructor Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bleadvertising-data.md Creates an empty advertising data object with maximum capacity of 31 bytes. ```cpp BLEAdvertisingData() ``` -------------------------------- ### scanForAddress() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Scans for a specific peripheral by MAC address. ```cpp int BLE.scanForAddress(String address, bool withDuplicates = false) ``` ```cpp BLE.scanForAddress("AA:BB:CC:DD:EE:FF"); ``` -------------------------------- ### scanForUuid() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Scans for peripherals advertising a specific service UUID. ```cpp int BLE.scanForUuid(String uuid, bool withDuplicates = false) ``` ```cpp BLE.scanForUuid("180F"); // Find battery service devices ``` -------------------------------- ### Local Name Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bledevice.md Retrieves the local name advertised by the device. ```cpp BLEDevice peripheral = BLE.available(); if (peripheral.hasLocalName()) { Serial.println(peripheral.localName()); } ``` -------------------------------- ### Get BLE Address Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Retrieves the local BLE device address as a String. ```arduino String address = BLE.address(); Serial.print("Local address is: "); Serial.println(address); ``` -------------------------------- ### Link Catch2 Testing Framework Source: https://github.com/arduino-libraries/arduinoble/blob/master/extras/test/CMakeLists.txt Links the Catch2 testing framework with the main entry point to the specified test targets. This is required for running unit tests. ```cmake target_link_libraries( TEST_TARGET_UUID Catch2WithMain ) ``` ```cmake target_link_libraries( TEST_TARGET_DISC_DEVICE Catch2WithMain ) ``` ```cmake target_link_libraries( TEST_TARGET_ADVERTISING_DATA Catch2WithMain ) ``` ```cmake target_link_libraries( TEST_TARGET_CHARACTERISTIC_DATA Catch2WithMain ) ``` -------------------------------- ### Scan and Discover BLE Peripherals Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Initializes the BLE module and scans for available peripherals to access their advertised service UUIDs. ```arduino // begin initialization if (!BLE.begin()) { Serial.println("starting Bluetooth® Low Energy module failed!"); while (1); } Serial.println("BLE Central scan"); // start scanning for peripheral BLE.scan(); BLEDevice peripheral = BLE.available(); if (peripheral) { // ... // print the advertised service UUIDs, if present if (peripheral.hasAdvertisedServiceUuid()) { Serial.print("Service UUIDs: "); for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) { Serial.print(peripheral.advertisedServiceUuid(i)); Serial.print(" "); } Serial.println(); } // ... } ``` -------------------------------- ### setManufacturerData() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Adds manufacturer-specific data to the advertising packet. ```cpp uint8_t mfgData[] = {0x42, 0x43}; BLE.setManufacturerData(0x004C, mfgData, 2); // Apple company ID ``` -------------------------------- ### Peripheral Mode (Arduino as BLE server) Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/README.md Example code for setting up an Arduino board as a BLE peripheral (server). This code initializes BLE, sets up a service and characteristic, configures advertising, and handles incoming connections and data writes. ```cpp #include // Create a service and characteristic BLEService ledService("180A"); BLEByteCharacteristic ledPin("2A29", BLERead | BLEWrite); void setup() { Serial.begin(9600); while (!Serial); // Initialize BLE if (!BLE.begin()) { Serial.println("Starting BLE module failed!"); while (1); } // Set up the service ledService.addCharacteristic(ledPin); BLE.addService(ledService); ledPin.writeValue(0); // Configure advertising BLE.setLocalName("Arduino-LED"); BLE.setAdvertisedService(ledService); // Start advertising if (!BLE.advertise()) { Serial.println("Starting advertising failed!"); while (1); } Serial.println("BLE LED Service started"); } void loop() { // Wait for central BLEDevice central = BLE.central(); if (central) { Serial.print("Connected to: "); Serial.println(central.address()); while (central.connected()) { if (ledPin.written()) { uint8_t value; ledPin.readValue(value); digitalWrite(LED_BUILTIN, value ? HIGH : LOW); } } Serial.println("Disconnected"); } } ``` -------------------------------- ### MAC Address Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bledevice.md Returns the Bluetooth MAC address of the remote device. ```cpp BLEDevice device = BLE.central(); Serial.println(device.address()); // "AA:BB:CC:DD:EE:FF" ``` -------------------------------- ### disconnect() Failure Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/errors.md Example of handling a failed disconnect operation. ```cpp if (!device.disconnect()) { Serial.println("Disconnect failed!"); // Already disconnected or error; continue } ``` -------------------------------- ### setGetLTK() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Registers a callback to retrieve a stored LTK for re-encryption. ```cpp void BLE.setGetLTK(int (*getLTK)(uint8_t* address, uint8_t* LTK)) ``` -------------------------------- ### setStoreLTK() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Registers a callback to persist Long-Term Keys for link encryption recovery after reconnection. ```cpp void BLE.setStoreLTK(int (*storeLTK)(uint8_t* address, uint8_t* LTK)) ``` -------------------------------- ### BLEService.characteristic() Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Get a BLECharacteristic representing a Bluetooth® Low Energy characteristic the service provides. ```APIDOC ## bleService.characteristic(uuid, [index]) or bleService.characteristic(index) ### Description Get a BLECharacteristic representing a Bluetooth® Low Energy characteristic the service provides. ### Method None (This is a method of the `BLEService` object) ### Endpoint N/A (This is a library function, not a network endpoint) ### Parameters #### Path Parameters None #### Query Parameters - **uuid** (String) - Required (if used with index) - The UUID of the characteristic. - **index** (int) - Required (if used with uuid) or Optional (if used alone) - The index of the characteristic. ### Request Example ```arduino BLECharacteristic batteryLevelCharacteristic = peripheral.characteristic("2a19"); ``` ### Response #### Success Response (200) - **BLECharacteristic** - The BLECharacteristic object for the provided parameters. #### Response Example ```json { "example": "" } ``` ``` -------------------------------- ### setPairable() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Configures whether the device accepts pairing requests. ```cpp void BLE.setPairable(uint8_t pairable) ``` ```cpp BLE.setPairable(YES); // Accept pairings BLE.setPairable(ONCE); // Accept one pairing, then reject BLE.setPairable(NO); // Reject all pairings ``` -------------------------------- ### setConnectable() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Enables or disables the connectable flag in advertising packets. ```cpp virtual void setConnectable(bool connectable) ``` -------------------------------- ### Create BLE Service Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Instantiates a new BLE service using a specific UUID. ```arduino BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service ``` -------------------------------- ### stopAdvertise() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Stops advertising. Does not affect connected centrals. ```cpp void BLE.stopAdvertise() ``` ```cpp BLE.stopAdvertise(); ``` -------------------------------- ### setAdvertisedService() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Advertises a previously created BLEService by extracting its UUID. ```cpp BLEService batteryService("180F"); BLE.setAdvertisedService(batteryService); ``` -------------------------------- ### bleDevice.discoverAttributes() Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Discovers all attributes of the BLE device. ```APIDOC ## bleDevice.discoverAttributes() ### Description Discover all of the attributes of Bluetooth Low Energy device. ### Returns - **boolean** - true if successful, false on failure. ``` -------------------------------- ### setDisplayCode() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/configuration.md Registers a callback to display a pairing confirmation code (passkey) to the user. ```cpp void BLE.setDisplayCode(void (*displayCode)(uint32_t confirmationCode)) ``` -------------------------------- ### Write to a Remote Characteristic Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/README.md Example of how to write a value to a characteristic on a peripheral. ```cpp BLECharacteristic ledControl = peripheral.characteristic("ABCD"); if (ledControl && ledControl.canWrite()) { ledControl.writeValue((uint8_t)1); // Turn on } ``` -------------------------------- ### Add Custom Post-Build Command for Unit Tests Source: https://github.com/arduino-libraries/arduinoble/blob/master/extras/test/CMakeLists.txt Configures a custom command to execute unit tests as a post-build step for various test targets. Ensure the test executables are located in the CMAKE_RUNTIME_OUTPUT_DIRECTORY. ```cmake add_custom_command(TARGET TEST_TARGET_UUID POST_BUILD COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_UUID ) ``` ```cmake add_custom_command(TARGET TEST_TARGET_DISC_DEVICE POST_BUILD COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_DISC_DEVICE ) ``` ```cmake add_custom_command(TARGET TEST_TARGET_ADVERTISING_DATA POST_BUILD COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_ADVERTISING_DATA ) ``` ```cmake add_custom_command(TARGET TEST_TARGET_CHARACTERISTIC_DATA POST_BUILD COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_CHARACTERISTIC_DATA ) ``` -------------------------------- ### BLE.scanForUuid Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Starts scanning for Bluetooth Low Energy devices advertising with a specific service UUID. ```APIDOC ## BLE.scanForUuid(uuid) ## BLE.scanForUuid(uuid, withDuplicates) ### Description Starts scanning for Bluetooth® Low Energy devices that are advertising with a particular (service) UUID. ### Method `BLE.scanForUuid` ### Parameters #### Path Parameters - **uuid** (String) - Required - The service UUID to filter for. - **withDuplicates** (boolean) - Optional - If `true`, advertisements received more than once will not be filtered. Defaults to `false`. ### Request Example ```arduino BLE.scanForUuid("aa10"); ``` ### Response #### Success Response (200) - **1** on success - **0** on failure #### Response Example (No response body) ``` -------------------------------- ### BLEService(const char* uuid) Constructor Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bleservice.md Creates a service with the specified UUID. ```cpp BLEService(const char* uuid) ``` -------------------------------- ### BLE.scanForAddress Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Starts scanning for Bluetooth Low Energy devices advertising with a specific Bluetooth address. ```APIDOC ## BLE.scanForAddress(address) ## BLE.scanForAddress(address, withDuplicates) ### Description Starts scanning for Bluetooth® Low Energy devices that are advertising with a particular (Bluetooth®) address. ### Method `BLE.scanForAddress` ### Parameters #### Path Parameters - **address** (String) - Required - The Bluetooth® address to filter for. - **withDuplicates** (boolean) - Optional - If `true`, advertisements received more than once will not be filtered. Defaults to `false`. ### Request Example ```arduino BLE.scanForAddress("aa:bb:cc:ee:dd:ff"); ``` ### Response #### Success Response (200) - **1** on success - **0** on failure #### Response Example (No response body) ``` -------------------------------- ### connect() Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/bledevice.md Initiates a connection to a discovered peripheral device (central mode). **Returns**: `bool` — true on successful connection initiation; false on failure. ```cpp bool connect() ``` -------------------------------- ### Retrieve BLE Device Address Source: https://github.com/arduino-libraries/arduinoble/blob/master/docs/api.md Gets the MAC address of the connected BLE device as a String. ```arduino // listen for Bluetooth® Low Energy peripherals to connect: BLEDevice central = BLE.central(); // if a central is connected to peripheral: if (central) { Serial.print("Connected to central: "); // print the central's MAC address: Serial.println(central.address()); } ``` -------------------------------- ### setEventHandler() Example Source: https://github.com/arduino-libraries/arduinoble/blob/master/_autodocs/api-reference/blelocal-device.md Registers a callback for device-level events (connected, disconnected, discovered). ```cpp virtual void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler) ``` ```cpp void deviceConnected(BLEDevice device) { Serial.println("Device connected"); } BLE.setEventHandler(BLEConnected, deviceConnected); ```