forked from upstream-mirrors/OpenTracks
Clean up BluetoothConnectionManager.
This commit is contained in:
+127
-247
@@ -16,8 +16,8 @@
|
|||||||
|
|
||||||
package com.google.android.apps.mytracks.services.sensors;
|
package com.google.android.apps.mytracks.services.sensors;
|
||||||
|
|
||||||
import com.google.android.apps.mytracks.Constants;
|
|
||||||
import com.google.android.apps.mytracks.content.Sensor;
|
import com.google.android.apps.mytracks.content.Sensor;
|
||||||
|
import com.google.android.apps.mytracks.content.Sensor.SensorState;
|
||||||
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
|
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
|
||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
@@ -30,382 +30,262 @@ import android.util.Log;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class does all the work for setting up and managing Bluetooth
|
* Manages bluetooth connection. It has a thread for connecting with a bluetooth
|
||||||
* connections with other devices. It has a thread that listens for incoming
|
* device and a thread for performing data transmission when connected.
|
||||||
* connections, a thread for connecting with a device, and a thread for
|
*
|
||||||
* performing data transmissions when connected.
|
|
||||||
*
|
|
||||||
* @author Sandor Dornbush
|
* @author Sandor Dornbush
|
||||||
*/
|
*/
|
||||||
public class BluetoothConnectionManager {
|
public class BluetoothConnectionManager {
|
||||||
|
|
||||||
// Unique Bluetooth UUID for My Tracks
|
// My Tracks UUID
|
||||||
public static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
|
public static final UUID MY_TRACKS_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
|
||||||
|
|
||||||
private MessageParser parser;
|
// Message types sent to hander
|
||||||
|
public static final int MESSAGE_STATE_CHANGE = 1;
|
||||||
|
public static final int MESSAGE_DEVICE_NAME = 2;
|
||||||
|
public static final int MESSAGE_READ = 3;
|
||||||
|
|
||||||
|
// Key for storing the device name
|
||||||
|
public static final String KEY_DEVICE_NAME = "device_name";
|
||||||
|
|
||||||
|
private static final String TAG = BluetoothConnectionManager.class.getSimpleName();
|
||||||
|
|
||||||
// Member fields
|
|
||||||
private final BluetoothAdapter adapter;
|
|
||||||
private final Handler handler;
|
private final Handler handler;
|
||||||
|
private final MessageParser messageParser;
|
||||||
|
private final BluetoothAdapter bluetoothAdapter;
|
||||||
|
private SensorState sensorState;
|
||||||
|
|
||||||
private ConnectThread connectThread;
|
private ConnectThread connectThread;
|
||||||
private ConnectedThread connectedThread;
|
private ConnectedThread connectedThread;
|
||||||
|
|
||||||
private Sensor.SensorState state;
|
|
||||||
|
|
||||||
// Message types sent from the BluetoothSenorService Handler
|
|
||||||
public static final int MESSAGE_STATE_CHANGE = 1;
|
|
||||||
public static final int MESSAGE_READ = 2;
|
|
||||||
public static final int MESSAGE_WRITE = 3;
|
|
||||||
public static final int MESSAGE_DEVICE_NAME = 4;
|
|
||||||
|
|
||||||
// Key names received from the BluetoothSenorService Handler
|
|
||||||
public static final String DEVICE_NAME = "device_name";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor. Prepares a new BluetoothSensor session.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param handler A Handler to send messages back to the UI Activity
|
* @param handler a hander for sending messages back to the UI activity
|
||||||
* @param parser A message parser
|
* @param messageParser a message parser
|
||||||
*/
|
*/
|
||||||
public BluetoothConnectionManager(Handler handler, MessageParser parser) {
|
public BluetoothConnectionManager(Handler handler, MessageParser messageParser) {
|
||||||
this.adapter = BluetoothAdapter.getDefaultAdapter();
|
|
||||||
this.state = Sensor.SensorState.NONE;
|
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
this.parser = parser;
|
this.messageParser = messageParser;
|
||||||
|
this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
this.sensorState = SensorState.NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the current state of the sensor connection
|
* Gets the sensor state.
|
||||||
*
|
|
||||||
* @param state An integer defining the current connection state
|
|
||||||
*/
|
*/
|
||||||
private synchronized void setState(Sensor.SensorState state) {
|
public synchronized SensorState getSensorState() {
|
||||||
// TODO pretty print this.
|
return sensorState;
|
||||||
Log.d(Constants.TAG, "setState(" + state + ")");
|
|
||||||
this.state = state;
|
|
||||||
|
|
||||||
// Give the new state to the Handler so the UI Activity can update
|
|
||||||
handler.obtainMessage(MESSAGE_STATE_CHANGE, state.getNumber(), -1).sendToTarget();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the current connection state.
|
* Sets the sensor state.
|
||||||
|
*
|
||||||
|
* @param sensorState the sensor state
|
||||||
*/
|
*/
|
||||||
public synchronized Sensor.SensorState getState() {
|
private synchronized void setState(Sensor.SensorState sensorState) {
|
||||||
return state;
|
this.sensorState = sensorState;
|
||||||
|
|
||||||
|
// Send the sensor state to the handler
|
||||||
|
handler.obtainMessage(MESSAGE_STATE_CHANGE, sensorState.getNumber(), -1).sendToTarget();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the sensor service. Specifically start AcceptThread to begin a session
|
* Resets the bluetooth connection manager.
|
||||||
* in listening (server) mode. Called by the Activity onResume()
|
|
||||||
*/
|
*/
|
||||||
public synchronized void start() {
|
public synchronized void reset() {
|
||||||
Log.d(Constants.TAG, "BluetoothConnectionManager.start()");
|
cancelThreads();
|
||||||
|
|
||||||
// Cancel any thread attempting to make a connection
|
|
||||||
if (connectThread != null) {
|
|
||||||
connectThread.cancel();
|
|
||||||
connectThread = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cancel any thread currently running a connection
|
|
||||||
if (connectedThread != null) {
|
|
||||||
connectedThread.cancel();
|
|
||||||
connectedThread = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
setState(Sensor.SensorState.NONE);
|
setState(Sensor.SensorState.NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the ConnectThread to initiate a connection to a remote device.
|
* Cancels all the threads.
|
||||||
*
|
|
||||||
* @param device The BluetoothDevice to connect
|
|
||||||
*/
|
*/
|
||||||
public synchronized void connect(BluetoothDevice device) {
|
private void cancelThreads() {
|
||||||
Log.d(Constants.TAG, "connect to: " + device);
|
if (connectThread != null) {
|
||||||
|
connectThread.cancel();
|
||||||
// Cancel any thread attempting to make a connection
|
connectThread = null;
|
||||||
if (state == Sensor.SensorState.CONNECTING) {
|
|
||||||
if (connectThread != null) {
|
|
||||||
connectThread.cancel();
|
|
||||||
connectThread = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cancel any thread currently running a connection
|
|
||||||
if (connectedThread != null) {
|
if (connectedThread != null) {
|
||||||
connectedThread.cancel();
|
connectedThread.cancel();
|
||||||
connectedThread = null;
|
connectedThread = null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Start the thread to connect with the given device
|
/**
|
||||||
connectThread = new ConnectThread(device);
|
* Connects to a bluetooth device.
|
||||||
|
*
|
||||||
|
* @param bluetoothDevice the bluetooth device
|
||||||
|
*/
|
||||||
|
public synchronized void connect(BluetoothDevice bluetoothDevice) {
|
||||||
|
Log.d(TAG, "connect to: " + bluetoothDevice);
|
||||||
|
cancelThreads();
|
||||||
|
|
||||||
|
connectThread = new ConnectThread(bluetoothDevice);
|
||||||
connectThread.start();
|
connectThread.start();
|
||||||
setState(Sensor.SensorState.CONNECTING);
|
setState(Sensor.SensorState.CONNECTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the ConnectedThread to begin managing a Bluetooth connection
|
* Starts the ConnectedThread to read data.
|
||||||
*
|
*
|
||||||
* @param socket The BluetoothSocket on which the connection was made
|
* @param bluetoothSocket the bluetooth socket
|
||||||
* @param device The BluetoothDevice that has been connected
|
* @param bluetoothDevice the bluetooth device
|
||||||
*/
|
*/
|
||||||
public synchronized void connected(BluetoothSocket socket,
|
private synchronized void connected(
|
||||||
BluetoothDevice device) {
|
BluetoothSocket bluetoothSocket, BluetoothDevice bluetoothDevice) {
|
||||||
Log.d(Constants.TAG, "connected");
|
cancelThreads();
|
||||||
|
|
||||||
// Cancel the thread that completed the connection
|
connectedThread = new ConnectedThread(bluetoothSocket);
|
||||||
if (connectThread != null) {
|
|
||||||
connectThread.cancel();
|
|
||||||
connectThread = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cancel any thread currently running a connection
|
|
||||||
if (connectedThread != null) {
|
|
||||||
connectedThread.cancel();
|
|
||||||
connectedThread = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start the thread to manage the connection and perform transmissions
|
|
||||||
connectedThread = new ConnectedThread(socket);
|
|
||||||
connectedThread.start();
|
connectedThread.start();
|
||||||
|
|
||||||
// Send the name of the connected device back to the UI Activity
|
// Send the device name to the handler
|
||||||
Message msg = handler.obtainMessage(MESSAGE_DEVICE_NAME);
|
Message message = handler.obtainMessage(MESSAGE_DEVICE_NAME);
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
bundle.putString(DEVICE_NAME, device.getName());
|
bundle.putString(KEY_DEVICE_NAME, bluetoothDevice.getName());
|
||||||
msg.setData(bundle);
|
message.setData(bundle);
|
||||||
handler.sendMessage(msg);
|
handler.sendMessage(message);
|
||||||
|
|
||||||
setState(Sensor.SensorState.CONNECTED);
|
setState(Sensor.SensorState.CONNECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop all threads
|
* A thread to connect to a bluetooth device.
|
||||||
*/
|
|
||||||
public synchronized void stop() {
|
|
||||||
Log.d(Constants.TAG, "stop()");
|
|
||||||
if (connectThread != null) {
|
|
||||||
connectThread.cancel();
|
|
||||||
connectThread = null;
|
|
||||||
}
|
|
||||||
if (connectedThread != null) {
|
|
||||||
connectedThread.cancel();
|
|
||||||
connectedThread = null;
|
|
||||||
}
|
|
||||||
setState(Sensor.SensorState.NONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write to the ConnectedThread in an unsynchronized manner
|
|
||||||
*
|
|
||||||
* @param out The bytes to write
|
|
||||||
* @see ConnectedThread#write(byte[])
|
|
||||||
*/
|
|
||||||
public void write(byte[] out) {
|
|
||||||
// Create temporary object
|
|
||||||
ConnectedThread r;
|
|
||||||
// Synchronize a copy of the ConnectedThread
|
|
||||||
synchronized (this) {
|
|
||||||
if (state != Sensor.SensorState.CONNECTED) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
r = connectedThread;
|
|
||||||
}
|
|
||||||
// Perform the write unsynchronized
|
|
||||||
r.write(out);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indicate that the connection attempt failed and notify the UI Activity.
|
|
||||||
*/
|
|
||||||
private void connectionFailed() {
|
|
||||||
setState(Sensor.SensorState.DISCONNECTED);
|
|
||||||
Log.i(Constants.TAG, "Bluetooth connection failed.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indicate that the connection was lost and notify the UI Activity.
|
|
||||||
*/
|
|
||||||
private void connectionLost() {
|
|
||||||
setState(Sensor.SensorState.DISCONNECTED);
|
|
||||||
Log.i(Constants.TAG, "Bluetooth connection lost.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This thread runs while attempting to make an outgoing connection with a
|
|
||||||
* device. It runs straight through; the connection either succeeds or fails.
|
|
||||||
*/
|
*/
|
||||||
private class ConnectThread extends Thread {
|
private class ConnectThread extends Thread {
|
||||||
private final BluetoothSocket socket;
|
private final BluetoothSocket bluetoothSocket;
|
||||||
private final BluetoothDevice device;
|
private final BluetoothDevice bluetoothDevice;
|
||||||
|
|
||||||
public ConnectThread(BluetoothDevice device) {
|
public ConnectThread(BluetoothDevice device) {
|
||||||
setName("ConnectThread-" + device.getName());
|
setName("ConnectThread-" + device.getName());
|
||||||
this.device = device;
|
this.bluetoothDevice = device;
|
||||||
BluetoothSocket tmp = null;
|
BluetoothSocket tmp = null;
|
||||||
|
|
||||||
// Get a BluetoothSocket for a connection with the
|
|
||||||
// given BluetoothDevice
|
|
||||||
try {
|
try {
|
||||||
tmp = ApiAdapterFactory.getApiAdapter().getBluetoothSocket(device);
|
tmp = ApiAdapterFactory.getApiAdapter().getBluetoothSocket(device);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(Constants.TAG, "create() failed", e);
|
Log.e(TAG, "Unable to get blueooth socket.", e);
|
||||||
}
|
}
|
||||||
socket = tmp;
|
bluetoothSocket = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.d(Constants.TAG, "BEGIN mConnectThread");
|
// Cancel discovery to prevent slow down
|
||||||
|
bluetoothAdapter.cancelDiscovery();
|
||||||
|
|
||||||
// Always cancel discovery because it will slow down a connection
|
|
||||||
adapter.cancelDiscovery();
|
|
||||||
|
|
||||||
// Make a connection to the BluetoothSocket
|
|
||||||
try {
|
try {
|
||||||
// This is a blocking call and will only return on a
|
bluetoothSocket.connect();
|
||||||
// successful connection or an exception
|
} catch (IOException connectException) {
|
||||||
socket.connect();
|
Log.i(TAG, "Unable to connect.", connectException);
|
||||||
} catch (IOException e) {
|
setState(Sensor.SensorState.DISCONNECTED);
|
||||||
connectionFailed();
|
|
||||||
// Close the socket
|
|
||||||
try {
|
try {
|
||||||
socket.close();
|
bluetoothSocket.close();
|
||||||
} catch (IOException e2) {
|
} catch (IOException e) {
|
||||||
Log.e(Constants.TAG,
|
Log.e(TAG, "Unable to close blueooth socket.", e);
|
||||||
"unable to close() socket during connection failure", e2);
|
|
||||||
}
|
}
|
||||||
// Start the service over to restart listening mode
|
// Reset the bluetooth connection manager
|
||||||
BluetoothConnectionManager.this.start();
|
BluetoothConnectionManager.this.reset();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the ConnectThread because we're done
|
// Reset the ConnectThread since we are done
|
||||||
synchronized (BluetoothConnectionManager.this) {
|
synchronized (BluetoothConnectionManager.this) {
|
||||||
connectThread = null;
|
connectThread = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the connected thread
|
// Start the connected thread
|
||||||
connected(socket, device);
|
connected(bluetoothSocket, bluetoothDevice);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancels this thread.
|
||||||
|
*/
|
||||||
public void cancel() {
|
public void cancel() {
|
||||||
try {
|
try {
|
||||||
socket.close();
|
bluetoothSocket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(Constants.TAG, "close() of connect socket failed", e);
|
Log.e(TAG, "Unable to close bluetooth socket.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This thread runs during a connection with a remote device. It handles all
|
* This thread handles data transmission when connected.
|
||||||
* incoming and outgoing transmissions.
|
|
||||||
*/
|
*/
|
||||||
private class ConnectedThread extends Thread {
|
private class ConnectedThread extends Thread {
|
||||||
private final BluetoothSocket btSocket;
|
private final BluetoothSocket bluetoothSSocket;
|
||||||
private final InputStream mmInStream;
|
private final InputStream inputStream;
|
||||||
private final OutputStream mmOutStream;
|
|
||||||
|
|
||||||
public ConnectedThread(BluetoothSocket socket) {
|
public ConnectedThread(BluetoothSocket bluetoothSocket) {
|
||||||
Log.d(Constants.TAG, "create ConnectedThread");
|
this.bluetoothSSocket = bluetoothSocket;
|
||||||
btSocket = socket;
|
InputStream tmp = null;
|
||||||
InputStream tmpIn = null;
|
|
||||||
OutputStream tmpOut = null;
|
|
||||||
|
|
||||||
// Get the BluetoothSocket input and output streams
|
|
||||||
try {
|
try {
|
||||||
tmpIn = socket.getInputStream();
|
tmp = bluetoothSocket.getInputStream();
|
||||||
tmpOut = socket.getOutputStream();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(Constants.TAG, "temp sockets not created", e);
|
Log.e(TAG, "Unable to get input stream.", e);
|
||||||
}
|
}
|
||||||
|
inputStream = tmp;
|
||||||
mmInStream = tmpIn;
|
|
||||||
mmOutStream = tmpOut;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.i(Constants.TAG, "BEGIN mConnectedThread");
|
byte[] buffer = new byte[messageParser.getFrameSize()];
|
||||||
byte[] buffer = new byte[parser.getFrameSize()];
|
int bytes; // bytes read
|
||||||
int bytes;
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
|
||||||
// Keep listening to the InputStream while connected
|
// Keep listening to the inputStream while connected
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
// Read from the InputStream
|
// Read from the inputStream
|
||||||
bytes = mmInStream.read(buffer, offset, parser.getFrameSize() - offset);
|
bytes = inputStream.read(buffer, offset, messageParser.getFrameSize() - offset);
|
||||||
|
|
||||||
if (bytes < 0) {
|
if (bytes == -1) { throw new IOException("EOF reached."); }
|
||||||
throw new IOException("EOF reached");
|
|
||||||
}
|
|
||||||
|
|
||||||
offset += bytes;
|
offset += bytes;
|
||||||
|
|
||||||
if (offset != parser.getFrameSize()) {
|
if (offset != messageParser.getFrameSize()) {
|
||||||
// partial frame received, call read() again to receive the rest
|
// Partial frame received. Call read again to receive the rest.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if its a valid frame
|
if (!messageParser.isValid(buffer)) {
|
||||||
if (!parser.isValid(buffer)) {
|
int index = messageParser.findNextAlignment(buffer);
|
||||||
int index = parser.findNextAlignment(buffer);
|
if (index == -1) {
|
||||||
if (index > 0) {
|
Log.w(TAG, "Could not find any valid data. Drop data.");
|
||||||
// re-align
|
offset = 0;
|
||||||
offset = parser.getFrameSize() - index;
|
|
||||||
System.arraycopy(buffer, index, buffer, 0, offset);
|
|
||||||
Log.w(Constants.TAG, "Misaligned data, found new message at " +
|
|
||||||
index + " recovering...");
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Log.w(Constants.TAG, "Could not find valid data, dropping data");
|
Log.w(TAG, "Misaligned data. Found new message at " + index + ". Recovering...");
|
||||||
offset = 0;
|
offset = messageParser.getFrameSize() - index;
|
||||||
|
System.arraycopy(buffer, index, buffer, 0, offset);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
offset = 0;
|
offset = 0;
|
||||||
|
|
||||||
// Send copy of the obtained bytes to the UI Activity.
|
// Send a copy of the obtained bytes to the handler to avoid memory
|
||||||
// Avoids memory inconsistency issues.
|
// inconsistency issues
|
||||||
handler.obtainMessage(MESSAGE_READ, bytes, -1, buffer.clone())
|
handler.obtainMessage(MESSAGE_READ, bytes, -1, buffer.clone()).sendToTarget();
|
||||||
.sendToTarget();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(Constants.TAG, "disconnected", e);
|
Log.i(TAG, "Bluetooth connection lost.", e);
|
||||||
connectionLost();
|
setState(Sensor.SensorState.DISCONNECTED);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write to the connected OutStream.
|
* Cancels this thread.
|
||||||
*
|
|
||||||
* @param buffer The bytes to write
|
|
||||||
*/
|
*/
|
||||||
public void write(byte[] buffer) {
|
|
||||||
try {
|
|
||||||
mmOutStream.write(buffer);
|
|
||||||
|
|
||||||
// Share the sent message back to the UI Activity
|
|
||||||
handler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(Constants.TAG, "Exception during write", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void cancel() {
|
public void cancel() {
|
||||||
try {
|
try {
|
||||||
btSocket.close();
|
bluetoothSSocket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(Constants.TAG, "close() of connect socket failed", e);
|
Log.e(TAG, "Unable to close bluetooth socket.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-13
@@ -154,10 +154,10 @@ public class BluetoothSensorManager extends SensorManager {
|
|||||||
if (connectionManager != null) {
|
if (connectionManager != null) {
|
||||||
// Only if the state is STATE_NONE, do we know that we haven't started
|
// Only if the state is STATE_NONE, do we know that we haven't started
|
||||||
// already
|
// already
|
||||||
if (connectionManager.getState() == Sensor.SensorState.NONE) {
|
if (connectionManager.getSensorState() == Sensor.SensorState.NONE) {
|
||||||
// Start the Bluetooth sensor services
|
// Reset the connection manager
|
||||||
Log.w(Constants.TAG, "Disabled manager onStartTrack");
|
Log.w(Constants.TAG, "Disabled manager onStartTrack");
|
||||||
connectionManager.start();
|
connectionManager.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -166,7 +166,7 @@ public class BluetoothSensorManager extends SensorManager {
|
|||||||
protected void tearDownChannel() {
|
protected void tearDownChannel() {
|
||||||
// Stop the Bluetooth sensor services
|
// Stop the Bluetooth sensor services
|
||||||
if (connectionManager != null) {
|
if (connectionManager != null) {
|
||||||
connectionManager.stop();
|
connectionManager.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +177,7 @@ public class BluetoothSensorManager extends SensorManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SensorState getSensorState() {
|
public SensorState getSensorState() {
|
||||||
return connectionManager == null ? Sensor.SensorState.NONE : connectionManager.getState();
|
return connectionManager == null ? Sensor.SensorState.NONE : connectionManager.getSensorState();
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Handler that gets information back from the BluetoothSensorService
|
// The Handler that gets information back from the BluetoothSensorService
|
||||||
@@ -189,7 +189,12 @@ public class BluetoothSensorManager extends SensorManager {
|
|||||||
// TODO should we update the SensorManager state var?
|
// TODO should we update the SensorManager state var?
|
||||||
Log.i(Constants.TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
|
Log.i(Constants.TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
|
||||||
break;
|
break;
|
||||||
case BluetoothConnectionManager.MESSAGE_WRITE:
|
case BluetoothConnectionManager.MESSAGE_DEVICE_NAME:
|
||||||
|
// Save the connected device name
|
||||||
|
connectedDeviceName = msg.getData().getString(BluetoothConnectionManager.KEY_DEVICE_NAME);
|
||||||
|
Toast.makeText(context,
|
||||||
|
context.getString(R.string.settings_sensor_bluetooth_connected, connectedDeviceName),
|
||||||
|
Toast.LENGTH_SHORT).show();
|
||||||
break;
|
break;
|
||||||
case BluetoothConnectionManager.MESSAGE_READ:
|
case BluetoothConnectionManager.MESSAGE_READ:
|
||||||
byte[] readBuf = null;
|
byte[] readBuf = null;
|
||||||
@@ -207,13 +212,6 @@ public class BluetoothSensorManager extends SensorManager {
|
|||||||
Log.i(Constants.TAG, "Unexpected exception on read.", re);
|
Log.i(Constants.TAG, "Unexpected exception on read.", re);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BluetoothConnectionManager.MESSAGE_DEVICE_NAME:
|
|
||||||
// Save the connected device name
|
|
||||||
connectedDeviceName = msg.getData().getString(BluetoothConnectionManager.DEVICE_NAME);
|
|
||||||
Toast.makeText(context,
|
|
||||||
context.getString(R.string.settings_sensor_bluetooth_connected, connectedDeviceName),
|
|
||||||
Toast.LENGTH_SHORT).show();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -37,10 +37,10 @@ public class Api10Adapter extends Api9Adapter {
|
|||||||
public BluetoothSocket getBluetoothSocket(BluetoothDevice bluetoothDevice) throws IOException {
|
public BluetoothSocket getBluetoothSocket(BluetoothDevice bluetoothDevice) throws IOException {
|
||||||
try {
|
try {
|
||||||
return bluetoothDevice.createInsecureRfcommSocketToServiceRecord(
|
return bluetoothDevice.createInsecureRfcommSocketToServiceRecord(
|
||||||
BluetoothConnectionManager.SPP_UUID);
|
BluetoothConnectionManager.MY_TRACKS_UUID);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.d(Constants.TAG, "Unable to create insecure connection", e);
|
Log.d(Constants.TAG, "Unable to create insecure connection", e);
|
||||||
}
|
}
|
||||||
return bluetoothDevice.createRfcommSocketToServiceRecord(BluetoothConnectionManager.SPP_UUID);
|
return bluetoothDevice.createRfcommSocketToServiceRecord(BluetoothConnectionManager.MY_TRACKS_UUID);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ public class Api7Adapter implements ApiAdapter {
|
|||||||
} catch (InvocationTargetException e) {
|
} catch (InvocationTargetException e) {
|
||||||
Log.d(Constants.TAG, "Unable to create insecure connection", e);
|
Log.d(Constants.TAG, "Unable to create insecure connection", e);
|
||||||
}
|
}
|
||||||
return bluetoothDevice.createRfcommSocketToServiceRecord(BluetoothConnectionManager.SPP_UUID);
|
return bluetoothDevice.createRfcommSocketToServiceRecord(BluetoothConnectionManager.MY_TRACKS_UUID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user