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