@@ -104,7 +104,8 @@ public class SdlRouterService extends Service{
104
104
105
105
private static boolean connectAsClient = false ;
106
106
private static boolean closing = false ;
107
- private boolean isTarnsportConnected = false ;
107
+ private boolean isTransportConnected = false ;
108
+ private TransportType connectedTransportType = null ;
108
109
private static Context currentContext = null ;
109
110
110
111
private Handler versionCheckTimeOutHandler , altTransportTimerHandler ;
@@ -320,9 +321,10 @@ public void handleMessage(Message msg) {
320
321
pingClients ();
321
322
Message message = Message .obtain ();
322
323
message .what = TransportConstants .ROUTER_REGISTER_CLIENT_RESPONSE ;
324
+ message .arg1 = TransportConstants .REGISTRATION_RESPONSE_SUCESS ;
323
325
long appId = receivedBundle .getLong (TransportConstants .APP_ID_EXTRA , -1 );
324
326
if (appId <0 || msg .replyTo == null ){
325
- Log .w (TAG , "Unable to requster app as no id or messenger was included" );
327
+ Log .w (TAG , "Unable to register app as no id or messenger was included" );
326
328
if (msg .replyTo !=null ){
327
329
message .arg1 = TransportConstants .REGISTRATION_RESPONSE_DENIED_APP_ID_NOT_INCLUDED ;
328
330
try {
@@ -356,11 +358,14 @@ public void handleMessage(Message msg) {
356
358
}
357
359
}
358
360
onAppRegistered (app );
359
-
361
+
360
362
returnBundle = new Bundle ();
361
-
362
- if (MultiplexBluetoothTransport .currentlyConnectedDevice !=null ){
363
- returnBundle .putString (CONNECTED_DEVICE_STRING_EXTRA_NAME , MultiplexBluetoothTransport .currentlyConnectedDevice );
363
+ //Add params if connected
364
+ if (SdlRouterService .this .isTransportConnected ){
365
+ returnBundle .putString (TransportConstants .HARDWARE_CONNECTED , SdlRouterService .this .connectedTransportType .name ());
366
+ if (MultiplexBluetoothTransport .currentlyConnectedDevice !=null ){
367
+ returnBundle .putString (CONNECTED_DEVICE_STRING_EXTRA_NAME , MultiplexBluetoothTransport .currentlyConnectedDevice );
368
+ }
364
369
}
365
370
if (!returnBundle .isEmpty ()){
366
371
message .setData (returnBundle );
@@ -371,7 +376,6 @@ public void handleMessage(Message msg) {
371
376
registeredApps .remove (appId );
372
377
}
373
378
}
374
-
375
379
break ;
376
380
case TransportConstants .ROUTER_UNREGISTER_CLIENT :
377
381
long appIdToUnregister = receivedBundle .getLong (TransportConstants .APP_ID_EXTRA , -1 );
@@ -520,7 +524,6 @@ public void handleMessage(Message msg) {
520
524
&& altTransportService .equals (msg .replyTo )){
521
525
//The same transport that was connected to the router service is now telling us it's disconnected. Let's inform clients and clear our saved messenger
522
526
altTransportService = null ;
523
- storeConnectedStatus (false );
524
527
onTransportDisconnected (TransportType .valueOf (receivedBundle .getString (TransportConstants .HARDWARE_DISCONNECTED )));
525
528
shouldServiceRemainOpen (null ); //this will close the service if bluetooth is not available
526
529
}
@@ -540,7 +543,6 @@ public void handleMessage(Message msg) {
540
543
altTransportTimerHandler = null ;
541
544
altTransportTimerRunnable = null ;
542
545
543
- storeConnectedStatus (true );
544
546
//Let the alt transport know they are good to go
545
547
retMsg .arg1 = TransportConstants .ROUTER_REGISTER_ALT_TRANSPORT_RESPONSE_SUCESS ;
546
548
onTransportConnected (TransportType .valueOf (receivedBundle .getString (TransportConstants .HARDWARE_CONNECTED )));
@@ -576,6 +578,39 @@ public void handleMessage(Message msg) {
576
578
577
579
}
578
580
};
581
+
582
+ /**
583
+ * Target we publish for alternative transport (USB) clients to send messages to RouterHandler.
584
+ */
585
+ final Messenger routerStatusMessenger = new Messenger (new RouterStatusHandler ());
586
+
587
+ /**
588
+ * Handler of incoming messages from an alternative transport (USB).
589
+ */
590
+ class RouterStatusHandler extends Handler {
591
+ ClassLoader loader = getClass ().getClassLoader ();
592
+ @ Override
593
+ public void handleMessage (Message msg ) {
594
+ switch (msg .what ){
595
+ case TransportConstants .ROUTER_STATUS_CONNECTED_STATE_REQUEST :
596
+ if (msg .replyTo ==null ){
597
+ break ;
598
+ }
599
+ Message message = Message .obtain ();
600
+ message .what = TransportConstants .ROUTER_STATUS_CONNECTED_STATE_RESPONSE ;
601
+ message .arg1 = (isTransportConnected == true ) ? 1 : 0 ;
602
+ try {
603
+ msg .replyTo .send (message );
604
+ } catch (RemoteException e ) {
605
+ e .printStackTrace ();
606
+ }
607
+ break ;
608
+ default :
609
+ Log .w (TAG , "Unsopported request: " + msg .what );
610
+ break ;
611
+ }
612
+ }
613
+ };
579
614
580
615
/* **************************************************************************************************************************************
581
616
*********************************************** Life Cycle **************************************************************
@@ -594,6 +629,8 @@ public IBinder onBind(Intent intent) {
594
629
return this .altTransportMessenger .getBinder ();
595
630
}else if (TransportConstants .BIND_REQUEST_TYPE_CLIENT .equals (requestType )){
596
631
return this .routerMessenger .getBinder ();
632
+ }else if (TransportConstants .BIND_REQUEST_TYPE_STATUS .equals (requestType )){
633
+ return this .routerStatusMessenger .getBinder ();
597
634
}else {
598
635
Log .w (TAG , "Uknown bind request type" );
599
636
}
@@ -691,7 +728,6 @@ public void onCreate() {
691
728
}
692
729
closing = false ;
693
730
currentContext = getBaseContext ();
694
- storeConnectedStatus (false );
695
731
696
732
startVersionCheck ();
697
733
Log .i (TAG , "SDL Router Service has been created" );
@@ -947,7 +983,6 @@ public boolean shouldServiceRemainOpen(Intent intent){
947
983
*/
948
984
public void closeSelf (){
949
985
closing = true ;
950
- storeConnectedStatus (false );
951
986
if (getBaseContext ()!=null ){
952
987
stopSelf ();
953
988
}else {
@@ -980,7 +1015,7 @@ private synchronized void initBluetoothSerialService(){
980
1015
}
981
1016
982
1017
public void onTransportConnected (final TransportType type ){
983
- isTarnsportConnected = true ;
1018
+ isTransportConnected = true ;
984
1019
enterForeground ();
985
1020
if (packetWriteTaskMaster !=null ){
986
1021
packetWriteTaskMaster .close ();
@@ -989,6 +1024,8 @@ public void onTransportConnected(final TransportType type){
989
1024
packetWriteTaskMaster = new PacketWriteTaskMaster ();
990
1025
packetWriteTaskMaster .start ();
991
1026
1027
+ connectedTransportType = type ;
1028
+
992
1029
Intent startService = new Intent ();
993
1030
startService .setAction (START_SERVICE_ACTION );
994
1031
startService .putExtra (TransportConstants .START_ROUTER_SERVICE_SDL_ENABLED_EXTRA , true );
@@ -997,15 +1034,32 @@ public void onTransportConnected(final TransportType type){
997
1034
startService .putExtra (TransportConstants .START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME , new ComponentName (this , this .getClass ()));
998
1035
sendBroadcast (startService );
999
1036
//HARDWARE_CONNECTED
1037
+ if (!(registeredApps == null || registeredApps .isEmpty ())){
1038
+ //If we have clients
1039
+ notifyClients (createHardwareConnectedMessage (type ));
1040
+ }
1041
+ }
1042
+
1043
+ private Message createHardwareConnectedMessage (final TransportType type ){
1044
+ Message message = Message .obtain ();
1045
+ message .what = TransportConstants .HARDWARE_CONNECTION_EVENT ;
1046
+ Bundle bundle = new Bundle ();
1047
+ bundle .putString (TransportConstants .HARDWARE_CONNECTED , type .name ());
1048
+ if (MultiplexBluetoothTransport .currentlyConnectedDevice !=null ){
1049
+ bundle .putString (CONNECTED_DEVICE_STRING_EXTRA_NAME , MultiplexBluetoothTransport .currentlyConnectedDevice );
1050
+ }
1051
+ message .setData (bundle );
1052
+ return message ;
1053
+
1000
1054
}
1001
1055
1002
1056
public void onTransportDisconnected (TransportType type ){
1003
1057
if (altTransportService !=null ){ //If we still have an alt transport open, then we don't need to tell the clients to close
1004
1058
return ;
1005
1059
}
1006
1060
Log .e (TAG , "Notifying client service of hardware disconnect." );
1007
-
1008
- isTarnsportConnected = false ;
1061
+ connectedTransportType = null ;
1062
+ isTransportConnected = false ;
1009
1063
stopClientPings ();
1010
1064
1011
1065
exitForeground ();//Leave our foreground state as we don't have a connection anymore
@@ -1076,18 +1130,15 @@ public void handleMessage(Message msg) {
1076
1130
case MESSAGE_STATE_CHANGE :
1077
1131
switch (msg .arg1 ) {
1078
1132
case MultiplexBluetoothTransport .STATE_CONNECTED :
1079
- storeConnectedStatus (true );
1080
1133
onTransportConnected (TransportType .BLUETOOTH );
1081
1134
break ;
1082
1135
case MultiplexBluetoothTransport .STATE_CONNECTING :
1083
1136
// Currently attempting to connect - update UI?
1084
1137
break ;
1085
1138
case MultiplexBluetoothTransport .STATE_LISTEN :
1086
- storeConnectedStatus (false );
1087
1139
break ;
1088
1140
case MultiplexBluetoothTransport .STATE_NONE :
1089
1141
// We've just lost the connection
1090
- storeConnectedStatus (false );
1091
1142
if (!connectAsClient ){
1092
1143
if (!legacyModeEnabled && !closing ){
1093
1144
initBluetoothSerialService ();
@@ -1425,17 +1476,6 @@ private synchronized boolean bluetoothConnect(BluetoothDevice device){
1425
1476
//**************************************************************************************************************************************
1426
1477
//********************************************************* PREFERENCES ****************************************************************
1427
1478
//**************************************************************************************************************************************
1428
-
1429
- @ SuppressLint ("WorldReadableFiles" )
1430
- @ SuppressWarnings ("deprecation" )
1431
- private void storeConnectedStatus (boolean isConnected ){
1432
- SharedPreferences prefs = getApplicationContext ().getSharedPreferences (getApplicationContext ().getPackageName ()+SdlBroadcastReceiver .TRANSPORT_GLOBAL_PREFS ,
1433
- Context .MODE_WORLD_READABLE );
1434
- SharedPreferences .Editor editor = prefs .edit ();
1435
- editor .putBoolean (SdlBroadcastReceiver .IS_TRANSPORT_CONNECTED , isConnected );
1436
- editor .commit ();
1437
- }
1438
-
1439
1479
/**
1440
1480
* This method will set the last known bluetooth connection method that worked with this phone.
1441
1481
* This helps speed up the process of connecting
@@ -1777,7 +1817,7 @@ protected PacketWriteTask getNextTask(){
1777
1817
1778
1818
private void startClientPings (){
1779
1819
synchronized (this ){
1780
- if (!isTarnsportConnected ){ //If we aren't connected, bail
1820
+ if (!isTransportConnected ){ //If we aren't connected, bail
1781
1821
return ;
1782
1822
}
1783
1823
if (isPingingClients ){
0 commit comments