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;
*/
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
......@@ -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);
}
}
}
......
......@@ -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
......@@ -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) {
......
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