Skip to content

Commit 5fc779b

Browse files
committed
Add BLEPeripheral library back compatible features
1 parent 30b88ef commit 5fc779b

11 files changed

+858
-5
lines changed

libraries/BLE/src/ArduinoBLE.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class BLECharacteristicImp;
3636
#include "BLEDescriptor.h"
3737
#include "BLEService.h"
3838

39+
#include "BLETypedCharacteristics.h"
40+
41+
#include "BLECentral.h"
42+
#include "BLEPeripheral.h"
3943

4044
extern BLEDevice BLE;
4145

libraries/BLE/src/BLECentral.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
BLE Central API (deprecated)
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 "ArduinoBLE.h"
21+
22+
BLECentral::BLECentral(BLEDevice& device) :
23+
_device(&device)
24+
{
25+
26+
}
27+
28+
bool BLECentral::connected(void)
29+
{
30+
return _device.connected();
31+
}
32+
33+
const char* BLECentral::address(void) const
34+
{
35+
return _device.address().c_str();
36+
}
37+
38+
bool BLECentral::disconnect(void)
39+
{
40+
return _device.disconnect();
41+
}
42+
43+
void BLECentral::poll(void)
44+
{
45+
_device.poll();
46+
}
47+
48+
BLECentral::operator bool(void) const
49+
{
50+
return _device;
51+
}
52+
53+
bool BLECentral::operator==(const BLECentral& rhs) const
54+
{
55+
return (_device == rhs._device);
56+
}
57+
58+
bool BLECentral::operator!=(const BLECentral& rhs) const
59+
{
60+
return (_device != rhs._device);
61+
}

libraries/BLE/src/BLECentral.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
BLE Central API (deprecated)
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+
#ifndef ARDUINO_CENTRAL_H
21+
#define ARDUINO_CENTRAL_H
22+
23+
class BLECentral {
24+
public:
25+
bool connected(void); // is the central connected
26+
27+
const char* address(void) const; // address of the Central in string form
28+
29+
bool disconnect(void); // Disconnect the central if it is connected
30+
void poll(void); // Poll the central for events
31+
32+
operator bool(void) const;
33+
bool operator==(const BLECentral& rhs) const;
34+
bool operator!=(const BLECentral& rhs) const;
35+
protected:
36+
friend void bleBackCompatiblePeripheralConnectHandler(BLEDevice central);
37+
friend void bleBackCompatiblePeripheralDisconnectHandler(BLEDevice central);
38+
friend class BLEPeripheral;
39+
BLECentral(BLEDevice& device);
40+
private:
41+
42+
BLEDevice _device;
43+
};
44+
45+
#endif

libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22
//#include "internal/ble_client.h"
3+
#include "ArduinoBLE.h"
4+
5+
#include "BLEUtils.h"
36

47
#include "BLECharacteristic.h"
58
#include "BLEProfileManager.h"
6-
#include "BLEUtils.h"
79

810
#include "BLECharacteristicImp.h"
911

@@ -87,10 +89,10 @@ int BLECharacteristic::valueSize() //const
8789
return valuesize;
8890
}
8991

90-
const byte* BLECharacteristic::value() //const
92+
const byte* BLECharacteristic::value() const
9193
{
9294
const byte* value_temp = NULL;
93-
BLECharacteristicImp *characteristicImp = getImplementation();
95+
BLECharacteristicImp *characteristicImp = _internal;//getImplementation();
9496
if (NULL != characteristicImp)
9597
{
9698
value_temp = characteristicImp->value();
@@ -127,6 +129,11 @@ byte BLECharacteristic::operator[] (int offset) //const
127129
return data;
128130
}
129131

132+
bool BLECharacteristic::setValue(const unsigned char value[], unsigned short length)
133+
{
134+
return writeValue(value, (int)length);
135+
}
136+
130137
bool BLECharacteristic::writeValue(const byte value[], int length)
131138
{
132139
bool retVar = false;

libraries/BLE/src/BLECharacteristic.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class BLECharacteristic //: public BLEAttributeWithValue
144144
*
145145
* @note none
146146
*/
147-
virtual const byte* value();//const
147+
virtual const byte* value() const;//
148148

149149
/**
150150
* @brief Get the current length of the value
@@ -168,6 +168,18 @@ class BLECharacteristic //: public BLEAttributeWithValue
168168
*/
169169
virtual byte operator[] (int offset);//const
170170

171+
/**
172+
* Set the current value of the Characteristic
173+
*
174+
* @param[in] value New value to set, as a byte array. Data is stored in internal copy.
175+
* @param[in] length Length, in bytes, of valid data in the array to write.
176+
* Must not exceed maxLength set for this characteristic.
177+
*
178+
* @return bool true set value success, false on error
179+
* @note GATT Server only
180+
*/
181+
bool setValue(const unsigned char value[], unsigned short length);
182+
171183
/**
172184
* @brief Write the value of the characteristic
173185
*

libraries/BLE/src/BLEDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ enum BLEDeviceEvent {
3636
BLEDeviceLastEvent
3737
};
3838

39-
typedef void (*BLEDeviceEventHandler)(BLEDevice& device);
39+
typedef void (*BLEDeviceEventHandler)(BLEDevice device);
4040

4141
class BLEDevice
4242
{

libraries/BLE/src/BLEPeripheral.cpp

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
BLE Peripheral API (deprecated)
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 "ArduinoBLE.h"
21+
22+
#include "BLEPeripheral.h"
23+
24+
static BLEPeripheralEventHandler m_eventHandlers[BLEDeviceLastEvent];
25+
26+
void bleBackCompatiblePeripheralConnectHandler(BLEDevice central)
27+
{
28+
if (m_eventHandlers[BLEConnected])
29+
{
30+
BLECentral temp(central);
31+
m_eventHandlers[BLEConnected](temp);
32+
}
33+
}
34+
35+
void bleBackCompatiblePeripheralDisconnectHandler(BLEDevice central)
36+
{
37+
if (m_eventHandlers[BLEDisconnected])
38+
{
39+
BLECentral temp(central);
40+
m_eventHandlers[BLEDisconnected](temp);
41+
}
42+
}
43+
44+
45+
BLEPeripheral::BLEPeripheral(void) :
46+
_initCalled(false),
47+
_lastService(NULL),
48+
_lastCharacteristic(NULL)
49+
{
50+
}
51+
52+
BLEPeripheral::~BLEPeripheral(void)
53+
{
54+
}
55+
56+
void BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid)
57+
{
58+
if (!_initCalled) {
59+
init();
60+
}
61+
62+
BLE.setAdvertisedServiceUuid(advertisedServiceUuid);
63+
}
64+
void BLEPeripheral::setLocalName(const char* localName)
65+
{
66+
if (!_initCalled) {
67+
init();
68+
}
69+
70+
BLE.setLocalName(localName);
71+
}
72+
73+
74+
void BLEPeripheral::setDeviceName(const char *deviceName)
75+
{
76+
if (!_initCalled) {
77+
init();
78+
}
79+
80+
BLE.setDeviceName(deviceName);
81+
}
82+
83+
void BLEPeripheral::setAppearance(const unsigned short appearance)
84+
{
85+
if (!_initCalled) {
86+
init();
87+
}
88+
89+
BLE.setAppearance(appearance);
90+
}
91+
92+
void BLEPeripheral::setConnectionInterval(const unsigned short minConnInterval, const unsigned short maxConnInterval)
93+
{
94+
if (!_initCalled) {
95+
init();
96+
}
97+
98+
BLE.setConnectionInterval(minConnInterval, maxConnInterval);
99+
}
100+
101+
void BLEPeripheral::addAttribute(BLEService& service)
102+
{
103+
if (!_initCalled)
104+
{
105+
init();
106+
}
107+
108+
BLE.addService(service);
109+
_lastService = &service;
110+
}
111+
112+
void BLEPeripheral::addAttribute(BLECharacteristic& characteristic)
113+
{
114+
if (!_initCalled)
115+
{
116+
init();
117+
}
118+
119+
if (_lastService)
120+
{
121+
_lastService->addCharacteristic(characteristic);
122+
_lastCharacteristic = &characteristic;
123+
}
124+
}
125+
126+
void BLEPeripheral::addAttribute(BLEDescriptor& descriptor)
127+
{
128+
if (!_initCalled)
129+
{
130+
init();
131+
}
132+
133+
if (_lastCharacteristic)
134+
{
135+
_lastCharacteristic->addDescriptor(descriptor);
136+
}
137+
}
138+
139+
void BLEPeripheral::setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler callback)
140+
{
141+
if (BLEConnected == event || BLEDisconnected == event)
142+
{
143+
m_eventHandlers[event] = callback;
144+
}
145+
}
146+
147+
bool BLEPeripheral::begin(void)
148+
{
149+
if (!_initCalled)
150+
{
151+
init();
152+
}
153+
154+
if (_lastService)
155+
{
156+
BLE.addService(*_lastService);
157+
}
158+
159+
BLE.setEventHandler(BLEDisconnected, bleBackCompatiblePeripheralDisconnectHandler);
160+
BLE.setEventHandler(BLEConnected, bleBackCompatiblePeripheralConnectHandler);
161+
162+
BLE.startAdvertising();
163+
return true;
164+
}
165+
166+
void BLEPeripheral::poll(void)
167+
{
168+
BLE.poll();
169+
}
170+
171+
void BLEPeripheral::end(void)
172+
{
173+
BLE.end();
174+
}
175+
176+
bool BLEPeripheral::disconnect(void)
177+
{
178+
return BLE.disconnect();
179+
}
180+
181+
BLECentral BLEPeripheral::central(void)
182+
{
183+
BLEDevice centralBle = BLE.central();
184+
return BLECentral(centralBle);
185+
}
186+
187+
bool BLEPeripheral::connected(void)
188+
{
189+
return BLE.connected();
190+
}
191+
192+
void BLEPeripheral::init()
193+
{
194+
if (!_initCalled)
195+
{
196+
BLE.begin();
197+
_initCalled = true;
198+
}
199+
}
200+

0 commit comments

Comments
 (0)