diff --git a/API.md b/API.md index 2524082..3aab16b 100644 --- a/API.md +++ b/API.md @@ -1,10 +1,10 @@ -# BLEPeripheral +# BLEPeripheralObserver ## Constructor -Creates the BLEPeripheral object. Pin Arguments are optional as pins are self detecting. See https://github.com/sandeepmistry/arduino-BLEPeripheral#pinouts +Creates the BLEPeripheralObserver object. Pin Arguments are optional as pins are self detecting. See https://github.com/sandeepmistry/arduino-BLEPeripheral#pinouts ```c -BLEPeripheral(unsigned char req = BLE_DEFAULT_REQ, unsigned char rdy = BLE_DEFAULT_RDY, unsigned char rst = BLE_DEFAULT_RST); +BLEPeripheralObserver(unsigned char req = BLE_DEFAULT_REQ, unsigned char rdy = BLE_DEFAULT_RDY, unsigned char rst = BLE_DEFAULT_RST); ``` * req - REQ pin * rdy - RDY pin @@ -45,7 +45,7 @@ void setAdvertisingInterval(unsigned short advertisingInterval); Sets the connection interval to use after a connection has been established. ```c -void setConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval); +void setConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval); ``` * minimumConnectionInterval - minimum connection interval in 1.25 ms increments diff --git a/README.md b/README.md index 3857f9c..62f3156 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Arduino BLEPeripheral +# Arduino BLEPeripheralObserver [![Build Status](https://travis-ci.org/sandeepmistry/arduino-BLEPeripheral.svg?branch=master)](https://travis-ci.org/sandeepmistry/arduino-BLEPeripheral) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sandeepmistry/arduino-BLEPeripheral?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) @@ -15,7 +15,7 @@ This is heavily based on @sandeepmistry's [arduino-BLEPeripheral](https://github * [nRF51822](http://redbearlab.com/redbearlab-nrf51822) * [BLE Nano](http://redbearlab.com/blenano/) * [RFduino](http://www.rfduino.com) with [Arduino Add-on](https://github.com/RFduino/RFduino) - + **WARNING**: Adafruit nRF51 boards are **NOT** compatible with this library, this includes the [Bluefruit LE UART Friend](https://www.adafruit.com/products/2479), [Bluefruit LE SPI Friend](https://www.adafruit.com/products/2633), [Bluefruit LE Shield](https://www.adafruit.com/products/2746), [Flora Wearable Bluefruit LE Module](https://www.adafruit.com/products/2487), [Feather 32u4 Bluefruit LE](https://www.adafruit.com/products/2829), and [Feather M0 Bluefruit LE](https://www.adafruit.com/products/2995). ### Nordic Semiconductor nRF51 or nRF52 based boards diff --git a/examples/HID/HID_joystick_mouse/HID_joystick_mouse.ino b/examples/HID/HID_joystick_mouse/HID_joystick_mouse.ino index b996aa5..2a029ed 100644 --- a/examples/HID/HID_joystick_mouse/HID_joystick_mouse.ino +++ b/examples/HID/HID_joystick_mouse/HID_joystick_mouse.ino @@ -1,7 +1,7 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include #include #include diff --git a/examples/HID/HID_keyboard/HID_keyboard.ino b/examples/HID/HID_keyboard/HID_keyboard.ino index 94b2bc4..839cf33 100644 --- a/examples/HID/HID_keyboard/HID_keyboard.ino +++ b/examples/HID/HID_keyboard/HID_keyboard.ino @@ -1,7 +1,7 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include #include #include diff --git a/examples/HID/HID_keypad/HID_keypad.ino b/examples/HID/HID_keypad/HID_keypad.ino index fa5fc92..e910eb4 100644 --- a/examples/HID/HID_keypad/HID_keypad.ino +++ b/examples/HID/HID_keypad/HID_keypad.ino @@ -1,7 +1,7 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include #include #include diff --git a/examples/HID/HID_test/HID_test.ino b/examples/HID/HID_test/HID_test.ino index 48918a3..9f6fd61 100644 --- a/examples/HID/HID_test/HID_test.ino +++ b/examples/HID/HID_test/HID_test.ino @@ -1,7 +1,7 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include #include #include diff --git a/examples/HID/HID_volume/HID_volume.ino b/examples/HID/HID_volume/HID_volume.ino index aae09e7..53cb97c 100644 --- a/examples/HID/HID_volume/HID_volume.ino +++ b/examples/HID/HID_volume/HID_volume.ino @@ -1,7 +1,7 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include #include #include diff --git a/examples/ancs/ancs.ino b/examples/ancs/ancs.ino index 1927b32..0d05216 100644 --- a/examples/ancs/ancs.ino +++ b/examples/ancs/ancs.ino @@ -1,14 +1,14 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include #include -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); BLEBondStore bleBondStore; // remote services @@ -29,38 +29,38 @@ void setup() { // clears bond data on every boot bleBondStore.clearData(); - blePeripheral.setBondStore(bleBondStore); + blePeriphObserv.setBondStore(bleBondStore); - blePeripheral.setServiceSolicitationUuid(ancsService.uuid()); - blePeripheral.setLocalName("ANCS"); + blePeriphObserv.setServiceSolicitationUuid(ancsService.uuid()); + blePeriphObserv.setLocalName("ANCS"); // set device name and appearance - blePeripheral.setDeviceName("Arduino ANCS"); - blePeripheral.setAppearance(0x0080); + blePeriphObserv.setDeviceName("Arduino ANCS"); + blePeriphObserv.setAppearance(0x0080); - blePeripheral.addRemoteAttribute(ancsService); - blePeripheral.addRemoteAttribute(ancsNotificationSourceCharacteristic); -// blePeripheral.addRemoteAttribute(ancsControlPointCharacteristic); -// blePeripheral.addRemoteAttribute(ancsDataSourceCharacteristic); + blePeriphObserv.addRemoteAttribute(ancsService); + blePeriphObserv.addRemoteAttribute(ancsNotificationSourceCharacteristic); +// blePeriphObserv.addRemoteAttribute(ancsControlPointCharacteristic); +// blePeriphObserv.addRemoteAttribute(ancsDataSourceCharacteristic); // assign event handlers for connected, disconnected to peripheral - blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler); - blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); - blePeripheral.setEventHandler(BLEBonded, blePeripheralBondedHandler); - blePeripheral.setEventHandler(BLERemoteServicesDiscovered, blePeripheralRemoteServicesDiscoveredHandler); + blePeriphObserv.setEventHandler(BLEConnected, blePeripheralConnectHandler); + blePeriphObserv.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); + blePeriphObserv.setEventHandler(BLEBonded, blePeripheralBondedHandler); + blePeriphObserv.setEventHandler(BLERemoteServicesDiscovered, blePeripheralRemoteServicesDiscoveredHandler); // assign event handlers for characteristic ancsNotificationSourceCharacteristic.setEventHandler(BLEValueUpdated, ancsNotificationSourceCharacteristicValueUpdated); // ancsDataSourceCharacteristic.setEventHandler(BLEValueUpdated, ancsDataSourceCharacteristicCharacteristicValueUpdated); // begin initialization - blePeripheral.begin(); + blePeriphObserv.begin(); Serial.println(F("BLE Peripheral - ANCS")); } void loop() { - blePeripheral.poll(); + blePeriphObserv.poll(); } void blePeripheralConnectHandler(BLECentral& central) { diff --git a/examples/iBeacon/iBeacon.ino b/examples/iBeacon/iBeacon.ino index e3f52fc..06911f7 100644 --- a/examples/iBeacon/iBeacon.ino +++ b/examples/iBeacon/iBeacon.ino @@ -1,7 +1,7 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -#include +#include #include #if !defined(NRF51) && !defined(NRF52) && !defined(__RFduino__) diff --git a/examples/ir_bridge/ir_bridge.ino b/examples/ir_bridge/ir_bridge.ino index d73890b..8486e2e 100644 --- a/examples/ir_bridge/ir_bridge.ino +++ b/examples/ir_bridge/ir_bridge.ino @@ -1,9 +1,9 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include // https://github.com/shirriff/Arduino-IRremote #include @@ -25,8 +25,8 @@ IRsend irSend = IRsend(/*IR_SEND_ IRrecv irRecv = IRrecv(IR_RECV_PIN); IRValue irValue; -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); // create service and characteristics BLEService irService = BLEService("00004952-0000-bbbb-0123-456789abcdef"); @@ -38,20 +38,20 @@ void setup() { Serial.begin(115200); // set advertised local name and service UUID - blePeripheral.setLocalName("IR"); - blePeripheral.setAdvertisedServiceUuid(irService.uuid()); + blePeriphObserv.setLocalName("IR"); + blePeriphObserv.setAdvertisedServiceUuid(irService.uuid()); // add service and characteristics - blePeripheral.addAttribute(irService); - blePeripheral.addAttribute(irOutputCharacteristic); - blePeripheral.addAttribute(irInputCharacteristic); + blePeriphObserv.addAttribute(irService); + blePeriphObserv.addAttribute(irOutputCharacteristic); + blePeriphObserv.addAttribute(irInputCharacteristic); // assign event handlers for connected, disconnected to peripheral - blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler); - blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); + blePeriphObserv.setEventHandler(BLEConnected, blePeripheralConnectHandler); + blePeriphObserv.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); // begin initialization - blePeripheral.begin(); + blePeriphObserv.begin(); Serial.println(F("BLE IR Peripheral")); @@ -63,7 +63,7 @@ void setup() { void loop() { // poll peripheral - blePeripheral.poll(); + blePeriphObserv.poll(); // poll the ouput characteristic pollIrOutput(); diff --git a/examples/led/led.ino b/examples/led/led.ino index 8fbcb80..0811e97 100644 --- a/examples/led/led.ino +++ b/examples/led/led.ino @@ -1,15 +1,15 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include // LED pin #define LED_PIN 3 -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); // create service BLEService ledService = BLEService("19b10000e8f2537e4f6cd104768a1214"); @@ -27,21 +27,21 @@ void setup() { pinMode(LED_PIN, OUTPUT); // set advertised local name and service UUID - blePeripheral.setLocalName("LED"); - blePeripheral.setAdvertisedServiceUuid(ledService.uuid()); + blePeriphObserv.setLocalName("LED"); + blePeriphObserv.setAdvertisedServiceUuid(ledService.uuid()); // add service and characteristic - blePeripheral.addAttribute(ledService); - blePeripheral.addAttribute(switchCharacteristic); + blePeriphObserv.addAttribute(ledService); + blePeriphObserv.addAttribute(switchCharacteristic); // begin initialization - blePeripheral.begin(); + blePeriphObserv.begin(); Serial.println(F("BLE LED Peripheral")); } void loop() { - BLECentral central = blePeripheral.central(); + BLECentral central = blePeriphObserv.central(); if (central) { // central connected to peripheral diff --git a/examples/led_callback/led_callback.ino b/examples/led_callback/led_callback.ino index c0d05d9..5ec2940 100644 --- a/examples/led_callback/led_callback.ino +++ b/examples/led_callback/led_callback.ino @@ -1,15 +1,15 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include // LED pin #define LED_PIN 3 -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); // create service BLEService ledService = BLEService("19b10000e8f2537e4f6cd104768a1214"); @@ -27,29 +27,29 @@ void setup() { pinMode(LED_PIN, OUTPUT); // set advertised local name and service UUID - blePeripheral.setLocalName("LED"); - blePeripheral.setAdvertisedServiceUuid(ledService.uuid()); + blePeriphObserv.setLocalName("LED"); + blePeriphObserv.setAdvertisedServiceUuid(ledService.uuid()); // add service and characteristic - blePeripheral.addAttribute(ledService); - blePeripheral.addAttribute(switchCharacteristic); + blePeriphObserv.addAttribute(ledService); + blePeriphObserv.addAttribute(switchCharacteristic); // assign event handlers for connected, disconnected to peripheral - blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler); - blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); + blePeriphObserv.setEventHandler(BLEConnected, blePeripheralConnectHandler); + blePeriphObserv.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); // assign event handlers for characteristic switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten); // begin initialization - blePeripheral.begin(); + blePeriphObserv.begin(); Serial.println(F("BLE LED Peripheral")); } void loop() { // poll peripheral - blePeripheral.poll(); + blePeriphObserv.poll(); } void blePeripheralConnectHandler(BLECentral& central) { diff --git a/examples/led_switch/led_switch.ino b/examples/led_switch/led_switch.ino index 5fbbd6d..ef06395 100644 --- a/examples/led_switch/led_switch.ino +++ b/examples/led_switch/led_switch.ino @@ -1,16 +1,16 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include // LED and button pin #define LED_PIN 3 #define BUTTON_PIN 4 -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); // create service BLEService ledService = BLEService("19b10010e8f2537e4f6cd104768a1214"); @@ -30,23 +30,23 @@ void setup() { pinMode(BUTTON_PIN, INPUT); // set advertised local name and service UUID - blePeripheral.setLocalName("LED Switch"); - blePeripheral.setAdvertisedServiceUuid(ledService.uuid()); + blePeriphObserv.setLocalName("LED Switch"); + blePeriphObserv.setAdvertisedServiceUuid(ledService.uuid()); // add service and characteristics - blePeripheral.addAttribute(ledService); - blePeripheral.addAttribute(switchCharacteristic); - blePeripheral.addAttribute(buttonCharacteristic); + blePeriphObserv.addAttribute(ledService); + blePeriphObserv.addAttribute(switchCharacteristic); + blePeriphObserv.addAttribute(buttonCharacteristic); // begin initialization - blePeripheral.begin(); + blePeriphObserv.begin(); Serial.println(F("BLE LED Switch Peripheral")); } void loop() { // poll peripheral - blePeripheral.poll(); + blePeriphObserv.poll(); // read the current button pin state char buttonValue = digitalRead(BUTTON_PIN); diff --git a/examples/observer/observer.ino b/examples/observer/observer.ino index 2d52265..b6f47b1 100644 --- a/examples/observer/observer.ino +++ b/examples/observer/observer.ino @@ -3,14 +3,14 @@ #define NRF51 #undef __RFduino__ -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include #include #include -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); void setup() { Serial.begin(115200); @@ -18,16 +18,16 @@ void setup() { delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board #endif - blePeripheral.setLocalName("foobaz"); // optional + blePeriphObserv.setLocalName("foobaz"); // optional - blePeripheral.setEventHandler(BLEAddressReceived, addrHandler); - blePeripheral.setEventHandler(BLEAdvertisementReceived, advHandler); + blePeriphObserv.setEventHandler(BLEAddressReceived, addrHandler); + blePeriphObserv.setEventHandler(BLEAdvertisementReceived, advHandler); // begin initialization - blePeripheral.begin(); - blePeripheral.setConnectable(false); - blePeripheral.setAdvertisingInterval(500); - blePeripheral.startAdvertising(); + blePeriphObserv.begin(); + blePeriphObserv.setConnectable(false); + blePeriphObserv.setAdvertisingInterval(500); + blePeriphObserv.startAdvertising(); } void addrHandler(const void* _addr) { @@ -46,15 +46,17 @@ void advHandler(const void* adv) { Serial.println(address); Serial.print(F("got adv with payload ")); Serial.println(report->dlen); + Serial.print(F("RSSI ")); + Serial.println(report->rssi); } void loop() { if (Serial.available() > 0) { Serial.read(); Serial.println("start scanning"); - blePeripheral.startScanning(); - blePeripheral.setLocalName("foobarg"); // optional - blePeripheral.startAdvertising(); + blePeriphObserv.startScanning(); + blePeriphObserv.setLocalName("foobarg"); // optional + blePeriphObserv.startAdvertising(); } - blePeripheral.poll(); + blePeriphObserv.poll(); } diff --git a/examples/remote_service/remote_service.ino b/examples/remote_service/remote_service.ino index 35f1785..96e39d8 100644 --- a/examples/remote_service/remote_service.ino +++ b/examples/remote_service/remote_service.ino @@ -1,12 +1,12 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); // create remote services BLERemoteService remoteGenericAttributeService = BLERemoteService("1800"); @@ -21,31 +21,31 @@ void setup() { while(!Serial); // wait for serial #endif - blePeripheral.setLocalName("remote-attributes"); + blePeriphObserv.setLocalName("remote-attributes"); // set device name and appearance - blePeripheral.setDeviceName("Remote Attributes"); - blePeripheral.setAppearance(0x0080); + blePeriphObserv.setDeviceName("Remote Attributes"); + blePeriphObserv.setAppearance(0x0080); - blePeripheral.addRemoteAttribute(remoteGenericAttributeService); - blePeripheral.addRemoteAttribute(remoteDeviceNameCharacteristic); + blePeriphObserv.addRemoteAttribute(remoteGenericAttributeService); + blePeriphObserv.addRemoteAttribute(remoteDeviceNameCharacteristic); // assign event handlers for connected, disconnected to peripheral - blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler); - blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); - blePeripheral.setEventHandler(BLERemoteServicesDiscovered, blePeripheralRemoteServicesDiscoveredHandler); + blePeriphObserv.setEventHandler(BLEConnected, blePeripheralConnectHandler); + blePeriphObserv.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); + blePeriphObserv.setEventHandler(BLERemoteServicesDiscovered, blePeripheralRemoteServicesDiscoveredHandler); // assign event handlers for characteristic remoteDeviceNameCharacteristic.setEventHandler(BLEValueUpdated, bleRemoteDeviceNameCharacteristicValueUpdatedHandle); // begin initialization - blePeripheral.begin(); + blePeriphObserv.begin(); Serial.println(F("BLE Peripheral - remote attributes")); } void loop() { - blePeripheral.poll(); + blePeriphObserv.poll(); } void blePeripheralConnectHandler(BLECentral& central) { diff --git a/examples/remote_test/remote_test.ino b/examples/remote_test/remote_test.ino index 311f3b5..a46d788 100644 --- a/examples/remote_test/remote_test.ino +++ b/examples/remote_test/remote_test.ino @@ -1,13 +1,13 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include #include -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); // create remote services BLERemoteService remoteService = BLERemoteService("fffffffffffffffffffffffffffffff0"); @@ -25,22 +25,22 @@ void setup() { while(!Serial); // wait for serial #endif - blePeripheral.setLocalName("remote-test"); + blePeriphObserv.setLocalName("remote-test"); // set device name and appearance - blePeripheral.setDeviceName("Remote Test"); - blePeripheral.setAppearance(0x0080); + blePeriphObserv.setDeviceName("Remote Test"); + blePeriphObserv.setAppearance(0x0080); - blePeripheral.addRemoteAttribute(remoteService); - blePeripheral.addRemoteAttribute(remoteCharacteristic1); - blePeripheral.addRemoteAttribute(remoteCharacteristic2); - blePeripheral.addRemoteAttribute(remoteCharacteristic3); - blePeripheral.addRemoteAttribute(remoteCharacteristic4); + blePeriphObserv.addRemoteAttribute(remoteService); + blePeriphObserv.addRemoteAttribute(remoteCharacteristic1); + blePeriphObserv.addRemoteAttribute(remoteCharacteristic2); + blePeriphObserv.addRemoteAttribute(remoteCharacteristic3); + blePeriphObserv.addRemoteAttribute(remoteCharacteristic4); // assign event handlers for connected, disconnected to peripheral - blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler); - blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); - blePeripheral.setEventHandler(BLERemoteServicesDiscovered, blePeripheralRemoteServicesDiscoveredHandler); + blePeriphObserv.setEventHandler(BLEConnected, blePeripheralConnectHandler); + blePeriphObserv.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); + blePeriphObserv.setEventHandler(BLERemoteServicesDiscovered, blePeripheralRemoteServicesDiscoveredHandler); // assign event handlers for characteristic remoteCharacteristic1.setEventHandler(BLEValueUpdated, bleRemoteCharacteristicValueUpdatedHandle); @@ -49,13 +49,13 @@ void setup() { // begin initialization - blePeripheral.begin(); + blePeriphObserv.begin(); Serial.println(F("BLE Peripheral - remote test")); } void loop() { - blePeripheral.poll(); + blePeriphObserv.poll(); } void blePeripheralConnectHandler(BLECentral& central) { diff --git a/examples/serial/BLESerial.cpp b/examples/serial/BLESerial.cpp index 02eeae7..e64aae8 100755 --- a/examples/serial/BLESerial.cpp +++ b/examples/serial/BLESerial.cpp @@ -5,7 +5,7 @@ BLESerial* BLESerial::_instance = NULL; BLESerial::BLESerial(unsigned char req, unsigned char rdy, unsigned char rst) : - BLEPeripheral(req, rdy, rst) + BLEPeripheralObserver(req, rdy, rst) { this->_txCount = 0; this->_rxHead = this->_rxTail = 0; @@ -23,7 +23,7 @@ BLESerial::BLESerial(unsigned char req, unsigned char rdy, unsigned char rst) : } void BLESerial::begin(...) { - BLEPeripheral::begin(); + BLEPeripheralObserver::begin(); #ifdef BLE_SERIAL_DEBUG Serial.println(F("BLESerial::begin()")); #endif @@ -31,7 +31,7 @@ void BLESerial::begin(...) { void BLESerial::poll() { if (millis() < this->_flushed + 100) { - BLEPeripheral::poll(); + BLEPeripheralObserver::poll(); } else { flush(); } @@ -41,11 +41,11 @@ void BLESerial::end() { this->_rxCharacteristic.setEventHandler(BLEWritten, NULL); this->_rxHead = this->_rxTail = 0; flush(); - BLEPeripheral::disconnect(); + BLEPeripheralObserver::disconnect(); } int BLESerial::available(void) { - BLEPeripheral::poll(); + BLEPeripheralObserver::poll(); int retval = (this->_rxHead - this->_rxTail + sizeof(this->_rxBuffer)) % sizeof(this->_rxBuffer); #ifdef BLE_SERIAL_DEBUG Serial.print(F("BLESerial::available() = ")); @@ -55,7 +55,7 @@ int BLESerial::available(void) { } int BLESerial::peek(void) { - BLEPeripheral::poll(); + BLEPeripheralObserver::poll(); if (this->_rxTail == this->_rxHead) return -1; uint8_t byte = this->_rxBuffer[ (this->_rxTail + 1) % sizeof(this->_rxBuffer)]; #ifdef BLE_SERIAL_DEBUG @@ -68,7 +68,7 @@ int BLESerial::peek(void) { } int BLESerial::read(void) { - BLEPeripheral::poll(); + BLEPeripheralObserver::poll(); if (this->_rxTail == this->_rxHead) return -1; this->_rxTail = (this->_rxTail + 1) % sizeof(this->_rxBuffer); uint8_t byte = this->_rxBuffer[this->_rxTail]; @@ -86,14 +86,14 @@ void BLESerial::flush(void) { this->_txCharacteristic.setValue(this->_txBuffer, this->_txCount); this->_flushed = millis(); this->_txCount = 0; - BLEPeripheral::poll(); + BLEPeripheralObserver::poll(); #ifdef BLE_SERIAL_DEBUG Serial.println(F("BLESerial::flush()")); #endif } size_t BLESerial::write(uint8_t byte) { - BLEPeripheral::poll(); + BLEPeripheralObserver::poll(); if (this->_txCharacteristic.subscribed() == false) return 0; this->_txBuffer[this->_txCount++] = byte; if (this->_txCount == sizeof(this->_txBuffer)) flush(); @@ -108,7 +108,7 @@ size_t BLESerial::write(uint8_t byte) { } BLESerial::operator bool() { - bool retval = BLEPeripheral::connected(); + bool retval = BLEPeripheralObserver::connected(); #ifdef BLE_SERIAL_DEBUG Serial.print(F("BLESerial::operator bool() = ")); Serial.println(retval); diff --git a/examples/serial/BLESerial.h b/examples/serial/BLESerial.h index 1d1f09b..f0a7b10 100755 --- a/examples/serial/BLESerial.h +++ b/examples/serial/BLESerial.h @@ -2,9 +2,9 @@ #define _BLE_SERIAL_H_ #include -#include +#include -class BLESerial : public BLEPeripheral, public Stream +class BLESerial : public BLEPeripheralObserver, public Stream { public: BLESerial(unsigned char req = BLE_DEFAULT_REQ, unsigned char rdy = BLE_DEFAULT_RDY, unsigned char rst = BLE_DEFAULT_RST); diff --git a/examples/serial/serial.ino b/examples/serial/serial.ino index 2a5aead..1c4dd00 100644 --- a/examples/serial/serial.ino +++ b/examples/serial/serial.ino @@ -15,9 +15,9 @@ */ -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include #include "BLESerial.h" //custom boards may override default pin definitions with BLESerial(PIN_REQ, PIN_RDY, PIN_RST) diff --git a/examples/starter/starter.ino b/examples/starter/starter.ino index 147ffd5..65b3ce8 100644 --- a/examples/starter/starter.ino +++ b/examples/starter/starter.ino @@ -1,12 +1,12 @@ // Copyright (c) Sandeep Mistry. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); // uuid's can be: // 16-bit: "ffff" @@ -27,23 +27,23 @@ void setup() { delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board #endif - blePeripheral.setLocalName("local-name"); // optional - blePeripheral.setAdvertisedServiceUuid(service.uuid()); // optional + blePeriphObserv.setLocalName("local-name"); // optional + blePeriphObserv.setAdvertisedServiceUuid(service.uuid()); // optional // add attributes (services, characteristics, descriptors) to peripheral - blePeripheral.addAttribute(service); - blePeripheral.addAttribute(characteristic); - blePeripheral.addAttribute(descriptor); + blePeriphObserv.addAttribute(service); + blePeriphObserv.addAttribute(characteristic); + blePeriphObserv.addAttribute(descriptor); // set initial value characteristic.setValue(0); // begin initialization - blePeripheral.begin(); + blePeriphObserv.begin(); } void loop() { - BLECentral central = blePeripheral.central(); + BLECentral central = blePeriphObserv.central(); if (central) { // central connected to peripheral diff --git a/examples/temp_sensor/temp_sensor.ino b/examples/temp_sensor/temp_sensor.ino index e2124fa..8819789 100644 --- a/examples/temp_sensor/temp_sensor.ino +++ b/examples/temp_sensor/temp_sensor.ino @@ -6,15 +6,15 @@ // DHT library: https://github.com/adafruit/DHT-sensor-library #include "DHT.h" #include -#include +#include #define DHTTYPE DHT22 #define DHTPIN 3 DHT dht(DHTPIN, DHTTYPE); -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); BLEService tempService = BLEService("CCC0"); BLEFloatCharacteristic tempCharacteristic = BLEFloatCharacteristic("CCC1", BLERead | BLENotify); @@ -35,22 +35,22 @@ void setup() { delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board #endif - blePeripheral.setLocalName("Temperature"); + blePeriphObserv.setLocalName("Temperature"); - blePeripheral.setAdvertisedServiceUuid(tempService.uuid()); - blePeripheral.addAttribute(tempService); - blePeripheral.addAttribute(tempCharacteristic); - blePeripheral.addAttribute(tempDescriptor); + blePeriphObserv.setAdvertisedServiceUuid(tempService.uuid()); + blePeriphObserv.addAttribute(tempService); + blePeriphObserv.addAttribute(tempCharacteristic); + blePeriphObserv.addAttribute(tempDescriptor); - blePeripheral.setAdvertisedServiceUuid(humidityService.uuid()); - blePeripheral.addAttribute(humidityService); - blePeripheral.addAttribute(humidityCharacteristic); - blePeripheral.addAttribute(humidityDescriptor); + blePeriphObserv.setAdvertisedServiceUuid(humidityService.uuid()); + blePeriphObserv.addAttribute(humidityService); + blePeriphObserv.addAttribute(humidityCharacteristic); + blePeriphObserv.addAttribute(humidityDescriptor); - blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler); - blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); + blePeriphObserv.setEventHandler(BLEConnected, blePeripheralConnectHandler); + blePeriphObserv.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); - blePeripheral.begin(); + blePeriphObserv.begin(); Timer1.initialize(2 * 1000000); // in milliseconds Timer1.attachInterrupt(timerHandler); @@ -59,7 +59,7 @@ void setup() { } void loop() { - blePeripheral.poll(); + blePeriphObserv.poll(); if (readFromSensor) { setTempCharacteristicValue(); diff --git a/examples/test/test.ino b/examples/test/test.ino index 5edc9c9..57c9c6f 100644 --- a/examples/test/test.ino +++ b/examples/test/test.ino @@ -7,12 +7,12 @@ #include #endif -// Import libraries (BLEPeripheral depends on SPI) +// Import libraries (BLEPeripheralObserver depends on SPI) #include -#include +#include -//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST) -BLEPeripheral blePeripheral = BLEPeripheral(); +//custom boards may override default pin definitions with BLEPeripheralObserver(PIN_REQ, PIN_RDY, PIN_RST) +BLEPeripheralObserver blePeriphObserv = BLEPeripheralObserver(); // create service BLEService testService = BLEService("fff0"); @@ -30,26 +30,26 @@ void setup() { delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board #endif - blePeripheral.setLocalName("test"); + blePeriphObserv.setLocalName("test"); #if 1 - blePeripheral.setAdvertisedServiceUuid(testService.uuid()); + blePeriphObserv.setAdvertisedServiceUuid(testService.uuid()); #else const char manufacturerData[4] = {0x12, 0x34, 0x56, 0x78}; - blePeripheral.setManufacturerData(manufacturerData, sizeof(manufacturerData)); + blePeriphObserv.setManufacturerData(manufacturerData, sizeof(manufacturerData)); #endif // set device name and appearance - blePeripheral.setDeviceName("Test"); - blePeripheral.setAppearance(0x0080); + blePeriphObserv.setDeviceName("Test"); + blePeriphObserv.setAppearance(0x0080); // add service, characteristic, and decriptor to peripheral - blePeripheral.addAttribute(testService); - blePeripheral.addAttribute(testCharacteristic); - blePeripheral.addAttribute(testDescriptor); + blePeriphObserv.addAttribute(testService); + blePeriphObserv.addAttribute(testCharacteristic); + blePeriphObserv.addAttribute(testDescriptor); // assign event handlers for connected, disconnected to peripheral - blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler); - blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); + blePeriphObserv.setEventHandler(BLEConnected, blePeripheralConnectHandler); + blePeriphObserv.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); // assign event handlers for characteristic testCharacteristic.setEventHandler(BLEWritten, characteristicWritten); @@ -60,7 +60,7 @@ void setup() { testCharacteristic.setValue(0); // begin initialization - blePeripheral.begin(); + blePeriphObserv.begin(); Serial.println(F("BLE Peripheral")); @@ -71,7 +71,7 @@ void setup() { } void loop() { - BLECentral central = blePeripheral.central(); + BLECentral central = blePeriphObserv.central(); if (central) { // central connected to peripheral diff --git a/keywords.txt b/keywords.txt index 843b2bc..2e9e4b5 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,5 +1,5 @@ ####################################### -# Syntax Coloring Map For BLEPeripheral +# Syntax Coloring Map For BLEPeripheralObserver ####################################### ####################################### @@ -12,7 +12,7 @@ BLECentral KEYWORD1 BLECharacteristic KEYWORD1 BLEDescriptor KEYWORD1 BLELocalAttribute KEYWORD1 -BLEPeripheral KEYWORD1 +BLEPeripheralObserver KEYWORD1 BLERemoteAttribute KEYWORD1 BLERemoteCharacteristic KEYWORD1 BLERemoteService KEYWORD1 diff --git a/set-version.sh b/set-version.sh index 491a5a5..dc90a05 100755 --- a/set-version.sh +++ b/set-version.sh @@ -20,7 +20,7 @@ function man () { echo " ${_B}${0##*/}${B_} ${_U}version${U_}" echo echo "${_B}DESCRIPTION${B_}" - echo " Utility for updating BLEPeripheral library to specified version." + echo " Utility for updating BLEPeripheralObserver library to specified version." echo echo "${_B}EXAMPLE${B_}" echo " ${_B}${0##*/}${B_} 4.1.22" diff --git a/src/BLECentral.cpp b/src/BLECentral.cpp index b533c94..b3246c5 100644 --- a/src/BLECentral.cpp +++ b/src/BLECentral.cpp @@ -3,12 +3,12 @@ #include "Arduino.h" -#include "BLEPeripheral.h" +#include "BLEPeripheralObserver.h" #include "BLECentral.h" #include "BLEUtil.h" -BLECentral::BLECentral(BLEPeripheral* peripheral) : +BLECentral::BLECentral(BLEPeripheralObserver* peripheral) : _peripheral(peripheral) { this->clearAddress(); diff --git a/src/BLECentral.h b/src/BLECentral.h index 79d5143..0c6771d 100644 --- a/src/BLECentral.h +++ b/src/BLECentral.h @@ -4,11 +4,11 @@ #ifndef _BLE_CENTRAL_H_ #define _BLE_CENTRAL_H_ -class BLEPeripheral; +class BLEPeripheralObserver; class BLECentral { - friend class BLEPeripheral; + friend class BLEPeripheralObserver; public: operator bool() const; @@ -22,12 +22,12 @@ class BLECentral void disconnect(); protected: - BLECentral(BLEPeripheral* peripheral); + BLECentral(BLEPeripheralObserver* peripheral); void setAddress(const unsigned char* address); void clearAddress(); private: - BLEPeripheral* _peripheral; + BLEPeripheralObserver* _peripheral; unsigned char _address[6]; }; diff --git a/src/BLECharacteristic.h b/src/BLECharacteristic.h index d632733..371168f 100644 --- a/src/BLECharacteristic.h +++ b/src/BLECharacteristic.h @@ -28,7 +28,7 @@ class BLECharacteristicValueChangeListener class BLECharacteristic : public BLELocalAttribute { - friend class BLEPeripheral; + friend class BLEPeripheralObserver; public: BLECharacteristic(const char* uuid, unsigned char properties, unsigned char valueSize); diff --git a/src/BLEDevice.h b/src/BLEDevice.h index 5416e58..86c0c35 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -43,7 +43,7 @@ class BLEDeviceEventListener class BLEDevice { - friend class BLEPeripheral; + friend class BLEPeripheralObserver; protected: BLEDevice(); diff --git a/src/BLEHIDPeripheral.cpp b/src/BLEHIDPeripheral.cpp index bb4bfbb..c49eb73 100644 --- a/src/BLEHIDPeripheral.cpp +++ b/src/BLEHIDPeripheral.cpp @@ -8,7 +8,7 @@ static const PROGMEM unsigned char hidInformationCharacteriticValue[] = { 0x11 BLEHIDPeripheral* BLEHIDPeripheral::_instance = NULL; BLEHIDPeripheral::BLEHIDPeripheral(unsigned char req, unsigned char rdy, unsigned char rst) : - BLEPeripheral(req, rdy, rst), + BLEPeripheralObserver(req, rdy, rst), _bleBondStore(), _hidService("1812"), @@ -58,7 +58,7 @@ void BLEHIDPeripheral::begin() { this->_hidReportMapCharacteristic.setHids(this->_hids, this->_numHids); // begin initialization - BLEPeripheral::begin(); + BLEPeripheralObserver::begin(); } void BLEHIDPeripheral::clearBondStoreData() { @@ -70,7 +70,7 @@ void BLEHIDPeripheral::setReportIdOffset(unsigned char reportIdOffset) { } void BLEHIDPeripheral::poll() { - BLEPeripheral::poll(); + BLEPeripheralObserver::poll(); } void BLEHIDPeripheral::addHID(BLEHID& hid) { diff --git a/src/BLEHIDPeripheral.h b/src/BLEHIDPeripheral.h index 0a7d1e8..8d8be58 100644 --- a/src/BLEHIDPeripheral.h +++ b/src/BLEHIDPeripheral.h @@ -6,9 +6,9 @@ #include "BLEHID.h" #include "BLEHIDReportMapCharacteristic.h" -#include "BLEPeripheral.h" +#include "BLEPeripheralObserver.h" -class BLEHIDPeripheral : public BLEPeripheral +class BLEHIDPeripheral : public BLEPeripheralObserver { friend class BLEHID; diff --git a/src/BLELocalAttribute.h b/src/BLELocalAttribute.h index 19dbe38..4e9f3f6 100644 --- a/src/BLELocalAttribute.h +++ b/src/BLELocalAttribute.h @@ -8,7 +8,7 @@ class BLELocalAttribute : public BLEAttribute { - friend class BLEPeripheral; + friend class BLEPeripheralObserver; public: BLELocalAttribute(const char* uuid, enum BLEAttributeType type); diff --git a/src/BLEPeripheral.h b/src/BLEPeripheral.h deleted file mode 100644 index 5476576..0000000 --- a/src/BLEPeripheral.h +++ /dev/null @@ -1,182 +0,0 @@ -// 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); - -enum BLEDeviceEvent { - BLETemperatureReceived = 0, - BLEBatteryLevelReceived = 1, - BLEAdvertisementReceived = 2, - BLEAddressReceived = 3 -}; - -typedef void (*BLEDeviceEventHandler)(const void* parameter); - -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 startAdvertising(); - void stopAdvertising(); - void startScanning(); - void stopScanning(); - - 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); - void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler 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); - virtual void BLEDeviceAdvertisementReceived(BLEDevice& device, const unsigned char* advertisement); - - private: - void initLocalAttributes(); - unsigned char updateAdvertismentData(); - - 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]; - BLEDeviceEventHandler _deviceEvents[3]; - - BLEEirData advertisementData[3]; - BLEEirData scanData; -}; - -#endif diff --git a/src/BLEPeripheral.cpp b/src/BLEPeripheralObserver.cpp similarity index 68% rename from src/BLEPeripheral.cpp rename to src/BLEPeripheralObserver.cpp index ff46b41..589d835 100644 --- a/src/BLEPeripheral.cpp +++ b/src/BLEPeripheralObserver.cpp @@ -6,14 +6,14 @@ #include "BLEDeviceLimits.h" #include "BLEUtil.h" -#include "BLEPeripheral.h" +#include "BLEPeripheralObserver.h" //#define BLE_PERIPHERAL_DEBUG #define DEFAULT_DEVICE_NAME "Arduino" #define DEFAULT_APPEARANCE 0x0000 -BLEPeripheral::BLEPeripheral(unsigned char req, unsigned char rdy, unsigned char rst) : +BLEPeripheralObserver::BLEPeripheralObserver(unsigned char req, unsigned char rdy, unsigned char rst) : #if defined(NRF51) || defined(NRF52) || defined(__RFduino__) _nRF51822(), #else @@ -57,7 +57,7 @@ BLEPeripheral::BLEPeripheral(unsigned char req, unsigned char rdy, unsigned char this->_device->setEventListener(this); } -BLEPeripheral::~BLEPeripheral() { +BLEPeripheralObserver::~BLEPeripheralObserver() { this->end(); if (this->_remoteAttributes) { @@ -69,7 +69,7 @@ BLEPeripheral::~BLEPeripheral() { } } -unsigned char BLEPeripheral::updateAdvertismentData() { +unsigned char BLEPeripheralObserver::updateAdvertismentData() { unsigned char advertisementDataSize = 0; scanData.length = 0; @@ -132,7 +132,7 @@ unsigned char BLEPeripheral::updateAdvertismentData() { return advertisementDataSize; } -void BLEPeripheral::begin() { +void BLEPeripheralObserver::begin() { if (this->_localAttributes == NULL) { this->initLocalAttributes(); @@ -170,44 +170,44 @@ void BLEPeripheral::begin() { this->_device->requestAddress(); } -void BLEPeripheral::poll() { +void BLEPeripheralObserver::poll() { this->_device->poll(); } -void BLEPeripheral::end() { +void BLEPeripheralObserver::end() { this->_device->end(); } -void BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid) { +void BLEPeripheralObserver::setAdvertisedServiceUuid(const char* advertisedServiceUuid) { this->_advertisedServiceUuid = advertisedServiceUuid; } -void BLEPeripheral::setServiceSolicitationUuid(const char* serviceSolicitationUuid) { +void BLEPeripheralObserver::setServiceSolicitationUuid(const char* serviceSolicitationUuid) { this->_serviceSolicitationUuid = serviceSolicitationUuid; } -void BLEPeripheral::setManufacturerData(const unsigned char manufacturerData[], unsigned char manufacturerDataLength) { +void BLEPeripheralObserver::setManufacturerData(const unsigned char manufacturerData[], unsigned char manufacturerDataLength) { this->_manufacturerData = manufacturerData; this->_manufacturerDataLength = manufacturerDataLength; } -void BLEPeripheral::setLocalName(const char* localName) { +void BLEPeripheralObserver::setLocalName(const char* localName) { this->_localName = localName; } -void BLEPeripheral::setConnectable(bool connectable) { +void BLEPeripheralObserver::setConnectable(bool connectable) { this->_device->setConnectable(connectable); } -bool BLEPeripheral::setTxPower(int txPower) { +bool BLEPeripheralObserver::setTxPower(int txPower) { return this->_device->setTxPower(txPower); } -void BLEPeripheral::setBondStore(BLEBondStore& bondStore) { +void BLEPeripheralObserver::setBondStore(BLEBondStore& bondStore) { this->_device->setBondStore(bondStore); } -void BLEPeripheral::startAdvertising() { +void BLEPeripheralObserver::startAdvertising() { int advertisementDataSize = updateAdvertismentData(); this->_device->updateAdvertisementData( advertisementDataSize, advertisementData, @@ -215,31 +215,31 @@ void BLEPeripheral::startAdvertising() { this->_device->startAdvertising(); } -void BLEPeripheral::stopAdvertising() { +void BLEPeripheralObserver::stopAdvertising() { this->_device->stopAdvertising(); } -void BLEPeripheral::startScanning() { +void BLEPeripheralObserver::startScanning() { this->_device->startScanning(); } -void BLEPeripheral::stopScanning() { +void BLEPeripheralObserver::stopScanning() { this->_device->stopScanning(); } -void BLEPeripheral::setDeviceName(const char* deviceName) { +void BLEPeripheralObserver::setDeviceName(const char* deviceName) { this->_deviceNameCharacteristic.setValue(deviceName); } -void BLEPeripheral::setAppearance(unsigned short appearance) { +void BLEPeripheralObserver::setAppearance(unsigned short appearance) { this->_appearanceCharacteristic.setValue((unsigned char *)&appearance, sizeof(appearance)); } -void BLEPeripheral::addAttribute(BLELocalAttribute& attribute) { +void BLEPeripheralObserver::addAttribute(BLELocalAttribute& attribute) { this->addLocalAttribute(attribute); } -void BLEPeripheral::addLocalAttribute(BLELocalAttribute& localAttribute) { +void BLEPeripheralObserver::addLocalAttribute(BLELocalAttribute& localAttribute) { if (this->_localAttributes == NULL) { this->initLocalAttributes(); } @@ -248,7 +248,7 @@ void BLEPeripheral::addLocalAttribute(BLELocalAttribute& localAttribute) { this->_numLocalAttributes++; } -void BLEPeripheral::addRemoteAttribute(BLERemoteAttribute& remoteAttribute) { +void BLEPeripheralObserver::addRemoteAttribute(BLERemoteAttribute& remoteAttribute) { if (this->_remoteAttributes == NULL) { this->_remoteAttributes = (BLERemoteAttribute**)malloc(BLERemoteAttribute::numAttributes() * sizeof(BLERemoteAttribute*)); } @@ -257,91 +257,91 @@ void BLEPeripheral::addRemoteAttribute(BLERemoteAttribute& remoteAttribute) { this->_numRemoteAttributes++; } -void BLEPeripheral::setAdvertisingInterval(unsigned short advertisingInterval) { +void BLEPeripheralObserver::setAdvertisingInterval(unsigned short advertisingInterval) { this->_device->setAdvertisingInterval(advertisingInterval); } -void BLEPeripheral::setConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval) { +void BLEPeripheralObserver::setConnectionInterval(unsigned short minimumConnectionInterval, unsigned short maximumConnectionInterval) { this->_device->setConnectionInterval(minimumConnectionInterval, maximumConnectionInterval); } -void BLEPeripheral::disconnect() { +void BLEPeripheralObserver::disconnect() { this->_device->disconnect(); } -BLECentral BLEPeripheral::central() { +BLECentral BLEPeripheralObserver::central() { this->poll(); return this->_central; } -bool BLEPeripheral::connected() { +bool BLEPeripheralObserver::connected() { this->poll(); return this->_central; } -void BLEPeripheral::setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler eventHandler) { +void BLEPeripheralObserver::setEventHandler(BLEPeripheralObserverEvent event, BLEPeripheralObserverEventHandler eventHandler) { if (event < sizeof(this->_eventHandlers)) { this->_eventHandlers[event] = eventHandler; } } -void BLEPeripheral::setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler) { +void BLEPeripheralObserver::setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler) { if (event < sizeof(this->_deviceEvents)) { this->_deviceEvents[event] = eventHandler; } } -bool BLEPeripheral::characteristicValueChanged(BLECharacteristic& characteristic) { +bool BLEPeripheralObserver::characteristicValueChanged(BLECharacteristic& characteristic) { return this->_device->updateCharacteristicValue(characteristic); } -bool BLEPeripheral::broadcastCharacteristic(BLECharacteristic& characteristic) { +bool BLEPeripheralObserver::broadcastCharacteristic(BLECharacteristic& characteristic) { return this->_device->broadcastCharacteristic(characteristic); } -bool BLEPeripheral::canNotifyCharacteristic(BLECharacteristic& characteristic) { +bool BLEPeripheralObserver::canNotifyCharacteristic(BLECharacteristic& characteristic) { return this->_device->canNotifyCharacteristic(characteristic); } -bool BLEPeripheral::canIndicateCharacteristic(BLECharacteristic& characteristic) { +bool BLEPeripheralObserver::canIndicateCharacteristic(BLECharacteristic& characteristic) { return this->_device->canIndicateCharacteristic(characteristic); } -bool BLEPeripheral::canReadRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { +bool BLEPeripheralObserver::canReadRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { return this->_device->canReadRemoteCharacteristic(characteristic); } -bool BLEPeripheral::readRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { +bool BLEPeripheralObserver::readRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { return this->_device->readRemoteCharacteristic(characteristic); } -bool BLEPeripheral::canWriteRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { +bool BLEPeripheralObserver::canWriteRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { return this->_device->canWriteRemoteCharacteristic(characteristic); } -bool BLEPeripheral::writeRemoteCharacteristic(BLERemoteCharacteristic& characteristic, const unsigned char value[], unsigned char length) { +bool BLEPeripheralObserver::writeRemoteCharacteristic(BLERemoteCharacteristic& characteristic, const unsigned char value[], unsigned char length) { return this->_device->writeRemoteCharacteristic(characteristic, value, length); } -bool BLEPeripheral::canSubscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { +bool BLEPeripheralObserver::canSubscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { return this->_device->canSubscribeRemoteCharacteristic(characteristic); } -bool BLEPeripheral::subscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { +bool BLEPeripheralObserver::subscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { return this->_device->subscribeRemoteCharacteristic(characteristic); } -bool BLEPeripheral::canUnsubscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { +bool BLEPeripheralObserver::canUnsubscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { return this->_device->canUnsubscribeRemoteCharacteristic(characteristic); } -bool BLEPeripheral::unsubcribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { +bool BLEPeripheralObserver::unsubcribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic) { return this->_device->unsubcribeRemoteCharacteristic(characteristic); } -void BLEPeripheral::BLEDeviceConnected(BLEDevice& /*device*/, const unsigned char* address) { +void BLEPeripheralObserver::BLEDeviceConnected(BLEDevice& /*device*/, const unsigned char* address) { this->_central.setAddress(address); #ifdef BLE_PERIPHERAL_DEBUG @@ -349,19 +349,19 @@ void BLEPeripheral::BLEDeviceConnected(BLEDevice& /*device*/, const unsigned cha Serial.println(this->_central.address()); #endif - BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEConnected]; + BLEPeripheralObserverEventHandler eventHandler = this->_eventHandlers[BLEConnected]; if (eventHandler) { eventHandler(this->_central); } } -void BLEPeripheral::BLEDeviceDisconnected(BLEDevice& /*device*/) { +void BLEPeripheralObserver::BLEDeviceDisconnected(BLEDevice& /*device*/) { #ifdef BLE_PERIPHERAL_DEBUG Serial.print(F("Peripheral disconnected from central: ")); Serial.println(this->_central.address()); #endif - BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEDisconnected]; + BLEPeripheralObserverEventHandler eventHandler = this->_eventHandlers[BLEDisconnected]; if (eventHandler) { eventHandler(this->_central); } @@ -369,43 +369,43 @@ void BLEPeripheral::BLEDeviceDisconnected(BLEDevice& /*device*/) { this->_central.clearAddress(); } -void BLEPeripheral::BLEDeviceBonded(BLEDevice& /*device*/) { +void BLEPeripheralObserver::BLEDeviceBonded(BLEDevice& /*device*/) { #ifdef BLE_PERIPHERAL_DEBUG Serial.print(F("Peripheral bonded: ")); Serial.println(this->_central.address()); #endif - BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEBonded]; + BLEPeripheralObserverEventHandler eventHandler = this->_eventHandlers[BLEBonded]; if (eventHandler) { eventHandler(this->_central); } } -void BLEPeripheral::BLEDeviceRemoteServicesDiscovered(BLEDevice& /*device*/) { +void BLEPeripheralObserver::BLEDeviceRemoteServicesDiscovered(BLEDevice& /*device*/) { #ifdef BLE_PERIPHERAL_DEBUG Serial.print(F("Peripheral discovered central remote services: ")); Serial.println(this->_central.address()); #endif - BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLERemoteServicesDiscovered]; + BLEPeripheralObserverEventHandler eventHandler = this->_eventHandlers[BLERemoteServicesDiscovered]; if (eventHandler) { eventHandler(this->_central); } } -void BLEPeripheral::BLEDeviceCharacteristicValueChanged(BLEDevice& /*device*/, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength) { +void BLEPeripheralObserver::BLEDeviceCharacteristicValueChanged(BLEDevice& /*device*/, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength) { characteristic.setValue(this->_central, value, valueLength); } -void BLEPeripheral::BLEDeviceCharacteristicSubscribedChanged(BLEDevice& /*device*/, BLECharacteristic& characteristic, bool subscribed) { +void BLEPeripheralObserver::BLEDeviceCharacteristicSubscribedChanged(BLEDevice& /*device*/, BLECharacteristic& characteristic, bool subscribed) { characteristic.setSubscribed(this->_central, subscribed); } -void BLEPeripheral::BLEDeviceRemoteCharacteristicValueChanged(BLEDevice& /*device*/, BLERemoteCharacteristic& remoteCharacteristic, const unsigned char* value, unsigned char valueLength) { +void BLEPeripheralObserver::BLEDeviceRemoteCharacteristicValueChanged(BLEDevice& /*device*/, BLERemoteCharacteristic& remoteCharacteristic, const unsigned char* value, unsigned char valueLength) { remoteCharacteristic.setValue(this->_central, value, valueLength); } -void BLEPeripheral::BLEDeviceAddressReceived(BLEDevice& device, const unsigned char* address) { +void BLEPeripheralObserver::BLEDeviceAddressReceived(BLEDevice& device, const unsigned char* address) { #ifdef BLE_PERIPHERAL_DEBUG char addressStr[18]; @@ -420,28 +420,28 @@ void BLEPeripheral::BLEDeviceAddressReceived(BLEDevice& device, const unsigned c } } -void BLEPeripheral::BLEDeviceTemperatureReceived(BLEDevice& device, float temperature) { +void BLEPeripheralObserver::BLEDeviceTemperatureReceived(BLEDevice& device, float temperature) { BLEDeviceEventHandler eventHandler = this->_deviceEvents[BLETemperatureReceived]; if (eventHandler) { eventHandler(&temperature); } } -void BLEPeripheral::BLEDeviceBatteryLevelReceived(BLEDevice& device, float batteryLevel) { +void BLEPeripheralObserver::BLEDeviceBatteryLevelReceived(BLEDevice& device, float batteryLevel) { BLEDeviceEventHandler eventHandler = this->_deviceEvents[BLEBatteryLevelReceived]; if (eventHandler) { eventHandler(&batteryLevel); } } -void BLEPeripheral::BLEDeviceAdvertisementReceived(BLEDevice& device, const unsigned char* advertisement) { +void BLEPeripheralObserver::BLEDeviceAdvertisementReceived(BLEDevice& device, const unsigned char* advertisement) { BLEDeviceEventHandler eventHandler = this->_deviceEvents[BLEAdvertisementReceived]; if (eventHandler) { eventHandler(advertisement); } } -void BLEPeripheral::initLocalAttributes() { +void BLEPeripheralObserver::initLocalAttributes() { this->_localAttributes = (BLELocalAttribute**)malloc(BLELocalAttribute::numAttributes() * sizeof(BLELocalAttribute*)); this->_localAttributes[0] = &this->_genericAccessService; diff --git a/src/BLEPeripheralObserver.h b/src/BLEPeripheralObserver.h index bddb52c..cf3f994 100644 --- a/src/BLEPeripheralObserver.h +++ b/src/BLEPeripheralObserver.h @@ -1 +1,182 @@ -#include "BLEPeripheral.h" +// 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 BLEPeripheralObserverEvent { + BLEConnected = 0, + BLEDisconnected = 1, + BLEBonded = 2, + BLERemoteServicesDiscovered = 3 +}; + +typedef void (*BLEPeripheralObserverEventHandler)(BLECentral& central); + +enum BLEDeviceEvent { + BLETemperatureReceived = 0, + BLEBatteryLevelReceived = 1, + BLEAdvertisementReceived = 2, + BLEAddressReceived = 3 +}; + +typedef void (*BLEDeviceEventHandler)(const void* parameter); + +class BLEPeripheralObserver : public BLEDeviceEventListener, + public BLECharacteristicValueChangeListener, + public BLERemoteCharacteristicValueChangeListener +{ + public: + BLEPeripheralObserver(unsigned char req = BLE_DEFAULT_REQ, unsigned char rdy = BLE_DEFAULT_RDY, unsigned char rst = BLE_DEFAULT_RST); + virtual ~BLEPeripheralObserver(); + + 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 startAdvertising(); + void stopAdvertising(); + void startScanning(); + void stopScanning(); + + 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(BLEPeripheralObserverEvent event, BLEPeripheralObserverEventHandler eventHandler); + void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler 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); + virtual void BLEDeviceAdvertisementReceived(BLEDevice& device, const unsigned char* advertisement); + + private: + void initLocalAttributes(); + unsigned char updateAdvertismentData(); + + 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; + BLEPeripheralObserverEventHandler _eventHandlers[4]; + BLEDeviceEventHandler _deviceEvents[4]; // TODO: use variable! + + BLEEirData advertisementData[3]; + BLEEirData scanData; +}; + +#endif diff --git a/src/BLERemoteAttribute.h b/src/BLERemoteAttribute.h index defe372..a4873ca 100644 --- a/src/BLERemoteAttribute.h +++ b/src/BLERemoteAttribute.h @@ -8,7 +8,7 @@ class BLERemoteAttribute : public BLEAttribute { - friend class BLEPeripheral; + friend class BLEPeripheralObserver; public: BLERemoteAttribute(const char* uuid, enum BLEAttributeType type); diff --git a/src/BLERemoteCharacteristic.h b/src/BLERemoteCharacteristic.h index 9c369bd..e8d737a 100644 --- a/src/BLERemoteCharacteristic.h +++ b/src/BLERemoteCharacteristic.h @@ -33,7 +33,7 @@ typedef void (*BLERemoteCharacteristicEventHandler)(BLECentral& central, BLERemo class BLERemoteCharacteristic : public BLERemoteAttribute { - friend class BLEPeripheral; + friend class BLEPeripheralObserver; public: BLERemoteCharacteristic(const char* uuid, unsigned char properties); diff --git a/src/EddystoneBeacon.cpp b/src/EddystoneBeacon.cpp index cc0ecda..ada587e 100644 --- a/src/EddystoneBeacon.cpp +++ b/src/EddystoneBeacon.cpp @@ -35,7 +35,7 @@ static const char* EDDYSTONE_URL_BEACON_SUFFIX_SUBSTITUTIONS[] = { }; EddystoneBeacon::EddystoneBeacon(unsigned char req, unsigned char rdy, unsigned char rst) : - BLEPeripheral(req, rdy, rst), + BLEPeripheralObserver(req, rdy, rst), _bleService("feaa"), _bleCharacteristic("feab", BLERead | BLEBroadcast, MAX_SERVICE_DATA_SIZE) { @@ -66,7 +66,7 @@ void EddystoneBeacon::begin(char power, const BLEUuid& uid) { this->setAdvertisedServiceUuid(this->_bleService.uuid()); - BLEPeripheral::begin(); + BLEPeripheralObserver::begin(); this->_bleCharacteristic.broadcast(); } @@ -77,7 +77,7 @@ void EddystoneBeacon::begin(char power, const char* uri) { this->setAdvertisedServiceUuid(this->_bleService.uuid()); - BLEPeripheral::begin(); + BLEPeripheralObserver::begin(); this->_bleCharacteristic.broadcast(); } diff --git a/src/EddystoneBeacon.h b/src/EddystoneBeacon.h index 707b6d9..0a78b3e 100644 --- a/src/EddystoneBeacon.h +++ b/src/EddystoneBeacon.h @@ -4,10 +4,10 @@ #ifndef _EDDYSTONE_BEACON_H_ #define _EDDYSTONE_BEACON_H_ -#include "BLEPeripheral.h" +#include "BLEPeripheralObserver.h" #include "BLEUuid.h" -class EddystoneBeacon : public BLEPeripheral +class EddystoneBeacon : public BLEPeripheralObserver { public: EddystoneBeacon(unsigned char req = BLE_DEFAULT_REQ, unsigned char rdy = BLE_DEFAULT_RDY, unsigned char rst = BLE_DEFAULT_RST); diff --git a/src/iBeacon.cpp b/src/iBeacon.cpp index d1f9c22..370a22c 100644 --- a/src/iBeacon.cpp +++ b/src/iBeacon.cpp @@ -6,7 +6,7 @@ #include "iBeacon.h" iBeacon::iBeacon() : - BLEPeripheral() + BLEPeripheralObserver() { this->setConnectable(false); } @@ -35,7 +35,7 @@ void iBeacon::begin(const char* uuidString, unsigned short major, unsigned short this->setManufacturerData(this->_manufacturerData, i); - BLEPeripheral::begin(); + BLEPeripheralObserver::begin(); } void iBeacon::loop() { diff --git a/src/iBeacon.h b/src/iBeacon.h index 06a317b..33c4d77 100644 --- a/src/iBeacon.h +++ b/src/iBeacon.h @@ -6,10 +6,10 @@ #if defined(NRF51) || defined(NRF52) || defined(__RFduino__) -#include "BLEPeripheral.h" +#include "BLEPeripheralObserver.h" #include "BLEUuid.h" -class iBeacon : public BLEPeripheral +class iBeacon : public BLEPeripheralObserver { public: iBeacon(); diff --git a/src/nRF51822.h b/src/nRF51822.h index 688abca..40c53f8 100644 --- a/src/nRF51822.h +++ b/src/nRF51822.h @@ -29,7 +29,7 @@ class nRF51822 : public BLEDevice { - friend class BLEPeripheral; + friend class BLEPeripheralObserver; protected: struct localCharacteristicInfo { diff --git a/src/nRF8001.h b/src/nRF8001.h index 9db02fe..320a87e 100644 --- a/src/nRF8001.h +++ b/src/nRF8001.h @@ -12,7 +12,7 @@ class nRF8001 : protected BLEDevice { - friend class BLEPeripheral; + friend class BLEPeripheralObserver; protected: struct localPipeInfo {