Skip to content

Commit 1537c0a

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 2ccef69 commit 1537c0a

File tree

3 files changed

+348
-0
lines changed

3 files changed

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

libraries/CurieBLE/examples/BatteryMonitor/BatteryMonitor.ino

+33
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ void loop() {
6363
if (currentMillis - previousMillis >= 200) {
6464
previousMillis = currentMillis;
6565
updateBatteryLevel();
66+
67+
static unsigned short count = 0;
68+
count++;
69+
// update the connection interval
70+
if(count%5 == 0){
71+
delay(1000);
72+
updateIntervalParams(central);
73+
}
6674
}
6775
}
6876
// when the central disconnects, turn off the LED:
@@ -87,6 +95,31 @@ void updateBatteryLevel() {
8795
}
8896
}
8997

98+
void updateIntervalParams(BLECentralHelper &central) {
99+
// read and update the connection interval that peer central device
100+
static unsigned short interval = 0x60;
101+
ble_conn_param_t m_conn_param;
102+
// Get connection interval that peer central device wanted
103+
central.getConnParams(m_conn_param);
104+
Serial.print("min interval = " );
105+
Serial.println(m_conn_param.interval_min );
106+
Serial.print("max interval = " );
107+
Serial.println(m_conn_param.interval_max );
108+
Serial.print("latency = " );
109+
Serial.println(m_conn_param.latency );
110+
Serial.print("timeout = " );
111+
Serial.println(m_conn_param.timeout );
112+
113+
//Update connection interval
114+
Serial.println("set Connection Interval");
115+
central.setConnectionInterval(interval,interval);
116+
117+
interval++;
118+
if(interval<0x06)
119+
interval = 0x06;
120+
if(interval>0x100)
121+
interval = 0x06;
122+
}
90123
/*
91124
Copyright (c) 2016 Intel Corporation. All rights reserved.
92125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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;
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 *data,
31+
uint8_t data_len,
32+
void *user_data);
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, (unsigned char *)(&ledstate), sizeof (ledstate));
74+
}
75+
}
76+
77+
}
78+
79+
void bleCentralConnectHandler(BLEHelper& peripheral)
80+
{
81+
// peripheral connected event handler
82+
blePeripheral1 = (BLEPeripheralHelper *)(&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 = (BLEPeripheralHelper *)(&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 *data,
145+
uint8_t data_len,
146+
void *user_data)
147+
{
148+
bt_addr_le_t *addr = (bt_addr_le_t *)user_data;
149+
int i;
150+
151+
Serial.print("[AD]:");
152+
Serial.print(type);
153+
Serial.print(" data_len ");
154+
Serial.println(data_len);
155+
156+
switch (type)
157+
{
158+
case BT_DATA_UUID16_SOME:
159+
case BT_DATA_UUID16_ALL:
160+
{
161+
if (data_len % MAX_UUID_SIZE_16 != 0)
162+
{
163+
Serial.println("AD malformed");
164+
return true;
165+
}
166+
struct bt_uuid * serviceuuid = batteryService.uuid();
167+
for (i = 0; i < data_len; i += MAX_UUID_SIZE_16)
168+
{
169+
if (memcmp (&((bt_uuid_16_t*)serviceuuid)->val, &data[i], MAX_UUID_SIZE_16) != 0)
170+
{
171+
continue;
172+
}
173+
174+
// Accept the advertisement
175+
if (!bleCentral.stopScan())
176+
{
177+
Serial.println("Stop LE scan failed");
178+
continue;
179+
}
180+
Serial.println("Connecting");
181+
// Connect to peripheral
182+
bleCentral.connect(addr, &conn_param);
183+
return false;
184+
}
185+
}
186+
}
187+
188+
return true;
189+
}
190+
191+
/*
192+
Copyright (c) 2016 Intel Corporation. All rights reserved.
193+
194+
This library is free software; you can redistribute it and/or
195+
modify it under the terms of the GNU Lesser General Public
196+
License as published by the Free Software Foundation; either
197+
version 2.1 of the License, or (at your option) any later version.
198+
199+
This library is distributed in the hope that it will be useful,
200+
but WITHOUT ANY WARRANTY; without even the implied warranty of
201+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
202+
Lesser General Public License for more details.
203+
204+
You should have received a copy of the GNU Lesser General Public
205+
License along with this library; if not, write to the Free Software
206+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
207+
*/

0 commit comments

Comments
 (0)