Skip to content

Commit 727e42a

Browse files
committed
Create BLE Example sketches
1. Fix Jira 664 demonstrates changing the ADV data 2. Fix Jira 671 Support update connection interval in central/peripheral
1 parent acfec8d commit 727e42a

File tree

3 files changed

+351
-0
lines changed

3 files changed

+351
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* Please see code cpyright at the bottom of this example code */
2+
/*
3+
This sketch illustrates how to change the advertising data so that it is visible but not
4+
connectable. Then after 10 seconds it changes to being connectable
5+
This sketch example partially implements the standard Bluetooth Low-Energy Battery service.
6+
7+
This sketch is not paired with a specific central example sketch,
8+
but to see how it works you need to use a BLE APP on your phone or central device
9+
and try connecting when it is either a connectable or not connectable state
10+
as displayed in the serial monitor.
11+
*/
12+
13+
#include <CurieBLE.h>
14+
15+
BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
16+
BLEService batteryService("180F"); // BLE Battery Service
17+
int count = 0;
18+
// BLE Battery Level Characteristic"
19+
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
20+
BLERead | BLENotify); // remote clients will be able to
21+
// get notifications if this characteristic changes
22+
23+
void setup() {
24+
Serial.begin(9600); // initialize serial communication
25+
pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
26+
while (!Serial) {
27+
//wait for Serial to connect
28+
}
29+
/* Set a local name for the BLE device
30+
This name will appear in advertising packets
31+
and can be used by remote devices to identify this BLE device
32+
The name can be changed but maybe be truncated based on space left in advertisement packet */
33+
blePeripheral.setLocalName("BatteryAdvChangeSketch");
34+
blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID
35+
blePeripheral.addAttribute(batteryService); // Add the BLE Battery service
36+
blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic
37+
38+
/* Now activate the BLE device. It will start continuously transmitting BLE
39+
advertising packets and will be visible to remote BLE central devices
40+
until it receives a new connection */
41+
42+
blePeripheral.begin();
43+
Serial.println("Bluetooth device active, waiting for connections...");
44+
Serial.println("Starts in Connectable mode");
45+
}
46+
47+
void loop() {
48+
// listen for BLE peripherals to connect:
49+
BLECentralHelper central = blePeripheral.central();
50+
// wait
51+
Serial.print(". ");
52+
if (count == 10) {
53+
Serial.print("\nReached count ");
54+
Serial.println(count);
55+
56+
}
57+
delay (1000);
58+
count++;
59+
// Switch from Connectable to Non Connectable and vice versa
60+
if (count > 10 ) {
61+
static bool change_discover = false;
62+
Serial.println("Stop Adv and pausing for 10 seconds. Device should be invisible");
63+
// Some central devices (phones included) may cache previous scan inofrmation
64+
// restart your central and it should not see this peripheral once stopAdvertising() is called
65+
blePeripheral.stopAdvertising();
66+
delay(10000);
67+
68+
if (change_discover)
69+
{
70+
71+
// Using the function setConnectable we specify that it now NOT connectable
72+
// The loop is for 10 seconds. Your central device may timeout later than that
73+
// and may eventually connect when we set it back to connectable mode below
74+
blePeripheral.setConnectable(false);
75+
Serial.println("In Non Connectable mode");
76+
77+
}
78+
else
79+
{
80+
81+
//using the function setConnectable we specify that it now connectable
82+
blePeripheral.setConnectable(true);
83+
Serial.println("In Connectable mode");
84+
}
85+
Serial.println("Start Adv");
86+
blePeripheral.startAdvertising();
87+
if (change_discover) {
88+
Serial.println("Adding 5 second delay in Non Connect Mode");
89+
delay(5000);
90+
}
91+
change_discover = !change_discover;
92+
count = 0;
93+
}
94+
}
95+
96+
/*
97+
Copyright (c) 2016 Intel Corporation. All rights reserved.
98+
99+
This library is free software; you can redistribute it and/or
100+
modify it under the terms of the GNU Lesser General Public
101+
License as published by the Free Software Foundation; either
102+
version 2.1 of the License, or (at your option) any later version.
103+
104+
This library is distributed in the hope that it will be useful,
105+
but WITHOUT ANY WARRANTY; without even the implied warranty of
106+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
107+
Lesser General Public License for more details.
108+
109+
You should have received a copy of the GNU Lesser General Public
110+
License along with this library; if not, write to the Free Software
111+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
112+
*/
113+

libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino

+33
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ void loop() {
6666
if (currentMillis - previousMillis >= 200) {
6767
previousMillis = currentMillis;
6868
updateBatteryLevel();
69+
70+
static unsigned short count = 0;
71+
count++;
72+
// update the connection interval
73+
if(count%5 == 0){
74+
delay(1000);
75+
updateIntervalParams(central);
76+
}
6977
}
7078
}
7179
// when the central disconnects, turn off the LED:
@@ -90,6 +98,31 @@ void updateBatteryLevel() {
9098
}
9199
}
92100

101+
void updateIntervalParams(BLECentralHelper &central) {
102+
// read and update the connection interval that peer central device
103+
static unsigned short interval = 0x60;
104+
ble_conn_param_t m_conn_param;
105+
// Get connection interval that peer central device wanted
106+
central.getConnParams(m_conn_param);
107+
Serial.print("min interval = " );
108+
Serial.println(m_conn_param.interval_min );
109+
Serial.print("max interval = " );
110+
Serial.println(m_conn_param.interval_max );
111+
Serial.print("latency = " );
112+
Serial.println(m_conn_param.latency );
113+
Serial.print("timeout = " );
114+
Serial.println(m_conn_param.timeout );
115+
116+
//Update connection interval
117+
Serial.println("set Connection Interval");
118+
central.setConnectionInterval(interval,interval);
119+
120+
interval++;
121+
if(interval<0x06)
122+
interval = 0x06;
123+
if(interval>0x100)
124+
interval = 0x06;
125+
}
93126
/*
94127
Copyright (c) 2016 Intel Corporation. All rights reserved.
95128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
5+
6+
#include <CurieBLE.h>
7+
8+
/*
9+
This example can work with BatteryMonitor
10+
to show how to control and response the connection intelval request.
11+
*/
12+
13+
// set up connection params
14+
15+
ble_conn_param_t conn_param = {30.0, // minimum interval in ms 7.5 - 4000
16+
50.0, // maximum interval in ms 7.5 -
17+
0, // latency
18+
4000 // timeout in ms 100 - 32000ms
19+
};
20+
21+
const int ledPin = 13; // set ledPin to use on-board LED
22+
BLECentral bleCentral; // create central instance
23+
BLEPeripheralHelper *blePeripheral1 = NULL; // // peer peripheral device
24+
25+
BLEService batteryService("180F"); // create service with a 16-bit UUID
26+
BLECharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify);// create switch characteristic
27+
//and allow remote device to read and notify
28+
29+
bool adv_found(uint8_t type,
30+
const uint8_t *dataPtr,
31+
uint8_t data_len,
32+
const bt_addr_le_t *addrPtr);
33+
34+
void setup()
35+
{
36+
Serial.begin(9600);
37+
38+
// add service and characteristic
39+
bleCentral.addAttribute(batteryService);
40+
bleCentral.addAttribute(batteryLevelChar);
41+
42+
// assign event handlers for connected, disconnected to central
43+
bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler);
44+
bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler);
45+
bleCentral.setEventHandler(BLEUpdateParam, bleCentralUpdateParam);
46+
47+
// advertise the service
48+
bleCentral.setAdvertiseHandler(adv_found);
49+
50+
// assign event handlers for characteristic
51+
batteryLevelChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
52+
53+
bleCentral.begin();
54+
Serial.println(("Bluetooth device active, waiting for connections..."));
55+
}
56+
57+
void loop()
58+
{
59+
static unsigned int counter = 0;
60+
static char ledstate = 0;
61+
delay(2000);
62+
if (blePeripheral1)
63+
{
64+
counter++;
65+
66+
if (counter % 3)
67+
{
68+
batteryLevelChar.read(*blePeripheral1);
69+
}
70+
else
71+
{
72+
ledstate = !ledstate;
73+
batteryLevelChar.write(*blePeripheral1, ledstate);
74+
}
75+
}
76+
77+
}
78+
79+
void bleCentralConnectHandler(BLEHelper& peripheral)
80+
{
81+
// peripheral connected event handler
82+
blePeripheral1 = bleCentral.getPeerPeripheralBLE(peripheral);
83+
Serial.print("Connected event, peripheral: ");
84+
Serial.println(peripheral.address());
85+
// Start discovery the profiles in peripheral device
86+
blePeripheral1->discover();
87+
}
88+
89+
void bleCentralDisconnectHandler(BLEHelper& peripheral)
90+
{
91+
// peripheral disconnected event handler
92+
blePeripheral1 = NULL;
93+
Serial.print("Disconnected event, peripheral: ");
94+
Serial.println(peripheral.address());
95+
bleCentral.startScan();
96+
}
97+
98+
void bleCentralUpdateParam(BLEHelper& peripheral)
99+
{
100+
// peripheral update the connection interval event handler
101+
Serial.print("UpdateParam event, peripheral: ");
102+
blePeripheral1 = bleCentral.getPeerPeripheralBLE(peripheral);;
103+
Serial.println(peripheral.address());
104+
105+
// Get connection interval that peer peripheral device wanted
106+
ble_conn_param_t m_conn_param;
107+
blePeripheral1->getConnParams(m_conn_param);
108+
Serial.print("min interval = " );
109+
Serial.println(m_conn_param.interval_min );
110+
Serial.print("max interval = " );
111+
Serial.println(m_conn_param.interval_max );
112+
Serial.print("latency = " );
113+
Serial.println(m_conn_param.latency );
114+
Serial.print("timeout = " );
115+
Serial.println(m_conn_param.timeout );
116+
117+
//Update the connection interval
118+
blePeripheral1->setConnectionInterval(m_conn_param.interval_min,m_conn_param.interval_max);
119+
}
120+
121+
void switchCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic)
122+
{
123+
// Read response/Notification wrote new value to characteristic, update LED
124+
Serial.print("Characteristic event, notify: ");
125+
126+
int battery = batteryLevelChar.value();
127+
if (battery)
128+
{
129+
Serial.print("Battery Level % is now: "); // print it
130+
Serial.println(battery);
131+
delay(100);
132+
133+
Serial.println("LED on");
134+
digitalWrite(ledPin, HIGH);
135+
}
136+
else
137+
{
138+
Serial.println("LED off");
139+
digitalWrite(ledPin, LOW);
140+
}
141+
}
142+
143+
bool adv_found(uint8_t type,
144+
const uint8_t *dataPtr,
145+
uint8_t data_len,
146+
const bt_addr_le_t *addrPtr)
147+
{
148+
int i;
149+
150+
Serial.print("[AD]:");
151+
Serial.print(type);
152+
Serial.print(" data_len ");
153+
Serial.println(data_len);
154+
155+
switch (type)
156+
{
157+
case BT_DATA_UUID16_SOME:
158+
case BT_DATA_UUID16_ALL:
159+
{
160+
if (data_len % UUID_SIZE_16 != 0)
161+
{
162+
Serial.println("AD malformed");
163+
return true;
164+
}
165+
for (i = 0; i < data_len; i += UUID_SIZE_16)
166+
{
167+
if (batteryService.uuidCompare(dataPtr + i, UUID_SIZE_16) == false)
168+
{
169+
continue;
170+
}
171+
172+
// Accept the advertisement
173+
if (!bleCentral.stopScan())
174+
{
175+
Serial.println("Stop LE scan failed");
176+
continue;
177+
}
178+
Serial.println("Connecting");
179+
// Connect to peripheral
180+
bleCentral.connect(addrPtr, &conn_param);
181+
return false;
182+
}
183+
}
184+
}
185+
186+
return true;
187+
}
188+
189+
/*
190+
Copyright (c) 2016 Intel Corporation. All rights reserved.
191+
192+
This library is free software; you can redistribute it and/or
193+
modify it under the terms of the GNU Lesser General Public
194+
License as published by the Free Software Foundation; either
195+
version 2.1 of the License, or (at your option) any later version.
196+
197+
This library is distributed in the hope that it will be useful,
198+
but WITHOUT ANY WARRANTY; without even the implied warranty of
199+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
200+
Lesser General Public License for more details.
201+
202+
You should have received a copy of the GNU Lesser General Public
203+
License along with this library; if not, write to the Free Software
204+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
205+
*/

0 commit comments

Comments
 (0)