-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathBLEPeripheral.h
169 lines (135 loc) · 6.54 KB
/
BLEPeripheral.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#ifndef _BLE_PERIPHERAL_H_
#define _BLE_PERIPHERAL_H_
#include "Arduino.h"
#include "BLEBondStore.h"
#include "BLECentral.h"
#include "BLEConstantCharacteristic.h"
#include "BLEDescriptor.h"
#include "BLEDevice.h"
#include "BLEFixedLengthCharacteristic.h"
#include "BLELocalAttribute.h"
#include "BLEProgmemConstantCharacteristic.h"
#include "BLERemoteAttribute.h"
#include "BLERemoteCharacteristic.h"
#include "BLERemoteService.h"
#include "BLEService.h"
#include "BLETypedCharacteristics.h"
#if defined(NRF51) || defined(NRF52) || defined(__RFduino__)
#include "nRF51822.h"
#else
#include "nRF8001.h"
#endif
#if defined(NRF51) || defined(NRF52) || defined(__RFduino__)
#define BLE_DEFAULT_REQ -1
#define BLE_DEFAULT_RDY -1
#define BLE_DEFAULT_RST -1
#elif defined(BLEND_MICRO)
#define BLE_DEFAULT_REQ 6
#define BLE_DEFAULT_RDY 7
#define BLE_DEFAULT_RST 4
#elif defined(BLEND)
#define BLE_DEFAULT_REQ 9
#define BLE_DEFAULT_RDY 8
#define BLE_DEFAULT_RST 4
#else
#define BLE_DEFAULT_REQ 10
#define BLE_DEFAULT_RDY 2
#define BLE_DEFAULT_RST 9
#endif
enum BLEPeripheralEvent {
BLEConnected = 0,
BLEDisconnected = 1,
BLEBonded = 2,
BLERemoteServicesDiscovered = 3
};
typedef void (*BLEPeripheralEventHandler)(BLECentral& central);
class BLEPeripheral : public BLEDeviceEventListener,
public BLECharacteristicValueChangeListener,
public BLERemoteCharacteristicValueChangeListener
{
public:
BLEPeripheral(unsigned char req = BLE_DEFAULT_REQ, unsigned char rdy = BLE_DEFAULT_RDY, unsigned char rst = BLE_DEFAULT_RST);
virtual ~BLEPeripheral();
void begin();
void poll();
void end();
void setAdvertisedServiceUuid(const char* advertisedServiceUuid);
void setServiceSolicitationUuid(const char* serviceSolicitationUuid);
void setManufacturerData(const unsigned char manufacturerData[], unsigned char manufacturerDataLength);
void setLocalName(const char *localName);
void setAdvertisingInterval(unsigned short advertisingInterval);
// connection intervals in 1.25 ms increments,
// must be between 0x0006 (7.5 ms) and 0x0c80 (4 s), values outside of this range will be ignored
void setConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval);
bool setTxPower(int txPower);
void setConnectable(bool connectable);
void setBondStore(BLEBondStore& bondStore);
void setDeviceName(const char* deviceName);
void setAppearance(unsigned short appearance);
/**
* @brief Add local services, characteristics and descriptors. Characteristics are assigned to the last added service.
*/
void addAttribute(BLELocalAttribute& attribute);
/**
* @brief Add local services, characteristics and descriptors. Characteristics are assigned to the last added service.
*/
void addLocalAttribute(BLELocalAttribute& localAttribute);
void addRemoteAttribute(BLERemoteAttribute& remoteAttribute);
void disconnect();
BLECentral central();
bool connected();
void setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler eventHandler);
protected:
bool characteristicValueChanged(BLECharacteristic& characteristic);
bool broadcastCharacteristic(BLECharacteristic& characteristic);
bool canNotifyCharacteristic(BLECharacteristic& characteristic);
bool canIndicateCharacteristic(BLECharacteristic& characteristic);
bool canReadRemoteCharacteristic(BLERemoteCharacteristic& characteristic);
bool readRemoteCharacteristic(BLERemoteCharacteristic& characteristic);
bool canWriteRemoteCharacteristic(BLERemoteCharacteristic& characteristic);
bool writeRemoteCharacteristic(BLERemoteCharacteristic& characteristic, const unsigned char value[], unsigned char length);
bool canSubscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic);
bool subscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic);
bool canUnsubscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic);
bool unsubcribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic);
virtual void BLEDeviceConnected(BLEDevice& device, const unsigned char* address);
virtual void BLEDeviceDisconnected(BLEDevice& device);
virtual void BLEDeviceBonded(BLEDevice& device);
virtual void BLEDeviceRemoteServicesDiscovered(BLEDevice& device);
virtual void BLEDeviceCharacteristicValueChanged(BLEDevice& device, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength);
virtual void BLEDeviceCharacteristicSubscribedChanged(BLEDevice& device, BLECharacteristic& characteristic, bool subscribed);
virtual void BLEDeviceRemoteCharacteristicValueChanged(BLEDevice& device, BLERemoteCharacteristic& remoteCharacteristic, const unsigned char* value, unsigned char valueLength);
virtual void BLEDeviceAddressReceived(BLEDevice& device, const unsigned char* address);
virtual void BLEDeviceTemperatureReceived(BLEDevice& device, float temperature);
virtual void BLEDeviceBatteryLevelReceived(BLEDevice& device, float batteryLevel);
private:
void initLocalAttributes();
private:
BLEDevice* _device;
#if defined(NRF51) || defined(NRF52) || defined(__RFduino__)
nRF51822 _nRF51822;
#else
nRF8001 _nRF8001;
#endif
const char* _advertisedServiceUuid;
const char* _serviceSolicitationUuid;
const unsigned char* _manufacturerData;
unsigned char _manufacturerDataLength;
const char* _localName;
BLELocalAttribute** _localAttributes;
unsigned char _numLocalAttributes;
BLERemoteAttribute** _remoteAttributes;
unsigned char _numRemoteAttributes;
BLEService _genericAccessService;
BLECharacteristic _deviceNameCharacteristic;
BLECharacteristic _appearanceCharacteristic;
BLEService _genericAttributeService;
BLECharacteristic _servicesChangedCharacteristic;
BLERemoteService _remoteGenericAttributeService;
BLERemoteCharacteristic _remoteServicesChangedCharacteristic;
BLECentral _central;
BLEPeripheralEventHandler _eventHandlers[4];
};
#endif