diff --git a/libraries/CurieBLE/examples/central/led_control/led_control.ino b/libraries/CurieBLE/examples/central/led_control/led_control.ino index fd8ce3c2..0ef64ff6 100644 --- a/libraries/CurieBLE/examples/central/led_control/led_control.ino +++ b/libraries/CurieBLE/examples/central/led_control/led_control.ino @@ -39,7 +39,7 @@ void setup() { Serial.println("BLE Central - LED control"); // start scanning for peripherals - BLE.scan(); + BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); } void loop() { @@ -56,16 +56,13 @@ void loop() { Serial.print(peripheral.advertisedServiceUuid()); Serial.println(); - // see if peripheral is advertising the LED service - if (peripheral.advertisedServiceUuid() == "19b10000-e8f2-537e-4f6c-d104768a1214") { - // stop scanning - BLE.stopScan(); + // stop scanning + BLE.stopScan(); - controlLed(peripheral); + controlLed(peripheral); - // peripheral disconnected, start scanning again - BLE.scan(); - } + // peripheral disconnected, start scanning again + BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); } } @@ -126,6 +123,8 @@ void controlLed(BLEDevice peripheral) { } } } + + Serial.println("Peripheral disconnected"); } /* diff --git a/libraries/CurieBLE/examples/peripheral/BatteryMonitor_Notification/BatteryMonitor_Notification.ino b/libraries/CurieBLE/examples/peripheral/BatteryMonitor/BatteryMonitor.ino similarity index 67% rename from libraries/CurieBLE/examples/peripheral/BatteryMonitor_Notification/BatteryMonitor_Notification.ino rename to libraries/CurieBLE/examples/peripheral/BatteryMonitor/BatteryMonitor.ino index a44cd6b6..e898e2aa 100644 --- a/libraries/CurieBLE/examples/peripheral/BatteryMonitor_Notification/BatteryMonitor_Notification.ino +++ b/libraries/CurieBLE/examples/peripheral/BatteryMonitor/BatteryMonitor.ino @@ -4,7 +4,7 @@ */ /* - * Sketch: BatteryMonitor_Notification.ino + * Sketch: BatteryMonitor.ino * * Description: * This sketch example partially implements the standard Bluetooth @@ -13,11 +13,6 @@ * For more information: * https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx * - * Notes: - * - * - Expected to work with BatteryMonitor_Central sketch. - * You can also use an android or IOS app that supports notifications. - * */ #include @@ -25,35 +20,39 @@ BLEService batteryService("180F"); // BLE Battery Service // BLE Battery Level Characteristic" -BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify); // standard 16-bit characteristic UUID defined in the URL above - // remote clients will be able to get notifications if this characteristic changes +BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID + BLERead | BLENotify); // remote clients will be able to +// get notifications if this characteristic changes int oldBatteryLevel = 0; // last battery level reading from analog input long previousMillis = 0; // last time the battery level was checked, in ms void setup() { - BLE.begin(); Serial.begin(9600); // initialize serial communication pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected + // begin initialization + BLE.begin(); + /* Set a local name for the BLE device This name will appear in advertising packets and can be used by remote devices to identify this BLE device The name can be changed but maybe be truncated based on space left in advertisement packet If you want to make this work with the BatteryMonitor_Central sketch, do not modufy the name. */ - BLE.setLocalName("BatteryMonitorSketch"); - BLE.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID - BLE.addService(batteryService); // Add the BLE Battery service + BLE.setLocalName("BatteryMonitor"); + BLE.setAdvertisedService(batteryService); // add the service UUID batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic + BLE.addService(batteryService); // Add the BLE Battery service batteryLevelChar.setValue(oldBatteryLevel); // initial value for this characteristic - /* Now activate the BLE device. It will start continuously transmitting BLE + /* Start advertising BLE. It will start continuously transmitting BLE advertising packets and will be visible to remote BLE central devices - until it receives a new connection - */ + until it receives a new connection */ + // start advertising BLE.advertise(); + Serial.println("Bluetooth device active, waiting for connections..."); } @@ -77,14 +76,6 @@ void loop() { if (currentMillis - previousMillis >= 200) { previousMillis = currentMillis; updateBatteryLevel(); - - static unsigned short count = 0; - count++; - // update the connection interval - if (count % 5 == 0) { - delay(1000); - updateIntervalParams(central); - } } } // when the central disconnects, turn off the LED: @@ -104,36 +95,11 @@ void updateBatteryLevel() { if (batteryLevel != oldBatteryLevel) { // if the battery level has changed Serial.print("Battery Level % is now: "); // print it Serial.println(batteryLevel); - batteryLevelChar.writeUnsignedChar(batteryLevel); // and update the battery level characteristic + batteryLevelChar.setValue(batteryLevel); // and update the battery level characteristic oldBatteryLevel = batteryLevel; // save the level for next comparison } } -void updateIntervalParams(BLEDevice central) { - // read and update the connection interval that peer central device - static unsigned short interval = 0x60; - ble_conn_param_t m_conn_param; - // Get connection interval that peer central device wanted - //central.getConnParams(m_conn_param); - Serial.print("min interval = " ); - Serial.println(m_conn_param.interval_min ); - Serial.print("max interval = " ); - Serial.println(m_conn_param.interval_max ); - Serial.print("latency = " ); - Serial.println(m_conn_param.latency ); - Serial.print("timeout = " ); - Serial.println(m_conn_param.timeout ); - - //Update connection interval - Serial.println("set Connection Interval"); - central.setConnectionInterval(interval, interval); - - interval++; - if (interval < 0x06) - interval = 0x06; - if (interval > 0x100) - interval = 0x06; -} /* Copyright (c) 2016 Intel Corporation. All rights reserved. diff --git a/libraries/CurieBLE/examples/peripheral/ButtonLED/ButtonLED.ino b/libraries/CurieBLE/examples/peripheral/ButtonLED/ButtonLED.ino new file mode 100644 index 00000000..72a88a5e --- /dev/null +++ b/libraries/CurieBLE/examples/peripheral/ButtonLED/ButtonLED.ino @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. + */ + +#include + +const int ledPin = 13; // set ledPin to on-board LED +const int buttonPin = 4; // set buttonPin to digital pin 4 + +BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service + + +// create switch characteristic and allow remote device to read and write +BLECharCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); +// create button characteristic and allow remote device to get notifications +BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // allows remote device to get notifications + +void setup() { + Serial.begin(9600); + pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output + pinMode(buttonPin, INPUT); // use button pin 4 as an input + + // begin initialization + BLE.begin(); + + // set the local name peripheral advertises + BLE.setLocalName("BtnLED"); + // set the UUID for the service this peripheral advertises: + BLE.setAdvertisedService(ledService); + + // add the characteristics to the service + ledService.addCharacteristic(ledCharacteristic); + ledService.addCharacteristic(buttonCharacteristic); + + // add the service + BLE.addService(ledService); + + ledCharacteristic.setValue(0); + buttonCharacteristic.setValue(0); + + // start advertising + BLE.advertise(); + + Serial.println("Bluetooth device active, waiting for connections..."); +} + +void loop() { + // poll for BLE events + BLE.poll(); + + // read the current button pin state + char buttonValue = digitalRead(buttonPin); + + // has the value changed since the last read + boolean buttonChanged = (buttonCharacteristic.value() != buttonValue); + + if (buttonChanged) { + // button state changed, update characteristics + ledCharacteristic.setValue(buttonValue); + buttonCharacteristic.setValue(buttonValue); + } + + if (ledCharacteristic.written() || buttonChanged) { + // update LED, either central has written to characteristic or button state has changed + if (ledCharacteristic.value()) { + Serial.println("LED on"); + digitalWrite(ledPin, HIGH); + } else { + Serial.println("LED off"); + digitalWrite(ledPin, LOW); + } + } +} + +/* + Copyright (c) 2016 Intel Corporation. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110- + 1301 USA +*/ + \ No newline at end of file diff --git a/libraries/CurieBLE/examples/peripheral/CallbackLED/CallbackLED.ino b/libraries/CurieBLE/examples/peripheral/CallbackLED/CallbackLED.ino new file mode 100644 index 00000000..c0d02e3e --- /dev/null +++ b/libraries/CurieBLE/examples/peripheral/CallbackLED/CallbackLED.ino @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. + */ + +#include + +const int ledPin = 13; // set ledPin to use on-board LED + +BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service + +// create switch characteristic and allow remote device to read and write +BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); + +void setup() { + Serial.begin(9600); + pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output + + // begin initialization + BLE.begin(); + + // set the local name peripheral advertises + BLE.setLocalName("LEDCB"); + // set the UUID for the service this peripheral advertises + BLE.setAdvertisedService(ledService); + + // add the characteristic to the service + ledService.addCharacteristic(switchChar); + + // add service + BLE.addService(ledService); + + // assign event handlers for connected, disconnected to peripheral + BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler); + BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); + + // assign event handlers for characteristic + switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten); + // set an initial value for the characteristic + switchChar.setValue(0); + + // start advertising + BLE.advertise(); + + Serial.println(("Bluetooth device active, waiting for connections...")); +} + +void loop() { + // poll for BLE events + BLE.poll(); +} + +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()); +} + +void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) { + // central wrote new value to characteristic, update LED + Serial.print("Characteristic event, written: "); + + if (switchChar.value()) { + Serial.println("LED on"); + digitalWrite(ledPin, HIGH); + } else { + Serial.println("LED off"); + digitalWrite(ledPin, LOW); + } +} + +/* + Copyright (c) 2016 Intel Corporation. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110- + 1301 USA +*/ diff --git a/libraries/CurieBLE/examples/peripheral/LED/LED.ino b/libraries/CurieBLE/examples/peripheral/LED/LED.ino new file mode 100644 index 00000000..258cb455 --- /dev/null +++ b/libraries/CurieBLE/examples/peripheral/LED/LED.ino @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. + */ + +/* + * Sketch: led.ino + * + * Description: + * This is a Peripheral sketch that works with a connected Central. + * It allows the Central to write a value and set/reset the led + * accordingly. + */ + +#include + +BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service + +// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central +BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); + +const int ledPin = 13; // pin to use for the LED + +void setup() { + Serial.begin(9600); + + // set LED pin to output mode + pinMode(ledPin, OUTPUT); + + // begin initialization + BLE.begin(); + + // set advertised local name and service UUID: + BLE.setLocalName("LED"); + BLE.setAdvertisedService(ledService); + + // add the characteristic to the service + ledService.addCharacteristic(switchCharacteristic); + + // add service + BLE.addService(ledService); + + // set the initial value for the characeristic: + switchCharacteristic.setValue(0); + + // start advertising + BLE.advertise(); + + Serial.println("BLE LED Peripheral"); +} + +void loop() { + // listen for BLE 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()); + } +} + +/* + Copyright (c) 2016 Intel Corporation. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ diff --git a/libraries/CurieBLE/examples/peripheral/led/led.ino b/libraries/CurieBLE/examples/peripheral/led/led.ino deleted file mode 100644 index 4cbb25dd..00000000 --- a/libraries/CurieBLE/examples/peripheral/led/led.ino +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. - */ - -/* - * Sketch: led.ino - * - * Description: - * This is a Peripheral sketch that works with a connected Central. - * It allows the Central to write a value and set/reset the led - * accordingly. - */ - -#include - -// LED pin -#define LED_PIN 13 - -// create service -BLEService ledService("19b10000e8f2537e4f6cd104768a1214"); - -// create switch characteristic -BLECharCharacteristic switchCharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLEWrite); - -BLEDescriptor switchDescriptor("2901", "switch"); - -void setup() { - Serial.begin(9600); - - // set LED pin to output mode - pinMode(LED_PIN, OUTPUT); - - // begin initialization - BLE.begin(); - Serial.println(BLE.address()); - - // set advertised local name and service UUID - BLE.setLocalName("LED"); - BLE.setAdvertisedServiceUuid(ledService.uuid()); - - switchCharacteristic.addDescriptor(switchDescriptor); - ledService.addCharacteristic(switchCharacteristic); - - // add service and characteristic - BLE.addService(ledService); - - BLE.advertise(); - - Serial.println(F("BLE LED Peripheral")); -} - -void loop() { - BLEDevice central = BLE.central(); - - if (central) { - // central connected to peripheral - Serial.print(F("Connected to central: ")); - Serial.println(central.address()); - - while (central.connected()) { - // central still connected to peripheral - if (switchCharacteristic.written()) { - // central wrote new value to characteristic, update LED - if (switchCharacteristic.value()) { - Serial.println(F("LED on")); - digitalWrite(LED_PIN, HIGH); - } else { - Serial.println(F("LED off")); - digitalWrite(LED_PIN, LOW); - } - } - } - - // central disconnected - Serial.print(F("Disconnected from central: ")); - Serial.println(central.address()); - } -} - - -/* - Arduino BLE Peripheral LED example - Copyright (c) 2016 Arduino LLC. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - - diff --git a/libraries/CurieBLE/examples/peripheral/led_callback/led_callback.ino b/libraries/CurieBLE/examples/peripheral/led_callback/led_callback.ino deleted file mode 100644 index 54a5c659..00000000 --- a/libraries/CurieBLE/examples/peripheral/led_callback/led_callback.ino +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. - */ - -// Import libraries -#include - -// LED pin -#define LED_PIN 13 - -// create service -BLEService ledService("19b10000e8f2537e4f6cd104768a1214"); - -// create switch characteristic -BLECharCharacteristic switchCharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLEWrite); - -void setup() { - Serial.begin(9600); - - // set LED pin to output mode - pinMode(LED_PIN, OUTPUT); - - // begin initialization - BLE.begin(); - - // set advertised local name and service UUID - BLE.setLocalName("LED"); - BLE.setAdvertisedServiceUuid(ledService.uuid()); - - ledService.addCharacteristic(switchCharacteristic); - - // add service - BLE.addService(ledService); - - // assign event handlers for connected, disconnected to peripheral - BLE.setEventHandler(BLEConnected, bleDeviceConnectHandler); - BLE.setEventHandler(BLEDisconnected, bleDeviceDisconnectHandler); - - // assign event handlers for characteristic - switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten); - - BLE.advertise(); - - Serial.println(F("BLE LED Peripheral")); -} - -void loop() { - // poll peripheral - BLE.poll(); -} - -void bleDeviceConnectHandler(BLEDevice central) { - // central connected event handler - Serial.print(F("Connected event, central: ")); - Serial.println(central.address()); -} - -void bleDeviceDisconnectHandler(BLEDevice central) { - // central disconnected event handler - Serial.print(F("Disconnected event, central: ")); - Serial.println(central.address()); -} - -void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) { - // central wrote new value to characteristic, update LED - Serial.print(F("Characteristic event, writen: ")); - - if (switchCharacteristic.value()) { - Serial.println(F("LED on")); - digitalWrite(LED_PIN, HIGH); - } else { - Serial.println(F("LED off")); - digitalWrite(LED_PIN, LOW); - } -} - - - -/* - Arduino BLE Peripheral LED callback example - Copyright (c) 2016 Arduino LLC. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - - diff --git a/libraries/CurieBLE/examples/central/BatteryMonitor_Central/BatteryMonitor_Central.ino b/libraries/CurieBLE/examples/test/BatteryMonitor_Central/BatteryMonitor_Central.ino similarity index 100% rename from libraries/CurieBLE/examples/central/BatteryMonitor_Central/BatteryMonitor_Central.ino rename to libraries/CurieBLE/examples/test/BatteryMonitor_Central/BatteryMonitor_Central.ino diff --git a/libraries/CurieBLE/examples/central/CentralDouble/CentralDouble.ino b/libraries/CurieBLE/examples/test/CentralDouble/CentralDouble.ino similarity index 100% rename from libraries/CurieBLE/examples/central/CentralDouble/CentralDouble.ino rename to libraries/CurieBLE/examples/test/CentralDouble/CentralDouble.ino diff --git a/libraries/CurieBLE/examples/central/IMUBleCentral/IMUBleCentral.ino b/libraries/CurieBLE/examples/test/IMUBleCentral/IMUBleCentral.ino similarity index 100% rename from libraries/CurieBLE/examples/central/IMUBleCentral/IMUBleCentral.ino rename to libraries/CurieBLE/examples/test/IMUBleCentral/IMUBleCentral.ino diff --git a/libraries/CurieBLE/examples/peripheral/IMUBleNotification/IMUBleNotification.ino b/libraries/CurieBLE/examples/test/IMUBleNotification/IMUBleNotification.ino similarity index 100% rename from libraries/CurieBLE/examples/peripheral/IMUBleNotification/IMUBleNotification.ino rename to libraries/CurieBLE/examples/test/IMUBleNotification/IMUBleNotification.ino diff --git a/libraries/CurieBLE/examples/peripheral/PeripheralDouble/PeripheralDouble.ino b/libraries/CurieBLE/examples/test/PeripheralDouble/PeripheralDouble.ino similarity index 100% rename from libraries/CurieBLE/examples/peripheral/PeripheralDouble/PeripheralDouble.ino rename to libraries/CurieBLE/examples/test/PeripheralDouble/PeripheralDouble.ino diff --git a/libraries/CurieBLE/keywords.txt b/libraries/CurieBLE/keywords.txt index 2db97095..a98db39d 100644 --- a/libraries/CurieBLE/keywords.txt +++ b/libraries/CurieBLE/keywords.txt @@ -104,6 +104,8 @@ setTxPower KEYWORD2 setConnectable KEYWORD2 setDeviceName KEYWORD2 addService KEYWORD2 +addCharacteristic KEYWORD2 +addDescriptor KEYWORD2 advertise KEYWORD2 stopAdvertise KEYWORD2 central KEYWORD2