19
19
20
20
#include < CurieBLE.h>
21
21
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
+
22
40
void setup () {
23
41
Serial.begin (9600 );
42
+ while (!Serial);
24
43
25
44
// initialize the BLE hardware
26
45
BLE.begin ();
27
46
28
47
Serial.println (" BLE Central - SensorTag button" );
48
+ Serial.println (" Make sure to turn on the device." );
29
49
30
50
// start scanning for peripheral
31
51
BLE.scan ();
@@ -45,8 +65,26 @@ void loop() {
45
65
Serial.print (peripheral.advertisedServiceUuid ());
46
66
Serial.println ();
47
67
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
+ {
50
88
// stop scanning
51
89
BLE.stopScan ();
52
90
@@ -58,7 +96,11 @@ void loop() {
58
96
}
59
97
}
60
98
61
- void monitorSensorTagButtons (BLEDevice peripheral) {
99
+ void monitorSensorTagButtons (BLEDevice peripheral)
100
+ {
101
+ static bool getAllServices = true ;
102
+ static int serviceIndx = 0 ;
103
+
62
104
// connect to the peripheral
63
105
Serial.println (" Connecting ..." );
64
106
if (peripheral.connect ()) {
@@ -68,18 +110,34 @@ void monitorSensorTagButtons(BLEDevice peripheral) {
68
110
return ;
69
111
}
70
112
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
+ }
75
124
} 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
+ }
79
137
}
80
138
81
139
// retrieve the simple key characteristic
82
- BLECharacteristic simpleKeyCharacteristic = peripheral.characteristic (" ffe1 " );
140
+ BLECharacteristic simpleKeyCharacteristic = peripheral.characteristic (" 0000ffe1-0000-1000-8000-00805f9b34fb " );
83
141
84
142
// subscribe to the simple key characteristic
85
143
Serial.println (" Subscribing to simple key characteristic ..." );
@@ -97,6 +155,7 @@ void monitorSensorTagButtons(BLEDevice peripheral) {
97
155
return ;
98
156
} else {
99
157
Serial.println (" Subscribed" );
158
+ Serial.println (" Press the right and left buttons on your Sensor Tag." );
100
159
}
101
160
102
161
while (peripheral.connected ()) {
@@ -118,4 +177,5 @@ void monitorSensorTagButtons(BLEDevice peripheral) {
118
177
}
119
178
}
120
179
}
180
+
121
181
}
0 commit comments