Skip to content

Commit f48c8aa

Browse files
committed
1. Add central read/write example.
2. Fix some build error for refactor the peripheral code.
1 parent 53f4742 commit f48c8aa

File tree

6 files changed

+208
-26
lines changed

6 files changed

+208
-26
lines changed

libraries/CurieBLE/examples/CallbackLED/CallbackLED.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ void loop() {
4545
blePeripheral.poll();
4646
}
4747

48-
void blePeripheralConnectHandler(BLECentral& central) {
48+
void blePeripheralConnectHandler(BLEHelper& central) {
4949
// central connected event handler
5050
Serial.print("Connected event, central: ");
5151
Serial.println(central.address());
5252
}
5353

54-
void blePeripheralDisconnectHandler(BLECentral& central) {
54+
void blePeripheralDisconnectHandler(BLEHelper& central) {
5555
// central disconnected event handler
5656
Serial.print("Disconnected event, central: ");
5757
Serial.println(central.address());
5858
}
5959

60-
void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
60+
void switchCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic) {
6161
// central wrote new value to characteristic, update LED
6262
Serial.print("Characteristic event, written: ");
6363

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
Copyright (c) 2016 Intel Corporation. All rights reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
17+
1301 USA
18+
*/
19+
20+
// This example can work with CallbackLED to show the profile read/write operation in central
21+
#include <CurieBLE.h>
22+
23+
struct bt_le_conn_param conn_param = {0x18, 0x28, 0, 400};
24+
25+
const int ledPin = 13; // set ledPin to use on-board LED
26+
BLECentral bleCentral; // create central instance
27+
BLEPeripheralHelper *blePeripheral1 = NULL;
28+
29+
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
30+
// create switch characteristic and allow remote device to read and write
31+
BLECharCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
32+
33+
34+
void bleCentralConnectHandler(BLEHelper& peripheral)
35+
{
36+
// peripheral connected event handler
37+
blePeripheral1 = (BLEPeripheralHelper *)(&peripheral);
38+
Serial.print("Connected event, peripheral: ");
39+
Serial.println(peripheral.address());
40+
// Start discovery the profiles in peripheral device
41+
blePeripheral1->discover();
42+
}
43+
44+
void bleCentralDisconnectHandler(BLEHelper& peripheral)
45+
{
46+
// peripheral disconnected event handler
47+
blePeripheral1 = NULL;
48+
Serial.print("Disconnected event, peripheral: ");
49+
Serial.println(peripheral.address());
50+
bleCentral.startScan();
51+
}
52+
53+
void switchCharacteristicWritten(BLEHelper& peripheral, BLECharacteristic& characteristic)
54+
{
55+
// Read response/Notification wrote new value to characteristic, update LED
56+
Serial.print("Characteristic event, written: ");
57+
58+
if (switchChar.value())
59+
{
60+
Serial.println("LED on");
61+
digitalWrite(ledPin, HIGH);
62+
}
63+
else
64+
{
65+
Serial.println("LED off");
66+
digitalWrite(ledPin, LOW);
67+
}
68+
}
69+
70+
bool adv_found(uint8_t type,
71+
const uint8_t *data,
72+
uint8_t data_len,
73+
void *user_data)
74+
{
75+
bt_addr_le_t *addr = (bt_addr_le_t *)user_data;
76+
int i;
77+
78+
pr_debug(LOG_MODULE_APP, "[AD]: %u data_len %u\n", type, data_len);
79+
80+
switch (type)
81+
{
82+
case BT_DATA_UUID128_SOME:
83+
case BT_DATA_UUID128_ALL:
84+
{
85+
if (data_len % MAX_UUID_SIZE != 0)
86+
{
87+
pr_debug(LOG_MODULE_APP, "AD malformed\n");
88+
return true;
89+
}
90+
struct bt_uuid * serviceuuid = ledService.uuid();
91+
for (i = 0; i < data_len; i += MAX_UUID_SIZE)
92+
{
93+
if (memcmp (((struct bt_uuid_128*)serviceuuid)->val, &data[i], MAX_UUID_SIZE) != 0)
94+
{
95+
continue;
96+
}
97+
98+
// Accept the advertisement
99+
if (!bleCentral.stopScan())
100+
{
101+
pr_debug(LOG_MODULE_APP, "Stop LE scan failed\n");
102+
continue;
103+
}
104+
pr_debug(LOG_MODULE_APP, "Connecting\n");
105+
// Connect to peripheral
106+
bleCentral.connect(addr, &conn_param);
107+
return false;
108+
}
109+
}
110+
}
111+
112+
return true;
113+
}
114+
115+
void setup() {
116+
Serial1.begin(115200);
117+
118+
Serial.begin(9600);
119+
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
120+
121+
// add service and characteristic
122+
bleCentral.addAttribute(ledService);
123+
bleCentral.addAttribute(switchChar);
124+
125+
// assign event handlers for connected, disconnected to central
126+
bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler);
127+
bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler);
128+
129+
// advertise the service
130+
bleCentral.setAdvertiseHandler(adv_found);
131+
132+
// assign event handlers for characteristic
133+
switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
134+
135+
bleCentral.begin();
136+
Serial.println(("Bluetooth device active, waiting for connections..."));
137+
}
138+
139+
void loop()
140+
{
141+
static unsigned char counter = 0;
142+
static char ledstate = 0;
143+
delay(2000);
144+
if (blePeripheral1)
145+
{
146+
counter++;
147+
148+
if (counter % 3)
149+
{
150+
switchChar.read(*blePeripheral1);
151+
}
152+
else
153+
{
154+
ledstate = !ledstate;
155+
switchChar.write(*blePeripheral1, (unsigned char *)(&ledstate), sizeof (ledstate));
156+
}
157+
}
158+
}

libraries/CurieBLE/examples/MIDIBLE/MIDIBLE.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,19 @@ void loop() {
140140
}
141141

142142

143-
void midiDeviceConnectHandler(BLECentral& central) {
143+
void midiDeviceConnectHandler(BLEHelper& central) {
144144
// central connected event handler
145145
Serial.print("Connected event, central: ");
146146
Serial.println(central.address());
147147
}
148148

149-
void midiDeviceDisconnectHandler(BLECentral& central) {
149+
void midiDeviceDisconnectHandler(BLEHelper& central) {
150150
// central disconnected event handler
151151
Serial.print("Disconnected event, central: ");
152152
Serial.println(central.address());
153153
}
154154

155-
void midiCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
155+
void midiCharacteristicWritten(BLEHelper& central, BLECharacteristic& characteristic) {
156156
// central wrote new value to characteristic, update LED
157157
Serial.print("Characteristic event, written: ");
158158
}

libraries/CurieBLE/src/BLECharacteristic.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ BLECharacteristic::setValue(BLEHelper& blehelper, const unsigned char* value, un
138138
_setValue(value, length);
139139

140140
_written = true;
141+
_reading = false;
141142

142143
if (_event_handlers[BLEWritten]) {
143144
_event_handlers[BLEWritten](blehelper, *this);
@@ -300,6 +301,8 @@ void BLECharacteristic::discover(struct bt_gatt_discover_params *params)
300301
params->uuid = this->uuid();
301302
// Start discovering
302303
_discoverying = true;
304+
// Re-Init the read/write parameter
305+
_reading = false;
303306
}
304307

305308

@@ -318,8 +321,17 @@ void BLECharacteristic::discover(const struct bt_gatt_attr *attr,
318321
{
319322
// Set Discover CCCD parameter
320323
params->start_handle = attr->handle + 2;
321-
params->type = BT_GATT_DISCOVER_DESCRIPTOR;
322-
params->uuid = this->getClientCharacteristicConfigUuid();
324+
if (subscribed())
325+
{
326+
// Include CCCD
327+
params->type = BT_GATT_DISCOVER_DESCRIPTOR;
328+
params->uuid = this->getClientCharacteristicConfigUuid();
329+
}
330+
else
331+
{
332+
// Complete the discover
333+
_discoverying = false;
334+
}
323335
}
324336
else if (params->uuid == this->getClientCharacteristicConfigUuid())
325337
{
@@ -347,6 +359,13 @@ bool BLECharacteristic::read(BLEPeripheralHelper &peripheral)
347359
_read_params.handle_count = 1;
348360
_read_params.single.handle = peripheral.valueHandle(this);
349361
_read_params.single.offset = 0;
362+
363+
if (0 == _read_params.single.handle)
364+
{
365+
// Discover not complete
366+
return false;
367+
}
368+
350369
conn = bt_conn_lookup_addr_le(peripheral.bt_le_address());
351370
if (NULL == conn)
352371
{
@@ -363,38 +382,25 @@ bool BLECharacteristic::read(BLEPeripheralHelper &peripheral)
363382
return _reading;
364383
}
365384

366-
#if 0
367385
bool BLECharacteristic::write(BLEPeripheralHelper &peripheral,
368386
const unsigned char value[],
369387
uint16_t length)
370388
{
371389
int retval = 0;
372390
struct bt_conn* conn = NULL;
373-
if (_reading)
374-
{
375-
// Already in reading state
376-
return false;
377-
}
378391

379-
_read_params.func = profile_read_rsp_process;
380-
_read_params.handle_count = 1;
381-
_read_params.single.handle = _attr_chrc_value->handle;
382-
_read_params.single.offset = 0;
383392
conn = bt_conn_lookup_addr_le(peripheral.bt_le_address());
384393
if (NULL == conn)
385394
{
386395
return false;
387396
}
388397

389398
// Send read request
390-
retval = bt_gatt_write_without_response(conn, &_read_params);
399+
retval = bt_gatt_write_without_response(conn,
400+
peripheral.valueHandle(this),
401+
value, length, false);
391402
bt_conn_unref(conn);
392-
if (0 == retval)
393-
{
394-
_reading = true;
395-
}
396-
return _reading;
403+
return (0 == retval);
397404
}
398-
#endif
399405

400406

libraries/CurieBLE/src/BLECharacteristic.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class BLECharacteristic : public BLEAttribute {
173173
static unsigned char numNotifyAttributes(void);
174174

175175
/**
176-
* @brief Schedue the read request to read the characteristic in peripheral
176+
* @brief Schedule the read request to read the characteristic in peripheral
177177
*
178178
* @param peripheral The peripheral device that want to read.
179179
*
@@ -182,6 +182,22 @@ class BLECharacteristic : public BLEAttribute {
182182
* @note Only for central device
183183
*/
184184
bool read(BLEPeripheralHelper &peripheral);
185+
186+
/**
187+
* @brief Schedule the write request to update the characteristic in peripheral
188+
*
189+
* @param peripheral The peripheral device that want to be updated
190+
* @param value New value to set, as a byte array. Data is stored in internal copy.
191+
* @param length Length, in bytes, of valid data in the array to write.
192+
* Must not exceed maxLength set for this characteristic.
193+
*
194+
* @return bool true set value success, false on error
195+
*
196+
* @note none
197+
*/
198+
bool write(BLEPeripheralHelper &peripheral,
199+
const unsigned char value[],
200+
uint16_t length);
185201

186202
protected:
187203
friend class BLEProfile;

libraries/CurieBLE/src/BLERoleBase.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ void bleDisconnectEventHandler(struct bt_conn *conn,
1818
void *param)
1919
{
2020
BLERoleBase* p = (BLERoleBase*)param;
21+
22+
pr_info(LOG_MODULE_BLE, "Connect lost. Reason: %d", reason);
2123

2224
p->handleDisconnectEvent(conn, reason);
2325
}

0 commit comments

Comments
 (0)