From 66a3dca25dab2c4b71e556df56e4c6bcbfe889f5 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Mon, 1 Jun 2015 00:19:40 +0200 Subject: [PATCH] get arduino peripheral demo to work --- .../examples/LEPeripheral/LEPeripheral.ino | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/platforms/arduino/examples/LEPeripheral/LEPeripheral.ino b/platforms/arduino/examples/LEPeripheral/LEPeripheral.ino index 45b035a7b..1d318c4a5 100644 --- a/platforms/arduino/examples/LEPeripheral/LEPeripheral.ino +++ b/platforms/arduino/examples/LEPeripheral/LEPeripheral.ino @@ -2,11 +2,11 @@ #include #include -static uint16_t value_handle; +static char characteristic_data = 'H'; void setup(void){ - setup_printf(9600); + Serial.begin(9600); // set callbacks BTstack.setBLEDeviceConnectedCallback(deviceConnectedCallback); @@ -15,15 +15,18 @@ void setup(void){ BTstack.setGATTCharacteristicWrite(gattWriteCallback); // setup GATT Database - int flags = 0; - uint8_t * data = NULL; - uint16_t data_len = 0; - BTstack.addGATTService(new UUID("B8E06067-62AD-41BA-9231-206AE80AB550")); - value_handle = BTstack.addGATTCharacteristic(new UUID("f897177b-aee8-4767-8ecc-cc694fd5fcee"), flags, "This is a String!"); + BTstack.addGATTService(new UUID("B8E06067-62AD-41BA-9231-206AE80AB551")); + BTstack.addGATTCharacteristic(new UUID("f897177b-aee8-4767-8ecc-cc694fd5fcef"), ATT_PROPERTY_READ, "This is a String!"); + BTstack.addGATTCharacteristicDynamic(new UUID("f897177b-aee8-4767-8ecc-cc694fd5fce0"), ATT_PROPERTY_READ | ATT_PROPERTY_WRITE | ATT_PROPERTY_NOTIFY, 0); + + // use dummy address to force refresh on iOS devices + bd_addr_t dummy = { 1,2,3,4,5,6}; + BTstack.setPublicBdAddr(dummy); // startup Bluetooth and activate advertisements BTstack.setup(); BTstack.startAdvertising(); + // BTstack.enablePacketLogger(); } @@ -34,7 +37,7 @@ void loop(void){ void deviceConnectedCallback(BLEStatus status, BLEDevice *device) { switch (status){ case BLE_STATUS_OK: - printf("Device connected!\n"); + Serial.println("Device connected!"); break; default: break; @@ -42,7 +45,7 @@ void deviceConnectedCallback(BLEStatus status, BLEDevice *device) { } void deviceDisconnectedCallback(BLEDevice * device){ - printf("Disconnected.\n"); + Serial.println("Disconnected."); } // ATT Client Read Callback for Dynamic Data @@ -52,8 +55,12 @@ void deviceDisconnectedCallback(BLEDevice * device){ // @param buffer // @param buffer_size uint16_t gattReadCallback(uint16_t value_handle, uint8_t * buffer, uint16_t buffer_size){ - printf("gattReadCallback, handle %u\n", value_handle); - return 0; + if (buffer){ + Serial.print("gattReadCallback, value: "); + Serial.println(characteristic_data, HEX); + buffer[0] = characteristic_data; + } + return 1; } // ATT Client Write Callback for Dynamic Data @@ -62,7 +69,9 @@ uint16_t gattReadCallback(uint16_t value_handle, uint8_t * buffer, uint16_t buff // @param buffer_size // @returns 0 if write was ok, ATT_ERROR_INVALID_OFFSET if offset is larger than max buffer int gattWriteCallback(uint16_t value_handle, uint8_t *buffer, uint16_t buffer_size){ - printf("gattWriteCallback, handle %u\n", value_handle); + characteristic_data = buffer[0]; + Serial.print("gattWriteCallback , value "); + Serial.println(characteristic_data, HEX); return 0; }