Skip to content

Commit 6219491

Browse files
authored
Modified SensorTag example and added examples derived from 1.0.7
1 parent 2391f48 commit 6219491

File tree

4 files changed

+504
-11
lines changed

4 files changed

+504
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <CurieBLE.h>
2+
3+
/*
4+
This sketch example works with BatteryMonitor_Notification.ino
5+
6+
BatteryMonitor_Notification will send notification to this central sketch.
7+
This sketch will receive the notifications and output the received data in the serial monitor.
8+
It also illustrates using a non-typed characteristic.
9+
Set the baud rate to 115200 on the serial monitor to accomodate the speed of constant data updates
10+
*/
11+
12+
#define LED_PIN 13
13+
14+
15+
void setup()
16+
{
17+
// This is set to higher baud rate because accelerometer data changes very quickly
18+
Serial.begin(9600); // initialize serial communication
19+
while (!Serial);
20+
pinMode(LED_PIN, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected
21+
22+
/* Now activate the BLE device. It will start continuously transmitting BLE
23+
advertising packets and will be visible to remote BLE central devices
24+
until it receives a new connection */
25+
BLE.begin();
26+
Serial.print("My Address is: ");
27+
Serial.println(BLE.address());
28+
29+
BLE.scanForName("BatteryMonitorSketch");
30+
Serial.println("Scan Started");
31+
}
32+
33+
34+
void loop()
35+
{
36+
BLEDevice peripheral = BLE.available();
37+
38+
if (peripheral)
39+
{
40+
Serial.print("Found ");
41+
Serial.print(peripheral.address());
42+
Serial.print(" '");
43+
Serial.print(peripheral.localName());
44+
Serial.print("' ");
45+
Serial.print(peripheral.advertisedServiceUuid());
46+
Serial.println();
47+
if ( peripheral.localName() == "BatteryMonitorSketch") {
48+
BLE.stopScan();
49+
processNotification(peripheral);
50+
51+
delay (1000);
52+
}
53+
54+
// central connected to peripheral
55+
56+
//delay (4000);
57+
// BLE.scan();//("BatteryMonitorSketch");
58+
}
59+
}
60+
61+
void processNotification(BLEDevice peripheral) {
62+
if (peripheral.connect()) {
63+
Serial.print("Connected: ");
64+
Serial.println(peripheral.address());
65+
// light pin to indicate connection
66+
digitalWrite(LED_PIN, HIGH);
67+
} else {
68+
Serial.println("Failed to connect!");
69+
return;
70+
}
71+
72+
// discover peripheral attributes
73+
Serial.println("Discovering attributes ...");
74+
if (peripheral.discoverAttributes()) {
75+
Serial.println("Attributes discovered");
76+
} else {
77+
Serial.println("Attribute discovery failed!");
78+
peripheral.disconnect();
79+
digitalWrite(LED_PIN, LOW);
80+
return;
81+
}
82+
BLECharacteristic batteryLevelChar = peripheral.characteristic("2A19");
83+
if (!batteryLevelChar) {
84+
peripheral.disconnect();
85+
Serial.println("Peripheral does not have battery level characteristic!");
86+
digitalWrite(LED_PIN, LOW);
87+
delay(1000);
88+
return;
89+
}
90+
91+
if (!batteryLevelChar.subscribe()) {
92+
Serial.println("subscription failed!");
93+
peripheral.disconnect();
94+
return;
95+
} else {
96+
Serial.println("Subscribed");
97+
}
98+
99+
while (peripheral.connected())
100+
{
101+
if (batteryLevelChar.valueUpdated()) {
102+
103+
printData(batteryLevelChar.value(), batteryLevelChar.valueLength());
104+
Serial.println("%");
105+
106+
}
107+
108+
}
109+
Serial.print("Disconnected");
110+
Serial.println(peripheral.address());
111+
digitalWrite(LED_PIN, LOW);
112+
}
113+
114+
115+
void printData(const unsigned char data[], int length) {
116+
for (int i = 0; i < length; i++) {
117+
unsigned char b = data[i];
118+
119+
if (b < 16) {
120+
Serial.print("0");
121+
}
122+
123+
Serial.print(b);
124+
}
125+
}

libraries/CurieBLE/examples/central/sensortag_button/sensortag_button.ino

+71-11
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,33 @@
1919

2020
#include <CurieBLE.h>
2121

22+
const int NUM_OF_SERVICE = 10;
23+
24+
char *serviceUUIDArray[NUM_OF_SERVICE] =
25+
26+
// These are the various services that are included in the CC2650 Sensor Tag
27+
// If you uncomment them you can see the various services
28+
{ //"f000aa00-0451-4000-b000-000000000000",
29+
// "f000aa20-0451-4000-b000-000000000000",
30+
// "f000aa40-0451-4000-b000-000000000000",
31+
// "f000aa70-0451-4000-b000-000000000000",
32+
// "f000aa80-0451-4000-b000-000000000000",
33+
// "f000aa64-0451-4000-b000-000000000000",
34+
// "f000ac00-0451-4000-b000-000000000000",
35+
// "f000ccc0-0451-4000-b000-000000000000",
36+
// "f000ffc0-0451-4000-b000-000000000000",
37+
"0000ffe0-0000-1000-8000-00805f9b34fb"
38+
};
39+
2240
void setup() {
2341
Serial.begin(9600);
42+
while (!Serial);
2443

2544
// initialize the BLE hardware
2645
BLE.begin();
2746

2847
Serial.println("BLE Central - SensorTag button");
48+
Serial.println("Make sure to turn on the device.");
2949

3050
// start scanning for peripheral
3151
BLE.scan();
@@ -45,8 +65,26 @@ void loop() {
4565
Serial.print(peripheral.advertisedServiceUuid());
4666
Serial.println();
4767

48-
// see if peripheral is a SensorTag
49-
if (peripheral.localName() == "SensorTag") {
68+
/*see if peripheral is a SensorTag
69+
The localName SensorTag is in the Scan Response data packet
70+
In this release we do not have the feature that gets the scan response data and hence
71+
the local name in the scan is blank
72+
We have to explicitly find the BLE mac address
73+
Please use another deviice like nrfConnect app to discover the Bluetooth Address
74+
*/
75+
//if (peripheral.localName() == "SensorTag") {
76+
77+
78+
/******************************************************
79+
* ATTENTION:
80+
* Change to the mac address according to your device!
81+
* Use a central app that can display the BT MAC address
82+
* ******************************************************
83+
*/
84+
85+
if (peripheral.address() == "24:71:89:07:27:80")
86+
87+
{
5088
// stop scanning
5189
BLE.stopScan();
5290

@@ -58,7 +96,11 @@ void loop() {
5896
}
5997
}
6098

61-
void monitorSensorTagButtons(BLEDevice peripheral) {
99+
void monitorSensorTagButtons(BLEDevice peripheral)
100+
{
101+
static bool getAllServices = true;
102+
static int serviceIndx = 0;
103+
62104
// connect to the peripheral
63105
Serial.println("Connecting ...");
64106
if (peripheral.connect()) {
@@ -68,18 +110,34 @@ void monitorSensorTagButtons(BLEDevice peripheral) {
68110
return;
69111
}
70112

71-
// discover peripheral attributes
72-
Serial.println("Discovering attributes ...");
73-
if (peripheral.discoverAttributes()) {
74-
Serial.println("Attributes discovered");
113+
if (getAllServices) {
114+
// discover peripheral attributes
115+
Serial.println("Discovering attributes ...");
116+
if (peripheral.discoverAttributes()) {
117+
Serial.println("Attributes discovered");
118+
} else {
119+
getAllServices = false;
120+
Serial.println("Attribute discovery failed.");
121+
peripheral.disconnect();
122+
return;
123+
}
75124
} else {
76-
Serial.println("Attribute discovery failed!");
77-
peripheral.disconnect();
78-
return;
125+
int tmp = serviceIndx;
126+
Serial.print("Discovering Service: ");
127+
Serial.println(serviceUUIDArray[tmp]);
128+
if (++serviceIndx >= NUM_OF_SERVICE)
129+
serviceIndx = 0;
130+
if (peripheral.discoverAttributesByService(serviceUUIDArray[tmp]) == false) {
131+
Serial.println("Can't find the Service.");
132+
peripheral.disconnect();
133+
return;
134+
} else {
135+
Serial.println("Service discovered.");
136+
}
79137
}
80138

81139
// retrieve the simple key characteristic
82-
BLECharacteristic simpleKeyCharacteristic = peripheral.characteristic("ffe1");
140+
BLECharacteristic simpleKeyCharacteristic = peripheral.characteristic("0000ffe1-0000-1000-8000-00805f9b34fb");
83141

84142
// subscribe to the simple key characteristic
85143
Serial.println("Subscribing to simple key characteristic ...");
@@ -97,6 +155,7 @@ void monitorSensorTagButtons(BLEDevice peripheral) {
97155
return;
98156
} else {
99157
Serial.println("Subscribed");
158+
Serial.println("Press the right and left buttons on your Sensor Tag.");
100159
}
101160

102161
while (peripheral.connected()) {
@@ -118,4 +177,5 @@ void monitorSensorTagButtons(BLEDevice peripheral) {
118177
}
119178
}
120179
}
180+
121181
}

0 commit comments

Comments
 (0)