diff options
author | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-09-06 11:35:30 +0200 |
---|---|---|
committer | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-09-06 11:37:07 +0200 |
commit | 9382fa6ce2fedad19742688d32d0981335c7dd21 (patch) | |
tree | 80ece387b6572bbd6140b859eaafdc6959fdc297 /android | |
parent | 6a1c29d7efd505346d4b43ee2b7080e8d769301e (diff) |
Automatically enable/disable bluetooth as necessary for searching/connection.
Change-Id: Ie7a11c05cf1ba6181e955a65ebef03117c956f1a
Diffstat (limited to 'android')
4 files changed, 59 insertions, 23 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java index 4e9ff2a0f435..dc1d6d09b80d 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java @@ -25,14 +25,22 @@ import android.support.v4.content.LocalBroadcastManager; */ public class BluetoothClient extends Client { + private boolean mBluetoothWasEnabled; + private BluetoothAdapter mAdapter; + public BluetoothClient(Server aServer, CommunicationService aCommunicationService) { super(aServer, aCommunicationService); try { - BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter(); - BluetoothDevice aDevice = aAdapter.getRemoteDevice(aServer + mAdapter = BluetoothAdapter.getDefaultAdapter(); + mBluetoothWasEnabled = mAdapter.isEnabled(); + if (!mBluetoothWasEnabled) { + mAdapter.enable(); + } + + BluetoothDevice aDevice = mAdapter.getRemoteDevice(aServer .getAddress()); - aAdapter.cancelDiscovery(); + mAdapter.cancelDiscovery(); BluetoothSocket aSocket = aDevice .createRfcommSocketToServiceRecord(UUID .fromString("00001101-0000-1000-8000-00805F9B34FB")); @@ -115,5 +123,11 @@ public class BluetoothClient extends Client { // } } + protected void onDisconnect() { + if (!mBluetoothWasEnabled) { + mAdapter.disable(); + } + } + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java index dedce232d4db..e55f2643cd47 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java @@ -31,12 +31,10 @@ public class BluetoothFinder { if (mAdapter == null) { return; // No bluetooth adapter found (emulator, special devices) } - System.out.println("BT:Discovery starting"); IntentFilter aFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); aFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + aFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); mContext.registerReceiver(mReceiver, aFilter); - - mAdapter.enable(); mAdapter.startDiscovery(); } @@ -62,31 +60,35 @@ public class BluetoothFinder { @Override public void onReceive(Context context, Intent aIntent) { - // TODO Auto-generated method stub if (aIntent.getAction().equals(BluetoothDevice.ACTION_FOUND)) { + System.out.println("Found"); BluetoothDevice aDevice = (BluetoothDevice) aIntent.getExtras() .get(BluetoothDevice.EXTRA_DEVICE); Server aServer = new Server(Protocol.BLUETOOTH, aDevice.getAddress(), aDevice.getName(), System.currentTimeMillis()); mServerList.put(aServer.getAddress(), aServer); - System.out.println("Added " + aServer.getName()); - System.out.println("Now we have: " + mServerList.size()); Intent aNIntent = new Intent( CommunicationService.MSG_SERVERLIST_CHANGED); LocalBroadcastManager.getInstance(mContext).sendBroadcast( aNIntent); } else if (aIntent.getAction().equals( - BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { + BluetoothAdapter.ACTION_DISCOVERY_FINISHED) + || aIntent.getAction() + .equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { // Start discovery again after a small delay. - Handler aHandler = new Handler(); - aHandler.postDelayed(new Runnable() { - @Override - public void run() { - mAdapter.startDiscovery(); - } - }, 1000 * 15); - ; + // but check whether device is on incase the user manually + // disabled bluetooth + if (mAdapter.isEnabled()) { + Handler aHandler = new Handler(); + aHandler.postDelayed(new Runnable() { + @Override + public void run() { + System.out.println("Looping"); + + } + }, 1000 * 15); + } } } diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java index 964b62ce409d..549d9cc2463c 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Client.java @@ -95,6 +95,7 @@ public abstract class Client { e1.printStackTrace(); } finally { latestInstance = null; + onDisconnect(); } } @@ -108,10 +109,7 @@ public abstract class Client { } /** - * Send a valid JSON string to the server. - * - * @param command - * Must be a valid JSON string. + * Send a valid command to the Server. */ public void sendCommand(String command) { try { @@ -125,5 +123,12 @@ public abstract class Client { } } + /** + * Called after the Client disconnects. Can be extended to allow for + * cleaning up bluetooth properties etc. + */ + protected void onDisconnect() { + } + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index 1d07a29f621a..8a8290daae18 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -102,14 +102,29 @@ public class CommunicationService extends Service implements Runnable { } + private boolean mBluetoothPreviouslyEnabled; + public void startSearching() { mNetworkFinder.startFinding(); - mBluetoothFinder.startFinding(); + BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter(); + if (aAdapter != null) { + mBluetoothPreviouslyEnabled = aAdapter.isEnabled(); + if (!mBluetoothPreviouslyEnabled) + aAdapter.enable(); + mBluetoothFinder.startFinding(); + } } public void stopSearching() { mNetworkFinder.stopFinding(); mBluetoothFinder.stopFinding(); + BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter(); + if (aAdapter != null) { + if (!mBluetoothPreviouslyEnabled) { + + aAdapter.disable(); + } + } } public void connectTo(Server aServer) { |