-
-
Notifications
You must be signed in to change notification settings - Fork 283
Improvements for CurieBLE library #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,122 @@ | ||
/* 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 <CurieBLE.h> | ||
|
||
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 <CurieBLE.h> | ||
|
||
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 | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <CurieBLE.h> | ||
|
@@ -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: | ||
|
@@ -68,26 +73,25 @@ void loop() { | |
previousMillis = currentMillis; | ||
updateBatteryLevel(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please isolate indentation and style changes into a single commit |
||
|
||
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); | ||
} | ||
} | ||
} | ||
// when the central disconnects, turn off the LED: | ||
digitalWrite(13, LOW); | ||
digitalWrite(LED_BUILTIN, LOW); | ||
Serial.print("Disconnected from central: "); | ||
Serial.println(central.address()); | ||
} | ||
} | ||
|
||
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); | ||
|
||
|
@@ -105,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 = "); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please isolate indentation and style changes into a single commit |
||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please isolate indentation and style changes into a single commit |
||
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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any idea why this file shows as the whole thing is changed, rather than line changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @calvinatintel,
I notice that the BatteryAdvChange.ino sketch has a strange formatting type.
Even if you change only a single line this file shows as the whole thing is changed.