8000 BLE Peripheral cannot discover Central attributes · arduino/ArduinoCore-arc32@6073bd5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6073bd5

Browse files
sgbihuSidLeung
authored andcommitted
BLE Peripheral cannot discover Central attributes
1. Add test sketches discoveratperipheral.ino and profileatcentral.ino 2. Modify BLEDeviceManager.cpp to add central to profile buffer in peripheral role.
1 parent 29daefb commit 6073bd5

File tree

5 files changed

+361
-2
lines changed

5 files changed

+361
-2
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
* See the bottom of this file for the license terms.
4+
*/
5+
6+
/*
7+
* Sketch: CentralDouble.ino.
8+
*
9+
* Description: This is a simple BLE sketch that initiates the
10+
* Arduino platform as a Central. It reads a double value from
11+
* a connected Peripheral. The sketch exercises: Scanning for
12+
* a specific Peripheral, connecting to it, discover its Attributes,
13+
* and exercise a specific Characteristic to read a double value
14+
* from the Peripheral.
15+
*
16+
* Notes: Expected Peripheral name: DataTest
17+
* Expected Peripheral Characteristic: 19b20001e8f2537e4f6cd104768a1214
18+
* Expected Characteristic read value: double.
19+
*/
20+
21+
#include "CurieBLE.h"
22+
23+
24+
// LED pin
25+
#define LED_PIN 13
26+
27+
void setup() {
28+
Serial.begin(9600);
29+
30+
// set LED pin to output mode
31+
pinMode(LED_PIN, OUTPUT);
32+
33+
// begin initialization
34+
BLE.begin();
35+
Serial.println(BLE.address());
36+
37+
BLE.scanForName("DataTest");
38+
}
39+
40+
void loop() {
41+
BLEDevice peripheral = BLE.available();
42+
if (peripheral)
43+
{
44+
Serial.println(peripheral.address());
45+
BLE.stopScan();
46+
// central connected to peripheral
47+
controlLogic(peripheral);
48+
BLE.scanForName("DataTest");
49+
}
50+
}
51+
52+
53+
void controlLogic(BLEDevice &peripheral)
54+
{
55+
// connect to the peripheral
56+
Serial.print("Connecting ... ");
57+
Serial.println(peripheral.address());
58+
59+
if (peripheral.connect())
60+
{
61+
Serial.print("Connected: ");
62+
Serial.println(peripheral.address());
63+
}
64+
else
65+
{
66+
Serial.println("Failed to connect!");
67+
return;
68+
}
69+
70+
if (peripheral.discoverAttributes() == false)
71+
{
72+
Serial.println("Discover failed, Disconnecting...");
73+
peripheral.disconnect();
74+
return;
75+
}
76+
77+
BLECharacteristic doubleCharacteristic = peripheral.characteristic("19b20001e8f2537e4f6cd104768a1214");
78+
79+
if (!doubleCharacteristic)
80+
{
81+
peripheral.disconnect();
82+
Serial.println("Peripheral does not have test double characteristic!");
83+
delay(5000);
84+
return;
85+
}
86+
doubleCharacteristic.subscribe();
87+
88+
while (peripheral.connected())
89+
{
90+
doubleCharacteristic.read();
91+
delay(1000);
92+
if (doubleCharacteristic.valueUpdated())
93+
{
94+
Serial.print("Double characteristic value: ");
95+
Serial.println(doubleCharacteristic.doubleValue());
96+
}
97+
delay(1000);
98+
}
99+
Serial.print("Disconnected");
100+
Serial.println(peripheral.address());
101+
}
102+
103+
104+
105+
/*
106+
Arduino BLE Peripheral Double read/write test example
107+
Copyright (c) 2016 Arduino LLC. All right reserved.
108+
109+
This library is free software; you can redistribute it and/or
110+
modify it under the terms of the GNU Lesser General Public
111+
License as published by the Free Software Foundation; either
112+
version 2.1 of the License, or (at your option) any later version.
113+
114+
This library is distributed in the hope that it will be useful,
115+
but WITHOUT ANY WARRANTY; without even the implied warranty of
116+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
117+
Lesser General Public License for more details.
118+
119+
You should have received a copy of the GNU Lesser General Public
120+
License along with this library; if not, write to the Free Software
121+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
122+
*/
123+
124+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Arduino BLE Peripheral Double read/write test example
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <CurieBLE.h>
21+
22+
// LED pin
23+
#define LED_PIN 13
24+
25+
// create service
26+
BLEService dataService("19b20000e8f2537e4f6cd104768a1214");
27+
28+
// create data characteristic
29+
BLEDoubleCharacteristic doubleCharacteristic("19b20001e8f2537e4f6cd104768a1214", BLERead | BLEWrite);
30+
31+
void setup() {
32+
Serial.begin(9600);
33+
34+
// set LED pin to output mode
35+
pinMode(LED_PIN, OUTPUT);
36+
37+
// begin initialization
38+
BLE.begin();
39+
Serial.println(BLE.address());
40+
41+
// set advertised local name and service UUID
42+
BLE.setLocalName("DataTest");
43+
44+
dataService.addCharacteristic(doubleCharacteristic);
45+
46+
// add service and characteristic
47+
BLE.addService(dataService);
48+
49+
BLE.advertise();
50+
51+
Serial.println(F("BLE Test Double Peripheral"));
52+
}
53+
54+
void loop() {
55+
BLEDevice central = BLE.central();
56+
double test_value = 0.1;
57+
58+
if (central) {
59+
// central connected to peripheral
60+
Serial.print(F("Connected to central: "));
61+
Serial.println(central.address());
62+
63+
while (central.connected())
64+
{
65+
// central still connected to peripheral
66+
doubleCharacteristic.writeDouble(test_value);
67+
test_value += 0.3;
68+
delay(2000);
69+
}
70+
71+
// central disconnected
72+
Serial.print(F("Disconnected from central: "));
73+
Serial.println(central.address());
74+
}
75+
}
76+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Arduino BLE Peripheral LED example
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <CurieBLE.h>
21+
22+
// LED pin
23+
#define LED_PIN 13
24+
25+
// create service
26+
BLEService ledService("19b10000e8f2537e4f6cd104768a1214");
27+
28+
void setup() {
29+
Serial.begin(9600);
30+
31+
// set LED pin to output mode
32+
pinMode(LED_PIN, OUTPUT);
33+
34+
// begin initialization
35+
BLE.begin();
36+
Serial.println(BLE.address());
37+
38+
// set advertised local name and service UUID
39+
BLE.setLocalName("LED");
40+
41+
BLE.advertise();
42+
43+
Serial.println(F("BLE LED Peripheral"));
44+
}
45+
46+
void loop() {
47+
BLEDevice central = BLE.central();
48+
49+
if (central) {
50+
// central connected to peripheral
51+
Serial.print(F("Connected to central: "));
52+
Serial.println(central.address());
53+
54+
controlLed(central);
55+
// central disconnected
56+
Serial.print(F("Disconnected from central: "));
57+
Serial.println(central.address());
58+
}
59+
}
60+
61+
void controlLed(BLEDevice &central)
62+
{
63+
if (central.discoverAttributes() == false)
64+
{
65+
Serial.println("Discover failed, Disconnecting...");
66+
central.disconnect();
67+
return;
68+
}
69+
70+
BLECharacteristic ledCharacteristic = central.characteristic("19b10101-e8f2-537e-4f6c-d104768a1214");
71+
72+
if (!ledCharacteristic)
73+
{
74+
central.disconnect();
75+
//while(1)
76+
{
77+
Serial.println("Central does not have LED characteristic!");
78+
delay(5000);
79+
}
80+
return;
81+
}
82+
83+
ledCharacteristic.subscribe();
84+
85+
unsigned char ledstate = 0;
86+
87+
while (central.connected())
88+
{
89+
if (ledstate == 1)
90+
{
91+
ledstate = 0;
92+
}
93+
else
94+
{
95+
ledstate = 1;
96+
}
97+
ledCharacteristic.write(&ledstate, sizeof(ledstate));
98+
delay(5000);
99+
}
100+
Serial.print("Disconnected");
101+
Serial.println(central.address());
102+
}
103+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
#include "CurieBLE.h"
3+
#include <errno.h>
4+
// LED pin
5+
#define LED_PIN 13
6+
BLEService ledService("19b10100e8f2537e4f6cd104768a1214");
7+
8+
BLECharacteristic switchCharacteristic("19b10101e8f2537e4f6cd104768a1214", BLERead | BLEWrite | BLENotify, 1);
9+
10+
BLEDescriptor switchDescriptor("2901", "switch");
11+
12+
void setup() {
13+
Serial.begin(115200);
14+
15+
// set LED pin to output mode
16+
pinMode(LED_PIN, OUTPUT);
17+
18+
// begin initialization
19+
BLE.begin();
20+
Serial.println(BLE.address());
21+
ledService.addCharacteristic(switchCharacteristic);
22+
switchCharacteristic.addDescriptor(switchDescriptor);
23+
BLE.addService(ledService);
24+
25+
BLE.scanForName("LED");
26+
}
27+
28+
29+
void loop() {
30+
BLEDevice peripheral = BLE.available();
31+
if (peripheral)
32+
{
33+
Serial.println(peripheral.address());
34+
35+
BLE.stopScan();
36+
37+
if (peripheral.connect())
38+
{
39+
Serial.print("Connected: ");
40+
Serial.println(peripheral.address());
41+
while (peripheral.connected())
42+
{
43+
delay (1000);
44+
}
45+
}
46+
else
47+
{
48+
Serial.println("Failed to connect!");
49+
}
50+
delay (4000);
51+
BLE.scanForName("LED");
52+
}
53+
}
54+
55+

libraries/CurieBLE/src/internal/BLEDeviceManager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,11 +1051,12 @@ void BLEDeviceManager::handleConnectEvent(bt_conn_t *conn, uint8_t err)
10511051
}
10521052
else
10531053
{
1054+
// Peripheral has established the connection with this Central device
10541055
memset(&_wait_for_connect_peripheral, 0, sizeof(_wait_for_connect_peripheral));
10551056
_connecting = false;
1056-
// Peripheral has established the connection with this Central device
1057-
BLEProfileManager::instance()->handleConnectedEvent(bt_conn_get_dst(conn));
10581057
}
1058+
// The peripheral and central can work as GATT server. Reserve one buffer for peer device
1059+
BLEProfileManager::instance()->handleConnectedEvent(bt_conn_get_dst(conn));
10591060

10601061
if (NULL != _device_events[BLEConnected])
10611062
{

0 commit comments

Comments
 (0)
0