Used to enable the Bluetooth® Low Energy module.
Initializes the Bluetooth® Low Energy device.
BLE.begin()
None
- 1 on success
- 0 on failure
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
Stops the Bluetooth® Low Energy device.
BLE.end()
None
Nothing
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// ....
BLE.end();
Poll for Bluetooth® Low Energy radio events and handle them.
BLE.poll()
BLE.poll(timeout)
timeout: optional timeout in ms, to wait for event. If not specified defaults to 0 ms.
Nothing
// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
BLE.poll();
Set the event handler (callback) function that will be called when the specified event occurs.
BLE.setEventHandler(eventType, callback)
- eventType: event type (BLEConnected, BLEDisconnected, BLEDiscovered, BLEAdvertised)
- callback: function to call when event occurs
Nothing.
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// ...
// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
}
Query if another Bluetooth® Low Energy device is connected
BLE.connected()
None
- true if another Bluetooth® Low Energy device is connected,
- otherwise false.
// while the central is still connected to peripheral:
while (BLE.connected()) {
// ...
}
Disconnect any Bluetooth® Low Energy devices that are connected
BLE.disconnect()
None
- true if any Bluetooth® Low Energy device that was previously connected was disconnected,
- otherwise false.
if (BLE.connected()) {
BLE.disconnect();
}
Query the Bluetooth® address of the Bluetooth® Low Energy device.
BLE.address()
None
- The Bluetooth® address of the Bluetooth® Low Energy device (as a String).
**String** address = BLE.address();
Serial.print(“Local address is: “);
Serial.println(address);
Query the RSSI (Received signal strength indication) of the connected Bluetooth® Low Energy device.
BLE.rssi()
None
- The RSSI of the connected Bluetooth® Low Energy device, 127 if no Bluetooth® Low Energy device is connected.
if (BLE.connected()) {
Serial.print(“RSSI = “);
Serial.println(BLE.rssi());
}
Set the advertised service UUID used when advertising.
BLE.setAdvertisedServiceUuid(uuid)
- uuid: 16-bit or 128-bit Bluetooth® Low Energy UUID in String format
Nothing
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setAdvertisedServiceUuid(“19B10000-E8F2-537E-4F6C-D104768A1214");
// ...
// start advertising
BLE.advertise();
Set the advertised service UUID used when advertising to the value of the BLEService provided.
BLE.setAdvertisedService(bleService)
- bleService: BLEService to use UUID from
Nothing
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();
Set the manufacturer data value used when advertising.
BLE.setManufacturerData(data, length)
- data: byte array containing manufacturer data
- length: length of manufacturer data array
Nothing
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
byte data[5] = { 0x01, 0x02, 0x03, 0x04, 0x05};
BLE.setManufacturerData(data, 5);
// ...
// start advertising
BLE.advertise();
Set the local value used when advertising.
BLE.setLocalName(name)
- name: local name value to use when advertising
Nothing
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setLocalName("LED");
// ...
// start advertising
BLE.advertise();
Set the device name in the built in device name characteristic. If not set, the value defaults “Arduino”.
BLE.setDeviceName(name)
- name: device name value
Nothing
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setDeviceName("LED");
// ...
// start advertising
BLE.advertise();
Set the appearance in the built in appearance characteristic. If not set, the value defaults 0x0000.
BLE.setAppearance(appearance)
- appearance: appearance value
Nothing
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setAppearance(0x8000);
// ...
// start advertising
BLE.advertise();
Add a BLEService to the set of services the Bluetooth® Low Energy device provides
BLE.addService(service)
- service: BLEService to add
Nothing
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.addService(ledService);
// ...
Start advertising.
BLE.advertise()
None
- 1 on success,
- 0 on failure.
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// ...
BLE.advertise();
// ...
Stop advertising.
BLE.stopAdvertise()
None
Nothing
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// ...
BLE.advertise();
// ...
BLE.stopAdvertise();
Query the central Bluetooth® Low Energy device connected.
BLE.central()
None
- BLEDevice representing the central.
// 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());
}
Set the advertising interval in units of 0.625 ms. Defaults to 100ms (160 * 0.625 ms) if not provided.
BLE.setAdvertisingInterval(advertisingInterval)
- advertisingInterval: advertising interval in units of 0.625 ms
Nothing.
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// ...
BLE.setAdvertisingInterval(320); // 200 * 0.625 ms
BLE.advertise();
Set the minimum and maximum desired connection intervals in units of 1.25 ms.
BLE.setConnectionInterval(minimum, maximum)
- minimum: minimum desired connection interval in units of 1.25 ms
- maximum: maximum desired connection interval in units of 1.25 ms
Nothing.
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// ...
BLE.setConnectionInterval(0x0006, 0x0c80); // 7.5 ms minimum, 4 s maximum
Set if the device is connectable after advertising, defaults to true.
BLE.setConnectable(connectable)
- true: the device will be connectable when advertising
- false: the device will NOT be connectable when advertising
Nothing.
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// ...
BLE.setConnectable(false); // make the device unconnectable when advertising
Start scanning for Bluetooth® Low Energy devices that are advertising.
BLE.scan()
BLE.scan(withDuplicates)
BLE.scan(withDuplicates, activeScan)
- withDuplicates: optional, defaults to false. If true, advertisements received more than once will not be filtered
- activeScan: optional, defaults to true. If true, an active scan is performed. Otherwise a passive scan is performed.
- 1 on success,
- 0 on failure.
// 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) {
// ...
}
Start scanning for Bluetooth® Low Energy devices that are advertising with a particular (local) name.
BLE.scanForName(name)
BLE.scanForName(name, withDuplicates)
BLE.scanForName(name, withDuplicates, activeScan)
- name: (local) name of device (as a String) to filter for
- withDuplicates: optional, defaults to false. If true, advertisements received more than once will not be filtered.
- activeScan: optional, defaults to true. If true, an active scan is performed. Otherwise a passive scan is performed. Note that it is not common to include the name is a passive advertisement. For most device an active scan should be used when scanning for name.
- 1 on success,
- 0 on failure.
// 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.scanForName("LED");
BLEDevice peripheral = BLE.available();
if (peripheral) {
// ...
}
Start scanning for Bluetooth® Low Energy devices that are advertising with a particular (Bluetooth®) address.
BLE.scanForAddress(address)
BLE.scanForAddress(address, withDuplicates)
BLE.scanForAddress(address, withDuplicates, activeScan)
- address: (Bluetooth®) address (as a String) to filter for
- withDuplicates: optional, defaults to false. If true, advertisements received more than once will not be filtered
- activeScan: optional, defaults to true. If true, an active scan is performed. Otherwise a passive scan is performed.
- 1 on success,
- 0 on failure.
// 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.scanForAddress("aa:bb:cc:ee:dd:ff");
BLEDevice peripheral = BLE.available();
if (peripheral) {
// ...
}
Start scanning for Bluetooth® Low Energy devices that are advertising with a particular (service) UUID.
BLE.scanForUuid(uuid)
BLE.scanForUuid(uuid, withDuplicates)
BLE.scanForUuid(uuid, withDuplicates, activeScan)
- uuid: (service) UUID (as a String) to filter for
- withDuplicates: optional, defaults to false. If true, advertisements received more than once will not be filtered.
- activeScan: optional, defaults to true. If true, an active scan is performed. Otherwise a passive scan is performed.
- 1 on success,
- 0 on failure.
// 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.scanForUuid("aa10");
BLEDevice peripheral = BLE.available();
if (peripheral) {
// ...
}
Stop scanning for Bluetooth® Low Energy devices that are advertising.
BLE.stopScan()
None
Nothing
// 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();
BLE.stopScan();
Query for a discovered Bluetooth® Low Energy device that was found during scanning.
BLE.available()
BLE.available(includeAdvertised)
- includeAdvertised: optional, defaults to false. If true, also devices for which only the passive advertise data is known are returned.
- BLEDevice representing the discovered device.
// 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) {
// ...
}
Used to get information about the devices connected or discovered while scanning
Poll for Bluetooth® Low Energy radio events for the specified Bluetooth® Low Energy device and handle them.
bleDevice.poll()
bleDevice.poll(timeout)
- timeout: optional timeout in ms, to wait for event. If not specified defaults to 0 ms.
Nothing
// listen for Bluetooth® Low Energy centrals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
central.poll();
// ...
}
Query if a Bluetooth® Low Energy device is connected
bleDevice.connected()
None
- true if the Bluetooth® Low Energy device is connected,
- otherwise false.
// listen for Bluetooth® Low Energy centrals to connect:
BLEDevice central = BLE.central();
// while the central is still connected
while (central.connected()) {
// ...
}
Disconnect the Bluetooth® Low Energy device, if connected
bleDevice.disconnect()
None
- true if the Bluetooth® Low Energy device was disconnected,
- otherwise false.
// listen for Bluetooth® Low Energy centrals to connect:
BLEDevice central = BLE.central();
central.disconnect();
Query the Bluetooth® address of the Bluetooth® Low Energy device.
bleDevice.address()
None
- Bluetooth® address of the Bluetooth® Low Energy device (as a String).
// 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());
.
}
Query the RSSI (Received signal strength indication) of the Bluetooth® Low Energy device.
bleDevice.rssi()
None
- RSSI of the connected Bluetooth® Low Energy device, 127 if the Bluetooth® Low Energy device is not connected.
if (bleDevice.connected()) {
Serial.print(“RSSI = “);
Serial.println(bleDevice.rssi());
}
Get a BLECharacteristic representing a Bluetooth® Low Energy characteristic the device provides.
bleDevice.characteristic(index)
bleDevice.characteristic(uuid)
bleDevice.characteristic(uuid, index)
- index: index of characteristic
- uuid: uuid (as a String)
- BLECharacteristic for provided parameters
// 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;
}
BLECharacteristic batteryLevelCharacterisic = peripheral.characteristic("2a19");
if (batteryLevelCharacterisic) {
// use the characteristic
} else {
Serial.println("Peripheral does NOT have battery level characteristic");
}
// ...
}
Discover all of the attributes of Bluetooth® Low Energy device.
bleDevice.discoverAttributes()
None
- true, if successful,
- false on failure.
// 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;
}
// ...
}
Discover the attributes of a particular service on the Bluetooth® Low Energy device.
bleDevice.discoverService(serviceUuid)
- serviceUuid: service UUID to discover (as a String)
- true, if successful,
- false on failure.
// 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 service attributes
Serial.println("Discovering service attributes ...");
if (peripheral.serviceUuid("fffe")) {
Serial.println("Service attributes discovered");
} else {
Serial.println("Service attribute discovery failed!");
peripheral.disconnect();
return;
}
// ...
}
Query the device name (BLE characteristic UUID 0x2a00) of a Bluetooth® Low Energy device.
bleDevice.deviceName()
None
- Device name (as a String).
// 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;
}
// read and print device name of peripheral
Serial.println();
Serial.print("Device name: ");
Serial.println(peripheral.deviceName());
Serial.print("Appearance: 0x");
Serial.println(peripheral.appearance(), HEX);
Serial.println();
// ...
}
Query the appearance (BLE characteristic UUID 0x2a01) of a Bluetooth® Low Energy device.
bleDevice.appearance()
None
- Appearance value (as a number).
// 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;
}
// read and print device name of peripheral
Serial.println();
Serial.print("Device name: ");
Serial.println(peripheral.deviceName());
Serial.print("Appearance: 0x");
Serial.println(peripheral.appearance(), HEX);
Serial.println();
// ...
}
Query the number of services discovered for the Bluetooth® Low Energy device.
bleDevice.serviceCount()
None
- The number of services discovered for the Bluetooth® Low Energy device.
// 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;
}
int serviceCount = peripheral.serviceCount();
Serial.print(serviceCount);
Serial.println(" services discovered");
// ...
}
Query if the Bluetooth® Low Energy device has a particular service.
bleDevice.hasService(uuid)
bleDevice.hasService(uuid, index)
- uuid: uuid to check (as a String)
- index: optional, index of service to check if the device provides more than on. Defaults to 0, if not provided.
- true, if the device provides the service,
- false otherwise.
// 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;
}
if (peripheral.hasService("180f")) {
Serial.println("Peripheral has battery service");
}
// ...
}
Get a BLEService representing a Bluetooth® Low Energy service the device provides.
bleDevice.service(index)
bleDevice.service(uuid)
bleDevice.service(uuid, index)
- index: index of service
- uuid: uuid (as a String)
- BLEService for provided parameters
// 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;
}
BLEService batteryService = peripheral.service("180f");
if (batteryService) {
// use the service
} else {
Serial.println("Peripheral does NOT have battery service");
}
// ...
}
Query the number of characteristics discovered for the Bluetooth® Low Energy device.
bleDevice.characteristicCount()
None
- The number of characteristics discovered for the Bluetooth® Low Energy device.
// 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;
}
int characteristicCount = peripheral.characteristicCount();
Serial.print(characteristicCount);
Serial.println(" characteristis discovered");
// ...
}
Query if the Bluetooth® Low Energy device has a particular characteristic.
bleDevice.hasCharacteristic(uuid)
bleDevice.hasCharacteristic(uuid, index)
- uuid: uuid to check (as a String)
- index: optional, index of characteristic to check if the device provides more than on. Defaults to 0, if not provided.
- true, if the device provides the characteristic,
- false otherwise.
// 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;
}
if (peripheral.hasCharacteristic("2a19")) {
Serial.println("Peripheral has battery level characteristic");
}
// ...
}
Query if a discovered Bluetooth® Low Energy device is advertising a local name.
bleDevice.hasLocalName()
Nothing
- true, if the device is advertising a local name,
- false otherwise.
// 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 local name, if present
if (peripheral.hasLocalName()) {
Serial.print("Local Name: ");
Serial.println(peripheral.localName());
}
// ...
}
Query if a discovered Bluetooth® Low Energy device is advertising a service UUID.
bleDevice.hasAdvertisedServiceUuid()
bleDevice.hasAdvertisedServiceUuid(index)
- index: optional, defaults to 0, the index of the service UUID, if the device is advertising more than one.
- true, if the device is advertising a service UUID,
- false otherwise.
// 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();
}
// ...
}
Query the number of advertised services a discovered Bluetooth® Low Energy device is advertising.
bleDevice.advertisedServiceUuidCount()
None
- The number of advertised services a discovered Bluetooth® Low Energy device is advertising.
// 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();
}
// ...
}
Query the local name a discovered Bluetooth® Low Energy device is advertising with.
bleDevice.localName()
Nothing
- Advertised local name (as a String).
// 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 local name, if present
if (peripheral.hasLocalName()) {
Serial.print("Local Name: ");
Serial.println(peripheral.localName());
}
// ...
}
Query an advertised service UUID discovered Bluetooth® Low Energy device is advertising.
bleDevice.advertisedServiceUuid()
bleDevice.advertisedServiceUuid(index)
- index: optional, defaults to 0, the index of the service UUID, if the device is advertising more than one.
- Advertised service UUID (as a String).
// 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();
}
// ...
}
Connect to a Bluetooth® Low Energy device.
bleDevice.connect()
None
- true, if the connection was successful,
- false otherwise.
// 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;
}
// ...
}
Used to enable the services board provides or interact with services a remote board provides.
Create a new Bluetooth® Low Energy service.
BLEService(uuid)
- uuid: 16-bit or 128-bit UUID in String format
- New BLEService with the specified UUID
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
Query the UUID of the specified BLEService.
bleService.uuid()
None
- UUID of the Bluetooth® Low Energy service as a String.
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
Serial.print(“LED service UUID = “);
Serial.println(ledService.uuid());
Add a BLECharateristic to the Bluetooth® Low Energy service.
bleService.addCharacteristic(bleCharacteristic)
None
Nothing
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, readable and writable by central
BLECharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite, 1);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
Query the number of characteristics discovered for the Bluetooth® Low Energy service.
bleService.characteristicCount()
None
- The number of characteristics discovered for the Bluetooth® Low Energy service.
// 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;
}
BLEService batteryService = peripheral.service("180f");
if (batteryService) {
// use the service
int characteristicCount = batteryService.characteristicCount();
Serial.print(characteristicCount);
Serial.println(" characteristics discovered in battery service");
} else {
Serial.println("Peripheral does NOT have battery service");
}
// ...
}
Query if the Bluetooth® Low Energy service has a particular characteristic.
bleService.hasCharacteristic(uuid)
bleService.hasCharacteristic(uuid, index)
- uuid: uuid to check (as a String)
- index: optional, index of characteristic to check if the device provides more than on. Defaults to 0, if not provided.
- true, if the service provides the characteristic,
- false otherwise.
// 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;
}
BLEService batteryService = peripheral.service("180f");
if (batteryService) {
// use the service
if (batteryService.hasCharacteristic("2a19")) {
Serial.println("Battery service has battery level characteristic");
}
} else {
Serial.println("Peripheral does NOT have battery service");
}
// ...
}
Get a BLECharacteristic representing a Bluetooth® Low Energy characteristic the service provides.
bleService.characteristic(index)
bleService.characteristic(uuid)
bleService.characteristic(uuid, index)
- index: index of characteristic
- uuid: uuid (as a String)
- BLECharacteristic for provided parameters
// 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;
}
BLEService batteryService = peripheral.service("180f");
if (batteryService) {
// use the service
BLECharacteristic batteryLevelCharacterisic = peripheral.characteristic("2a19");
if (batteryLevelCharacterisic) {
// use the characteristic
} else {
Serial.println("Peripheral does NOT have battery level characteristic");
}
} else {
Serial.println("Peripheral does NOT have battery service");
}
// ...
}
Used to enable the characteristics board offers in a service or interact with characteristics a remote board provides.
Create a new Bluetooth® Low Energy characteristic.
BLECharacteristic(uuid, properties, value, valueSize)
BLECharacteristic(uuid, properties, stringValue)
BLEBoolCharacteristic(uuid, properties)
BLEBooleanCharacteristic(uuid, properties)
BLECharCharacteristic(uuid, properties)
BLEUnsignedCharCharacteristic(uuid, properties)
BLEByteCharacteristic(uuid, properties)
BLEShortCharacteristic(uuid, properties)
BLEUnsignedShortCharacteristic(uuid, properties)
BLEWordCharacteristic(uuid, properties)
BLEIntCharacteristic(uuid, properties)
BLEUnsignedIntCharacteristic(uuid, properties)
BLELongCharacteristic(uuid, properties)
BLEUnsignedLongCharacteristic(uuid, properties)
BLEFloatCharacteristic(uuid, properties)
BLEDoubleCharacteristic(uuid, properties)
- uuid: 16-bit or 128-bit UUID in String format
- properties: mask of the properties (BLEBroadcast, BLERead, BLEWriteWithoutResponse, BLEWrite, BLENotify, BLEIndicate)
- valueSize: (maximum) size of characteristic value
- stringValue: value as a string
- New BLECharacteristic with the specified UUID and value
// 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
Query the UUID of the specified BLECharacteristic.
bleCharacteristic.uuid()
None
- UUID of the Bluetooth® Low Energy service as a String.
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
Serial.print(“Switch characteristic UUID = “);
Serial.println(switchCharacteristic.uuid());
Query the property mask of the specified BLECharacteristic.
bleCharacteristic.properties()
None
- Properties of the characteristic masked (BLEBroadcast, BLERead, BLEWriteWithoutResponse, BLEWrite, BLENotify, BLEIndicate)
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
byte properties = switchCharacteristic.properties();
if (properties & BLERead) {
// characteristic is readable ...
}
if (properties & (BLEWrite | BLEWriteWithoutResponse)) {
// characteristic is writable ...
}
Query the maximum value size of the specified BLECharacteristic.
bleCharacteristic.valueSize()
None
- The maximum value size of the characteristic (in bytes)
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
Serial.print(“value size = “);
Serial.println(switchCharacteristic.valueSize());
Query the current value of the specified BLECharacteristic.
bleCharacteristic.value()
None
- The current value of the characteristic, value type depends on the constructor used
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
Query the current value size of the specified BLECharacteristic.
bleCharacteristic.valueLength()
None
- The current value size of the characteristic (in bytes)
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
Serial.print(“value length = “);
Serial.println(switchCharacteristic.valueLength());
Read the current value of the characteristic. If the characteristic is on a remote device, a read request will be sent.
bleCharacteristic.readValue(buffer, length)
bleCharacteristic.readValue(value)
- buffer: byte array to read value into length: size of buffer argument in bytes
- value: variable to read value into (by reference)
- Number of bytes read
while (peripheral.connected()) {
// while the peripheral is connected
// check if the value of the simple key characteristic has been updated
if (simpleKeyCharacteristic.valueUpdated()) {
// yes, get the value, characteristic is 1 byte so use byte value
byte value = 0;
simpleKeyCharacteristic.readValue(value);
if (value & 0x01) {
// first bit corresponds to the right button
Serial.println("Right button pressed");
}
if (value & 0x02) {
// second bit corresponds to the left button
Serial.println("Left button pressed");
}
}
}
Write the value of the characteristic. If the characteristic is on a remote device, a write request or command will be sent.
bleCharacteristic.writeValue(buffer, length)
bleCharacteristic.writeValue(value)
- buffer: byte array to write value with
- length: number of bytes of the buffer argument to write
- value: value to write
- 1 on success,
- 0 on failure
// read the button pin
int buttonState = digitalRead(buttonPin);
if (oldButtonState != buttonState) {
// button changed
oldButtonState = buttonState;
if (buttonState) {
Serial.println("button pressed");
// button is pressed, write 0x01 to turn the LED on
ledCharacteristic.writeValue((byte)0x01);
} else {
Serial.println("button released");
// button is released, write 0x00 to turn the LED off
ledCharacteristic.writeValue((byte)0x00);
}
}
Set the event handler (callback) function that will be called when the specified event occurs.
bleCharacteristic.setEventHandler(eventType, callback)
- eventType: event type (BLESubscribed, BLEUnsubscribed, BLERead, BLEWritten)
- callback: function to call when the event occurs
Nothing
// create switch characteristic and allow remote device to read and write
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
// assign event handlers for characteristic
switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);
void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
// central wrote new value to characteristic, update LED
Serial.print("Characteristic event, written: ");
if (switchCharacteristic.value()) {
Serial.println("LED on");
digitalWrite(ledPin, HIGH);
} else {
Serial.println("LED off");
digitalWrite(ledPin, LOW);
}
}
Broadcast the characteristics value as service data when advertising.
bleCharacteristic.broadcast()
None
- 1 on success,
- 0 on failure
// create button characteristic and allow remote device to get notifications
BLEByteCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify | BLEBroadcast);
buttonCharacteristic.broadcast();
Query if the characteristic value has been written by another Bluetooth® Low Energy device.
bleCharacteristic.written()
None
- true if the characteristic value has been written by another Bluetooth® Low Energy device,
- false otherwise
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
// 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());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
Query if the characteristic has been subscribed to by another Bluetooth® Low Energy device.
bleCharacteristic.subscribed()
None
- true if the characteristic value has been subscribed to by another Bluetooth® Low Energy device,
- false otherwise
// 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
if (batteryLevelChar.subscribed()) {
// set a new value , that well be pushed to subscribed Bluetooth® Low Energy devices
batteryLevelChar.writeValue(0xab);
}
Add a BLEDescriptor to the characteristic.
bleCharacteristic.addDescriptor(bleDescriptor)
- bleDescriptor: descriptor to add to the characteristic
Nothing
// 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
BLEDescriptor batteryLevelDescriptor("2901", "millis");
batteryLevelChar.addDescriptor(batteryLevelDescriptor);
Query the number of Bluetooth® Low Energy descriptors discovered for the characteristic.
bleCharacteristic.descriptorCount()
None
- The number of Bluetooth® Low Energy descriptors discovered for the characteristic
// loop the descriptors of the characteristic and explore each
for (int i = 0; i < characteristic.descriptorCount(); i++) {
BLEDescriptor descriptor = characteristic.descriptor(i);
// ...
}
Check if a characteristic has a particular descriptor.
bleCharacteristic.hasDescriptor(uuid)
bleCharacteristic.hasDescriptor(uuid, index)
- index: index of descriptor
- uuid: uuid (as a String)
- true, if the characteristic has a matching descriptor,
- otherwise false.
if (characteristic.hasDescriptor("2901")) {
Serial.println("characteristic has description descriptor");
}
Get a BLEDescriptor that represents a characteristics Bluetooth® Low Energy descriptor.
bleCharacteristic.descriptor(index)
bleCharacteristic.descriptor(uuid)
bleCharacteristic.descriptor(uuid, index)
- index: index of descriptor
- uuid: uuid (as a String)
- BLEDescriptor that represents a characteristics Bluetooth® Low Energy descriptor
if (characteristic.hasDescriptor("2901")) {
Serial.println("characteristic has description descriptor");
}
Query if a Bluetooth® Low Energy characteristic is readable.
bleCharacteristic.canRead()
None
- true, if characteristic is readable,
- false otherwise
if (characteristic.canRead("2901")) {
Serial.println("characteristic is readable");
}
read
Perform a read request for the characteristic.
bleCharacteristic.read()
None
- true, if successful,
- false on failure
if (characteristic.read()) {
Serial.println("characteristic value read");
// ...
} else {
Serial.println("error reading characteristic value");
}
Query if a Bluetooth® Low Energy characteristic is writable.
bleCharacteristic.canWrite()
None
- true, if characteristic is writable,
- false otherwise
if (characteristic.canWrite()) {
Serial.println("characteristic is writable");
}
Query if a Bluetooth® Low Energy characteristic is subscribable.
bleCharacteristic.canSubscribe()
None
- true, if characteristic is subscribable,
- false otherwise
if (characteristic.canSubscribe()) {
Serial.println("characteristic is subscribable");
}
Subscribe to a Bluetooth® Low Energy characteristics notification or indications.
bleCharacteristic.subscribe()
None
- true, on success,
- false on failure
// ...
// retrieve the simple key characteristic
BLECharacteristic simpleKeyCharacteristic = peripheral.characteristic("ffe1");
// subscribe to the simple key characteristic
Serial.println("Subscribing to simple key characteristic ...");
if (!simpleKeyCharacteristic) {
Serial.println("no simple key characteristic found!");
peripheral.disconnect();
return;
} else if (!simpleKeyCharacteristic.canSubscribe()) {
Serial.println("simple key characteristic is not subscribable!");
peripheral.disconnect();
return;
} else if (!simpleKeyCharacteristic.subscribe()) {
Serial.println("subscription failed!");
peripheral.disconnect();
return;
}
// ...
Query if a Bluetooth® Low Energy characteristic is unsubscribable.
bleCharacteristic.canUnsubscribe()
None
- true, if characteristic is unsubscribable,
- false otherwise
if (characteristic.canUnsubscribe()) {
Serial.println("characteristic is unsubscribable");
}
Unsubscribe to a Bluetooth® Low Energy characteristics notifications or indications.
bleCharacteristic.unsubscribe()
None
- true, on success,
- false on failure
// ...
// retrieve the simple key characteristic
BLECharacteristic simpleKeyCharacteristic = peripheral.characteristic("ffe1");
// subscribe to the simple key characteristic
Serial.println("Subscribing to simple key characteristic ...");
if (!simpleKeyCharacteristic) {
Serial.println("no simple key characteristic found!");
peripheral.disconnect();
return;
} else if (!simpleKeyCharacteristic.canSubscribe()) {
Serial.println("simple key characteristic is not subscribable!");
peripheral.disconnect();
return;
} else if (!simpleKeyCharacteristic.subscribe()) {
Serial.println("subscription failed!");
peripheral.disconnect();
return;
}
// ...
simpleKeyCharacteristic.unsubscribe();
Has the characteristics value been updated via a notification or indication.
bleCharacteristic.valueUpdated()
None
- true, if the characteristics value been updated via a notification or indication
while (peripheral.connected()) {
// while the peripheral is connected
// check if the value of the simple key characteristic has been updated
if (simpleKeyCharacteristic.valueUpdated()) {
// yes, get the value, characteristic is 1 byte so use byte value
byte value = 0;
simpleKeyCharacteristic.readValue(value);
if (value & 0x01) {
// first bit corresponds to the right button
Serial.println("Right button pressed");
}
if (value & 0x02) {
// second bit corresponds to the left button
Serial.println("Left button pressed");
}
}
}
Used to describe a characteristic the board offers
Create a new Bluetooth® Low Energy descriptor.
BLEDescriptor(uuid, value, valueSize)
BLEDescriptor(uuid, stringValue)
- uuid: 16-bit or 128-bit UUID in string format
- value: byte array value
- valueSize: size of byte array value
- stringValue: value as a string
- New BLEDescriptor with the specified UUID and value
BLEDescriptor millisLabelDescriptor("2901", "millis");
Query the UUID of the specified BLEDescriptor.
bleDescriptor.uuid()
None
- UUID of the Bluetooth® Low Energy descriptor (as a String).
BLEDescriptor millisLabelDescriptor("2901", "millis");
Serial.print(“millis label descriptor UUID = “);
Serial.println(millisLabelDescriptor.uuid());
Query the value size of the specified BLEDescriptor.
bleDescriptor.valueSize()
None
- Value size (in bytes) of the Bluetooth® Low Energy descriptor.
BLEDescriptor millisLabelDescriptor("2901", "millis");
Serial.print(“millis label descriptor value size = “);
Serial.println(millisLabelDescriptor.valueSize());
Query the length, in bytes, of the descriptor current value.
bleDescriptor.valueLength()
None
- Length of descriptor value in bytes.
// read the descriptor value
descriptor.read();
// print out the value of the descriptor
Serial.print(", value 0x");
printData(descriptor.value(), descriptor.valueLength());
// ...
void printData(const unsigned char data[], int length) {
for (int i = 0; i < length; i++) {
unsigned char b = data[i];
if (b < 16) {
Serial.print("0");
}
Serial.print(b, HEX);
}
}
Query the value of the specified BLEDescriptor.
bleDescriptor.value()
None
- Value byte array of the BLE descriptor.
BLEDescriptor millisLabelDescriptor("2901", "millis");
int descriptorValueSize = millisLabelDescriptor.valueSize();
byte descriptorValue[descriptorValueSize];
for (int i = 0; i < descriptorValueSize; i++) {
descriptorValue[i] = millisLabelDescriptor.value()[i];
}
Read the current value of the descriptor. If the descriptor is on a remote device, a read request will be sent.
bleDescriptor.readValue(buffer, length)
bleDescriptor.readValue(value)
- buffer: byte array to read value into
- length: size of buffer argument in bytes
- value: variable to read value into (by reference)
- Number of bytes read
byte value = 0;
/get the value, descriptor is 1 byte so use byte value
descriptor.readValue(value);
Perform a read request for the descriptor.
bleDescriptor.read()
None
- true, if successful,
- false on failure
if (descriptor.read()) {
Serial.println("descriptor value read");
// ...
} else {
Serial.println("error reading descriptor value");
}