Skip to content

Commit e239fc7

Browse files
committed
Jira788 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 1357f52 commit e239fc7

File tree

3 files changed

+161
-2
lines changed

3 files changed

+161
-2
lines changed
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+
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

+3-2
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)