From 054ab9d96eb9b13bbe6d4be3cf48eeeaa53219cb Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 18 Jul 2017 21:49:43 +0700 Subject: [PATCH] add client cts example, discovered OK --- .../Peripheral/client_cts/client_cts.ino | 116 ++++++++++++++++++ .../src/clients/BLEClientCts.cpp | 7 +- 2 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/client_cts/client_cts.ino diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/client_cts/client_cts.ino b/libraries/Bluefruit52Lib/examples/Peripheral/client_cts/client_cts.ino new file mode 100644 index 00000000..2279f0a3 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Peripheral/client_cts/client_cts.ino @@ -0,0 +1,116 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This sketch demonstrate the client Current Time service using BLEClientCts + * API(). After uploading, go to iOS setting and connect to Bluefruit52, + * and then press PAIR. + * + * Note: Currently only iOS act as CTS server, Android does not, The easiest + * way to test this sketch is using an iOS device. + */ + +#include + +// BLE Client Current Time Service +BLEClientCts bleCTime; + +void setup() +{ + Serial.begin(115200); + Serial.println("Bluefruit52 BLE Client Current Time Example"); + Serial.println("-------------------------------------------\n"); + + Serial.println("Go to iOS's Bluetooth settings and connect to Bluefruit52"); + Serial.println("It may appear up as 'Accessory' depending on your OS version."); + + Bluefruit.begin(); + Bluefruit.setName("Bluefruit52"); + Bluefruit.setConnectCallback(connect_callback); + Bluefruit.setDisconnectCallback(disconnect_callback); + + // Configure CTS client + bleCTime.begin(); + + // Set up and start advertising + startAdv(); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include CTS client uuid + Bluefruit.Advertising.addService(bleCTime); + + // Includes name + Bluefruit.Advertising.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // Not connected, wait for a connection + if ( !Bluefruit.connected() ) return; + + // If service is not yet discovered + if ( !bleCTime.discovered() ) return; + + // Your code here +} + +void connect_callback(uint16_t conn_handle) +{ + Serial.println("Connected"); + + Serial.print("Discovering CTS ... "); + if ( bleCTime.discover(conn_handle) ) + { + Serial.println("Discovered"); + + // ANCS requires pairing to work, it makes sense to request security here as well + Serial.print("Attempting to PAIR with the iOS device, please press PAIR on your phone ... "); + if ( Bluefruit.requestPairing() ) + { + Serial.println("Done"); +// Serial.println("Enabling notifications"); +// Serial.println(); +// bleancs.enableNotification(); + } + + Serial.println(); + } +} + +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) reason; + + Serial.println(); + Serial.println("Disconnected"); +} diff --git a/libraries/Bluefruit52Lib/src/clients/BLEClientCts.cpp b/libraries/Bluefruit52Lib/src/clients/BLEClientCts.cpp index c66200e2..e48589bb 100644 --- a/libraries/Bluefruit52Lib/src/clients/BLEClientCts.cpp +++ b/libraries/Bluefruit52Lib/src/clients/BLEClientCts.cpp @@ -59,10 +59,11 @@ bool BLEClientCts::discover(uint16_t conn_handle) VERIFY( BLEClientService::discover(conn_handle) ); _conn_hdl = BLE_CONN_HANDLE_INVALID; // make as invalid until we found all chars - // Discover Current Time chars ( mandatory ) + // Discover characteristics + Bluefruit.Discovery.discoverCharacteristic(conn_handle, _cur_time, _local_info); - - // Discover Local Time Information chars ( optional ) + // Current Time chars is mandatory + VERIFY( _cur_time.valueHandle() != BLE_GATT_HANDLE_INVALID, false); return true; }