CurieBLE Library
A library designed to access the Bluetooth® Low Energy features on the Arduino 101 board.
This library is included in the Arc32 core. This core can be installed through the Arduino IDEs , where the package is named "Intel Curie Boards".
With the Arduino/Genuino 101, using this library, it is possible to use Bluetooth® Low Energy features to communicate and interact with other devices like smartphones and tablet.
To use this library
1#include <CurieBLE.h>
A quick introduction to Bluetooth® Low Energy
Bluetooth® 4.0 includes both traditional Bluetooth, now labeled "Bluetooth® Classic", and the new Bluetooth® Low Energy. Bluetooth® Low Energy is optimized for low power use at low data rates, and was designed to operate from simple lithium coin cell batteries.
Unlike standard Bluetooth® communication basically based on an asynchronous serial connection (UART) a Bluetooth® Low Energy radio acts like a community bulletin board. The computers that connect to it are like community members that read the bulletin board. Each radio acts as either the bulletin board or the reader. If your radio is a bulletin board (called a peripheral device in Bluetooth® Low Energy parlance) it posts data for all radios in the community to read. If your radio is a reader (called a central device in Blueooth LE terms) it reads from any of the bulletin boards (peripheral devices) that have information about which it cares. You can also think of peripheral devices as the servers in a client-server transaction, because they contain the information that reader radios ask for. Similarly, central devices are the clients of the Bluetooth® Low Energy world because they read information available from the peripherals.
Think of a Bluetooth® Low Energy peripheral device as a bulletin board and central devices as viewers of the board. Central devices view the services, get the data, then move on. Each transaction is quick (a few milliseconds), so multiple central devices can get data from one peripheral.
The information presented by a peripheral is structured as services, each of which is subdivided into characteristics. You can think of services as the notices on a bulletin board, and characteristics as the individual paragraphs of those notices. If you're a peripheral device, you just update each service characteristic when it needs updating and don't worry about whether the central devices read them or not. If you're a central device, you connect to the peripheral then read the boxes you want. If a given characteristic is readable and writable, then the peripheral and central can both change it.
Notify
The Bluetooth® Low Energy specification includes a mechanism known as notify that lets you know when data's changed. When notify on a characteristic is enabled and the sender writes to it, the new value is automatically sent to the receiver, without the receiver explicitly issuing a read command. This is commonly used for streaming data such as accelerometer or other sensor readings. There's a variation on this specification called indicate which works similarly, but in the indicate specification, the reader sends an acknowledgement of the pushed data.
The client-server structure of Bluetooth® Low Energy, combined with the notify characteristic, is generally called a publish-and-subscribe model.
Update a characteristic
Your peripheral should update characteristics when there's a significant change to them. For example, when a switch changes from off to on, update its characteristic. When an analog sensor changes by a significant amount, update its characteristic.
Just as with writing to a characteristic, you could update your characteristics on a regular interval, but this wastes processing power and energy if the characteristic has not changed.
Central and Peripheral Devices
Central devices are clients. They read and write data from peripheral devices. Peripheral devices are servers. They provide data from sensors as readable characteristics, and provide read/writable characteristics to control actuators like motors, lights, and so forth.
Services, characteristics, and UUIDs
A Bluetooth® Low Energy peripheral will provide services, which in turn provide characteristics. You can define your own services, or use standard services.
Services are identified by unique numbers known as UUIDs. You know about UUIDs from other contexts. Standard services have a 16-bit UUID and custom services have a 128-bit UUID. The ability to define services and characteristics depends on the radio you're using and its firmware.
Service design patterns
A characteristic value can be up to 20 bytes long. This is a key constraint in designing services. Given this limit, you should consider how best to store data about your sensors and actuators most effectively for your application. The simplest design pattern is to store one sensor or actuator value per characteristic, in ASCII encoded values.
Characteristic | Value |
---|---|
Accelerometer X | 200 |
Accelerometer Y | 134 |
Accelerometer Z | 150 |
This is also the most expensive in memory terms, and would take the longest to read. But it's the simplest for development and debugging.
You could also combine readings into a single characteristic, when a given sensor or actuator has multiple values associated with it.
Characteristic | Value |
---|---|
Motor speed, direction | 150,1 |
Accelerometer X, Y, Z | 200, 133, 150 |
This is more efficient, but you need to be careful not to exceed the 20-byte limit. The accelerometer characteristic above, for example, takes 11 bytes as a ASCII-encoded string.
Read/write/notify/indicate
There are 4 things a central device can do with a characteristic:
- Read : ask the peripheral to send back the current value of the characteristic. - Often used for characteristics that don't change very often, for example characteristics used for configuration, version numbers, etc.
- Write: modify the value of the characteristic. Often used for things that are like commands, for example telling the peripheral to turn a motor on or off.
- Indicate and Notify: ask the peripheral to continuously send updated values of the characteristic, without the central having to constantly ask for it.
Advertising and GAP
Advertising and GAP
BLE devices let other devices know that they exist by advertising using the General Advertising Profile (GAP). Advertising packets can contain a device name, some other information, and also a list of the services it provides.
Advertising packets have a limited size. You will only be able to fit a single 128-bit service UUID in the packet. Make sure the device name is not too long, or you won't even be able to fit that.
You can provide additional services that are not advertised. Central devices will learn about these through the connection/bonding process. Non-advertised services cannot be used to discover devices, though. Sometimes this is not an issue. For example, you may have a custom peripheral device with a custom service, but in your central device app you may know that it also provides the Battery Service and other services.
GATT
The Bluetooth® Low Energy protocol operates on multiple layers. General Attribute Profile (GATT) is the layer that defines services and characteristics and enables read/write/notify/indicate operations on them. When reading more about GATT, you may encounter GATT concepts of a "server" and "client". These don't always correspond to central and peripherals. In most cases, though, the peripheral is the GATT server (since it provides the services and characteristics), while the central is the GATT client.
Library structure
As the library enables multiple types of functionality, there are a number of different classes.
- BLEPeripheral used to enable the Bluetooth® Low Energy module
- BLEDescriptor that prepares the functions that the board will show
- BLECentral that represent the device the board is connected to
- BLECharacteristic used to enable the characteristics board offers
- BLEService used to enable the services board provides
Examples
BLEPeripheral class
BLEPeripheral constructor
BLEPeripheral constructor
Description
The Bluetooth® Low Energy peripheral device is typically the board you are programming. Peripheral connects to the central to expose its characteristics
Syntax
1BLEPeripheral yourBlePeripheralName
Parameters
none
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
begin
begin
Description
Initializes the Bluetooth® Low Energy peripheral in order to use all its methods within the sketch.
Syntax
1yourBlePeripheralName.begin()
Returns
true in case of correct initialization; false otherwise.
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
poll()
poll()
Description
Poll the radio for events
Syntax
1yourBlePeripheralName.poll()
Returns
Nothing
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
end()
end()
Description
Stop advertising and disconnect a central if connected
Syntax
1yourBlePeripheralName.end()
Returns
true in case of correct initialization; false otherwise.
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
setAdvertisedServiceUuid()
setAdvertisedServiceUuid()
Description
TBD
Syntax
1yourBlePeripheralName.setAdvertisedServiceUuid(const char* advertisedServiceUuid)
Parameters
advertisedServiceUuid: TBD
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
setLocalName()
setLocalName()
Description
Sets the local name of your Bluetooth® Low Energy peripheral
Syntax
1yourBlePeripheralName.setLocalName(const char* localName)
Parameters
localName: the name to be set
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
setDeviceName()
setDeviceName()
Description
Sets the device name of your Bluetooth® Low Energy peripheral
Syntax
1yourBlePeripheralName.setLocalName(const char* deviceName)
Parameters
deviceName: the name to be set
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
setAppearance()
setAppearance()
Description
TBD
Syntax
1yourBlePeripheralName.setAppearance(unsigned int appearance)
Parameters
appearance: TBD
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
setEventHandler()
setEventHandler()
Description
Sets the callback function to be called in case of match with the chosen event.
Syntax
1yourBlePeripheralName.setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler callback)
Parameters
event: the chosen matching event. It can assume one of the following values:
- BLEConnected
- BLEDisconnected
- BLEPeripheralEventLast
- callback: the name of the function to call in case of match
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8const int ledPin = 13; // set ledPin to use on-board LED9BLEPeripheral blePeripheral; // create peripheral instance10
11BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service12
13// create switch characteristic and allow remote device to read and write14BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);15
16void setup() {17 Serial.begin(9600);18 pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output19
20 // set the local name peripheral advertises21 blePeripheral.setLocalName("LEDCB");22 // set the UUID for the service this peripheral advertises23 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());24
25 // add service and characteristic26 blePeripheral.addAttribute(ledService);27 blePeripheral.addAttribute(switchChar);28
29 // assign event handlers for connected, disconnected to peripheral30 blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);31 blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);32
33 // assign event handlers for characteristic34 switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);35// set an initial value for the characteristic36 switchChar.setValue(0);37
38 // advertise the service39 blePeripheral.begin();40 Serial.println(("Bluetooth® device active, waiting for connections..."));41}42
43void loop() {44 // poll peripheral45 blePeripheral.poll();46}47
48void blePeripheralConnectHandler(BLECentral& central) {49 // central connected event handler50 Serial.print("Connected event, central: ");51 Serial.println(central.address());52}53
54void blePeripheralDisconnectHandler(BLECentral& central) {55 // central disconnected event handler56 Serial.print("Disconnected event, central: ");57 Serial.println(central.address());58}59
60void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {61 // central wrote new value to characteristic, update LED62 Serial.print("Characteristic event, written: ");63
64 if (switchChar.value()) {65 Serial.println("LED on");66 digitalWrite(ledPin, HIGH);67 } else {68 Serial.println("LED off");69 digitalWrite(ledPin, LOW);70 }71}72
73/*74 Copyright (c) 2016 Intel Corporation. All rights reserved.75
76 This library is free software; you can redistribute it and/or77 modify it under the terms of the GNU Lesser General Public78 License as published by the Free Software Foundation; either79 version 2.1 of the License, or (at your option) any later version.80
81 This library is distributed in the hope that it will be useful,82 but WITHOUT ANY WARRANTY; without even the implied warranty of83 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU84 Lesser General Public License for more details.85
86 You should have received a copy of the GNU Lesser General Public87 License along with this library; if not, write to the Free Software88 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-89 1301 USA90*/
addAttribute()
addAttribute()
Description
Adds an attribute to the peripheral
Syntax
1yourBlePeripheralName.addAttribute(BLEAttribute attributeName)
Parameters
attributeName: the name of the characteristic or the service to be added as an attribute
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
disconnect()
disconnect()
Description
Disconnect the central connected if there is one connected
Syntax
1yourBlePeripheralName.disconnect()
Returns
true if success; false otherwise
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
central()
central()
Description
Checks if the connection to the central is active or not
Syntax
1yourBlePeripheralName.central()
Returns
true in case of connection; false otherwise
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
connected()
connected()
Description
Checks if the device is connected or not
Syntax
1yourBlePeripheralName.connected()
Returns
true in case of connection; false otherwise
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
BLEDescriptor class
BLEDescriptor constructor
BLEDescriptor constructor
Description
Descriptors are defined attributes that describe a characteristic value
Syntax
1BLEDescriptor(const char* uuid, const unsigned char value[], unsigned char valueSize);2BLEDescriptor(const char* uuid, const char* value);
Parameters
- UUID: standard 16-bit characteristic UUID
- properties: what remote clients will be able to get notifications if this characteristic changes. It can assume the following values:
- BLERead
- BLEWrite
- BLENotify
- uuid: UUID of descriptor
- value: value data
- valueLength: length of value data in bytes or
value: string value
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
BLECentral class
BLECentral constructor
BLECentral constructor
Description
The BLE central is typically the device that asks the BLE peripheral its data
Syntax
1BLECentral yourBleCentralName
Parameters
none
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6/*7 * Sketch: LedControl.ino8 *9 * Description:10 * This is a Central sketch that looks for a particular Service with a11 * certain Characteristic from a Peripheral. Upon successful discovery,12 * it reads the state of a button and write that value to the13 * Peripheral Characteristic.14 *15 * Notes:16 *17 * - Expected Peripheral Service: 19b10000-e8f2-537e-4f6c-d104768a121418 * - Expected Peripheral Characteristic: 19b10001-e8f2-537e-4f6c-d104768a121419 * - Expected Peripheral sketch:20 *21 */22
23#include <CurieBLE.h>24
25// variables for button26const int buttonPin = 2;27int oldButtonState = LOW;28
29
30void setup() {31 Serial.begin(9600);32
33 // configure the button pin as input34 pinMode(buttonPin, INPUT);35
36 // initialize the BLE hardware37 BLE.begin();38
39 Serial.println("BLE Central - LED control");40
41 // start scanning for peripherals42 BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");43}44
45void loop() {46 // check if a peripheral has been discovered47 BLEDevice peripheral = BLE.available();48
49 if (peripheral) {50 // discovered a peripheral, print out address, local name, and advertised service51 Serial.print("Found ");52 Serial.print(peripheral.address());53 Serial.print(" '");54 Serial.print(peripheral.localName());55 Serial.print("' ");56 Serial.print(peripheral.advertisedServiceUuid());57 Serial.println();58
59 // stop scanning60 BLE.stopScan();61
62 controlLed(peripheral);63
64 // peripheral disconnected, start scanning again65 BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");66 }67}68
69void controlLed(BLEDevice peripheral) {70 // connect to the peripheral71 Serial.println("Connecting ...");72
73 if (peripheral.connect()) {74 Serial.println("Connected");75 } else {76 Serial.println("Failed to connect!");77 return;78 }79
80 // discover peripheral attributes81 Serial.println("Discovering attributes ...");82 if (peripheral.discoverAttributes()) {83 Serial.println("Attributes discovered");84 } else {85 Serial.println("Attribute discovery failed!");86 peripheral.disconnect();87 return;88 }89
90 // retrieve the LED characteristic91 BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");92
93 if (!ledCharacteristic) {94 Serial.println("Peripheral does not have LED characteristic!");95 peripheral.disconnect();96 return;97 } else if (!ledCharacteristic.canWrite()) {98 Serial.println("Peripheral does not have a writable LED characteristic!");99 peripheral.disconnect();100 return;101 }102
103 while (peripheral.connected()) {104 // while the peripheral is connection105
106 // read the button pin107 int buttonState = digitalRead(buttonPin);108
109 if (oldButtonState != buttonState) {110 // button changed111 oldButtonState = buttonState;112
113 if (buttonState) {114 Serial.println("button pressed");115
116 // button is pressed, write 0x01 to turn the LED on117 ledCharacteristic.writeByte(0x01);118 } else {119 Serial.println("button released");120
121 // button is released, write 0x00 to turn the LED of122 ledCharacteristic.writeByte(0x00);123 }124 }125 }126
127 Serial.println("Peripheral disconnected");128}129
130/*131 Arduino BLE Central LED Control example132 Copyright (c) 2016 Arduino LLC. All right reserved.133
134 This library is free software; you can redistribute it and/or135 modify it under the terms of the GNU Lesser General Public136 License as published by the Free Software Foundation; either137 version 2.1 of the License, or (at your option) any later version.138
139 This library is distributed in the hope that it will be useful,140 but WITHOUT ANY WARRANTY; without even the implied warranty of141 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU142 Lesser General Public License for more details.143
144 You should have received a copy of the GNU Lesser General Public145 License along with this library; if not, write to the Free Software146 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA147*/
connected()
connected()
Description
Checks if the device is connected or not
Syntax
1yourBleCentralName.connected()
Returns
true in case of connection; false otherwise
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
address()
address()
Description
Returns the central's address
Syntax
1yourBleCentralName.address()
Returns
The central's address
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
disconnect()
disconnect()
Description
Disconnect the central if it is connected
Syntax
1yourBleCentralName.disconnect()
Returns
true if success; false otherwise
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
poll()
poll()
Description
Polls the central for events
Syntax
1yourBleCentralName.poll()
Parameters
None
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
BLECharacteristic class
BLECharacteristic constructor
BLECharacteristic constructor
Description
Characteristics contain at least two attributes: a characteristic declaration, which contains metadata about the data, and the characteristic value, which contains the data itself. Since characteristics have:
names UUIDs values read/write/notify property There are many Characteristic constructors depending on the data type you plan to use, like showed below.
Syntax
1BLEBoolCharacteristic yourCharacteristicName(UUID, properties)2BLECharCharacteristic yourCharacteristicName(UUID, properties)3BLEUnsignedCharCharacteristic yourCharacteristicName(UUID, properties)4BLEShortCharacteristic yourCharacteristicName(UUID, properties)5BLEUnsignedShortCharacteristic yourCharacteristicName(UUID, properties)6BLEIntCharacteristic yourCharacteristicName(UUID, properties)7BLEUnsignedIntCharacteristic yourCharacteristicName(UUID, properties)8BLELongCharacteristic yourCharacteristicName(UUID, properties)9BLEUnsignedLongCharacteristic yourCharacteristicName(UUID, properties)10BLEFloatCharacteristic yourCharacteristicName(UUID, properties)11BLEDoubleCharacteristic yourCharacteristicName(UUID, properties)12Note: BLE characteristics are typed.
Parameters
- UUID: standard 16-bit characteristic UUID
- properties: what remote clients will be able to get notifications if this characteristic changes. It can assume the following values: BLERead, BLEWrite, BLENotify
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
BLEService class
BLEService constructor
BLEService constructor
Description
The Bluetooth® Low Energy service allows you to create the service you want to show through your Bluetooth® Low Energy device
Syntax
1BLEService (const char* uuid)
Parameters
uuid: the 16 bit or 128 bit UUID defined by Bluetooth® Low Energy standard
Example
1/*2 * Copyright (c) 2016 Intel Corporation. All rights reserved.3 * See the bottom of this file for the license terms.4 */5
6#include <CurieBLE.h>7
8BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)9BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service10
11// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central12BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);13
14const int ledPin = 13; // pin to use for the LED15
16void setup() {17 Serial.begin(9600);18
19 // set LED pin to output mode20 pinMode(ledPin, OUTPUT);21
22 // set advertised local name and service UUID:23 blePeripheral.setLocalName("LED");24 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());25
26 // add service and characteristic:27 blePeripheral.addAttribute(ledService);28 blePeripheral.addAttribute(switchCharacteristic);29
30 // set the initial value for the characeristic:31 switchCharacteristic.setValue(0);32
33 // begin advertising BLE service:34 blePeripheral.begin();35
36 Serial.println("BLE LED Peripheral");37}38
39void loop() {40 // listen for BLE peripherals to connect:41 BLECentral central = blePeripheral.central();42
43 // if a central is connected to peripheral:44 if (central) {45 Serial.print("Connected to central: ");46 // print the central's MAC address:47 Serial.println(central.address());48
49 // while the central is still connected to peripheral:50 while (central.connected()) {51 // if the remote device wrote to the characteristic,52 // use the value to control the LED:53 if (switchCharacteristic.written()) {54 if (switchCharacteristic.value()) { // any value other than 055 Serial.println("LED on");56 digitalWrite(ledPin, HIGH); // will turn the LED on57 } else { // a 0 value58 Serial.println(F("LED off"));59 digitalWrite(ledPin, LOW); // will turn the LED off60 }61 }62 }63
64 // when the central disconnects, print it out:65 Serial.print(F("Disconnected from central: "));66 Serial.println(central.address());67 }68}69
70/*71 Copyright (c) 2016 Intel Corporation. All rights reserved.72
73 This library is free software; you can redistribute it and/or74 modify it under the terms of the GNU Lesser General Public75 License as published by the Free Software Foundation; either76 version 2.1 of the License, or (at your option) any later version.77
78 This library is distributed in the hope that it will be useful,79 but WITHOUT ANY WARRANTY; without even the implied warranty of80 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU81 Lesser General Public License for more details.82
83 You should have received a copy of the GNU Lesser General Public84 License along with this library; if not, write to the Free Software85 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA86*/
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.