1919 * Note: Currently only iOS act as a CTS server, Android does not. The
2020 * easiest way to test this sketch is using an iOS device.
2121 *
22+ * This sketch is similar to client_cts but also uses a Feather OLED Wing
23+ * to display time https://www.adafruit.com/product/2900
24+ *
2225 * Current Time Service info:
2326 * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.current_time.xml
2427 * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.current_time.xml
2528 * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.local_time_information.xml
2629 */
2730
2831#include < bluefruit.h>
32+ #include < Adafruit_GFX.h>
33+ #include < Adafruit_SSD1306.h>
34+
35+ #define OLED_RESET 4
36+ Adafruit_SSD1306 oled (OLED_RESET);
2937
3038// BLE Client Current Time Service
3139BLEClientCts bleCTime;
3240
3341void setup ()
3442{
35- Serial. begin ( 115200 );
36- Serial. println ( " Bluefruit52 BLE Client Current Time Example " );
37- Serial. println ( " ------------------------------------------- \n " );
43+ // init with the I2C addr 0x3C (for the 128x32) and show splashscreen
44+ oled. begin (SSD1306_SWITCHCAPVCC, 0x3C );
45+ oled. display ( );
3846
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. " );
47+ oled. setTextSize ( 1 ); // max is 4 line, 21 chars each
48+ oled. setTextColor (WHITE );
4149
4250 Bluefruit.begin ();
4351 // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
@@ -49,13 +57,16 @@ void setup()
4957 // Configure CTS client
5058 bleCTime.begin ();
5159
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-
5760 // Set up and start advertising
5861 startAdv ();
62+
63+ // splash screen effect
64+ delay (100 );
65+
66+ oled.clearDisplay ();
67+ oled.setCursor (0 , 0 );
68+ oled.println (" Waiting to connect" );
69+ oled.display ();
5970}
6071
6172void startAdv (void )
@@ -88,17 +99,15 @@ void startAdv(void)
8899
89100void loop ()
90101{
91- // Not connected, wait for a connection
92- if ( !Bluefruit.connected () ) return ;
93-
94102 // If service is not yet discovered
95- if ( !bleCTime.discovered () ) return ;
103+ if ( !bleCTime.discovered () && !Bluefruit. connPaired () ) return ;
96104
97105 // Get Time from iOS once per second
98- // Note it is advised to update this quickly
106+ // Note it is not advised to update this quickly
99107 // Application should use local clock and update time after
100- // a long period (e.g an hour or day)s
108+ // a long period (e.g an hour or day)
101109 bleCTime.getCurrentTime ();
110+ bleCTime.getLocalTimeInfo ();
102111
103112 printTime ();
104113
@@ -107,61 +116,68 @@ void loop()
107116
108117void connect_callback (uint16_t conn_handle)
109118{
110- Serial.println (" Connected" );
111-
112- Serial.print (" Discovering CTS ... " );
119+ oled.clearDisplay ();
120+ oled.setCursor (0 , 0 );
121+ oled.println (" Connected." );
122+ oled.print (" Discovering ... " );
123+ oled.display ();
124+
113125 if ( bleCTime.discover (conn_handle) )
114126 {
115- Serial.println (" Discovered" );
127+ oled.println (" OK" );
128+
129+ // ANCS requires pairing to work
130+ oled.print (" Paring ... " );
116131
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 ... " );
132+ oled. display ();
133+
119134 if ( Bluefruit.requestPairing () )
120135 {
121- Serial .println (" Done " );
122- Serial. println ( " Enabling Time Adjust Notify " );
136+ oled .println (" OK " );
137+
123138 bleCTime.enableAdjust ();
124139
125- Serial. print ( " Get Current Time chars value " );
140+ oled. println ( " Receiving Time... " );
126141 bleCTime.getCurrentTime ();
127-
128- Serial.print (" Get Local Time Info chars value" );
129142 bleCTime.getLocalTimeInfo ();
130-
131- Serial.println ();
132143 }
133-
134- Serial.println ();
135144 }
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" };
141145
142- Serial.println (" iOS Device time changed due to " );
143- Serial.println ( reason_str[reason] );
146+ oled.display ();
144147}
145148
146149void printTime (void )
147150{
148151 const char * day_of_week_str[] = { " Mon" , " Tue" , " Wed" , " Thu" , " Fri" , " Sat" , " Sun" };
152+ const char * month_str[] = { " na" , " Jan" , " Feb" , " Mar" , " Apr" , " May" , " Jun" , " Jul" , " Aug" , " Sep" , " Oct" , " Nov" , " Dec" };
149153
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 ]);
154+ oled.clearDisplay ();
155+ oled.setCursor (0 , 0 );
156+
157+ oled.print (day_of_week_str[bleCTime.Time .weekday ]);
158+ oled.printf (" %s %02d %04d\n " , month_str[bleCTime.Time .month ], bleCTime.Time .day , bleCTime.Time .year );
153159
160+
161+ oled.setTextSize (2 );
162+ oled.printf (" %02d:%02d:%02d\n " , bleCTime.Time .hour , bleCTime.Time .minute , bleCTime.Time .second );
163+
154164 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 ();
165+
166+ oled.setTextSize (1 );
167+ oled.printf (" UTC %+d:%02d, " , utc_offset/60 , utc_offset%60 );
168+ oled.printf (" DST %+.1f" , ((float ) bleCTime.LocalInfo .dst_offset *15 )/60 );
169+ oled.println ();
170+
171+ oled.display ();
158172}
159173
160174
161175void disconnect_callback (uint16_t conn_handle, uint8_t reason)
162176{
163177 (void ) reason;
164178
165- Serial.println ();
166- Serial.println (" Disconnected" );
179+ oled.clearDisplay ();
180+ oled.setCursor (0 , 0 );
181+ oled.println (" Waiting to connect" );
182+ oled.display ();
167183}
0 commit comments