3737import android .content .pm .PackageManager ;
3838import android .os .Bundle ;
3939import android .os .ParcelUuid ;
40+ import android .text .format .DateFormat ;
4041import android .util .Log ;
42+ import android .view .WindowManager ;
43+ import android .widget .TextView ;
4144
4245import java .util .Arrays ;
46+ import java .util .Date ;
4347import java .util .HashSet ;
4448import java .util .Set ;
4549
4650public class GattServerActivity extends Activity {
4751 private static final String TAG = GattServerActivity .class .getSimpleName ();
4852
53+ /* Local UI */
54+ private TextView mLocalTimeView ;
4955 /* Bluetooth API */
56+ private BluetoothManager mBluetoothManager ;
5057 private BluetoothGattServer mBluetoothGattServer ;
5158 private BluetoothLeAdvertiser mBluetoothLeAdvertiser ;
5259 /* Collection of notification subscribers */
@@ -55,10 +62,15 @@ public class GattServerActivity extends Activity {
5562 @ Override
5663 protected void onCreate (Bundle savedInstanceState ) {
5764 super .onCreate (savedInstanceState );
65+ setContentView (R .layout .activity_server );
5866
59- BluetoothManager manager =
60- (BluetoothManager ) getSystemService (BLUETOOTH_SERVICE );
61- BluetoothAdapter bluetoothAdapter = manager .getAdapter ();
67+ mLocalTimeView = (TextView ) findViewById (R .id .text_time );
68+
69+ // Devices with a display should not go to sleep
70+ getWindow ().addFlags (WindowManager .LayoutParams .FLAG_KEEP_SCREEN_ON );
71+
72+ mBluetoothManager = (BluetoothManager ) getSystemService (BLUETOOTH_SERVICE );
73+ BluetoothAdapter bluetoothAdapter = mBluetoothManager .getAdapter ();
6274 // We can't continue without proper Bluetooth support
6375 if (!checkBluetoothSupport (bluetoothAdapter )) {
6476 finish ();
@@ -72,8 +84,8 @@ protected void onCreate(Bundle savedInstanceState) {
7284 bluetoothAdapter .enable ();
7385 } else {
7486 Log .d (TAG , "Bluetooth enabled...starting services" );
75- startAdvertising (manager );
76- startServer (manager );
87+ startAdvertising ();
88+ startServer ();
7789 }
7890 }
7991
@@ -98,9 +110,7 @@ protected void onStop() {
98110 protected void onDestroy () {
99111 super .onDestroy ();
100112
101- BluetoothManager manager =
102- (BluetoothManager ) getSystemService (BLUETOOTH_SERVICE );
103- BluetoothAdapter bluetoothAdapter = manager .getAdapter ();
113+ BluetoothAdapter bluetoothAdapter = mBluetoothManager .getAdapter ();
104114 if (bluetoothAdapter .isEnabled ()) {
105115 stopServer ();
106116 stopAdvertising ();
@@ -136,7 +146,7 @@ private boolean checkBluetoothSupport(BluetoothAdapter bluetoothAdapter) {
136146 private BroadcastReceiver mTimeReceiver = new BroadcastReceiver () {
137147 @ Override
138148 public void onReceive (Context context , Intent intent ) {
139- int adjustReason ;
149+ byte adjustReason ;
140150 switch (intent .getAction ()) {
141151 case Intent .ACTION_TIME_CHANGED :
142152 adjustReason = TimeProfile .ADJUST_MANUAL ;
@@ -149,7 +159,9 @@ public void onReceive(Context context, Intent intent) {
149159 adjustReason = TimeProfile .ADJUST_NONE ;
150160 break ;
151161 }
152- notifyRegisteredDevices (adjustReason );
162+ long now = System .currentTimeMillis ();
163+ notifyRegisteredDevices (now , adjustReason );
164+ updateLocalUi (now );
153165 }
154166 };
155167
@@ -162,12 +174,10 @@ public void onReceive(Context context, Intent intent) {
162174 public void onReceive (Context context , Intent intent ) {
163175 int state = intent .getIntExtra (BluetoothAdapter .EXTRA_STATE , BluetoothAdapter .STATE_OFF );
164176
165- BluetoothManager manager =
166- (BluetoothManager ) getSystemService (BLUETOOTH_SERVICE );
167177 switch (state ) {
168178 case BluetoothAdapter .STATE_ON :
169- startAdvertising (manager );
170- startServer (manager );
179+ startAdvertising ();
180+ startServer ();
171181 break ;
172182 case BluetoothAdapter .STATE_OFF :
173183 stopServer ();
@@ -184,8 +194,8 @@ public void onReceive(Context context, Intent intent) {
184194 * Begin advertising over Bluetooth that this device is connectable
185195 * and supports the Current Time Service.
186196 */
187- private void startAdvertising (BluetoothManager bluetoothManager ) {
188- BluetoothAdapter bluetoothAdapter = bluetoothManager .getAdapter ();
197+ private void startAdvertising () {
198+ BluetoothAdapter bluetoothAdapter = mBluetoothManager .getAdapter ();
189199 mBluetoothLeAdvertiser = bluetoothAdapter .getBluetoothLeAdvertiser ();
190200 if (mBluetoothLeAdvertiser == null ) {
191201 Log .w (TAG , "Failed to create advertiser" );
@@ -222,14 +232,17 @@ private void stopAdvertising() {
222232 * Initialize the GATT server instance with the services/characteristics
223233 * from the Time Profile.
224234 */
225- private void startServer (BluetoothManager bluetoothManager ) {
226- mBluetoothGattServer = bluetoothManager .openGattServer (this , mGattServerCallback );
235+ private void startServer () {
236+ mBluetoothGattServer = mBluetoothManager .openGattServer (this , mGattServerCallback );
227237 if (mBluetoothGattServer == null ) {
228238 Log .w (TAG , "Unable to create GATT server" );
229239 return ;
230240 }
231241
232242 mBluetoothGattServer .addService (TimeProfile .createTimeService ());
243+
244+ // Initialize the local UI
245+ updateLocalUi (System .currentTimeMillis ());
233246 }
234247
235248 /**
@@ -260,12 +273,12 @@ public void onStartFailure(int errorCode) {
260273 * Send a time service notification to any devices that are subscribed
261274 * to the characteristic.
262275 */
263- public void notifyRegisteredDevices (int adjustReason ) {
276+ private void notifyRegisteredDevices (long timestamp , byte adjustReason ) {
264277 if (mRegisteredDevices .isEmpty ()) {
265278 Log .i (TAG , "No subscribers registered" );
266279 return ;
267280 }
268- byte [] exactTime = TimeProfile .getExactTime (System . currentTimeMillis () , adjustReason );
281+ byte [] exactTime = TimeProfile .getExactTime (timestamp , adjustReason );
269282
270283 Log .i (TAG , "Sending update to " + mRegisteredDevices .size () + " subscribers" );
271284 for (BluetoothDevice device : mRegisteredDevices ) {
@@ -277,6 +290,17 @@ public void notifyRegisteredDevices(int adjustReason) {
277290 }
278291 }
279292
293+ /**
294+ * Update graphical UI on devices that support it with the current time.
295+ */
296+ private void updateLocalUi (long timestamp ) {
297+ Date date = new Date (timestamp );
298+ String displayDate = DateFormat .getMediumDateFormat (this ).format (date )
299+ + "\n "
300+ + DateFormat .getTimeFormat (this ).format (date );
301+ mLocalTimeView .setText (displayDate );
302+ }
303+
280304 /**
281305 * Callback to handle incoming requests to the GATT server.
282306 * All read/write requests for characteristics and descriptors are handled here.
@@ -289,6 +313,8 @@ public void onConnectionStateChange(BluetoothDevice device, int status, int newS
289313 Log .i (TAG , "BluetoothDevice CONNECTED: " + device );
290314 } else if (newState == BluetoothProfile .STATE_DISCONNECTED ) {
291315 Log .i (TAG , "BluetoothDevice DISCONNECTED: " + device );
316+ //Remove device from any active subscriptions
317+ mRegisteredDevices .remove (device );
292318 }
293319 }
294320
0 commit comments