Skip to content

Commit d3f526c

Browse files
Merge pull request #1685 from smartdevicelink/feature/allow_device_listen_during_connection
Allow SdlDeviceListener to start after BT connection
2 parents d8c09da + 820c8a1 commit d3f526c

3 files changed

Lines changed: 31 additions & 14 deletions

File tree

android/sdl_android/src/main/java/com/smartdevicelink/transport/MultiplexBluetoothTransport.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public class MultiplexBluetoothTransport extends MultiplexBaseTransport {
7777
Handler timeOutHandler;
7878
Runnable socketRunnable;
7979
boolean keepSocketAlive = true;
80-
80+
BluetoothDevice connectedDevice;
81+
8182
/**
8283
* Constructor. Prepares a new BluetoothChat session.
8384
*
@@ -112,6 +113,14 @@ public void setKeepSocketAlive(boolean keepSocketAlive) {
112113
this.keepSocketAlive = keepSocketAlive;
113114
}
114115

116+
/**
117+
* A method to retrieve the currently connected bluetooth device
118+
* @return the connected bluetooth device if connected, null otherwise.
119+
*/
120+
public BluetoothDevice getConnectedDevice(){
121+
return connectedDevice;
122+
}
123+
115124
/**
116125
* Start the chat service. Specifically start AcceptThread to begin a
117126
* session in listening (server) mode. Called by the Activity onResume()
@@ -225,6 +234,7 @@ public synchronized void connected(BluetoothSocket socket, BluetoothDevice devic
225234

226235
//Store a static name of the device that is connected.
227236
if (device != null) {
237+
connectedDevice = device;
228238
connectedDeviceName = device.getName();
229239
connectedDeviceAddress = device.getAddress();
230240
if (connectedDeviceAddress != null) {

android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlBroadcastReceiver.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,16 @@ public void onComplete(Vector<ComponentName> routerServices) {
275275
DebugTool.logInfo(TAG, ": This app's package: " + myPackage);
276276
DebugTool.logInfo(TAG, ": Router service app's package: " + routerServicePackage);
277277
if (myPackage != null && myPackage.equalsIgnoreCase(routerServicePackage)) {
278-
SdlDeviceListener sdlDeviceListener = getSdlDeviceListener(context, device);
279-
if (!sdlDeviceListener.isRunning()) {
280-
sdlDeviceListener.start();
278+
//If the device is not null the listener should start as well as the
279+
//case where this app was installed after BT connected and is the
280+
//only SDL app installed on the device. (Rare corner case)
281+
if(device != null || sdlAppInfoList.size() == 1) {
282+
SdlDeviceListener sdlDeviceListener = getSdlDeviceListener(context, device);
283+
if (!sdlDeviceListener.isRunning()) {
284+
sdlDeviceListener.start();
285+
}
286+
} else {
287+
DebugTool.logInfo(TAG, "Not starting device listener, bluetooth device is null and other SDL apps installed.");
281288
}
282289
} else {
283290
DebugTool.logInfo(TAG, ": Not the app to start the router service nor device listener");

android/sdl_android/src/main/java/com/smartdevicelink/transport/utl/SdlDeviceListener.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class SdlDeviceListener {
6161

6262
private final WeakReference<Context> contextWeakReference;
6363
private final Callback callback;
64-
private final BluetoothDevice connectedDevice;
64+
private BluetoothDevice connectedDevice;
6565
private MultiplexBluetoothTransport bluetoothTransport;
6666
private TransportHandler bluetoothHandler;
6767
private Handler timeoutHandler;
@@ -87,24 +87,21 @@ public SdlDeviceListener(Context context, BluetoothDevice device, Callback callb
8787
public void start() {
8888
if (connectedDevice == null) {
8989
DebugTool.logInfo(TAG, ": No supplied bluetooth device");
90-
if (callback != null) {
91-
callback.onTransportError(null);
92-
}
93-
return;
94-
}
95-
96-
if (hasSDLConnected(contextWeakReference.get(), connectedDevice.getAddress())) {
90+
} else if (hasSDLConnected(contextWeakReference.get(), connectedDevice.getAddress())) {
9791
DebugTool.logInfo(TAG, ": Confirmed SDL device, should start router service");
9892
//This device has connected to SDL previously, it is ok to start the RS right now
9993
callback.onTransportConnected(contextWeakReference.get(), connectedDevice);
10094
return;
10195
}
96+
10297
synchronized (RUNNING_LOCK) {
10398
isRunning = true;
10499
// set timeout = if first time seeing BT device, 30s, if not 15s
105-
int timeout = isFirstStatusCheck(connectedDevice.getAddress()) ? 30000 : 15000;
100+
int timeout = connectedDevice != null && isFirstStatusCheck(connectedDevice.getAddress()) ? 30000 : 15000;
106101
//Set our preference as false for this device for now
107-
setSDLConnectedStatus(contextWeakReference.get(), connectedDevice.getAddress(), false);
102+
if(connectedDevice != null) {
103+
setSDLConnectedStatus(contextWeakReference.get(), connectedDevice.getAddress(), false);
104+
}
108105
bluetoothHandler = new TransportHandler(this);
109106
bluetoothTransport = new MultiplexBluetoothTransport(bluetoothHandler);
110107
bluetoothTransport.start();
@@ -155,6 +152,9 @@ public void handleMessage(@NonNull Message msg) {
155152
case SdlRouterService.MESSAGE_STATE_CHANGE:
156153
switch (msg.arg1) {
157154
case MultiplexBaseTransport.STATE_CONNECTED:
155+
if(sdlListener.connectedDevice == null) {
156+
sdlListener.connectedDevice = sdlListener.bluetoothTransport.getConnectedDevice();
157+
}
158158
sdlListener.setSDLConnectedStatus(sdlListener.contextWeakReference.get(), sdlListener.connectedDevice.getAddress(), true);
159159
boolean keepConnectionOpen = sdlListener.callback.onTransportConnected(sdlListener.contextWeakReference.get(), sdlListener.connectedDevice);
160160
if (!keepConnectionOpen) {

0 commit comments

Comments
 (0)