1
1
/* *
2
2
* A BLE client example that is rich in capabilities.
3
+ * There is a lot new capabilities implemented.
4
+ * author unknown
5
+ * updated by chegewara
3
6
*/
4
7
5
8
#include " BLEDevice.h"
6
9
// #include "BLEScan.h"
7
10
8
11
// The remote service we wish to connect to.
9
- static BLEUUID serviceUUID (" 91bad492-b950-4226-aa2b-4ede9fa42f59 " );
12
+ static BLEUUID serviceUUID (" 4fafc201-1fb5-459e-8fcc-c5c9c331914b " );
10
13
// The characteristic of the remote service we are interested in.
11
- static BLEUUID charUUID (" 0d563a58-196a-48ce-ace2-dfec78acc814 " );
14
+ static BLEUUID charUUID (" beb5483e-36e1-4688-b7f5-ea07361b26a8 " );
12
15
13
- static BLEAddress *pServerAddress;
14
16
static boolean doConnect = false ;
15
17
static boolean connected = false ;
18
+ static doScan = false ;
16
19
static BLERemoteCharacteristic* pRemoteCharacteristic;
20
+ static BLEAdvertisedDevice* myDevice;
17
21
18
22
static void notifyCallback (
19
23
BLERemoteCharacteristic* pBLERemoteCharacteristic,
@@ -24,24 +28,39 @@ static void notifyCallback(
24
28
Serial.print (pBLERemoteCharacteristic->getUUID ().toString ().c_str ());
25
29
Serial.print (" of data length " );
26
30
Serial.println (length);
31
+ Serial.print (" data: " );
32
+ Serial.println ((char *)pData);
27
33
}
28
34
29
- bool connectToServer (BLEAddress pAddress) {
35
+ class MyClientCallback : public BLEClientCallbacks {
36
+ void onConnect (BLEClient* pclient) {
37
+ }
38
+
39
+ void onDisconnect (BLEClient* pclient) {
40
+ connected = false ;
41
+ Serial.println (" onDisconnect" );
42
+ }
43
+ };
44
+
45
+ bool connectToServer () {
30
46
Serial.print (" Forming a connection to " );
31
- Serial.println (pAddress .toString ().c_str ());
47
+ Serial.println (myDevice-> getAddress () .toString ().c_str ());
32
48
33
49
BLEClient* pClient = BLEDevice::createClient ();
34
50
Serial.println (" - Created client" );
35
51
52
+ pClient->setClientCallbacks (new MyClientCallback ());
53
+
36
54
// Connect to the remove BLE Server.
37
- pClient->connect (pAddress);
55
+ pClient->connect (myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
38
56
Serial.println (" - Connected to server" );
39
57
40
58
// Obtain a reference to the service we are after in the remote BLE server.
41
59
BLERemoteService* pRemoteService = pClient->getService (serviceUUID);
42
60
if (pRemoteService == nullptr ) {
43
61
Serial.print (" Failed to find our service UUID: " );
44
62
Serial.println (serviceUUID.toString ().c_str ());
63
+ pClient->disconnect ();
45
64
return false ;
46
65
}
47
66
Serial.println (" - Found our service" );
@@ -52,16 +71,22 @@ bool connectToServer(BLEAddress pAddress) {
52
71
if (pRemoteCharacteristic == nullptr ) {
53
72
Serial.print (" Failed to find our characteristic UUID: " );
54
73
Serial.println (charUUID.toString ().c_str ());
74
+ pClient->disconnect ();
55
75
return false ;
56
76
}
57
77
Serial.println (" - Found our characteristic" );
58
78
59
79
// Read the value of the characteristic.
60
- std::string value = pRemoteCharacteristic->readValue ();
61
- Serial.print (" The characteristic value was: " );
62
- Serial.println (value.c_str ());
80
+ if (pRemoteCharacteristic->canRead ()) {
81
+ std::string value = pRemoteCharacteristic->readValue ();
82
+ Serial.print (" The characteristic value was: " );
83
+ Serial.println (value.c_str ());
84
+ }
63
85
64
- pRemoteCharacteristic->registerForNotify (notifyCallback);
86
+ if (pRemoteCharacteristic->canNotify ())
87
+ pRemoteCharacteristic->registerForNotify (notifyCallback);
88
+
89
+ connected = true ;
65
90
}
66
91
/* *
67
92
* Scan for BLE servers and find the first one that advertises the service we are looking for.
@@ -75,14 +100,12 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
75
100
Serial.println (advertisedDevice.toString ().c_str ());
76
101
77
102
// We have found a device, let us now see if it contains the service we are looking for.
78
- if (advertisedDevice.haveServiceUUID () && advertisedDevice.getServiceUUID ().equals (serviceUUID)) {
79
-
80
- //
81
- Serial.print (" Found our device! address: " );
82
- advertisedDevice.getScan ()->stop ();
103
+ if (advertisedDevice.haveServiceUUID () && advertisedDevice.isAdvertisingService (serviceUUID)) {
83
104
84
- pServerAddress = new BLEAddress (advertisedDevice.getAddress ());
105
+ BLEDevice::getScan ()->stop ();
106
+ myDevice = new BLEAdvertisedDevice (advertisedDevice);
85
107
doConnect = true ;
108
+ doScan = true ;
86
109
87
110
} // Found our server
88
111
} // onResult
@@ -96,11 +119,13 @@ void setup() {
96
119
97
120
// Retrieve a Scanner and set the callback we want to use to be informed when we
98
121
// have detected a new device. Specify that we want active scanning and start the
99
- // scan to run for 30 seconds.
122
+ // scan to run for 5 seconds.
100
123
BLEScan* pBLEScan = BLEDevice::getScan ();
101
124
pBLEScan->setAdvertisedDeviceCallbacks (new MyAdvertisedDeviceCallbacks ());
125
+ pBLEScan->setInterval (1349 );
126
+ pBLEScan->setWindow (449 );
102
127
pBLEScan->setActiveScan (true );
103
- pBLEScan->start (30 );
128
+ pBLEScan->start (5 , false );
104
129
} // End of setup.
105
130
106
131
@@ -111,9 +136,8 @@ void loop() {
111
136
// BLE Server with which we wish to connect. Now we connect to it. Once we are
112
137
// connected we set the connected flag to be true.
113
138
if (doConnect == true ) {
114
- if (connectToServer (*pServerAddress )) {
139
+ if (connectToServer ()) {
115
140
Serial.println (" We are now connected to the BLE Server." );
116
- connected = true ;
117
141
} else {
118
142
Serial.println (" We have failed to connect to the server; there is nothin more we will do." );
119
143
}
@@ -128,7 +152,9 @@ void loop() {
128
152
129
153
// Set the characteristic's value to be the array of bytes that is actually a string.
130
154
pRemoteCharacteristic->writeValue (newValue.c_str (), newValue.length ());
155
+ }else if (doScan){
156
+ BLEDevice::getScan ()->start (0 ); // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
131
157
}
132
158
133
159
delay (1000 ); // Delay a second between loops.
134
- } // End of loop
160
+ } // End of loop
0 commit comments