Skip to content

Update BLE examples #375

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions libraries/CurieBLE/examples/central/led_control/led_control.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void setup() {
Serial.println("BLE Central - LED control");

// start scanning for peripherals
BLE.scan();
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}

void loop() {
Expand All @@ -56,16 +56,13 @@ void loop() {
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();

// see if peripheral is advertising the LED service
if (peripheral.advertisedServiceUuid() == "19b10000-e8f2-537e-4f6c-d104768a1214") {
// stop scanning
BLE.stopScan();
// stop scanning
BLE.stopScan();

controlLed(peripheral);
controlLed(peripheral);

// peripheral disconnected, start scanning again
BLE.scan();
}
// peripheral disconnected, start scanning again
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}
}

Expand Down Expand Up @@ -126,6 +123,8 @@ void controlLed(BLEDevice peripheral) {
}
}
}

Serial.println("Peripheral disconnected");
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/*
* Sketch: BatteryMonitor_Notification.ino
* Sketch: BatteryMonitor.ino
*
* Description:
* This sketch example partially implements the standard Bluetooth
Expand All @@ -13,47 +13,46 @@
* For more information:
* https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
*
* Notes:
*
* - Expected to work with BatteryMonitor_Central sketch.
* You can also use an android or IOS app that supports notifications.
*
*/

#include <CurieBLE.h>

BLEService batteryService("180F"); // BLE Battery Service

// BLE Battery Level Characteristic"
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify); // standard 16-bit characteristic UUID defined in the URL above
// remote clients will be able to get notifications if this characteristic changes
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
BLERead | BLENotify); // remote clients will be able to
// get notifications if this characteristic changes

int oldBatteryLevel = 0; // last battery level reading from analog input
long previousMillis = 0; // last time the battery level was checked, in ms

void setup() {
BLE.begin();
Serial.begin(9600); // initialize serial communication
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected

// begin initialization
BLE.begin();

/* Set a local name for the BLE device
This name will appear in advertising packets
and can be used by remote devices to identify this BLE device
The name can be changed but maybe be truncated based on space left in advertisement packet
If you want to make this work with the BatteryMonitor_Central sketch, do not modufy the name.
*/
BLE.setLocalName("BatteryMonitorSketch");
BLE.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID
BLE.addService(batteryService); // Add the BLE Battery service
BLE.setLocalName("BatteryMonitor");
BLE.setAdvertisedService(batteryService); // add the service UUID
batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic
BLE.addService(batteryService); // Add the BLE Battery service
batteryLevelChar.setValue(oldBatteryLevel); // initial value for this characteristic

/* Now activate the BLE device. It will start continuously transmitting BLE
/* Start advertising BLE. It will start continuously transmitting BLE
advertising packets and will be visible to remote BLE central devices
until it receives a new connection
*/
until it receives a new connection */

// start advertising
BLE.advertise();

Serial.println("Bluetooth device active, waiting for connections...");
}

Expand All @@ -77,14 +76,6 @@ void loop() {
if (currentMillis - previousMillis >= 200) {
previousMillis = currentMillis;
updateBatteryLevel();

static unsigned short count = 0;
count++;
// update the connection interval
if (count % 5 == 0) {
delay(1000);
updateIntervalParams(central);
}
}
}
// when the central disconnects, turn off the LED:
Expand All @@ -104,36 +95,11 @@ void updateBatteryLevel() {
if (batteryLevel != oldBatteryLevel) { // if the battery level has changed
Serial.print("Battery Level % is now: "); // print it
Serial.println(batteryLevel);
batteryLevelChar.writeUnsignedChar(batteryLevel); // and update the battery level characteristic
batteryLevelChar.setValue(batteryLevel); // and update the battery level characteristic
oldBatteryLevel = batteryLevel; // save the level for next comparison
}
}

void updateIntervalParams(BLEDevice central) {
// read and update the connection interval that peer central device
static unsigned short interval = 0x60;
ble_conn_param_t m_conn_param;
// Get connection interval that peer central device wanted
//central.getConnParams(m_conn_param);
Serial.print("min interval = " );
Serial.println(m_conn_param.interval_min );
Serial.print("max interval = " );
Serial.println(m_conn_param.interval_max );
Serial.print("latency = " );
Serial.println(m_conn_param.latency );
Serial.print("timeout = " );
Serial.println(m_conn_param.timeout );

//Update connection interval
Serial.println("set Connection Interval");
central.setConnectionInterval(interval, interval);

interval++;
if (interval < 0x06)
interval = 0x06;
if (interval > 0x100)
interval = 0x06;
}
/*
Copyright (c) 2016 Intel Corporation. All rights reserved.

Expand Down
94 changes: 94 additions & 0 deletions libraries/CurieBLE/examples/peripheral/ButtonLED/ButtonLED.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2016 Intel Corporation. All rights reserved.
* See the bottom of this file for the license terms.
*/

#include <CurieBLE.h>

const int ledPin = 13; // set ledPin to on-board LED
const int buttonPin = 4; // set buttonPin to digital pin 4

BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service


// create switch characteristic and allow remote device to read and write
BLECharCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
// create button characteristic and allow remote device to get notifications
BLECharCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // allows remote device to get notifications

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
pinMode(buttonPin, INPUT); // use button pin 4 as an input

// begin initialization
BLE.begin();

// set the local name peripheral advertises
BLE.setLocalName("BtnLED");
// set the UUID for the service this peripheral advertises:
BLE.setAdvertisedService(ledService);

// add the characteristics to the service
ledService.addCharacteristic(ledCharacteristic);
ledService.addCharacteristic(buttonCharacteristic);

// add the service
BLE.addService(ledService);

ledCharacteristic.setValue(0);
buttonCharacteristic.setValue(0);

// start advertising
BLE.advertise();

Serial.println("Bluetooth device active, waiting for connections...");
}

void loop() {
// poll for BLE events
BLE.poll();

// read the current button pin state
char buttonValue = digitalRead(buttonPin);

// has the value changed since the last read
boolean buttonChanged = (buttonCharacteristic.value() != buttonValue);

if (buttonChanged) {
// button state changed, update characteristics
ledCharacteristic.setValue(buttonValue);
buttonCharacteristic.setValue(buttonValue);
}

if (ledCharacteristic.written() || buttonChanged) {
// update LED, either central has written to characteristic or button state has changed
if (ledCharacteristic.value()) {
Serial.println("LED on");
digitalWrite(ledPin, HIGH);
} else {
Serial.println("LED off");
digitalWrite(ledPin, LOW);
}
}
}

/*
Copyright (c) 2016 Intel Corporation. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
1301 USA
*/

95 changes: 95 additions & 0 deletions libraries/CurieBLE/examples/peripheral/CallbackLED/CallbackLED.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2016 Intel Corporation. All rights reserved.
* See the bottom of this file for the license terms.
*/

#include <CurieBLE.h>

const int ledPin = 13; // set ledPin to use on-board LED

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service

// create switch characteristic and allow remote device to read and write
BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output

// begin initialization
BLE.begin();

// set the local name peripheral advertises
BLE.setLocalName("LEDCB");
// set the UUID for the service this peripheral advertises
BLE.setAdvertisedService(ledService);

// add the characteristic to the service
ledService.addCharacteristic(switchChar);

// add service
BLE.addService(ledService);

// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);

// assign event handlers for characteristic
switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
// set an initial value for the characteristic
switchChar.setValue(0);

// start advertising
BLE.advertise();

Serial.println(("Bluetooth device active, waiting for connections..."));
}

void loop() {
// poll for BLE events
BLE.poll();
}

void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
}

void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
}

void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
// central wrote new value to characteristic, update LED
Serial.print("Characteristic event, written: ");

if (switchChar.value()) {
Serial.println("LED on");
digitalWrite(ledPin, HIGH);
} else {
Serial.println("LED off");
digitalWrite(ledPin, LOW);
}
}

/*
Copyright (c) 2016 Intel Corporation. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
1301 USA
*/
Loading