android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java | 20 +++++- android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java | 32 +++++----- android/sdremote/src/org/libreoffice/impressremote/communication/Client.java | 13 ++-- android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java | 17 +++++ 4 files changed, 59 insertions(+), 23 deletions(-)
New commits: commit 9382fa6ce2fedad19742688d32d0981335c7dd21 Author: Andrzej J.R. Hunt <andr...@ahunt.org> Date: Thu Sep 6 11:35:30 2012 +0200 Automatically enable/disable bluetooth as necessary for searching/connection. Change-Id: Ie7a11c05cf1ba6181e955a65ebef03117c956f1a diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothClient.java index 4e9ff2a..dc1d6d0 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 dedce23..e55f264 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 964b62c..549d9cc 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 1d07a29..8a8290d 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) { _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits