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+
2240void 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