From bd86efdb211afa2c7241e4f95b8c469ae86951b2 Mon Sep 17 00:00:00 2001 From: Biagio Montaruli Date: Fri, 23 Sep 2016 10:38:50 +0200 Subject: [PATCH 1/4] Update some classes of CurieBLE library Improve documentation and code formatting of BLEAttribute, BLECharacteristic and BLEProfile classes Signed-off-by: Biagio Montaruli --- libraries/CurieBLE/src/BLEAttribute.h | 4 ++-- libraries/CurieBLE/src/BLECharacteristic.h | 6 ++---- libraries/CurieBLE/src/BLEProfile.h | 4 ++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/libraries/CurieBLE/src/BLEAttribute.h b/libraries/CurieBLE/src/BLEAttribute.h index 1de33613..fbb5f786 100644 --- a/libraries/CurieBLE/src/BLEAttribute.h +++ b/libraries/CurieBLE/src/BLEAttribute.h @@ -70,7 +70,7 @@ class BLEAttribute { * @param[in] uuidsize The max size of UUID * * @return bool true - UUID is the same with data - * false- UUID is not the same with data + * false- UUID is not the same with data * * @note none */ @@ -104,7 +104,7 @@ class BLEAttribute { void setHandle(uint16_t handle); static unsigned char numAttributes(void); - // The below APIs are for central device to discover the + // The below APIs are for central device to discover peripheral devices virtual void discover(bt_gatt_discover_params_t *params) = 0; virtual void discover(const bt_gatt_attr_t *attr, bt_gatt_discover_params_t *params) = 0; diff --git a/libraries/CurieBLE/src/BLECharacteristic.h b/libraries/CurieBLE/src/BLECharacteristic.h index 58274f67..200c5761 100644 --- a/libraries/CurieBLE/src/BLECharacteristic.h +++ b/libraries/CurieBLE/src/BLECharacteristic.h @@ -166,7 +166,7 @@ class BLECharacteristic : public BLEAttribute { * * @param none * - * @return unsigned char The totla number of the notify attributes + * @return unsigned char The total number of the notify attributes * * @note none */ @@ -303,8 +303,6 @@ class BLECharacteristic : public BLEAttribute { private: void _setValue(const uint8_t value[], uint16_t length); - -private: static unsigned char _numNotifyAttributes; static bt_uuid_16_t _gatt_chrc_uuid; @@ -329,7 +327,7 @@ class BLECharacteristic : public BLEAttribute { bt_gatt_attr_t *_attr_cccd; // For central device to subscribe the Notification/Indication - bt_gatt_subscribe_params_t _sub_params; + bt_gatt_subscribe_params_t _sub_params; bool _reading; bt_gatt_read_params_t _read_params; diff --git a/libraries/CurieBLE/src/BLEProfile.h b/libraries/CurieBLE/src/BLEProfile.h index 300d54e8..48ed223b 100644 --- a/libraries/CurieBLE/src/BLEProfile.h +++ b/libraries/CurieBLE/src/BLEProfile.h @@ -166,7 +166,7 @@ class BLEProfile{ * @param[in] BLEAttribute * The BLEAttribute object * * @return bt_gatt_attr_t* NULL - Not found - * Not NULL - The bt_gatt_attr in the stack + * Not NULL - The bt_gatt_attr in the stack * * @note none */ @@ -206,7 +206,7 @@ class BLEProfile{ BLEAttribute** _attributes; uint16_t _num_attributes; - bt_gatt_subscribe_params_t *_sub_param; + bt_gatt_subscribe_params_t *_sub_param; int _sub_param_idx; bt_gatt_discover_params_t _discover_params; From f3afb380432a3cc1cd58c5eb7022837e3d984d3f Mon Sep 17 00:00:00 2001 From: Biagio Montaruli Date: Fri, 23 Sep 2016 10:40:08 +0200 Subject: [PATCH 2/4] Improve keyword.txt file of CurieBLE library Add CurieBLE and new names of methods defined in some classes of CurieBLE library Signed-off-by: Biagio Montaruli --- libraries/CurieBLE/keywords.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/libraries/CurieBLE/keywords.txt b/libraries/CurieBLE/keywords.txt index bb0f37ea..47e74fd6 100644 --- a/libraries/CurieBLE/keywords.txt +++ b/libraries/CurieBLE/keywords.txt @@ -6,6 +6,7 @@ # Datatypes (KEYWORD1) ####################################### +CurieBLE KEYWORD1 BLEAttribute KEYWORD1 BLECentral KEYWORD1 BLECharacteristic KEYWORD1 @@ -16,6 +17,9 @@ BLETypedCharacteristic KEYWORD1 BLEHelper KEYWORD1 BLECentralHelper KEYWORD1 BLEPeripheralHelper KEYWORD1 +BLERoleBase KEYWORD1 +BLECentralRole KEYWORD1 +BLEPeripheralRole KEYWORD1 BLECharCharacteristic KEYWORD1 BLEUnsignedCharCharacteristic KEYWORD1 @@ -38,11 +42,16 @@ numAttributes KEYWORD2 connected KEYWORD2 address KEYWORD2 poll KEYWORD2 +connect KEYWORD2 disconnect KEYWORD2 discover KEYWORD2 startScan KEYWORD2 stopScan KEYWORD2 +setScanParam KEYWORD2 +getScanParam KEYWORD2 +setAdvertiseHandler KEYWORD2 + getConnParams KEYWORD2 setConnectionInterval KEYWORD2 @@ -54,8 +63,10 @@ setValue KEYWORD2 setEventHandler KEYWORD2 written KEYWORD2 subscribed KEYWORD2 +numNotifyAttributes KEYWORD2 begin KEYWORD2 +end KEYWORD2 stopAdvertising KEYWORD2 setConnectable KEYWORD2 @@ -66,13 +77,19 @@ setLocalName KEYWORD2 setDeviceName KEYWORD2 setAppearance KEYWORD2 setConnectionInterval KEYWORD2 +setConnectionParameters KEYWORD2 addAttribute KEYWORD2 +attribute KEYWORD2 central KEYWORD2 +peripheral KEYWORD2 setValueLE KEYWORD2 valueLE KEYWORD2 setValueBE KEYWORD2 valueBE KEYWORD2 +valueHandle KEYWORD2 +registerProfile KEYWORD2 +cccdHandle KEYWORD2 getPeerPeripheralBLE KEYWORD2 getPeerCentralBLE KEYWORD2 From e044d9bebf843226554fed0b661ce4164fb2f190 Mon Sep 17 00:00:00 2001 From: Biagio Montaruli Date: Sun, 25 Sep 2016 18:12:34 +0200 Subject: [PATCH 3/4] Update sketches of CurieBLE library - Improve documentation and code for sketches of CurieBLE library - Add support for USB virtual serial port : Arduino/Genuino 101 uses USB native port as Serial port, moreover the sketches of CurieBLE library use Serial communication and they send data to the Serial Monitor in the 'void setup()' function, so adding 'while(!Serial)' waits for the Serial port to connect in order to not loose data already sent to the Serial Monitor. Added also documentation to inform the user to open the Serial Monitor to continue executing the sketch because 'while(!Serial)' also waits for the user to open the Serial Monitor Signed-off-by: Biagio Montaruli --- .../BatteryAdvChange/BatteryAdvChange.ino | 242 +++++++++--------- .../BatteryMonitor/BatteryMonitor.ino | 27 +- .../CurieBLE/examples/ButtonLED/ButtonLED.ino | 48 ++-- .../examples/CallbackLED/CallbackLED.ino | 32 ++- .../examples/IMUBleCentral/IMUBleCentral.ino | 49 +++- .../IMUBleNotification/IMUBleNotification.ino | 51 ++-- libraries/CurieBLE/examples/LED/LED.ino | 28 +- .../examples/LEDCentral/LEDCentral.ino | 24 +- .../CurieBLE/examples/MIDIBLE/MIDIBLE.ino | 57 +++-- .../CurieBLE/examples/Scanning/Scanning.ino | 17 +- .../UpdateConnectionInterval.ino | 25 +- 11 files changed, 369 insertions(+), 231 deletions(-) diff --git a/libraries/CurieBLE/examples/BatteryAdvChange/BatteryAdvChange.ino b/libraries/CurieBLE/examples/BatteryAdvChange/BatteryAdvChange.ino index 8b977a39..3ce1601f 100644 --- a/libraries/CurieBLE/examples/BatteryAdvChange/BatteryAdvChange.ino +++ b/libraries/CurieBLE/examples/BatteryAdvChange/BatteryAdvChange.ino @@ -1,116 +1,126 @@ -/* Please see code copyright at the bottom of this example code */ - -/* - This example can work with phone BLE app. - - This sketch illustrates how to change the advertising data so that it is visible but not - connectable. Then after 10 seconds it changes to being connectable. - This sketch example partially implements the standard Bluetooth Low-Energy Battery service. - - This sketch is not paired with a specific central example sketch, - but to see how it works you need to use a BLE APP on your phone or central device - and try connecting when it is either a connectable or not connectable state - as displayed in the serial monitor. -*/ - -#include - -BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) -BLEService batteryService("180F"); // BLE Battery Service -int count = 0; -// BLE Battery Level Characteristic" -BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID - BLERead | BLENotify); // remote clients will be able to -// get notifications if this characteristic changes - -void setup() { - Serial.begin(9600); // initialize serial communication - pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected - while (!Serial) { - //wait for Serial to connect - } - /* 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 */ - blePeripheral.setLocalName("BatteryAdvChangeSketch"); - blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID - blePeripheral.addAttribute(batteryService); // Add the BLE Battery service - blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic - - /* Now activate the BLE device. It will start continuously transmitting BLE - advertising packets and will be visible to remote BLE central devices - until it receives a new connection */ - - blePeripheral.begin(); - Serial.println("Bluetooth device active, waiting for connections..."); - Serial.println("Starts in Connectable mode"); -} - -void loop() { - // listen for BLE peripherals to connect: - BLECentralHelper central = blePeripheral.central(); - // wait - Serial.print(". "); - if (count == 10) { - Serial.print("\nReached count "); - Serial.println(count); - - } - delay (1000); - count++; - // Switch from Connectable to Non Connectable and vice versa - if (count > 10 ) { - static bool change_discover = false; - Serial.println("Stop Adv and pausing for 10 seconds. Device should be invisible"); - // Some central devices (phones included) may cache previous scan inofrmation - // restart your central and it should not see this peripheral once stopAdvertising() is called - blePeripheral.stopAdvertising(); - delay(10000); - - if (change_discover) - { - - // Using the function setConnectable we specify that it now NOT connectable - // The loop is for 10 seconds. Your central device may timeout later than that - // and may eventually connect when we set it back to connectable mode below - blePeripheral.setConnectable(false); - Serial.println("In Non Connectable mode"); - - } - else - { - - //using the function setConnectable we specify that it now connectable - blePeripheral.setConnectable(true); - Serial.println("In Connectable mode"); - } - Serial.println("Start Adv"); - blePeripheral.startAdvertising(); - if (change_discover) { - Serial.println("Adding 5 second delay in Non Connect Mode"); - delay(5000); - } - change_discover = !change_discover; - count = 0; - } -} - -/* - 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 -*/ - +/* Please see code copyright at the bottom of this example code */ + +/* + This example can work with phone BLE app. + + This sketch illustrates how to change the advertising data so that it is visible but not + connectable. Then after 10 seconds it changes to being connectable. + This sketch example partially implements the standard Bluetooth Low-Energy Battery service. + + This sketch is not paired with a specific central example sketch, + but to see how it works you need to use a BLE APP on your phone or central device + and try connecting when it is either a connectable or not connectable state + as displayed in the serial monitor. +*/ + +#include + +BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) +BLEService batteryService("180F"); // BLE Battery Service + +// BLE Battery Level Characteristic with standard 16-bit characteristic UUID. +// This characteristic has Read and Notify properties that allow remote clients +// to get notifications when this characteristic changes +BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify); + +int count = 0; +bool change_discover = false; + +void setup() { + // initialize serial communication + Serial.begin(9600); + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; + // initialize the LED on pin 13. When the BLE device will switch to connectable mode + // the on-board LED will be turned on, otherwise turn the on-board LED off + pinMode(LED_BUILTIN, OUTPUT); + + /* 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 advertising packets */ + blePeripheral.setLocalName("BatteryAdvChangeSketch"); + blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID + blePeripheral.addAttribute(batteryService); // Add the BLE Battery service + blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic + + /* Now activate the BLE device. It will start continuously transmitting BLE + advertising packets and will be visible to remote BLE central devices + until it receives a new connection */ + + blePeripheral.begin(); + Serial.println("Bluetooth device active, waiting for connections..."); + Serial.println("Starts in Connectable mode"); +} + +void loop() { + // listen for BLE peripherals to connect: + BLECentralHelper central = blePeripheral.central(); + // wait + Serial.print(". "); + if (count == 10) { + Serial.print("\nReached count "); + Serial.println(count); + + } + delay (1000); + count++; + // Switch from Connectable to Non Connectable and vice versa + if (count > 10 ) { + Serial.println("Stop Advertising and wait for 10 seconds. Device should be invisible"); + // Some central devices (phones included) may cache previous scan informations. + // Restart your central device and it should not see this peripheral once stopAdvertising() is called + blePeripheral.stopAdvertising(); + delay(10000); + + if (change_discover) + { + + // Using the method setConnectable() we specify that it is now in NON connectable mode + // The loop is for 10 seconds. Your central device may timeout later than that + // and may eventually connect when we set it back to connectable mode below + blePeripheral.setConnectable(false); + Serial.println("In Non Connectable mode"); + // turn the on-board LED off + digitalWrite(LED_BUILTIN, LOW); + } + else + { + + // Switch to connectable mode by calling the setConnectable() method + blePeripheral.setConnectable(true); + Serial.println("In Connectable mode"); + // turn the on-board LED on + digitalWrite(LED_BUILTIN, HIGH); + } + Serial.println("Start Advertising..."); + blePeripheral.startAdvertising(); + if (change_discover) { + Serial.println("Adding 5 second delay in Non Connect Mode"); + delay(5000); + } + change_discover = !change_discover; + count = 0; + } +} + +/* + 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/BatteryMonitor/BatteryMonitor.ino b/libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino index e3a297cf..4492f7b0 100644 --- a/libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino +++ b/libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino @@ -14,26 +14,31 @@ For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx */ -/* */ BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) -BLEService batteryService("180F"); // BLE Battery Service +BLEService batteryService("180F"); // BLE Battery Service with standard 16-bit UUID -// BLE Battery Level Characteristic" -BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID defined in the URL above - BLERead | BLENotify); // remote clients will be able to - // get notifications if this characteristic changes +// BLE Battery Level Characteristic with standard 16-bit characteristic UUID +// This characteristic has Read and Notify properties that allow remote clients +// to get notifications when this characteristic changes +BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify); int oldBatteryLevel = 0; // last battery level reading from analog input long previousMillis = 0; // last time the battery level was checked, in ms void setup() { - Serial.begin(9600); // initialize serial communication - pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected + // initialize serial communication + Serial.begin(9600); + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; + // initialize the LED on pin 13 to indicate when a central is connected + pinMode(LED_BUILTIN, OUTPUT); /* 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 */ + The name can be changed but maybe be truncated based on space left in advertising packets */ blePeripheral.setLocalName("BatteryMonitorSketch"); blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID blePeripheral.addAttribute(batteryService); // Add the BLE Battery service @@ -57,7 +62,7 @@ void loop() { // print the central's MAC address: Serial.println(central.address()); // turn on the LED to indicate the connection: - digitalWrite(13, HIGH); + digitalWrite(LED_BUILTIN, HIGH); // check the battery level every 200ms // as long as the central is still connected: @@ -78,7 +83,7 @@ void loop() { } } // when the central disconnects, turn off the LED: - digitalWrite(13, LOW); + digitalWrite(LED_BUILTIN, LOW); Serial.print("Disconnected from central: "); Serial.println(central.address()); } diff --git a/libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino b/libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino index 7a46c164..070c8e72 100644 --- a/libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino +++ b/libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino @@ -17,23 +17,35 @@ const int ledPin = 13; // set ledPin to on-board LED const int buttonPin = 4; // set buttonPin to digital pin 4 -BLEPeripheral blePeripheral; // create peripheral instance -BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service with a 128-bit UUID (32 characters exclusive of dashes). - // Long UUID denote custom user created UUID - +// create peripheral instance +BLEPeripheral blePeripheral; + +// create a new service with a 128-bit UUID (32 characters exclusive of dashes). +// Long UUID denote custom user created UUID +BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); -// create switch characteristic and allow remote device to read and write +// create switch characteristic with Read and Write properties that allow remote clients +// to read and write this characteristic value 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 + +// create button characteristic with Read and Notify properties that allow remote clients +// to get notifications when this characteristic value changes +BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // Note use of Typed Characteristics. These previous 2 characeristics are of the type char void setup() { + // initialize serial communication 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 - - // set the local name peripheral advertises + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; + pinMode(ledPin, OUTPUT); // set the LED pin 13 as output + pinMode(buttonPin, INPUT); // set the button pin 4 as input + + // 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 blePeripheral.setLocalName("ButtonLED"); // set the UUID for the service this peripheral advertises: blePeripheral.setAdvertisedServiceUuid(ledService.uuid()); @@ -47,7 +59,7 @@ void setup() { ledCharacteristic.setValue(0); buttonCharacteristic.setValue(0); - // advertise the service + // start advertising ledService blePeripheral.begin(); Serial.println("Bluetooth device active, waiting for connections..."); @@ -60,24 +72,24 @@ void loop() { // read the current button pin state char buttonValue = digitalRead(buttonPin); - // has the value changed since the last read + // check if the value of buttonCharacteristic has changed since the last read boolean buttonChanged = (buttonCharacteristic.value() != buttonValue); if (buttonChanged) { - // button state changed, update characteristics + // if 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 you are using a phone or a BLE central device that is aware of this characteristic, writing a value of 0x40 for example - // Will be interpreted as written + // update LED, either central has written to characteristic or button state has changed. + // If you are using a phone or a BLE central device that is aware of this characteristic, + // writing a value of 0x40 for example will be interpreted as written and the LED will be turned on if (ledCharacteristic.value()) { Serial.println("LED on"); digitalWrite(ledPin, HIGH); } else { - // If central writes a 0 value then it is interpreted as no value and turns off the LED + // If central writes a 0 value (0x00) then it is interpreted as no value and turns the LED off Serial.println("LED off"); digitalWrite(ledPin, LOW); } diff --git a/libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino b/libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino index 2a661189..98d53ce4 100644 --- a/libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino +++ b/libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino @@ -21,17 +21,27 @@ const int ledPin = 13; // set ledPin to use on-board LED BLEPeripheral blePeripheral; // create peripheral instance BLECentralHelper *bleCentral1 = NULL; // peer central device -BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service with a 128-bit UUID (32 characters exclusive of dashes). - // Long UUID denote custom user created UUID +// create service with a 128-bit UUID (32 characters exclusive of dashes). +// Long UUID denote custom user created UUID. +BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); -// create switch characteristic and allow remote device to read and write +// create switch characteristic with a custom 128-bit UUID with Read and Write properties +// to allow remote device to read and write this characteristic value BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); void setup() { + // initialize serial communication Serial.begin(9600); - pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output - - // set the local name peripheral advertises + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; + // set the pin 13 of the on-board LED as output + pinMode(ledPin, OUTPUT); + + // Set the 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 blePeripheral.setLocalName("LEDCB"); // set the UUID for the service this peripheral advertises blePeripheral.setAdvertisedServiceUuid(ledService.uuid()); @@ -46,12 +56,12 @@ void setup() { // assign event handlers for characteristic switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten); -// set an initial value for the characteristic + // set the initial value of switchChar characteristic switchChar.setValue(0); - // advertise the service + // start advertising ledService blePeripheral.begin(); - Serial.println(("Bluetooth device active, waiting for connections...")); + Serial.println("Bluetooth device active, waiting for connections..."); } void loop() { @@ -75,10 +85,10 @@ void blePeripheralDisconnectHandler(BLEHelper& central) { Serial.println(central.address()); } -// In addtion to the BLECentral& central parameter, we also have to have to BLECharacteristic& characteristic parameter +// In addtion to the BLEHelper& central parameter, we also have to have to BLECharacteristic& characteristic parameter void switchCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic) { - // central wrote new value to characteristic, update LED + // when central writes a new value to switchChar characteristic, update the LED state Serial.print("Characteristic event, written: "); if (switchChar.value()) { diff --git a/libraries/CurieBLE/examples/IMUBleCentral/IMUBleCentral.ino b/libraries/CurieBLE/examples/IMUBleCentral/IMUBleCentral.ino index a5359509..6bed902e 100644 --- a/libraries/CurieBLE/examples/IMUBleCentral/IMUBleCentral.ino +++ b/libraries/CurieBLE/examples/IMUBleCentral/IMUBleCentral.ino @@ -8,7 +8,7 @@ /* This sketch example works with IMUBleNotification.ino - IMUBleNotification.ino will send notification to this central sketch. + IMUBleNotification.ino will send notifications to this central sketch. This sketch will receive the notifications and output the received data in the serial monitor. It also illustrates using a non-typed characteristic. Set the baud rate to 115200 on the serial monitor to accomodate the speed of constant data updates from IMU subsystem. @@ -31,14 +31,18 @@ typedef struct { imuFrameType imuBuf[MAX_IMU_RECORD]; BLECentral bleCentral; // BLE Central Device (the board you're programming) +// create a new service with a custom 128-bit UUID BLEService bleImuService("F7580001-153E-D4F6-F26D-43D8D98EEB13"); -BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", // standard 128-bit characteristic UUID - BLERead | BLENotify, sizeof(imuBuf)); // remote clients will be able to - // get notifications if this characteristic changes - // We have a third parameter which is the size of imyBuffer. This is because it is a non-typed characteristic - // If we are only writing to this characteristic we can set this buffer to 512 bytes - // But because of the limitation of the Nordic FW, please do not set this to more than 128 if you intend to read it. - // MAX_IMU_RECORD value is 1 so we are safe + +// standard 128-bit characteristic UUID with Read and Notify properties that allow +// remote clients to get notifications when this characteristic changes +// We have a third parameter which is the size of imyBuffer. This is because it is a non-typed characteristic. +// If we are only writing to this characteristic we can set this buffer to 512 bytes, +// but because of the limitation of the Nordic FW, please do not set this to more than 128 if you intend to read it. +// MAX_IMU_RECORD value is 1 so we are safe +BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", + BLERead | BLENotify, sizeof(imuBuf)); + // function prototype for function that determines if the advertising data is found bool adv_found(uint8_t type, const uint8_t *dataPtr, @@ -47,18 +51,26 @@ bool adv_found(uint8_t type, void setup() { + // initialize serial communication and set the baud rate to 115200 bps. // This is set to higher baud rate because accelerometer data changes very quickly - Serial.begin(115200); // initialize serial communication - pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected + Serial.begin(115200); + // wait for the Serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; + // initialize the LED on pin 13 to indicate when a central is connected + pinMode(LED_BUILTIN, OUTPUT); + // set the event handeler function for the bleImuChar characteristic bleImuChar.setEventHandler(BLEWritten, bleImuCharacteristicWritten); bleCentral.addAttribute(bleImuService); // Add the BLE IMU service bleCentral.addAttribute(bleImuChar); // Add the BLE IMU characteristic - // Setup callback whenever a Peripheral advertising data is found) + // Setup callback whenever a Peripheral advertising data is found bleCentral.setAdvertiseHandler(adv_found); bleCentral.setEventHandler(BLEConnected, ble_connected); + bleCentral.setEventHandler(BLEDisconnected, ble_disconnected); /* Now activate the BLE device. It will start continuously transmitting BLE advertising packets and will be visible to remote BLE central devices @@ -80,12 +92,25 @@ void ble_connected(BLEHelper &role) { // since we are a central device we create a BLEPeripheralHelper peripheral BLEPeripheralHelper *peripheral = bleCentral.getPeerPeripheralBLE(role); - Serial.println("Connected"); + Serial.print("Connected to peripheral "); + // print MAC Address of peripheral device + Serial.println(peripheral->address()); + // the BLE central device is connected to peripheral so turn the on-board LED on + digitalWrite(LED_BUILTIN, HIGH); // Start discovery the profiles in peripheral device peripheral->discover(); } +void ble_disconnected(BLEHelper &peripheral) +{ + Serial.print("Disconnected form peripheral "); + // print MAC Address of peripheral device + Serial.println(peripheral.address()); + // if peripheral disconnects from the BLE central device turn the on-board LED off + digitalWrite(LED_BUILTIN, LOW); +} + void bleImuCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic) { // Peripheral wrote new value to characteristic by Notification/Indication diff --git a/libraries/CurieBLE/examples/IMUBleNotification/IMUBleNotification.ino b/libraries/CurieBLE/examples/IMUBleNotification/IMUBleNotification.ino index ee57d295..a8eedc5e 100644 --- a/libraries/CurieBLE/examples/IMUBleNotification/IMUBleNotification.ino +++ b/libraries/CurieBLE/examples/IMUBleNotification/IMUBleNotification.ino @@ -14,6 +14,7 @@ */ #define MAX_IMU_RECORD 1 +#define IMU_BUF_INDEX 0 typedef struct { int index; @@ -23,32 +24,47 @@ typedef struct { // Buffer to hold IMU data imuFrameType imuBuf[MAX_IMU_RECORD]; -unsigned seqNum = 0; +unsigned int seqNum = 0; BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) -BLEService bleImuService("F7580001-153E-D4F6-F26D-43D8D98EEB13"); // Tx IMU data Characteristic -BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", // standard 128-bit characteristic UUID - BLERead | BLENotify, sizeof(imuBuf)); // remote clients will be able to - // get notifications if this characteristic changes + +// create a new service with a custom 128-bit UUID +BLEService bleImuService("F7580001-153E-D4F6-F26D-43D8D98EEB13"); + +// standard 128-bit characteristic UUID with Read and Notify properties that allow +// remote clients to get notifications when this characteristic changes +BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", + BLERead | BLENotify, sizeof(imuBuf)); + void setup() { - Serial.begin(9600); // initialize serial communication - pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected + // initialize serial communication + Serial.begin(9600); + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; + // initialize the LED on pin 13 to indicate when a central is connected + pinMode(LED_BUILTIN, OUTPUT); /* 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 */ - blePeripheral.setLocalName("Imu"); - blePeripheral.setAdvertisedServiceUuid(bleImuService.uuid()); // add the service UUID - blePeripheral.addAttribute(bleImuService); // Add the Imu service - blePeripheral.addAttribute(bleImuChar); // add the Imu characteristic + blePeripheral.setLocalName("ImuBle"); + // add the 128-bit UUID of bleImuService + blePeripheral.setAdvertisedServiceUuid(bleImuService.uuid()); + // add the bleImuService + blePeripheral.addAttribute(bleImuService); + // add the bleImuChar characteristic + blePeripheral.addAttribute(bleImuChar); /* Now activate the BLE device. It will start continuously transmitting BLE advertising packets and will be visible to remote BLE central devices until it receives a new connection */ blePeripheral.begin(); + Serial.println("ImuBle Peripheral"); // Start the IMU CurieIMU.begin(); } @@ -56,7 +72,7 @@ void setup() void loop() { // listen for BLE peripherals to connect: - // Since we are a peripheral we need a central object to connect to + // since we are a peripheral we need a central object to connect to BLECentralHelper central = blePeripheral.central(); // if a central is connected to peripheral: @@ -69,8 +85,9 @@ void loop() Serial.print("IMU buffer size: "); Serial.println(sizeof(imuBuf)); - // turn on the LED to indicate the connection: - digitalWrite(13, HIGH); + // turn the LED on to indicate that the BLE peripheral (Arduino/Genuino 101) + // is connected to the central device + digitalWrite(LED_BUILTIN, HIGH); long currentMillis, sentTime; @@ -81,14 +98,14 @@ void loop() // Take IMU data every 100 msec if ((millis() - sentTime) >= 100) { - recordImuData(0); + recordImuData(IMU_BUF_INDEX); sentTime = millis(); - bleImuChar.setValue((unsigned char *)&(imuBuf[0]), sizeof(imuBuf)); + bleImuChar.setValue((unsigned char *)&(imuBuf[IMU_BUF_INDEX]), sizeof(imuBuf)); } } // end of while loop // when the central disconnects, turn off the LED: - digitalWrite(13, LOW); + digitalWrite(LED_BUILTIN, LOW); Serial.print("Disconnected from central: "); Serial.println(central.address()); } diff --git a/libraries/CurieBLE/examples/LED/LED.ino b/libraries/CurieBLE/examples/LED/LED.ino index 34c26a8c..828f1009 100644 --- a/libraries/CurieBLE/examples/LED/LED.ino +++ b/libraries/CurieBLE/examples/LED/LED.ino @@ -10,7 +10,7 @@ It does not use callbacks. In the loop it interogates the connection state with central. Checks if the characteristic is written and turns the LED on or off accordingly. To test interactively, use a phone app like nrf Controller (Android) or Light Blue (iOS). - Connect to BLE device named LEDCB and explore characteristic with UUID 19B10001-E8F2-537E-4F6C-D104768A1214. + Connect to BLE device named LED and explore characteristic with UUID 19B10001-E8F2-537E-4F6C-D104768A1214. Writing a byte value such as 0x40 should turn on the LED. Writing a byte value of 0x00 should turn off the LED. */ @@ -18,31 +18,43 @@ #include BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) -BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service -// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central +// BLE LED Service - custom 128-bit UUID +BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); + +// BLE LED Switch Characteristic - custom 128-bit UUID +// This characteristic has Read and Write properties that allow +// remote clients to read and write the characteristic value BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); const int ledPin = 13; // pin to use for the LED void setup() { + // initialize serial communication Serial.begin(9600); + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; // set LED pin to output mode pinMode(ledPin, OUTPUT); - // set advertised local name and service UUID: + // set the local name of this BLE peripheral device + // This name will appear in advertising packets + // and can be used by remote devices to identify this BLE device blePeripheral.setLocalName("LED"); + // set the UUID of the ledService that is advertised by the BLE peripheral device blePeripheral.setAdvertisedServiceUuid(ledService.uuid()); // add service and characteristic: blePeripheral.addAttribute(ledService); blePeripheral.addAttribute(switchCharacteristic); - // set the initial value for the characeristic: + // set the initial value for the characteristic: switchCharacteristic.setValue(0); - // begin advertising BLE service: + // begin advertising BLE ledService: blePeripheral.begin(); Serial.println("BLE LED Peripheral"); @@ -67,14 +79,14 @@ void loop() { Serial.println("LED on"); digitalWrite(ledPin, HIGH); // will turn the LED on } else { // a 0 value - Serial.println(F("LED off")); + Serial.println("LED off"); digitalWrite(ledPin, LOW); // will turn the LED off } } } // when the central disconnects, print it out: - Serial.print(F("Disconnected from central: ")); + Serial.print("Disconnected from central: "); Serial.println(central.address()); } } diff --git a/libraries/CurieBLE/examples/LEDCentral/LEDCentral.ino b/libraries/CurieBLE/examples/LEDCentral/LEDCentral.ino index e26e9451..86f1ff96 100644 --- a/libraries/CurieBLE/examples/LEDCentral/LEDCentral.ino +++ b/libraries/CurieBLE/examples/LEDCentral/LEDCentral.ino @@ -20,13 +20,17 @@ ble_conn_param_t conn_param = {30.0, // minimum interval in ms 7.5 - 4000 4000 // timeout in ms 100 - 32000ms }; -const int ledPin = 13; // set ledPin to use on-board LED +const int ledPin = 13; // set ledPin to use the on-board LED BLECentral bleCentral; // create central instance BLEPeripheralHelper *blePeripheral1 = NULL; // peer peripheral device -BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service with a 128-bit UUID (32 characters exclusive of dashes). - // Long UUID denote custom user created UUID -BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);// create switch characteristic and allow remote device to read and write +// create a new service with a 128-bit UUID (32 characters exclusive of dashes). +// Long UUID denote custom user created UUID +BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); + +// create switch characteristic with Read and Write properties to allow remote device +// to read and write this characteristic value +BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); // function prototype for function that determines if the advertising data is found bool adv_found(uint8_t type, @@ -36,8 +40,12 @@ bool adv_found(uint8_t type, void setup() { - Serial.begin(9600); - pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output + // initialize serial communication + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; + pinMode(ledPin, OUTPUT); // set the pin of the on-board LED as output // add service and characteristic bleCentral.addAttribute(ledService); @@ -47,14 +55,14 @@ void setup() bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler); bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler); - // advertise the service + // add adv_fund() function to get advertising packets bleCentral.setAdvertiseHandler(adv_found); // assign event handlers for characteristic switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten); bleCentral.begin(); - Serial.println(("Bluetooth device active, waiting for connections...")); + Serial.println("Bluetooth device active, waiting for connections..."); } void loop() diff --git a/libraries/CurieBLE/examples/MIDIBLE/MIDIBLE.ino b/libraries/CurieBLE/examples/MIDIBLE/MIDIBLE.ino index 7d78df27..18b8fac7 100644 --- a/libraries/CurieBLE/examples/MIDIBLE/MIDIBLE.ino +++ b/libraries/CurieBLE/examples/MIDIBLE/MIDIBLE.ino @@ -90,56 +90,66 @@ void noteOff(char chan, char note) //channel 1 BLEPeripheral midiDevice; // create peripheral instance -BLEService midiSvc("03B80E5A-EDE8-4B33-A751-6CE34EC4C700"); // create service +// create a new service with a custom 128-bit UUID +BLEService midiService("03B80E5A-EDE8-4B33-A751-6CE34EC4C700"); -// create switch characteristic and allow remote device to read and write -BLECharacteristic midiChar("7772E5DB-3868-4112-A1A9-F2669D106BF3", BLEWrite | BLEWriteWithoutResponse | BLENotify | BLERead, 5); +// create switch characteristic with Read, Write, Write without response and Notify properties +// to allow remote central devices to read and write characteristic +// and to get notifications when this characteristic changes +BLECharacteristic midiChar("7772E5DB-3868-4112-A1A9-F2669D106BF3", BLEWrite | BLEWriteWithoutResponse | BLENotify | BLERead, sizeof(midiData)); void setup() { + // initialize serial communication Serial.begin(9600); + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; BLESetup(); - Serial.println(("Bluetooth device active, waiting for connections...")); + Serial.println("Bluetooth device active, waiting for connections..."); } void loop() { - /*Simple randome note player to test MIDI output + /*Simple random note player to test MIDI output Plays random note every 400ms */ int note = random(0, 127); - //readMIDI(); + noteOn(0, note, 127); //loads up midiData buffer - midiChar.setValue(midiData, 5);//midiData); //posts 5 bytes + midiChar.setValue(midiData, sizeof(midiData)); // post 5 bytes delay(200); noteOff(0, note); - midiChar.setValue(midiData, 5);//midiData); //posts 5 bytes + midiChar.setValue(midiData, sizeof(midiData)); // post 5 bytes delay(200); } void BLESetup() { - // set the local name peripheral advertises + // Set the local name for this BLE device + // This name will appear in advertising packets + // and can be used by remote devices to identify this BLE device midiDevice.setLocalName("Auxren"); midiDevice.setDeviceName("Auxren"); // set the UUID for the service this peripheral advertises - midiDevice.setAdvertisedServiceUuid(midiSvc.uuid()); + midiDevice.setAdvertisedServiceUuid(midiService.uuid()); // add service and characteristic - midiDevice.addAttribute(midiSvc); + midiDevice.addAttribute(midiService); midiDevice.addAttribute(midiChar); // assign event handlers for connected, disconnected to peripheral midiDevice.setEventHandler(BLEConnected, midiDeviceConnectHandler); midiDevice.setEventHandler(BLEDisconnected, midiDeviceDisconnectHandler); - // assign event handlers for characteristic + // assign event handler for characteristic midiChar.setEventHandler(BLEWritten, midiCharacteristicWritten); - // set an initial value for the characteristic - midiChar.setValue(midiData, 5); + // set the initial value of midiChar characteristic + midiChar.setValue(midiData, sizeof(midiData)); - // advertise the service + // start advertising midiService midiDevice.begin(); } @@ -156,6 +166,19 @@ void midiDeviceDisconnectHandler(BLEHelper& central) { } void midiCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic) { - // central wrote new value to characteristic, update LED - Serial.print("Characteristic event, written: "); + // when central writes new data to midiChar characteristic, print the data received: + const unsigned char *newData = characteristic.value(); + unsigned int dataLength = characteristic.valueLength(); + if(dataLength == sizeof(midiData)) { + Serial.println("MIDI data written by central device:"); + for(uint8_t value = 0; value < dataLength; value++) { + Serial.print(newData[value], HEX); + Serial.print(" "); + // update midiData buffer + if(value >= 2) { + midiData[value] = newData[value]; + } + } + } + Serial.println(); } diff --git a/libraries/CurieBLE/examples/Scanning/Scanning.ino b/libraries/CurieBLE/examples/Scanning/Scanning.ino index 966bd3b4..bef4c9b2 100644 --- a/libraries/CurieBLE/examples/Scanning/Scanning.ino +++ b/libraries/CurieBLE/examples/Scanning/Scanning.ino @@ -34,13 +34,19 @@ bool adv_found(uint8_t type, void setup() { - Serial.begin(115200); // initialize serial communication + // initialize serial communication + Serial.begin(115200); + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; - /* Setup callback */ + // Setup adv_found() callback function for advertising packets bleCentral.setAdvertiseHandler(adv_found); - /* Now activate the BLE device. - It will start continuously scanning BLE advertising + /* Now activate the BLE central device. + It will start continuously scanning BLE advertising packets + sent by BLE peripheral devices */ bleCentral.begin(); Serial.println("Bluetooth device active, start scanning..."); @@ -52,7 +58,8 @@ void loop() delay(3000); Serial.print("\r\n\r\n\t\t\tScaning result\r\n \tMAC\t\t\t\tLocal Name\r\n"); Serial.print("-------------------------------------------------------------\r\n"); - + + // print MAC address and local name of new scanned devices for (int i = 0; i < list_index; i++) { diff --git a/libraries/CurieBLE/examples/UpdateConnectionInterval/UpdateConnectionInterval.ino b/libraries/CurieBLE/examples/UpdateConnectionInterval/UpdateConnectionInterval.ino index 852b8e3b..1b6f7d28 100644 --- a/libraries/CurieBLE/examples/UpdateConnectionInterval/UpdateConnectionInterval.ino +++ b/libraries/CurieBLE/examples/UpdateConnectionInterval/UpdateConnectionInterval.ino @@ -21,11 +21,14 @@ ble_conn_param_t conn_param = {30.0, // minimum interval in ms 7.5 - 4000 const int ledPin = 13; // set ledPin to use on-board LED BLECentral bleCentral; // create central instance -BLEPeripheralHelper *blePeripheral1 = NULL; // // peer peripheral device +BLEPeripheralHelper *blePeripheral1 = NULL; // peer peripheral device -BLEService batteryService("180F"); // create service with a 16-bit UUID -BLECharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify);// create switch characteristic -//and allow remote device to read and notify +// create a new service with a 16-bit UUID +BLEService batteryService("180F"); + +// create switch characteristic with Read and Notify properties +// to allow remote devices to read and notify the characteristic value +BLECharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify); bool adv_found(uint8_t type, const uint8_t *dataPtr, @@ -34,8 +37,13 @@ bool adv_found(uint8_t type, void setup() { + // initialize serial communication Serial.begin(9600); - + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while(!Serial) ; + // add service and characteristic bleCentral.addAttribute(batteryService); bleCentral.addAttribute(batteryLevelChar); @@ -43,16 +51,17 @@ void setup() // assign event handlers for connected, disconnected to central bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler); bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler); + // assign event handler to central device to update connection params bleCentral.setEventHandler(BLEUpdateParam, bleCentralUpdateParam); - // advertise the service + // add adv_fund() handler to get advertising packets bleCentral.setAdvertiseHandler(adv_found); - // assign event handlers for characteristic + // assign event handler for characteristic batteryLevelChar.setEventHandler(BLEWritten, switchCharacteristicWritten); bleCentral.begin(); - Serial.println(("Bluetooth device active, waiting for connections...")); + Serial.println("Bluetooth device active, waiting for connections..."); } void loop() From 2e6a3272510aaeeb962eb75327fe73ad81989446 Mon Sep 17 00:00:00 2001 From: Biagio Montaruli Date: Sun, 25 Sep 2016 18:55:16 +0200 Subject: [PATCH 4/4] Improve code formatting for sketches of CurieBLE library Update code formatting for sketches of CurieBLE library with the Auto Format tool of Arduino IDE Signed-off-by: Biagio Montaruli --- .../BatteryAdvChange/BatteryAdvChange.ino | 28 +- .../BatteryMonitor/BatteryMonitor.ino | 47 ++- .../CurieBLE/examples/ButtonLED/ButtonLED.ino | 27 +- .../examples/CallbackLED/CallbackLED.ino | 19 +- .../examples/IMUBleCentral/IMUBleCentral.ino | 228 +++++++------- .../IMUBleNotification/IMUBleNotification.ino | 169 +++++------ libraries/CurieBLE/examples/LED/LED.ino | 16 +- .../examples/LEDCentral/LEDCentral.ino | 211 ++++++------- .../CurieBLE/examples/MIDIBLE/MIDIBLE.ino | 41 ++- .../CurieBLE/examples/Scanning/Scanning.ino | 175 +++++------ .../UpdateConnectionInterval.ino | 280 ++++++++---------- 11 files changed, 587 insertions(+), 654 deletions(-) diff --git a/libraries/CurieBLE/examples/BatteryAdvChange/BatteryAdvChange.ino b/libraries/CurieBLE/examples/BatteryAdvChange/BatteryAdvChange.ino index 3ce1601f..f9cd1296 100644 --- a/libraries/CurieBLE/examples/BatteryAdvChange/BatteryAdvChange.ino +++ b/libraries/CurieBLE/examples/BatteryAdvChange/BatteryAdvChange.ino @@ -19,7 +19,7 @@ BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're pr BLEService batteryService("180F"); // BLE Battery Service // BLE Battery Level Characteristic with standard 16-bit characteristic UUID. -// This characteristic has Read and Notify properties that allow remote clients +// This characteristic has Read and Notify properties that allow remote clients // to get notifications when this characteristic changes BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify); @@ -27,16 +27,16 @@ int count = 0; bool change_discover = false; void setup() { - // initialize serial communication + // initialize serial communication Serial.begin(9600); // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // If you don't care to see text messages sent to the Serial Monitor during board initialization, // remove or comment out the next line - while(!Serial) ; - // initialize the LED on pin 13. When the BLE device will switch to connectable mode - // the on-board LED will be turned on, otherwise turn the on-board LED off + while (!Serial) ; + // initialize the LED on pin 13. When the BLE device will switch to connectable mode + // the on-board LED will be turned on, otherwise turn the on-board LED off pinMode(LED_BUILTIN, OUTPUT); - + /* 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 @@ -46,7 +46,7 @@ void setup() { blePeripheral.addAttribute(batteryService); // Add the BLE Battery service blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic - /* Now activate the BLE device. It will start continuously transmitting BLE + /* Now activate the BLE device. It will start continuously transmitting BLE advertising packets and will be visible to remote BLE central devices until it receives a new connection */ @@ -65,19 +65,17 @@ void loop() { Serial.println(count); } - delay (1000); + delay(1000); count++; // Switch from Connectable to Non Connectable and vice versa - if (count > 10 ) { + if (count > 10) { Serial.println("Stop Advertising and wait for 10 seconds. Device should be invisible"); // Some central devices (phones included) may cache previous scan informations. // Restart your central device and it should not see this peripheral once stopAdvertising() is called blePeripheral.stopAdvertising(); delay(10000); - if (change_discover) - { - + if (change_discover) { // Using the method setConnectable() we specify that it is now in NON connectable mode // The loop is for 10 seconds. Your central device may timeout later than that // and may eventually connect when we set it back to connectable mode below @@ -86,9 +84,7 @@ void loop() { // turn the on-board LED off digitalWrite(LED_BUILTIN, LOW); } - else - { - + else { // Switch to connectable mode by calling the setConnectable() method blePeripheral.setConnectable(true); Serial.println("In Connectable mode"); diff --git a/libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino b/libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino index 4492f7b0..b14a444e 100644 --- a/libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino +++ b/libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino @@ -1,6 +1,6 @@ /* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. */ #include @@ -18,7 +18,7 @@ BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're pr BLEService batteryService("180F"); // BLE Battery Service with standard 16-bit UUID // BLE Battery Level Characteristic with standard 16-bit characteristic UUID -// This characteristic has Read and Notify properties that allow remote clients +// This characteristic has Read and Notify properties that allow remote clients // to get notifications when this characteristic changes BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify); @@ -29,9 +29,9 @@ void setup() { // initialize serial communication Serial.begin(9600); // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // If you don't care to see text messages sent to the Serial Monitor during board initialization, // remove or comment out the next line - while(!Serial) ; + while (!Serial) ; // initialize the LED on pin 13 to indicate when a central is connected pinMode(LED_BUILTIN, OUTPUT); @@ -73,12 +73,12 @@ void loop() { previousMillis = currentMillis; updateBatteryLevel(); - static unsigned short count = 0; + static unsigned short count = 0; count++; // update the connection interval - if(count%5 == 0){ + if (count % 5 == 0) { delay(1000); - updateIntervalParams(central); + updateIntervalParams(central); } } } @@ -90,9 +90,8 @@ void loop() { } void updateBatteryLevel() { - /* Read the current voltage level on the A0 analog input pin. - This is used here to simulate the charge level of a battery. - */ + // Read the current voltage level on the A0 analog input pin. + // This is used here to simulate the charge level of a battery. int battery = analogRead(A0); int batteryLevel = map(battery, 0, 1023, 0, 100); @@ -110,24 +109,24 @@ void updateIntervalParams(BLECentralHelper ¢ral) { 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 ); - + 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); + central.setConnectionInterval(interval, interval); interval++; - if(interval<0x06) + if (interval < 0x06) + interval = 0x06; + if (interval > 0x100) interval = 0x06; - if(interval>0x100) - interval = 0x06; } /* Copyright (c) 2016 Intel Corporation. All rights reserved. diff --git a/libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino b/libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino index 070c8e72..f814ace0 100644 --- a/libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino +++ b/libraries/CurieBLE/examples/ButtonLED/ButtonLED.ino @@ -1,6 +1,6 @@ /* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. */ /* @@ -11,7 +11,7 @@ After the sketch starts connect to a BLE app on a phone and set notification to the Characteristic and you should see it update whenever the button is pressed. This sketch is not written to pair with any of the central examples. */ - + #include const int ledPin = 13; // set ledPin to on-board LED @@ -19,16 +19,16 @@ const int buttonPin = 4; // set buttonPin to digital pin 4 // create peripheral instance BLEPeripheral blePeripheral; - + // create a new service with a 128-bit UUID (32 characters exclusive of dashes). // Long UUID denote custom user created UUID BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); -// create switch characteristic with Read and Write properties that allow remote clients +// create switch characteristic with Read and Write properties that allow remote clients // to read and write this characteristic value BLECharCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); -// create button characteristic with Read and Notify properties that allow remote clients +// create button characteristic with Read and Notify properties that allow remote clients // to get notifications when this characteristic value changes BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // Note use of Typed Characteristics. These previous 2 characeristics are of the type char @@ -37,14 +37,14 @@ void setup() { // initialize serial communication Serial.begin(9600); // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // If you don't care to see text messages sent to the Serial Monitor during board initialization, // remove or comment out the next line - while(!Serial) ; + while (!Serial) ; pinMode(ledPin, OUTPUT); // set the LED pin 13 as output pinMode(buttonPin, INPUT); // set the button pin 4 as input - // Set a local name for the BLE device. - // This name will appear in advertising packets + // 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 blePeripheral.setLocalName("ButtonLED"); // set the UUID for the service this peripheral advertises: @@ -83,13 +83,14 @@ void loop() { if (ledCharacteristic.written() || buttonChanged) { // update LED, either central has written to characteristic or button state has changed. - // If you are using a phone or a BLE central device that is aware of this characteristic, + // If you are using a phone or a BLE central device that is aware of this characteristic, // writing a value of 0x40 for example will be interpreted as written and the LED will be turned on if (ledCharacteristic.value()) { Serial.println("LED on"); digitalWrite(ledPin, HIGH); - } else { - // If central writes a 0 value (0x00) then it is interpreted as no value and turns the LED off + } + else { + // If central writes a 0 value (0x00) then it is interpreted as no value and turns the LED off Serial.println("LED off"); digitalWrite(ledPin, LOW); } diff --git a/libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino b/libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino index 98d53ce4..826fae4e 100644 --- a/libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino +++ b/libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino @@ -1,9 +1,9 @@ /* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. */ - - /* + +/* This example can work with LEDCentral. You should see the LED blink on and off. @@ -13,13 +13,13 @@ Connect to BLE device named LEDCB and explore characteristic with UUID 19B10001-E8F2-537E-4F6C-D104768A1214. Writing a byte value such as 0x40 should turn on the LED. Writing a byte value of 0x00 should turn off the LED. - */ +*/ #include const int ledPin = 13; // set ledPin to use on-board LED BLEPeripheral blePeripheral; // create peripheral instance -BLECentralHelper *bleCentral1 = NULL; // peer central device +BLECentralHelper *bleCentral1 = NULL; // peer central device // create service with a 128-bit UUID (32 characters exclusive of dashes). // Long UUID denote custom user created UUID. @@ -33,9 +33,9 @@ void setup() { // initialize serial communication Serial.begin(9600); // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // If you don't care to see text messages sent to the Serial Monitor during board initialization, // remove or comment out the next line - while(!Serial) ; + while (!Serial) ; // set the pin 13 of the on-board LED as output pinMode(ledPin, OUTPUT); @@ -94,7 +94,8 @@ void switchCharacteristicWritten(BLEHelper& central, BLECharacteristic& characte if (switchChar.value()) { Serial.println("LED on"); digitalWrite(ledPin, HIGH); - } else { + } + else { Serial.println("LED off"); digitalWrite(ledPin, LOW); } diff --git a/libraries/CurieBLE/examples/IMUBleCentral/IMUBleCentral.ino b/libraries/CurieBLE/examples/IMUBleCentral/IMUBleCentral.ino index 6bed902e..bfd66688 100644 --- a/libraries/CurieBLE/examples/IMUBleCentral/IMUBleCentral.ino +++ b/libraries/CurieBLE/examples/IMUBleCentral/IMUBleCentral.ino @@ -1,6 +1,6 @@ /* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. */ #include @@ -18,18 +18,18 @@ ble_conn_param_t conn_param = {30.0, // minimum interval in ms 7.5 - 4000 50.0, // maximum interval in ms 7.5 - - 0, // latency + 0, // latency 4000 // timeout in ms 100 - 32000ms - }; -// define a structure that will serve as buffer for holding IMU data + }; +// define a structure that will serve as buffer for holding IMU data typedef struct { - int index; - unsigned int slot[3]; + int index; + unsigned int slot[3]; } imuFrameType; imuFrameType imuBuf[MAX_IMU_RECORD]; -BLECentral bleCentral; // BLE Central Device (the board you're programming) +BLECentral bleCentral; // BLE Central Device (the board you're programming) // create a new service with a custom 128-bit UUID BLEService bleImuService("F7580001-153E-D4F6-F26D-43D8D98EEB13"); @@ -49,133 +49,123 @@ bool adv_found(uint8_t type, uint8_t data_len, const bt_addr_le_t *addrPtr); -void setup() -{ - // initialize serial communication and set the baud rate to 115200 bps. - // This is set to higher baud rate because accelerometer data changes very quickly - Serial.begin(115200); - // wait for the Serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, - // remove or comment out the next line - while(!Serial) ; - // initialize the LED on pin 13 to indicate when a central is connected - pinMode(LED_BUILTIN, OUTPUT); - - // set the event handeler function for the bleImuChar characteristic - bleImuChar.setEventHandler(BLEWritten, bleImuCharacteristicWritten); - - bleCentral.addAttribute(bleImuService); // Add the BLE IMU service - bleCentral.addAttribute(bleImuChar); // Add the BLE IMU characteristic - - // Setup callback whenever a Peripheral advertising data is found - bleCentral.setAdvertiseHandler(adv_found); - bleCentral.setEventHandler(BLEConnected, ble_connected); - bleCentral.setEventHandler(BLEDisconnected, ble_disconnected); - - /* Now activate the BLE device. It will start continuously transmitting BLE - advertising packets and will be visible to remote BLE central devices - until it receives a new connection */ - bleCentral.begin(); +void setup() { + // initialize serial communication and set the baud rate to 115200 bps. + // This is set to higher baud rate because accelerometer data changes very quickly + Serial.begin(115200); + // wait for the Serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while (!Serial) ; + // initialize the LED on pin 13 to indicate when a central is connected + pinMode(LED_BUILTIN, OUTPUT); + + // set the event handeler function for the bleImuChar characteristic + bleImuChar.setEventHandler(BLEWritten, bleImuCharacteristicWritten); + + bleCentral.addAttribute(bleImuService); // Add the BLE IMU service + bleCentral.addAttribute(bleImuChar); // Add the BLE IMU characteristic + + // Setup callback whenever a Peripheral advertising data is found + bleCentral.setAdvertiseHandler(adv_found); + bleCentral.setEventHandler(BLEConnected, ble_connected); + bleCentral.setEventHandler(BLEDisconnected, ble_disconnected); + + /* Now activate the BLE device. It will start continuously transmitting BLE + advertising packets and will be visible to remote BLE central devices + until it receives a new connection */ + bleCentral.begin(); } -void loop() -{ - // we put a 2 second delay - // Even though this looks empty, since we setup 2 callbacks by setting the advertising handler adv_found - // and event handler for BLEConnected, we basically are lsitening for advertising data and connected events. - - delay(2000); +void loop() { + // we put a 2 seconds delay + // Even though this looks empty, since we setup 2 callbacks by setting the advertising handler adv_found + // and event handler for BLEConnected, we basically are lsitening for advertising data and connected events. + + delay(2000); } -void ble_connected(BLEHelper &role) -{ - // since we are a central device we create a BLEPeripheralHelper peripheral - BLEPeripheralHelper *peripheral = bleCentral.getPeerPeripheralBLE(role); - Serial.print("Connected to peripheral "); - // print MAC Address of peripheral device - Serial.println(peripheral->address()); - // the BLE central device is connected to peripheral so turn the on-board LED on - digitalWrite(LED_BUILTIN, HIGH); - - // Start discovery the profiles in peripheral device - peripheral->discover(); +void ble_connected(BLEHelper &role) { + // since we are a central device we create a BLEPeripheralHelper peripheral + BLEPeripheralHelper *peripheral = bleCentral.getPeerPeripheralBLE(role); + Serial.print("Connected to peripheral "); + // print MAC Address of peripheral device + Serial.println(peripheral->address()); + // the BLE central device is connected to peripheral so turn the on-board LED on + digitalWrite(LED_BUILTIN, HIGH); + + // Start discovery the profiles in peripheral device + peripheral->discover(); } -void ble_disconnected(BLEHelper &peripheral) -{ - Serial.print("Disconnected form peripheral "); - // print MAC Address of peripheral device - Serial.println(peripheral.address()); - // if peripheral disconnects from the BLE central device turn the on-board LED off - digitalWrite(LED_BUILTIN, LOW); +void ble_disconnected(BLEHelper &peripheral) { + Serial.print("Disconnected form peripheral "); + // print MAC Address of peripheral device + Serial.println(peripheral.address()); + // if peripheral disconnects from the BLE central device turn the on-board LED off + digitalWrite(LED_BUILTIN, LOW); } -void bleImuCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic) -{ - // Peripheral wrote new value to characteristic by Notification/Indication - // We have to use pointers because we are NOT using a type characteristic - // In other examples our charcteristics are typed so we not have to use pointers and can access the value directly - // The parent non typde characteristic class, the value method gives a pointer to the characteristic value - - const unsigned char *cvalue = characteristic.value(); - const imuFrameType *value = (const imuFrameType *)cvalue; - Serial.print("\r\nCharacteristic event, written: "); - Serial.print(value->index); - Serial.print("\t"); - Serial.print(value->slot[0]); - Serial.print("\t"); - Serial.print(value->slot[1]); - Serial.print("\t"); - Serial.println(value->slot[2]); +void bleImuCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic) { + // Peripheral wrote new value to characteristic by Notification/Indication + // We have to use pointers because we are NOT using a type characteristic + // In other examples our charcteristics are typed so we not have to use pointers and can access the value directly + // The parent non typde characteristic class, the value method gives a pointer to the characteristic value + + const unsigned char *cvalue = characteristic.value(); + const imuFrameType *value = (const imuFrameType *)cvalue; + Serial.print("\r\nCharacteristic event, written: "); + Serial.print(value->index); + Serial.print("\t"); + Serial.print(value->slot[0]); + Serial.print("\t"); + Serial.print(value->slot[1]); + Serial.print("\t"); + Serial.println(value->slot[2]); } -bool adv_found(uint8_t type, - const uint8_t *dataPtr, +bool adv_found(uint8_t type, + const uint8_t *dataPtr, uint8_t data_len, const bt_addr_le_t *addrPtr) { - int i; - - Serial.print("[AD]:"); - Serial.print(type); - Serial.print(" data_len "); - Serial.println(data_len); - // Please see https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile - // To decode the data the central device cares. - // This example use UUID as identity. - switch (type) - { - case BT_DATA_UUID128_SOME: - case BT_DATA_UUID128_ALL: - { - if (data_len % UUID_SIZE_128 != 0) - { - Serial.println("AD malformed"); - return true; - } - for (i = 0; i < data_len; i += UUID_SIZE_128) - { - if (bleImuService.uuidCompare(dataPtr + i, UUID_SIZE_128) == false) - { - continue; - } - - // Accept the advertisement - if (!bleCentral.stopScan()) - { - Serial.println("Stop LE scan failed"); - continue; - } - Serial.println("Connecting"); - // Connect to peripheral - bleCentral.connect(addrPtr, &conn_param); - return false; - } + int i; + + Serial.print("[AD]:"); + Serial.print(type); + Serial.print(" data_len "); + Serial.println(data_len); + // Please see https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile + // To decode the data the central device cares. + // This example use UUID as identity. + switch (type) { + case BT_DATA_UUID128_SOME: + case BT_DATA_UUID128_ALL: + { + if (data_len % UUID_SIZE_128 != 0) { + Serial.println("AD malformed"); + return true; + } + for (i = 0; i < data_len; i += UUID_SIZE_128) { + if (bleImuService.uuidCompare(dataPtr + i, UUID_SIZE_128) == false) { + continue; + } + + // Accept the advertising data + if (!bleCentral.stopScan()) { + Serial.println("Stop LE scan failed"); + continue; + } + Serial.println("Connecting"); + // Connect to peripheral + bleCentral.connect(addrPtr, &conn_param); + return false; } - } + } + } - return true; + return true; } /* diff --git a/libraries/CurieBLE/examples/IMUBleNotification/IMUBleNotification.ino b/libraries/CurieBLE/examples/IMUBleNotification/IMUBleNotification.ino index a8eedc5e..1d105338 100644 --- a/libraries/CurieBLE/examples/IMUBleNotification/IMUBleNotification.ino +++ b/libraries/CurieBLE/examples/IMUBleNotification/IMUBleNotification.ino @@ -1,24 +1,24 @@ /* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. */ #include #include /* - This sketch example works with IMUBleCentral.ino. + This sketch example works with IMUBleCentral.ino. - This sketch will read IMU data from sensor and send notifications to IMUBleCentral.ino. - IMUBleCentral.ino will receive the notifications and output the received data. + This sketch will read IMU data from sensor and send notifications to IMUBleCentral.ino. + IMUBleCentral.ino will receive the notifications and output the received data. */ #define MAX_IMU_RECORD 1 #define IMU_BUF_INDEX 0 typedef struct { - int index; - unsigned int slot[3]; + int index; + unsigned int slot[3]; } imuFrameType; // Buffer to hold IMU data @@ -26,106 +26,99 @@ imuFrameType imuBuf[MAX_IMU_RECORD]; unsigned int seqNum = 0; -BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) +BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) // create a new service with a custom 128-bit UUID BLEService bleImuService("F7580001-153E-D4F6-F26D-43D8D98EEB13"); // standard 128-bit characteristic UUID with Read and Notify properties that allow // remote clients to get notifications when this characteristic changes -BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", +BLECharacteristic bleImuChar("F7580003-153E-D4F6-F26D-43D8D98EEB13", BLERead | BLENotify, sizeof(imuBuf)); -void setup() -{ - // initialize serial communication - Serial.begin(9600); - // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, - // remove or comment out the next line - while(!Serial) ; - // initialize the LED on pin 13 to indicate when a central is connected - pinMode(LED_BUILTIN, OUTPUT); +void setup() { + // initialize serial communication + Serial.begin(9600); + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while (!Serial) ; + // initialize the LED on pin 13 to indicate when a central is connected + pinMode(LED_BUILTIN, OUTPUT); - /* Set a local name for the BLE device + /* 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 */ - blePeripheral.setLocalName("ImuBle"); - // add the 128-bit UUID of bleImuService - blePeripheral.setAdvertisedServiceUuid(bleImuService.uuid()); - // add the bleImuService - blePeripheral.addAttribute(bleImuService); - // add the bleImuChar characteristic - blePeripheral.addAttribute(bleImuChar); - - /* Now activate the BLE device. It will start continuously transmitting BLE + blePeripheral.setLocalName("ImuBle"); + // add the 128-bit UUID of bleImuService + blePeripheral.setAdvertisedServiceUuid(bleImuService.uuid()); + // add the bleImuService + blePeripheral.addAttribute(bleImuService); + // add the bleImuChar characteristic + blePeripheral.addAttribute(bleImuChar); + + /* Now activate the BLE device. It will start continuously transmitting BLE advertising packets and will be visible to remote BLE central devices until it receives a new connection */ - blePeripheral.begin(); - Serial.println("ImuBle Peripheral"); - // Start the IMU - CurieIMU.begin(); + blePeripheral.begin(); + Serial.println("ImuBle Peripheral"); + // Start the IMU + CurieIMU.begin(); } -void loop() -{ - // listen for BLE peripherals to connect: - // since we are a peripheral we need a central object to connect to - BLECentralHelper central = blePeripheral.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()); - - Serial.print("IMU buffer size: "); - Serial.println(sizeof(imuBuf)); - - // turn the LED on to indicate that the BLE peripheral (Arduino/Genuino 101) - // is connected to the central device - digitalWrite(LED_BUILTIN, HIGH); - - long currentMillis, sentTime; - - // Send IMU data as long as the central is still connected - currentMillis = sentTime = millis(); - while (central.connected()) - { - // Take IMU data every 100 msec - if ((millis() - sentTime) >= 100) - { - recordImuData(IMU_BUF_INDEX); - sentTime = millis(); - bleImuChar.setValue((unsigned char *)&(imuBuf[IMU_BUF_INDEX]), sizeof(imuBuf)); - } - } // end of while loop - - // when the central disconnects, turn off the LED: - digitalWrite(LED_BUILTIN, LOW); - Serial.print("Disconnected from central: "); - Serial.println(central.address()); - } +void loop() { + // listen for BLE peripherals to connect: + // since we are a peripheral we need a central object to connect to + BLECentralHelper central = blePeripheral.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()); + + Serial.print("IMU buffer size: "); + Serial.println(sizeof(imuBuf)); + + // turn the LED on to indicate that the BLE peripheral (Arduino/Genuino 101) + // is connected to the central device + digitalWrite(LED_BUILTIN, HIGH); + + long currentMillis, sentTime; + + // Send IMU data as long as the central is still connected + currentMillis = sentTime = millis(); + while (central.connected()) { + // Take IMU data every 100 msec + if ((millis() - sentTime) >= 100) { + recordImuData(IMU_BUF_INDEX); + sentTime = millis(); + bleImuChar.setValue((unsigned char *) & (imuBuf[IMU_BUF_INDEX]), sizeof(imuBuf)); + } + } // end of while loop + + // when the central disconnects, turn off the LED: + digitalWrite(LED_BUILTIN, LOW); + Serial.print("Disconnected from central: "); + Serial.println(central.address()); + } } -// This function records the IMU data that we send to the central -void recordImuData(int index) -{ - /* Read IMU data. - */ - int ax, ay, az; - int gx, gy, gz; - - imuBuf[index].index = seqNum++; - CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz); - - // Encode the data into the buffer - imuBuf[index].slot[0] = (unsigned int)((ax << 16) | (ay & 0x0FFFF)); - imuBuf[index].slot[1] = (unsigned int)((az << 16) | (gx & 0x0FFFF)); - imuBuf[index].slot[2] = (unsigned int)((gy << 16) | (gz & 0x0FFFF)); +// This function records the IMU data that we send to the central +void recordImuData(int index) { + // Read IMU data. + int ax, ay, az; + int gx, gy, gz; + + imuBuf[index].index = seqNum++; + CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz); + + // Encode the data into the buffer + imuBuf[index].slot[0] = (unsigned int)((ax << 16) | (ay & 0x0FFFF)); + imuBuf[index].slot[1] = (unsigned int)((az << 16) | (gx & 0x0FFFF)); + imuBuf[index].slot[2] = (unsigned int)((gy << 16) | (gz & 0x0FFFF)); } diff --git a/libraries/CurieBLE/examples/LED/LED.ino b/libraries/CurieBLE/examples/LED/LED.ino index 828f1009..14e7b03c 100644 --- a/libraries/CurieBLE/examples/LED/LED.ino +++ b/libraries/CurieBLE/examples/LED/LED.ino @@ -1,9 +1,9 @@ /* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. */ - - /* + +/* This example can work with LEDCentral This example is similar to CallbackLED example in functionality. @@ -13,8 +13,8 @@ Connect to BLE device named LED and explore characteristic with UUID 19B10001-E8F2-537E-4F6C-D104768A1214. Writing a byte value such as 0x40 should turn on the LED. Writing a byte value of 0x00 should turn off the LED. - */ - +*/ + #include BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) @@ -33,9 +33,9 @@ void setup() { // initialize serial communication Serial.begin(9600); // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // If you don't care to see text messages sent to the Serial Monitor during board initialization, // remove or comment out the next line - while(!Serial) ; + while (!Serial) ; // set LED pin to output mode pinMode(ledPin, OUTPUT); diff --git a/libraries/CurieBLE/examples/LEDCentral/LEDCentral.ino b/libraries/CurieBLE/examples/LEDCentral/LEDCentral.ino index 86f1ff96..f03651dd 100644 --- a/libraries/CurieBLE/examples/LEDCentral/LEDCentral.ino +++ b/libraries/CurieBLE/examples/LEDCentral/LEDCentral.ino @@ -1,11 +1,11 @@ /* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. */ #include -/* +/* This example can work with CallbackLED and LED sketches. To show how a central device can do charcteristic read and write operations. @@ -16,9 +16,9 @@ ble_conn_param_t conn_param = {30.0, // minimum interval in ms 7.5 - 4000 50.0, // maximum interval in ms 7.5 - - 0, // latency + 0, // latency 4000 // timeout in ms 100 - 32000ms - }; + }; const int ledPin = 13; // set ledPin to use the on-board LED BLECentral bleCentral; // create central instance @@ -28,7 +28,7 @@ BLEPeripheralHelper *blePeripheral1 = NULL; // peer peripheral device // Long UUID denote custom user created UUID BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); -// create switch characteristic with Read and Write properties to allow remote device +// create switch characteristic with Read and Write properties to allow remote device // to read and write this characteristic value BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); @@ -38,134 +38,119 @@ bool adv_found(uint8_t type, uint8_t data_len, const bt_addr_le_t *addrPtr); -void setup() -{ - // initialize serial communication - // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, - // remove or comment out the next line - while(!Serial) ; - pinMode(ledPin, OUTPUT); // set the pin of the on-board LED as output +void setup() { + // initialize serial communication + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while (!Serial) ; + pinMode(ledPin, OUTPUT); // set the pin of the on-board LED as output - // add service and characteristic - bleCentral.addAttribute(ledService); - bleCentral.addAttribute(switchChar); + // add service and characteristic + bleCentral.addAttribute(ledService); + bleCentral.addAttribute(switchChar); - // assign event handlers for connected, disconnected to central - bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler); - bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler); + // assign event handlers for connected, disconnected to central + bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler); + bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler); - // add adv_fund() function to get advertising packets - bleCentral.setAdvertiseHandler(adv_found); + // add adv_fund() function to get advertising packets + bleCentral.setAdvertiseHandler(adv_found); - // assign event handlers for characteristic - switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten); + // assign event handlers for characteristic + switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten); - bleCentral.begin(); - Serial.println("Bluetooth device active, waiting for connections..."); + bleCentral.begin(); + Serial.println("Bluetooth device active, waiting for connections..."); } -void loop() -{ - static unsigned int counter = 0; - static char ledstate = 0; - delay(2000); - - if (blePeripheral1) - { - counter++; - if (counter % 3) - { - switchChar.read(*blePeripheral1); - } - else - { - ledstate = !ledstate; - switchChar.write(*blePeripheral1, ledstate); - } +void loop() { + static unsigned int counter = 0; + static char ledstate = 0; + delay(2000); + + if (blePeripheral1) { + counter++; + if (counter % 3) { + switchChar.read(*blePeripheral1); + } + else { + ledstate = !ledstate; + switchChar.write(*blePeripheral1, ledstate); } + } } -void bleCentralConnectHandler(BLEHelper& peripheral) -{ - // peripheral connected event handler - blePeripheral1 = bleCentral.getPeerPeripheralBLE(peripheral); - Serial.print("Connected event, peripheral: "); - Serial.println(blePeripheral1->address()); - // Start discovery the profiles in peripheral device - blePeripheral1->discover(); +void bleCentralConnectHandler(BLEHelper& peripheral) { + // peripheral connected event handler + blePeripheral1 = bleCentral.getPeerPeripheralBLE(peripheral); + Serial.print("Connected event, peripheral: "); + Serial.println(blePeripheral1->address()); + // Start discovery the profiles in peripheral device + blePeripheral1->discover(); } -void bleCentralDisconnectHandler(BLEHelper& peripheral) -{ - // peripheral disconnected event handler - blePeripheral1 = NULL; - Serial.print("Disconnected event, peripheral: "); - Serial.println(peripheral.address()); - bleCentral.startScan(); +void bleCentralDisconnectHandler(BLEHelper& peripheral) { + // peripheral disconnected event handler + blePeripheral1 = NULL; + Serial.print("Disconnected event, peripheral: "); + Serial.println(peripheral.address()); + bleCentral.startScan(); } -void switchCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic) -{ - // Read response/Notification 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); - } +void switchCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic) { + // Read response/Notification 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); + } } -bool adv_found(uint8_t type, - const uint8_t *dataPtr, +bool adv_found(uint8_t type, + const uint8_t *dataPtr, uint8_t data_len, const bt_addr_le_t *addrPtr) { - int i; - - Serial.print("[AD]:"); - Serial.print(type); - Serial.print(" data_len "); - Serial.println(data_len); - - switch (type) - { - case BT_DATA_UUID128_SOME: - case BT_DATA_UUID128_ALL: - { - if (data_len % UUID_SIZE_128 != 0) - { - Serial.println("AD malformed"); - return true; - } - for (i = 0; i < data_len; i += UUID_SIZE_128) - { - if (ledService.uuidCompare(dataPtr + i, UUID_SIZE_128) == false) - { - continue; - } - - // Accept the advertisement - if (!bleCentral.stopScan()) - { - Serial.println("Stop LE scan failed"); - continue; - } - Serial.println("Connecting"); - // Connect to peripheral - bleCentral.connect(addrPtr, &conn_param); - return false; - } + int i; + + Serial.print("[AD]:"); + Serial.print(type); + Serial.print(" data_len "); + Serial.println(data_len); + + switch (type) { + case BT_DATA_UUID128_SOME: + case BT_DATA_UUID128_ALL: + { + if (data_len % UUID_SIZE_128 != 0) { + Serial.println("AD malformed"); + return true; } - } + for (i = 0; i < data_len; i += UUID_SIZE_128) { + if (ledService.uuidCompare(dataPtr + i, UUID_SIZE_128) == false) { + continue; + } + + // Accept the advertising data + if (!bleCentral.stopScan()) { + Serial.println("Stop LE scan failed"); + continue; + } + Serial.println("Connecting"); + // Connect to peripheral + bleCentral.connect(addrPtr, &conn_param); + return false; + } + } + } - return true; + return true; } /* diff --git a/libraries/CurieBLE/examples/MIDIBLE/MIDIBLE.ino b/libraries/CurieBLE/examples/MIDIBLE/MIDIBLE.ino index 18b8fac7..bf40dc35 100644 --- a/libraries/CurieBLE/examples/MIDIBLE/MIDIBLE.ino +++ b/libraries/CurieBLE/examples/MIDIBLE/MIDIBLE.ino @@ -2,7 +2,7 @@ America's Greatest Makers with help from Intel. MIDI over BLE info from: https://developer.apple.com/bluetooth/Apple-Bluetooth-Low-Energy-MIDI-Specification.pdf This sketch is not written to pair with any of the central examples. - + This sketch plays a random MIDI note (between 0 and 127) every 400ms. For a 'smarter' sketch, check out my Airpeggiator example. The Airpeggiator uses the Curie's IMU to allow you to play @@ -26,8 +26,8 @@ Towards the bottom of advanced, you will see 'Bluetooth MIDI devices'. You should see your Arduino 101 advertising in the list. Connect to your device and it should be available to all other iOS MIDI apps you have. - - If you do not have iOS, you can still use a BLE app on Android and just subscribe + + If you do not have iOS, you can still use a BLE app on Android and just subscribe to the midiChar charcteristic and see the updates. To send data, you use the following line: char.setValue(d, n); where char is @@ -62,17 +62,17 @@ */ #include -#define TXRX_BUF_LEN 20 //max number of bytes -#define RX_BUF_LEN 20 //max number of bytes +#define TXRX_BUF_LEN 20 // max number of bytes +#define RX_BUF_LEN 20 // max number of bytes uint8_t rx_buf[RX_BUF_LEN]; int rx_buf_num, rx_state = 0; uint8_t rx_temp_buf[20]; uint8_t outBufMidi[128]; -//Buffer to hold 5 bytes of MIDI data. Note the timestamp is forced +// Buffer to hold 5 bytes of MIDI data. Note the timestamp is forced uint8_t midiData[] = {0x80, 0x80, 0x00, 0x00, 0x00}; -//Loads up buffer with values for note On +// Loads up buffer with values for note On void noteOn(char chan, char note, char vel) //channel 1 { midiData[2] = 0x90 + chan; @@ -80,8 +80,8 @@ void noteOn(char chan, char note, char vel) //channel 1 midiData[4] = vel; } -//Loads up buffer with values for note Off -void noteOff(char chan, char note) //channel 1 +// Loads up buffer with values for note Off +void noteOff(char chan, char note) // channel 1 { midiData[2] = 0x80 + chan; midiData[3] = note; @@ -93,8 +93,8 @@ BLEPeripheral midiDevice; // create peripheral instance // create a new service with a custom 128-bit UUID BLEService midiService("03B80E5A-EDE8-4B33-A751-6CE34EC4C700"); -// create switch characteristic with Read, Write, Write without response and Notify properties -// to allow remote central devices to read and write characteristic +// create switch characteristic with Read, Write, Write without response and Notify properties +// to allow remote central devices to read and write characteristic // and to get notifications when this characteristic changes BLECharacteristic midiChar("7772E5DB-3868-4112-A1A9-F2669D106BF3", BLEWrite | BLEWriteWithoutResponse | BLENotify | BLERead, sizeof(midiData)); @@ -102,9 +102,9 @@ void setup() { // initialize serial communication Serial.begin(9600); // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // If you don't care to see text messages sent to the Serial Monitor during board initialization, // remove or comment out the next line - while(!Serial) ; + while (!Serial) ; BLESetup(); Serial.println("Bluetooth device active, waiting for connections..."); @@ -112,12 +112,11 @@ void setup() { void loop() { - /*Simple random note player to test MIDI output - Plays random note every 400ms - */ + // Simple random note player to test MIDI output. + // Plays random note every 400ms int note = random(0, 127); - - noteOn(0, note, 127); //loads up midiData buffer + + noteOn(0, note, 127); // loads up midiData buffer midiChar.setValue(midiData, sizeof(midiData)); // post 5 bytes delay(200); noteOff(0, note); @@ -169,13 +168,13 @@ void midiCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteri // when central writes new data to midiChar characteristic, print the data received: const unsigned char *newData = characteristic.value(); unsigned int dataLength = characteristic.valueLength(); - if(dataLength == sizeof(midiData)) { + if (dataLength == sizeof(midiData)) { Serial.println("MIDI data written by central device:"); - for(uint8_t value = 0; value < dataLength; value++) { + for (uint8_t value = 0; value < dataLength; value++) { Serial.print(newData[value], HEX); Serial.print(" "); // update midiData buffer - if(value >= 2) { + if (value >= 2) { midiData[value] = newData[value]; } } diff --git a/libraries/CurieBLE/examples/Scanning/Scanning.ino b/libraries/CurieBLE/examples/Scanning/Scanning.ino index bef4c9b2..12a6b1bd 100644 --- a/libraries/CurieBLE/examples/Scanning/Scanning.ino +++ b/libraries/CurieBLE/examples/Scanning/Scanning.ino @@ -1,6 +1,6 @@ /* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. */ #include @@ -17,126 +17,113 @@ const int bleScanMaxCnt = 5; -typedef struct{ - char macaddr[32]; // BLE MAC address. - char loacalname[22]; // Device's name -}ble_device_info_t; +typedef struct { + char macaddr[32]; // BLE MAC address. + char loacalname[22]; // Device's name +} ble_device_info_t; ble_device_info_t device_list[bleScanMaxCnt]; uint8_t list_index = 0; -BLECentral bleCentral; // BLE Central Device (the board you're programming) +BLECentral bleCentral; // BLE Central Device (the board you're programming) bool adv_found(uint8_t type, const uint8_t *dataPtr, uint8_t data_len, const bt_addr_le_t *addrPtr); -void setup() -{ - // initialize serial communication - Serial.begin(115200); - // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, - // remove or comment out the next line - while(!Serial) ; - - // Setup adv_found() callback function for advertising packets - bleCentral.setAdvertiseHandler(adv_found); - - /* Now activate the BLE central device. - It will start continuously scanning BLE advertising packets - sent by BLE peripheral devices - */ - bleCentral.begin(); - Serial.println("Bluetooth device active, start scanning..."); +void setup() { + // initialize serial communication + Serial.begin(115200); + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while (!Serial) ; + + // Setup adv_found() callback function for advertising packets + bleCentral.setAdvertiseHandler(adv_found); + + /* Now activate the BLE central device. + It will start continuously scanning BLE advertising packets + sent by BLE peripheral devices + */ + bleCentral.begin(); + Serial.println("Bluetooth device active, start scanning..."); } -void loop() -{ - // Output the scanned device per 3s - delay(3000); - Serial.print("\r\n\r\n\t\t\tScaning result\r\n \tMAC\t\t\t\tLocal Name\r\n"); - Serial.print("-------------------------------------------------------------\r\n"); - - // print MAC address and local name of new scanned devices - for (int i = 0; i < list_index; i++) - { - - Serial.print(device_list[i].macaddr); - Serial.print(" | "); - Serial.println(device_list[i].loacalname); - } - if (list_index == 0) - { - Serial.print("No device found\r\n"); - } - Serial.print("-------------------------------------------------------------\r\n"); - adv_list_clear(); +void loop() { + // Output the scanned device per 3s + delay(3000); + Serial.print("\r\n\r\n\t\t\tScaning result\r\n \tMAC\t\t\t\tLocal Name\r\n"); + Serial.print("-------------------------------------------------------------\r\n"); + + // print MAC address and local name of new scanned devices + for (int i = 0; i < list_index; i++) { + Serial.print(device_list[i].macaddr); + Serial.print(" | "); + Serial.println(device_list[i].loacalname); + } + if (list_index == 0) { + Serial.print("No device found\r\n"); + } + Serial.print("-------------------------------------------------------------\r\n"); + adv_list_clear(); } // Add the scanned BLE device into the global variables. -bool adv_list_add(ble_device_info_t &device) -{ - if (list_index >= bleScanMaxCnt) - { - return false; - } - for (int i = 0; i < list_index; i++) - { - if (0 == memcmp(device.macaddr, device_list[i].macaddr, sizeof (device.macaddr))) - { - // Found and update the item - return false; - } +bool adv_list_add(ble_device_info_t &device) { + if (list_index >= bleScanMaxCnt) { + return false; + } + for (int i = 0; i < list_index; i++) { + if (0 == memcmp(device.macaddr, device_list[i].macaddr, sizeof (device.macaddr))) { + // Found and update the item + return false; } - // Add the device - memcpy(&device_list[list_index], &device, sizeof (ble_device_info_t)); - list_index++; - return true; + } + // Add the device + memcpy(&device_list[list_index], &device, sizeof (ble_device_info_t)); + list_index++; + return true; } -bool adv_list_update(ble_device_info_t &device) -{ - for (int i = 0; i < list_index; i++) - { - if (0 == memcmp(device.macaddr, device_list[i].macaddr, sizeof (device.macaddr))) - { - // Found and update the item - memcpy(device_list[i].loacalname, device.loacalname, sizeof(device.loacalname)); - return true; - } +bool adv_list_update(ble_device_info_t &device) { + for (int i = 0; i < list_index; i++) { + if (0 == memcmp(device.macaddr, device_list[i].macaddr, sizeof (device.macaddr))) { + // Found and update the item + memcpy(device_list[i].loacalname, device.loacalname, sizeof(device.loacalname)); + return true; } - return false; + } + return false; } -void adv_list_clear() -{ - list_index = 0; - memset(device_list, 0x00, sizeof(device_list)); +void adv_list_clear() { + list_index = 0; + memset(device_list, 0x00, sizeof(device_list)); } // Process the Advertisement data -bool adv_found(uint8_t type, - const uint8_t *dataPtr, +bool adv_found(uint8_t type, + const uint8_t *dataPtr, uint8_t data_len, const bt_addr_le_t *addrPtr) { - ble_device_info_t device; - bt_addr_le_to_str (addrPtr, device.macaddr, sizeof (device.macaddr)); - memcpy(device.loacalname, " -NA-", sizeof(" -NA-")); - // Please see https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile - switch (type) { - case BT_DATA_NAME_SHORTENED: - case BT_DATA_NAME_COMPLETE: - memcpy(device.loacalname, dataPtr, data_len); - device.loacalname[data_len] = '\0'; - adv_list_update(device); - break; - } - adv_list_add(device); - return true; + ble_device_info_t device; + bt_addr_le_to_str (addrPtr, device.macaddr, sizeof (device.macaddr)); + memcpy(device.loacalname, " -NA-", sizeof(" -NA-")); + // Please see https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile + switch (type) { + case BT_DATA_NAME_SHORTENED: + case BT_DATA_NAME_COMPLETE: + memcpy(device.loacalname, dataPtr, data_len); + device.loacalname[data_len] = '\0'; + adv_list_update(device); + break; + } + adv_list_add(device); + return true; } /* diff --git a/libraries/CurieBLE/examples/UpdateConnectionInterval/UpdateConnectionInterval.ino b/libraries/CurieBLE/examples/UpdateConnectionInterval/UpdateConnectionInterval.ino index 1b6f7d28..9a20896b 100644 --- a/libraries/CurieBLE/examples/UpdateConnectionInterval/UpdateConnectionInterval.ino +++ b/libraries/CurieBLE/examples/UpdateConnectionInterval/UpdateConnectionInterval.ino @@ -1,23 +1,22 @@ /* - * Copyright (c) 2016 Intel Corporation. All rights reserved. - * See the bottom of this file for the license terms. + * Copyright (c) 2016 Intel Corporation. All rights reserved. + * See the bottom of this file for the license terms. */ #include -/* - This example can work with BatteryMonitor. - - Show how to control and response the connection interval request. +/* + This example can work with BatteryMonitor. + It shows how to control and response the connection interval request. */ // set up connection params ble_conn_param_t conn_param = {30.0, // minimum interval in ms 7.5 - 4000 50.0, // maximum interval in ms 7.5 - - 0, // latency + 0, // latency 4000 // timeout in ms 100 - 32000ms - }; + }; const int ledPin = 13; // set ledPin to use on-board LED BLECentral bleCentral; // create central instance @@ -35,165 +34,148 @@ bool adv_found(uint8_t type, uint8_t data_len, const bt_addr_le_t *addrPtr); -void setup() -{ - // initialize serial communication - Serial.begin(9600); - // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch - // If you don't care to see text messages sent to the Serial Monitor during board initialization, - // remove or comment out the next line - while(!Serial) ; - - // add service and characteristic - bleCentral.addAttribute(batteryService); - bleCentral.addAttribute(batteryLevelChar); - - // assign event handlers for connected, disconnected to central - bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler); - bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler); - // assign event handler to central device to update connection params - bleCentral.setEventHandler(BLEUpdateParam, bleCentralUpdateParam); - - // add adv_fund() handler to get advertising packets - bleCentral.setAdvertiseHandler(adv_found); - - // assign event handler for characteristic - batteryLevelChar.setEventHandler(BLEWritten, switchCharacteristicWritten); - - bleCentral.begin(); - Serial.println("Bluetooth device active, waiting for connections..."); +void setup() { + // initialize serial communication + Serial.begin(9600); + // wait for the serial port to connect. Open the Serial Monitor to continue executing the sketch + // If you don't care to see text messages sent to the Serial Monitor during board initialization, + // remove or comment out the next line + while (!Serial) ; + + // add service and characteristic + bleCentral.addAttribute(batteryService); + bleCentral.addAttribute(batteryLevelChar); + + // assign event handlers for connected, disconnected to central + bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler); + bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler); + // assign event handler to central device to update connection params + bleCentral.setEventHandler(BLEUpdateParam, bleCentralUpdateParam); + + // add adv_fund() handler to get advertising packets + bleCentral.setAdvertiseHandler(adv_found); + + // assign event handler for characteristic + batteryLevelChar.setEventHandler(BLEWritten, switchCharacteristicWritten); + + bleCentral.begin(); + Serial.println("Bluetooth device active, waiting for connections..."); } -void loop() -{ - static unsigned int counter = 0; - static char ledstate = 0; - delay(2000); - if (blePeripheral1) - { - counter++; - - if (counter % 3) - { - batteryLevelChar.read(*blePeripheral1); - } - else - { - ledstate = !ledstate; - batteryLevelChar.write(*blePeripheral1, ledstate); - } - } +void loop() { + static unsigned int counter = 0; + static char ledstate = 0; + delay(2000); + if (blePeripheral1) { + counter++; + if (counter % 3) { + batteryLevelChar.read(*blePeripheral1); + } + else { + ledstate = !ledstate; + batteryLevelChar.write(*blePeripheral1, ledstate); + } + } } -void bleCentralConnectHandler(BLEHelper& peripheral) -{ - // peripheral connected event handler - blePeripheral1 = bleCentral.getPeerPeripheralBLE(peripheral); - Serial.print("Connected event, peripheral: "); - Serial.println(peripheral.address()); - // Start discovery the profiles in peripheral device - blePeripheral1->discover(); +void bleCentralConnectHandler(BLEHelper& peripheral) { + // peripheral connected event handler + blePeripheral1 = bleCentral.getPeerPeripheralBLE(peripheral); + Serial.print("Connected event, peripheral: "); + Serial.println(peripheral.address()); + // Start discovery the profiles in peripheral device + blePeripheral1->discover(); } -void bleCentralDisconnectHandler(BLEHelper& peripheral) -{ - // peripheral disconnected event handler - blePeripheral1 = NULL; - Serial.print("Disconnected event, peripheral: "); - Serial.println(peripheral.address()); - bleCentral.startScan(); +void bleCentralDisconnectHandler(BLEHelper& peripheral) { + // peripheral disconnected event handler + blePeripheral1 = NULL; + Serial.print("Disconnected event, peripheral: "); + Serial.println(peripheral.address()); + bleCentral.startScan(); } -void bleCentralUpdateParam(BLEHelper& peripheral) -{ - // peripheral update the connection interval event handler - Serial.print("UpdateParam event, peripheral: "); - blePeripheral1 = bleCentral.getPeerPeripheralBLE(peripheral);; - Serial.println(peripheral.address()); - - // Get connection interval that peer peripheral device wanted - ble_conn_param_t m_conn_param; - blePeripheral1->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 the connection interval - blePeripheral1->setConnectionInterval(m_conn_param.interval_min,m_conn_param.interval_max); +void bleCentralUpdateParam(BLEHelper& peripheral) { + // peripheral update the connection interval event handler + Serial.print("UpdateParam event, peripheral: "); + blePeripheral1 = bleCentral.getPeerPeripheralBLE(peripheral);; + Serial.println(peripheral.address()); + + // Get connection interval that peer peripheral device wanted + ble_conn_param_t m_conn_param; + blePeripheral1->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 the connection interval + blePeripheral1->setConnectionInterval(m_conn_param.interval_min, m_conn_param.interval_max); } -void switchCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic) -{ - // Read response/Notification wrote new value to characteristic, update LED - Serial.print("Characteristic event, notify: "); - - int battery = batteryLevelChar.value(); - if (battery) - { - Serial.print("Battery Level % is now: "); // print it - Serial.println(battery); - delay(100); - - Serial.println("LED on"); - digitalWrite(ledPin, HIGH); - } - else - { - Serial.println("LED off"); - digitalWrite(ledPin, LOW); - } +void switchCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic) { + // Read response/Notification wrote new value to characteristic, update LED + Serial.print("Characteristic event, notify: "); + + int battery = batteryLevelChar.value(); + if (battery) { + Serial.print("Battery Level % is now: "); // print it + Serial.println(battery); + delay(100); + + Serial.println("LED on"); + digitalWrite(ledPin, HIGH); + } + else { + Serial.println("LED off"); + digitalWrite(ledPin, LOW); + } } -bool adv_found(uint8_t type, - const uint8_t *dataPtr, +bool adv_found(uint8_t type, + const uint8_t *dataPtr, uint8_t data_len, const bt_addr_le_t *addrPtr) { - int i; - - Serial.print("[AD]:"); - Serial.print(type); - Serial.print(" data_len "); - Serial.println(data_len); - - switch (type) - { - case BT_DATA_UUID16_SOME: - case BT_DATA_UUID16_ALL: - { - if (data_len % UUID_SIZE_16 != 0) - { - Serial.println("AD malformed"); - return true; - } - for (i = 0; i < data_len; i += UUID_SIZE_16) - { - if (batteryService.uuidCompare(dataPtr + i, UUID_SIZE_16) == false) - { - continue; - } - - // Accept the advertisement - if (!bleCentral.stopScan()) - { - Serial.println("Stop LE scan failed"); - continue; - } - Serial.println("Connecting"); - // Connect to peripheral - bleCentral.connect(addrPtr, &conn_param); - return false; - } + int i; + + Serial.print("[AD]:"); + Serial.print(type); + Serial.print(" data_len "); + Serial.println(data_len); + + switch (type) { + case BT_DATA_UUID16_SOME: + case BT_DATA_UUID16_ALL: + { + if (data_len % UUID_SIZE_16 != 0) { + Serial.println("AD malformed"); + return true; } - } + for (i = 0; i < data_len; i += UUID_SIZE_16) { + if (batteryService.uuidCompare(dataPtr + i, UUID_SIZE_16) == false) { + continue; + } + + // Accept the advertising data + if (!bleCentral.stopScan()) { + Serial.println("Stop LE scan failed"); + continue; + } + Serial.println("Connecting"); + // Connect to peripheral + bleCentral.connect(addrPtr, &conn_param); + return false; + } + } + } - return true; + return true; } /*