xref: /btstack/port/arduino/examples/LEPeripheral/LEPeripheral.ino (revision 8caefee39d444df6d8908a96a844825f10fbdaa4)
1// LE Peripheral Example - not working yet
2#include <BTstack.h>
3#include <SPI.h>
4
5/*
6 * EXAMPLE_START(LEPeripheral): LE Peripheral
7 *
8 * @text BTstack allows to setup a GATT Services and Characteristics directly
9 * from the setup function without using other tools outside of the Arduino IDE.
10 *
11 * @section Setup
12 *
13 * @text First, a number of callbacks are set. Then, a Service with a Read-only
14 * Characteristic and a dynamic Characteristic is added to the GATT database.
15 * In BTstack, a dynamic Characteristic is a Characteristic where reads and writes
16 * are forwarded to the Sketch. In this example, the dynamic Characteristic is
17 * provided by the single byte variable characteristic_data.
18 */
19
20/* LISTING_START(LEPeripheralSetup): Setup */
21static char characteristic_data = 'H';
22
23void setup(void){
24
25    Serial.begin(9600);
26
27    // set callbacks
28    BTstack.setBLEDeviceConnectedCallback(deviceConnectedCallback);
29    BTstack.setBLEDeviceDisconnectedCallback(deviceDisconnectedCallback);
30    BTstack.setGATTCharacteristicRead(gattReadCallback);
31    BTstack.setGATTCharacteristicWrite(gattWriteCallback);
32
33    // setup GATT Database
34    BTstack.addGATTService(new UUID("B8E06067-62AD-41BA-9231-206AE80AB551"));
35    BTstack.addGATTCharacteristic(new UUID("f897177b-aee8-4767-8ecc-cc694fd5fcef"), ATT_PROPERTY_READ, "This is a String!");
36    BTstack.addGATTCharacteristicDynamic(new UUID("f897177b-aee8-4767-8ecc-cc694fd5fce0"), ATT_PROPERTY_READ | ATT_PROPERTY_WRITE | ATT_PROPERTY_NOTIFY, 0);
37
38    // startup Bluetooth and activate advertisements
39    BTstack.setup();
40    BTstack.startAdvertising();
41}
42/* LISTING_END(LEPeripheralSetup): Setup */
43
44void loop(void){
45    BTstack.loop();
46}
47
48/*
49 * @section Device Connected Callback
50 *
51 * @text When a remove device connects, device connected callback is callec.
52 */
53/* LISTING_START(LEPeripheralDeviceConnectedCallback): Device Connected Callback */
54void deviceConnectedCallback(BLEStatus status, BLEDevice *device) {
55    switch (status){
56        case BLE_STATUS_OK:
57            Serial.println("Device connected!");
58            break;
59        default:
60            break;
61    }
62}
63/* LISTING_END(LEPeripheralDeviceConnectedCallback): Device Connected Callback */
64
65/*
66 * @section Device Disconnected Callback
67 *
68 * @text If the connection to a device breaks, the device disconnected callback
69 * is called.
70 */
71/* LISTING_START(LEPeripheralDeviceDisconnectedCallback): Device Disconnected Callback */
72void deviceDisconnectedCallback(BLEDevice * device){
73    Serial.println("Disconnected.");
74}
75/* LISTING_END(LEPeripheralDeviceDisconnectedCallback): Device Disconnected Callback */
76
77/*
78 * @section Read Callback
79 *
80 * @text In BTstack, the Read Callback is first called to query the size of the
81 * Charcteristic Value, before it is called to provide the data.
82 * Both times, the size has to be returned. The data is only stored in the provided
83 * buffer, if the buffer argeument is not NULL.
84 * If more than one dynamic Characteristics is used, the value handle is used
85 * to distinguish them.
86 */
87/* LISTING_START(LEPeripheralReadCallback): Read Callback */
88uint16_t gattReadCallback(uint16_t value_handle, uint8_t * buffer, uint16_t buffer_size){
89    if (buffer){
90        Serial.print("gattReadCallback, value: ");
91        Serial.println(characteristic_data, HEX);
92        buffer[0] = characteristic_data;
93    }
94    return 1;
95}
96/* LISTING_END(LEPeripheralDeviceDisconnectedCallback): Read Callback */
97
98/*
99 * @section Write Callback
100 *
101 * @text When the remove device writes a Characteristic Value, the Write callback
102 * is called. The buffer arguments points to the data of size size/
103 * If more than one dynamic Characteristics is used, the value handle is used
104 * to distinguish them.
105 */
106/* LISTING_START(LEPeripheralWriteCallback): Write Callback */
107int gattWriteCallback(uint16_t value_handle, uint8_t *buffer, uint16_t size){
108    characteristic_data = buffer[0];
109    Serial.print("gattWriteCallback , value ");
110    Serial.println(characteristic_data, HEX);
111    return 0;
112}
113/* LISTING_END(LEPeripheralWriteCallback): Write Callback */
114
115