forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLEPeripheral.h
136 lines (105 loc) · 5.28 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
#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(__RFduino__)
#include "nRF51822.h"
#else
#include "nRF8001.h"
#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(BLE_Tx_Power_Level tx_power_level, unsigned char req, unsigned char rdy, unsigned char rst);
virtual ~BLEPeripheral();
void begin();
void poll();
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);
void setConnectable(bool connectable);
void setBondStore(BLEBondStore& bondStore);
void setDeviceName(const char* deviceName);
void setAppearance(unsigned short appearance);
void addAttribute(BLELocalAttribute& attribute);
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(__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