Kaydet (Commit) 9382fa6c authored tarafından Andrzej J.R. Hunt's avatar Andrzej J.R. Hunt

Automatically enable/disable bluetooth as necessary for searching/connection.

Change-Id: Ie7a11c05cf1ba6181e955a65ebef03117c956f1a
üst 6a1c29d7
...@@ -25,14 +25,22 @@ import android.support.v4.content.LocalBroadcastManager; ...@@ -25,14 +25,22 @@ import android.support.v4.content.LocalBroadcastManager;
*/ */
public class BluetoothClient extends Client { public class BluetoothClient extends Client {
private boolean mBluetoothWasEnabled;
private BluetoothAdapter mAdapter;
public BluetoothClient(Server aServer, public BluetoothClient(Server aServer,
CommunicationService aCommunicationService) { CommunicationService aCommunicationService) {
super(aServer, aCommunicationService); super(aServer, aCommunicationService);
try { try {
BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter(); mAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice aDevice = aAdapter.getRemoteDevice(aServer mBluetoothWasEnabled = mAdapter.isEnabled();
if (!mBluetoothWasEnabled) {
mAdapter.enable();
}
BluetoothDevice aDevice = mAdapter.getRemoteDevice(aServer
.getAddress()); .getAddress());
aAdapter.cancelDiscovery(); mAdapter.cancelDiscovery();
BluetoothSocket aSocket = aDevice BluetoothSocket aSocket = aDevice
.createRfcommSocketToServiceRecord(UUID .createRfcommSocketToServiceRecord(UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB")); .fromString("00001101-0000-1000-8000-00805F9B34FB"));
...@@ -115,5 +123,11 @@ public class BluetoothClient extends Client { ...@@ -115,5 +123,11 @@ public class BluetoothClient extends Client {
// } // }
} }
protected void onDisconnect() {
if (!mBluetoothWasEnabled) {
mAdapter.disable();
}
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file
...@@ -31,12 +31,10 @@ public class BluetoothFinder { ...@@ -31,12 +31,10 @@ public class BluetoothFinder {
if (mAdapter == null) { if (mAdapter == null) {
return; // No bluetooth adapter found (emulator, special devices) return; // No bluetooth adapter found (emulator, special devices)
} }
System.out.println("BT:Discovery starting");
IntentFilter aFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); IntentFilter aFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
aFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); aFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
aFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
mContext.registerReceiver(mReceiver, aFilter); mContext.registerReceiver(mReceiver, aFilter);
mAdapter.enable();
mAdapter.startDiscovery(); mAdapter.startDiscovery();
} }
...@@ -62,31 +60,35 @@ public class BluetoothFinder { ...@@ -62,31 +60,35 @@ public class BluetoothFinder {
@Override @Override
public void onReceive(Context context, Intent aIntent) { public void onReceive(Context context, Intent aIntent) {
// TODO Auto-generated method stub
if (aIntent.getAction().equals(BluetoothDevice.ACTION_FOUND)) { if (aIntent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
System.out.println("Found");
BluetoothDevice aDevice = (BluetoothDevice) aIntent.getExtras() BluetoothDevice aDevice = (BluetoothDevice) aIntent.getExtras()
.get(BluetoothDevice.EXTRA_DEVICE); .get(BluetoothDevice.EXTRA_DEVICE);
Server aServer = new Server(Protocol.BLUETOOTH, Server aServer = new Server(Protocol.BLUETOOTH,
aDevice.getAddress(), aDevice.getName(), aDevice.getAddress(), aDevice.getName(),
System.currentTimeMillis()); System.currentTimeMillis());
mServerList.put(aServer.getAddress(), aServer); mServerList.put(aServer.getAddress(), aServer);
System.out.println("Added " + aServer.getName());
System.out.println("Now we have: " + mServerList.size());
Intent aNIntent = new Intent( Intent aNIntent = new Intent(
CommunicationService.MSG_SERVERLIST_CHANGED); CommunicationService.MSG_SERVERLIST_CHANGED);
LocalBroadcastManager.getInstance(mContext).sendBroadcast( LocalBroadcastManager.getInstance(mContext).sendBroadcast(
aNIntent); aNIntent);
} else if (aIntent.getAction().equals( } 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. // Start discovery again after a small delay.
Handler aHandler = new Handler(); // but check whether device is on incase the user manually
aHandler.postDelayed(new Runnable() { // disabled bluetooth
@Override if (mAdapter.isEnabled()) {
public void run() { Handler aHandler = new Handler();
mAdapter.startDiscovery(); aHandler.postDelayed(new Runnable() {
} @Override
}, 1000 * 15); public void run() {
; System.out.println("Looping");
}
}, 1000 * 15);
}
} }
} }
......
...@@ -95,6 +95,7 @@ public abstract class Client { ...@@ -95,6 +95,7 @@ public abstract class Client {
e1.printStackTrace(); e1.printStackTrace();
} finally { } finally {
latestInstance = null; latestInstance = null;
onDisconnect();
} }
} }
...@@ -108,10 +109,7 @@ public abstract class Client { ...@@ -108,10 +109,7 @@ public abstract class Client {
} }
/** /**
* Send a valid JSON string to the server. * Send a valid command to the Server.
*
* @param command
* Must be a valid JSON string.
*/ */
public void sendCommand(String command) { public void sendCommand(String command) {
try { try {
...@@ -125,5 +123,12 @@ public abstract class Client { ...@@ -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: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file
...@@ -102,14 +102,29 @@ public class CommunicationService extends Service implements Runnable { ...@@ -102,14 +102,29 @@ public class CommunicationService extends Service implements Runnable {
} }
private boolean mBluetoothPreviouslyEnabled;
public void startSearching() { public void startSearching() {
mNetworkFinder.startFinding(); mNetworkFinder.startFinding();
mBluetoothFinder.startFinding(); BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter();
if (aAdapter != null) {
mBluetoothPreviouslyEnabled = aAdapter.isEnabled();
if (!mBluetoothPreviouslyEnabled)
aAdapter.enable();
mBluetoothFinder.startFinding();
}
} }
public void stopSearching() { public void stopSearching() {
mNetworkFinder.stopFinding(); mNetworkFinder.stopFinding();
mBluetoothFinder.stopFinding(); mBluetoothFinder.stopFinding();
BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter();
if (aAdapter != null) {
if (!mBluetoothPreviouslyEnabled) {
aAdapter.disable();
}
}
} }
public void connectTo(Server aServer) { public void connectTo(Server aServer) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment