diff options
author | Artur Dryomov <artur.dryomov@gmail.com> | 2013-08-01 19:32:19 +0300 |
---|---|---|
committer | Artur Dryomov <artur.dryomov@gmail.com> | 2013-08-01 19:47:07 +0300 |
commit | 9864a2d5d4dbc9988ada970dfe713559e7db9326 (patch) | |
tree | ed6b4d3b731216b8fab8a7d208e1d4c377ae873d /android | |
parent | 610a4a9498c78293566e3996a9672269a7f085e2 (diff) |
Fix Bluetooth devices discovery.
* Listen async Bluetooth status messages and start discovery properly.
* Remember Bluetooth status before running the app and manage the state.
Change-Id: I2be3fb6a503c5f6ace96732ebd0302935f1afb84
Diffstat (limited to 'android')
3 files changed, 64 insertions, 46 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java index 35be694d17f7..4f1029b0c526 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java @@ -24,10 +24,9 @@ import android.support.v4.content.LocalBroadcastManager; import org.libreoffice.impressremote.util.BluetoothOperator; import org.libreoffice.impressremote.util.Intents; -import org.libreoffice.impressremote.communication.Server.Protocol; class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, Runnable { - private static final int SEARCH_DELAY_IN_MILLISECONDS = 1000 * 5; + private static final int SEARCH_DELAY_IN_MILLISECONDS = 1000 * 10; private final Context mContext; @@ -49,55 +48,53 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, return; } - setUpSearchResultsReceiver(); + setUpBluetoothActionsReceiver(); + + if (!BluetoothOperator.getAdapter().isEnabled()) { + return; + } BluetoothOperator.getAdapter().startDiscovery(); } - private void setUpSearchResultsReceiver() { - IntentFilter aSearchResultsFilter = new IntentFilter(); - aSearchResultsFilter.addAction(BluetoothDevice.ACTION_FOUND); - aSearchResultsFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + private void setUpBluetoothActionsReceiver() { + IntentFilter aBluetoothActionsFilter = new IntentFilter(); + aBluetoothActionsFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); + aBluetoothActionsFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + aBluetoothActionsFilter.addAction(BluetoothDevice.ACTION_FOUND); - mContext.registerReceiver(this, aSearchResultsFilter); + mContext.registerReceiver(this, aBluetoothActionsFilter); } @Override public void onReceive(Context aContext, Intent aIntent) { - if (BluetoothDevice.ACTION_FOUND.equals(aIntent.getAction())) { - BluetoothDevice aBluetoothDevice = aIntent - .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - - addServer(buildServer(aBluetoothDevice)); - callUpdatingServersList(); - - return; + if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(aIntent.getAction())) { + switch (aIntent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) { + case BluetoothAdapter.STATE_ON: + BluetoothOperator.getAdapter().startDiscovery(); + return; + + default: + return; + } } if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(aIntent.getAction())) { startDiscoveryDelayed(); + return; } - } - - private void addServer(Server aServer) { - mServers.put(aServer.getAddress(), aServer); - } - private Server buildServer(BluetoothDevice aBluetoothDevice) { - String aServerAddress = aBluetoothDevice.getAddress(); - String aServerName = aBluetoothDevice.getName(); - - return new Server(Protocol.BLUETOOTH, aServerAddress, aServerName); - } + if (BluetoothDevice.ACTION_FOUND.equals(aIntent.getAction())) { + BluetoothDevice aBluetoothDevice = aIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - private void callUpdatingServersList() { - Intent aIntent = Intents.buildServersListChangedIntent(); - LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent); + addServer(buildServer(aBluetoothDevice)); + callUpdatingServersList(); + } } private void startDiscoveryDelayed() { // Start discovery again after a small delay. - // but check whether device is on in case the user manually + // Check whether device is on in case the user manually // disabled bluetooth if (!BluetoothOperator.getAdapter().isEnabled()) { @@ -113,18 +110,34 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, BluetoothOperator.getAdapter().startDiscovery(); } + private void addServer(Server aServer) { + mServers.put(aServer.getAddress(), aServer); + } + + private Server buildServer(BluetoothDevice aBluetoothDevice) { + String aServerAddress = aBluetoothDevice.getAddress(); + String aServerName = aBluetoothDevice.getName(); + + return Server.newBluetoothInstance(aServerAddress, aServerName); + } + + private void callUpdatingServersList() { + Intent aIntent = Intents.buildServersListChangedIntent(); + LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent); + } + @Override public void stopSearch() { if (!BluetoothOperator.isAvailable()) { return; } - tearDownSearchResultsReceiver(); + tearDownBluetoothActionsReceiver(); BluetoothOperator.getAdapter().cancelDiscovery(); } - private void tearDownSearchResultsReceiver() { + private void tearDownBluetoothActionsReceiver() { try { mContext.unregisterReceiver(this); } catch (IllegalArgumentException e) { diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index 8ded38f64fef..51ca64c90996 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -11,12 +11,12 @@ package org.libreoffice.impressremote.communication; import java.util.List; import android.app.Service; -import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.v4.content.LocalBroadcastManager; +import org.libreoffice.impressremote.util.BluetoothOperator; import org.libreoffice.impressremote.util.Intents; public class CommunicationService extends Service implements Runnable, MessagesListener, Timer.TimerListener { @@ -37,14 +37,14 @@ public class CommunicationService extends Service implements Runnable, MessagesL private IBinder mBinder; private ServersManager mServersManager; - private static boolean gWasBluetoothEnabled = false; - private static boolean gWasBluetoothEnabledFetched = false; private ServerConnection mServerConnection; private MessagesReceiver mMessagesReceiver; private CommandsTransmitter mCommandsTransmitter; + private boolean mBluetoothWasEnabled; + private SlideShow mSlideShow; private Thread mThread; @@ -60,6 +60,8 @@ public class CommunicationService extends Service implements Runnable, MessagesL mServersManager = new ServersManager(this); + mBluetoothWasEnabled = false; + mSlideShow = new SlideShow(new Timer(this)); mThread = new Thread(this); @@ -163,13 +165,11 @@ public class CommunicationService extends Service implements Runnable, MessagesL public void startSearch() { mState = State.SEARCHING; - if (BluetoothAdapter.getDefaultAdapter() != null) { - if (!gWasBluetoothEnabledFetched) { - gWasBluetoothEnabled = BluetoothAdapter.getDefaultAdapter().isEnabled(); - gWasBluetoothEnabledFetched = true; + if (BluetoothOperator.isAvailable()) { + mBluetoothWasEnabled = BluetoothOperator.getAdapter().isEnabled(); - if (!gWasBluetoothEnabled) - BluetoothAdapter.getDefaultAdapter().enable(); + if (!BluetoothOperator.getAdapter().isEnabled()) { + BluetoothOperator.getAdapter().enable(); } } @@ -179,9 +179,10 @@ public class CommunicationService extends Service implements Runnable, MessagesL public void stopSearch() { mServersManager.stopServersSearch(); - if (BluetoothAdapter.getDefaultAdapter() != null) { - if (!gWasBluetoothEnabled) - BluetoothAdapter.getDefaultAdapter().disable(); + if (BluetoothOperator.isAvailable()) { + if (!mBluetoothWasEnabled) { + BluetoothOperator.getAdapter().disable(); + } } } diff --git a/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java b/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java index 5a1167e2286b..a531d860b349 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java +++ b/android/sdremote/src/org/libreoffice/impressremote/util/BluetoothOperator.java @@ -15,10 +15,14 @@ public final class BluetoothOperator { } public static boolean isAvailable() { - return BluetoothAdapter.getDefaultAdapter() != null; + return getAdapter() != null; } public static BluetoothAdapter getAdapter() { + // TODO: should be acquired other way on Jelly Bean MR2 + // Look at the BluetoothAdapter’s docs for details. + // It will require to use the latest version of SDK to get needed constant. + return BluetoothAdapter.getDefaultAdapter(); } } |