Skip to content

Commit 521d1db

Browse files
committed
Support Notify/Indicate feature
1. Add the Notify/Indicate feature 2. Add the notify central/peripheral example sketches
1 parent 35027d3 commit 521d1db

File tree

10 files changed

+364
-246
lines changed

10 files changed

+364
-246
lines changed

libraries/BLE/examples/central/central.ino

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
#define LED_PIN 13
99

1010
void setup() {
11-
Serial1.begin(115200);
12-
Serial1.println("test---");
11+
Serial.begin(115200);
12+
Serial.println("test---");
1313

1414
// set LED pin to output mode
1515
pinMode(LED_PIN, OUTPUT);
1616

1717
// begin initialization
1818
BLE.begin();
19-
Serial1.println(BLE.address());
19+
Serial.println(BLE.address());
2020

2121
BLE.startScanning("LED");
2222
}
@@ -25,17 +25,17 @@ void controlLed(BLEDevice &peripheral)
2525
{
2626
static bool discovered = false;
2727
// connect to the peripheral
28-
Serial1.print("Connecting ... ");
29-
Serial1.println(peripheral.address());
28+
Serial.print("Connecting ... ");
29+
Serial.println(peripheral.address());
3030

3131
if (peripheral.connect())
3232
{
33-
Serial1.print("Connected: ");
34-
Serial1.println(peripheral.address());
33+
Serial.print("Connected: ");
34+
Serial.println(peripheral.address());
3535
}
3636
else
3737
{
38-
Serial1.println("Failed to connect!");
38+
Serial.println("Failed to connect!");
3939
return;
4040
}
4141

@@ -48,7 +48,7 @@ void controlLed(BLEDevice &peripheral)
4848
//peripheral.disconnect();
4949
while(1)
5050
{
51-
Serial1.println("Peripheral does not have LED characteristic!");
51+
Serial.println("Peripheral does not have LED characteristic!");
5252
delay(5000);
5353
}
5454
return;
@@ -71,8 +71,8 @@ void controlLed(BLEDevice &peripheral)
7171
ledCharacteristic.write(&ledstate, sizeof(ledstate));
7272
delay(5000);
7373
}
74-
Serial1.print("Disconnected");
75-
Serial1.println(peripheral.address());
74+
Serial.print("Disconnected");
75+
Serial.println(peripheral.address());
7676
}
7777

7878
void loop() {
@@ -81,7 +81,7 @@ void loop() {
8181
//pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__);
8282
if (peripheral)
8383
{
84-
Serial1.println(peripheral.address());
84+
Serial.println(peripheral.address());
8585
BLE.stopScanning();
8686
delay (1000);
8787
// central connected to peripheral
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
#include "ArduinoBLE.h"
3+
#include "BLEAttribute.h"
4+
#include "BLECharacteristicImp.h"
5+
#include "BLEProfileManager.h"
6+
7+
// LED pin
8+
#define LED_PIN 13
9+
10+
// create service
11+
BLEService ledService("19b10100e8f2537e4f6cd104768a1214");
12+
13+
BLECharacteristic switchCharacteristic("19b10101e8f2537e4f6cd104768a1214", BLERead | BLEWrite | BLENotify, 1);
14+
15+
BLEDescriptor switchDescriptor("2901", "switch");
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
Serial.println("test---");
20+
21+
// set LED pin to output mode
22+
pinMode(LED_PIN, OUTPUT);
23+
24+
// begin initialization
25+
BLE.begin();
26+
Serial.println(BLE.address());
27+
28+
// set advertised local name and service UUID
29+
BLE.setLocalName("LED");
30+
BLE.setAdvertisedServiceUuid(ledService.uuid());
31+
32+
// add service and characteristic
33+
BLE.addService(ledService);
34+
ledService.addCharacteristic(switchCharacteristic);
35+
switchCharacteristic.addDescriptor(switchDescriptor);
36+
unsigned char test = 1;
37+
switchCharacteristic.writeValue(&test,1);
38+
BLE.startAdvertising();
39+
}
40+
41+
void loop() {
42+
static int i = 0;
43+
BLEDevice central = BLE.central();
44+
bool temp = central;
45+
i++;
46+
if (temp) {
47+
// central connected to peripheral
48+
Serial.print(i);
49+
Serial.print(F("Connected to central: "));
50+
Serial.println(central.address());
51+
52+
Serial.print(temp);
53+
54+
unsigned char ledstate = 0;
55+
56+
while (central.connected()) {
57+
// central still connected to peripheral
58+
if (switchCharacteristic.canNotify())
59+
{
60+
61+
if (ledstate == 1)
62+
{
63+
ledstate = 0;
64+
digitalWrite(LED_PIN, LOW);
65+
}
66+
else
67+
{
68+
ledstate = 1;
69+
digitalWrite(LED_PIN, HIGH);
70+
}
71+
switchCharacteristic.writeValue(&ledstate, sizeof(ledstate));
72+
delay(5000);
73+
}
74+
}
75+
76+
// central disconnected
77+
Serial.print(F("Disconnected from central: "));
78+
Serial.println(central.address());
79+
}
80+
//delay (1000);
81+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
#include "ArduinoBLE.h"
3+
#include "BLEAttribute.h"
4+
#include "BLECharacteristicImp.h"
5+
#include "BLEProfileManager.h"
6+
7+
// LED pin
8+
#define LED_PIN 13
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
Serial.println("test---");
13+
14+
// set LED pin to output mode
15+
pinMode(LED_PIN, OUTPUT);
16+
17+
// begin initialization
18+
BLE.begin();
19+
Serial.println(BLE.address());
20+
21+
BLE.startScanning("LED");
22+
}
23+
24+
void controlLed(BLEDevice &peripheral)
25+
{
26+
static bool discovered = false;
27+
// connect to the peripheral
28+
Serial.print("Connecting ... ");
29+
Serial.println(peripheral.address());
30+
31+
if (peripheral.connect())
32+
{
33+
Serial.print("Connected: ");
34+
Serial.println(peripheral.address());
35+
}
36+
else
37+
{
38+
Serial.println("Failed to connect!");
39+
return;
40+
}
41+
42+
peripheral.discoverAttributes();
43+
44+
BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10101-e8f2-537e-4f6c-d104768a1214");
45+
46+
if (!ledCharacteristic)
47+
{
48+
//peripheral.disconnect();
49+
while(1)
50+
{
51+
Serial.println("Peripheral does not have LED characteristic!");
52+
delay(5000);
53+
}
54+
return;
55+
}
56+
ledCharacteristic.subscribe();
57+
58+
59+
discovered = false;
60+
while (peripheral.connected())
61+
{
62+
if (ledCharacteristic.valueUpdated())
63+
{
64+
char ledValue = *ledCharacteristic.value();
65+
66+
if (ledValue) {
67+
Serial.println(F("LED on"));
68+
digitalWrite(LED_PIN, HIGH);
69+
} else {
70+
Serial.println(F("LED off"));
71+
digitalWrite(LED_PIN, LOW);
72+
}
73+
}
74+
}
75+
Serial.print("Disconnected");
76+
Serial.println(peripheral.address());
77+
}
78+
79+
void loop() {
80+
//pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__);
81+
BLEDevice peripheral = BLE.available();
82+
//pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__);
83+
if (peripheral)
84+
{
85+
Serial.println(peripheral.address());
86+
BLE.stopScanning();
87+
delay (1000);
88+
// central connected to peripheral
89+
controlLed(peripheral);
90+
delay (4000);
91+
BLE.startScanning("LED");
92+
}
93+
}
94+
95+

libraries/BLE/examples/test/test.ino

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ BLECharacteristic switchCharacteristic("19b10101e8f2537e4f6cd104768a1214", BLE
1515
BLEDescriptor switchDescriptor("2901", "switch");
1616

1717
void setup() {
18-
Serial1.begin(115200);
19-
Serial1.println("test---");
18+
Serial.begin(115200);
19+
Serial.println("test---");
2020

2121
// set LED pin to output mode
2222
pinMode(LED_PIN, OUTPUT);
2323

2424
// begin initialization
2525
BLE.begin();
26-
Serial1.println(BLE.address());
26+
Serial.println(BLE.address());
2727

2828
// set advertised local name and service UUID
2929
BLE.setLocalName("LED");
@@ -45,30 +45,30 @@ void loop() {
4545
i++;
4646
if (temp) {
4747
// central connected to peripheral
48-
Serial1.print(i);
49-
Serial1.print(F("Connected to central: "));
50-
Serial1.println(central.address());
48+
Serial.print(i);
49+
Serial.print(F("Connected to central: "));
50+
Serial.println(central.address());
5151

52-
Serial1.print(temp);
52+
Serial.print(temp);
5353

5454
while (central.connected()) {
5555
// central still connected to peripheral
5656
if (switchCharacteristic.written()) {
5757
char ledValue = *switchCharacteristic.value();
5858
// central wrote new value to characteristic, update LED
5959
if (ledValue) {
60-
Serial1.println(F("LED on"));
60+
Serial.println(F("LED on"));
6161
digitalWrite(LED_PIN, HIGH);
6262
} else {
63-
Serial1.println(F("LED off"));
63+
Serial.println(F("LED off"));
6464
digitalWrite(LED_PIN, LOW);
6565
}
6666
}
6767
}
6868

6969
// central disconnected
70-
Serial1.print(F("Disconnected from central: "));
71-
Serial1.println(central.address());
70+
Serial.print(F("Disconnected from central: "));
71+
Serial.println(central.address());
7272
}
7373
//delay (1000);
7474
}

libraries/BLE/src/BLECallbacks.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,14 @@ uint8_t profile_notify_process (bt_conn_t *conn,
9797
//BLEAttribute* notifyatt = peripheral->attribute(params); // Find attribute by params
9898
BLECharacteristicImp* chrc = NULL;
9999
BLEDevice bleDevice(bt_conn_get_dst(conn));
100-
BLEProfileManager::instance()->characteristic(bleDevice, params->value_handle);
100+
chrc = BLEProfileManager::instance()->characteristic(bleDevice, params->value_handle);
101101

102102
//assert(notifyatt->type() == BLETypeCharacteristic);
103103
pr_debug(LOG_MODULE_APP, "%s1", __FUNCTION__);
104-
105-
chrc->setValue((const unsigned char *)data, length);
104+
if (NULL != chrc)
105+
{
106+
chrc->setValue((const unsigned char *)data, length);
107+
}
106108
return BT_GATT_ITER_CONTINUE;
107109
}
108110

libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ bool BLECharacteristic::writeValue(const byte value[], int length)
127127

128128
if (NULL != characteristicImp)
129129
{
130-
characteristicImp->setValue((const unsigned char *)value, (uint16_t)length);
130+
characteristicImp->writeValue(value, length);
131131
retVar = true;
132132
}
133133
return retVar;
@@ -189,8 +189,14 @@ bool BLECharacteristic::canNotify()
189189

190190
bool BLECharacteristic::canIndicate()
191191
{
192-
// TODO: Need more confirmation
193-
return false;
192+
bool retVar = false;
193+
BLECharacteristicImp *characteristicImp = getImplementation();
194+
195+
if (NULL != characteristicImp)
196+
{
197+
retVar = characteristicImp->canIndicate();
198+
}
199+
return retVar;
194200
}
195201

196202
bool BLECharacteristic::canRead()
@@ -245,8 +251,7 @@ bool BLECharacteristic::subscribe()
245251

246252
if (NULL != characteristicImp)
247253
{
248-
// TODO: Central feature. Peripheral first
249-
//retVar = characteristicImp->s();
254+
retVar = characteristicImp->subscribe();
250255
}
251256
return retVar;
252257
}
@@ -258,8 +263,7 @@ bool BLECharacteristic::unsubscribe()
258263

259264
if (NULL != characteristicImp)
260265
{
261-
// TODO: Central feature
262-
//retVar = characteristicImp->canNotify();
266+
retVar = characteristicImp->unsubscribe();
263267
}
264268
return retVar;
265269
}

0 commit comments

Comments
 (0)