|
| 1 | +/********************************************************************* |
| 2 | + This is an example for our nRF52 based Bluefruit LE modules |
| 3 | +
|
| 4 | + Pick one up today in the adafruit shop! |
| 5 | +
|
| 6 | + Adafruit invests time and resources providing this open source code, |
| 7 | + please support Adafruit and open-source hardware by purchasing |
| 8 | + products from Adafruit! |
| 9 | +
|
| 10 | + MIT license, check LICENSE for more information |
| 11 | + All text above, and the splash screen below must be included in |
| 12 | + any redistribution |
| 13 | +*********************************************************************/ |
| 14 | + |
| 15 | +/* This sketch demonstrates the client Current Time Service using the |
| 16 | + * BLEClientCts API(). After uploading, go to iOS setting and connect |
| 17 | + * to Bluefruit52, and then press PAIR. |
| 18 | + * |
| 19 | + * Note: Currently only iOS act as a CTS server, Android does not. The |
| 20 | + * easiest way to test this sketch is using an iOS device. |
| 21 | + * |
| 22 | + * Current Time Service info: |
| 23 | + * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.current_time.xml |
| 24 | + * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.current_time.xml |
| 25 | + * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.local_time_information.xml |
| 26 | + */ |
| 27 | + |
| 28 | +#include <bluefruit.h> |
| 29 | + |
| 30 | +// BLE Client Current Time Service |
| 31 | +BLEClientCts bleCTime; |
| 32 | + |
| 33 | +void setup() |
| 34 | +{ |
| 35 | + Serial.begin(115200); |
| 36 | + Serial.println("Bluefruit52 BLE Client Current Time Example"); |
| 37 | + Serial.println("-------------------------------------------\n"); |
| 38 | + |
| 39 | + Serial.println("Go to iOS's Bluetooth settings and connect to Bluefruit52"); |
| 40 | + Serial.println("It may appear up as 'Accessory' depending on your iOS version."); |
| 41 | + |
| 42 | + Bluefruit.begin(); |
| 43 | + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 |
| 44 | + Bluefruit.setTxPower(4); |
| 45 | + Bluefruit.setName("Bluefruit52"); |
| 46 | + Bluefruit.setConnectCallback(connect_callback); |
| 47 | + Bluefruit.setDisconnectCallback(disconnect_callback); |
| 48 | + |
| 49 | + // Configure CTS client |
| 50 | + bleCTime.begin(); |
| 51 | + |
| 52 | + // Callback invoked when iOS device time changes |
| 53 | + // To test this go to Setting -> Date & Time -> Toggle Time Zone "Set Automatically" |
| 54 | + // Or change the time manually etc ... |
| 55 | + bleCTime.setAdjustCallback(cts_adjust_callback); |
| 56 | + |
| 57 | + // Set up and start advertising |
| 58 | + startAdv(); |
| 59 | +} |
| 60 | + |
| 61 | +void startAdv(void) |
| 62 | +{ |
| 63 | + // Advertising packet |
| 64 | + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
| 65 | + Bluefruit.Advertising.addTxPower(); |
| 66 | + Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_GENERIC_CLOCK); |
| 67 | + |
| 68 | + // Include CTS client UUID |
| 69 | + Bluefruit.Advertising.addService(bleCTime); |
| 70 | + |
| 71 | + // Includes name |
| 72 | + Bluefruit.Advertising.addName(); |
| 73 | + |
| 74 | + /* Start Advertising |
| 75 | + * - Enable auto advertising if disconnected |
| 76 | + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms |
| 77 | + * - Timeout for fast mode is 30 seconds |
| 78 | + * - Start(timeout) with timeout = 0 will advertise forever (until connected) |
| 79 | + * |
| 80 | + * For recommended advertising interval |
| 81 | + * https://developer.apple.com/library/content/qa/qa1931/_index.html |
| 82 | + */ |
| 83 | + Bluefruit.Advertising.restartOnDisconnect(true); |
| 84 | + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms |
| 85 | + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode |
| 86 | + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds |
| 87 | +} |
| 88 | + |
| 89 | +void loop() |
| 90 | +{ |
| 91 | + // Not connected, wait for a connection |
| 92 | + if ( !Bluefruit.connected() ) return; |
| 93 | + |
| 94 | + // If service is not yet discovered |
| 95 | + if ( !bleCTime.discovered() ) return; |
| 96 | + |
| 97 | + // Get Time from iOS once per second |
| 98 | + // Note it is advised to update this quickly |
| 99 | + // Application should use local clock and update time after |
| 100 | + // a long period (e.g an hour or day)s |
| 101 | + bleCTime.getCurrentTime(); |
| 102 | + |
| 103 | + printTime(); |
| 104 | + |
| 105 | + delay(1000); |
| 106 | +} |
| 107 | + |
| 108 | +void connect_callback(uint16_t conn_handle) |
| 109 | +{ |
| 110 | + Serial.println("Connected"); |
| 111 | + |
| 112 | + Serial.print("Discovering CTS ... "); |
| 113 | + if ( bleCTime.discover(conn_handle) ) |
| 114 | + { |
| 115 | + Serial.println("Discovered"); |
| 116 | + |
| 117 | + // iOS requires pairing to work, it makes sense to request security here as well |
| 118 | + Serial.print("Attempting to PAIR with the iOS device, please press PAIR on your phone ... "); |
| 119 | + if ( Bluefruit.requestPairing() ) |
| 120 | + { |
| 121 | + Serial.println("Done"); |
| 122 | + Serial.println("Enabling Time Adjust Notify"); |
| 123 | + bleCTime.enableAdjust(); |
| 124 | + |
| 125 | + Serial.print("Get Current Time chars value"); |
| 126 | + bleCTime.getCurrentTime(); |
| 127 | + |
| 128 | + Serial.print("Get Local Time Info chars value"); |
| 129 | + bleCTime.getLocalTimeInfo(); |
| 130 | + |
| 131 | + Serial.println(); |
| 132 | + } |
| 133 | + |
| 134 | + Serial.println(); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +void cts_adjust_callback(uint8_t reason) |
| 139 | +{ |
| 140 | + const char * reason_str[] = { "Manual", "External Reference", "Change of Time Zone", "Change of DST" }; |
| 141 | + |
| 142 | + Serial.println("iOS Device time changed due to "); |
| 143 | + Serial.println( reason_str[reason] ); |
| 144 | +} |
| 145 | + |
| 146 | +void printTime(void) |
| 147 | +{ |
| 148 | + const char * day_of_week_str[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; |
| 149 | + |
| 150 | + Serial.printf("%04d-%02d-%02d ", bleCTime.Time.year, bleCTime.Time.month, bleCTime.Time.day); |
| 151 | + Serial.printf("%02d:%02d:%02d ", bleCTime.Time.hour, bleCTime.Time.minute, bleCTime.Time.second); |
| 152 | + Serial.print(day_of_week_str[bleCTime.Time.weekday]); |
| 153 | + |
| 154 | + int utc_offset = bleCTime.LocalInfo.timezone*15; // in 15 minutes unit |
| 155 | + Serial.printf(" (UTC %+d:%02d, ", utc_offset/60, utc_offset%60); |
| 156 | + Serial.printf("DST %+.1f)", ((float) bleCTime.LocalInfo.dst_offset*15)/60 ); |
| 157 | + Serial.println(); |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +void disconnect_callback(uint16_t conn_handle, uint8_t reason) |
| 162 | +{ |
| 163 | + (void) reason; |
| 164 | + |
| 165 | + Serial.println(); |
| 166 | + Serial.println("Disconnected"); |
| 167 | +} |
0 commit comments