import static MyTracksConstants.TAG

This commit is contained in:
Matthew Simmons
2011-02-28 16:59:52 -05:00
parent 0bbeff0b5b
commit b8f40e8314
8 changed files with 102 additions and 111 deletions
@@ -16,16 +16,16 @@
package com.google.android.apps.mytracks.services.sensors; package com.google.android.apps.mytracks.services.sensors;
import static com.google.android.apps.mytracks.MyTracksConstants.TAG; import static com.google.android.apps.mytracks.MyTracksConstants.TAG;
import com.dsi.ant.AntDefine;
import com.dsi.ant.AntMesg;
import com.dsi.ant.exception.AntInterfaceException;
import com.google.android.apps.mytracks.MyTracksSettings; import com.google.android.apps.mytracks.MyTracksSettings;
import com.google.android.apps.mytracks.content.Sensor; import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.apps.mytracks.services.sensors.ant.AntChannelIdMessage; import com.google.android.apps.mytracks.services.sensors.ant.AntChannelIdMessage;
import com.google.android.apps.mytracks.services.sensors.ant.AntChannelResponseMessage; import com.google.android.apps.mytracks.services.sensors.ant.AntChannelResponseMessage;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import com.dsi.ant.AntDefine;
import com.dsi.ant.AntMesg;
import com.dsi.ant.exception.AntInterfaceException;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.util.Log; import android.util.Log;
@@ -15,14 +15,14 @@
*/ */
package com.google.android.apps.mytracks.services.sensors; package com.google.android.apps.mytracks.services.sensors;
import com.google.android.apps.mytracks.MyTracksConstants; import static com.google.android.apps.mytracks.MyTracksConstants.TAG;
import com.google.android.apps.mytracks.MyTracksSettings;
import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.maps.mytracks.R;
import com.dsi.ant.AntDefine; import com.dsi.ant.AntDefine;
import com.dsi.ant.AntMesg; import com.dsi.ant.AntMesg;
import com.dsi.ant.exception.AntInterfaceException; import com.dsi.ant.exception.AntInterfaceException;
import com.google.android.apps.mytracks.MyTracksSettings;
import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.maps.mytracks.R;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@@ -82,10 +82,10 @@ public class AntSRMSensorManager extends AntSensorManager {
@Override @Override
public void handleMessage(byte[] antMessage) { public void handleMessage(byte[] antMessage) {
// Parse channel number // Parse channel number
byte recievedChannel = (byte) (antMessage[AntMesg.MESG_DATA_OFFSET] byte receivedChannel = (byte) (antMessage[AntMesg.MESG_DATA_OFFSET]
& AntDefine.CHANNEL_NUMBER_MASK); & AntDefine.CHANNEL_NUMBER_MASK);
if (recievedChannel != channel) { if (receivedChannel != channel) {
Log.d(MyTracksConstants.TAG, "Unexpected channel: " + recievedChannel); Log.d(TAG, "Unexpected channel: " + receivedChannel);
return; return;
} }
switch (antMessage[AntMesg.MESG_ID_OFFSET]) { switch (antMessage[AntMesg.MESG_ID_OFFSET]) {
@@ -99,18 +99,17 @@ public class AntSRMSensorManager extends AntSensorManager {
handleChannelId(antMessage); handleChannelId(antMessage);
break; break;
default: default:
Log.e(MyTracksConstants.TAG, Log.e(TAG, "Unexpected message id: " + antMessage[3]);
"Unexpected message id: " + antMessage[3]);
} }
} }
private void handleBroadcastData(byte[] antMessage) { private void handleBroadcastData(byte[] antMessage) {
if (deviceId == WILDCARD) { if (deviceId == WILDCARD) {
try { try {
Log.d(MyTracksConstants.TAG, "Requesting channel id id."); Log.d(TAG, "Requesting channel id id.");
getAntReceiver().ANTRequestMessage(channel, AntMesg.MESG_CHANNEL_ID_ID); getAntReceiver().ANTRequestMessage(channel, AntMesg.MESG_CHANNEL_ID_ID);
} catch (AntInterfaceException e) { } catch (AntInterfaceException e) {
Log.e(MyTracksConstants.TAG, "Failed to request channel id id", e); Log.e(TAG, "Failed to request channel id id", e);
} }
} }
setSensorState(Sensor.SensorState.CONNECTED); setSensorState(Sensor.SensorState.CONNECTED);
@@ -122,15 +121,14 @@ public class AntSRMSensorManager extends AntSensorManager {
parseSensorData(antMessage); parseSensorData(antMessage);
break; break;
default: default:
Log.e(MyTracksConstants.TAG, Log.e(TAG, "Unexpected message type: " + antMessage[MESSAGE_TYPE_INDEX]);
"Unexpected message type: " + antMessage[MESSAGE_TYPE_INDEX]);
} }
} }
private void handleChannelId(byte[] antMessage) { private void handleChannelId(byte[] antMessage) {
// Store the device id. // Store the device id.
deviceId = antMessage[3]; deviceId = antMessage[3];
Log.i(MyTracksConstants.TAG, "Found device id: " + deviceId); Log.i(TAG, "Found device id: " + deviceId);
SharedPreferences prefs = context.getSharedPreferences( SharedPreferences prefs = context.getSharedPreferences(
MyTracksSettings.SETTINGS_NAME, Context.MODE_PRIVATE); MyTracksSettings.SETTINGS_NAME, Context.MODE_PRIVATE);
@@ -143,38 +141,34 @@ public class AntSRMSensorManager extends AntSensorManager {
if (antMessage[3] == AntMesg.MESG_EVENT_ID if (antMessage[3] == AntMesg.MESG_EVENT_ID
&& antMessage[4] == AntDefine.EVENT_RX_SEARCH_TIMEOUT) { && antMessage[4] == AntDefine.EVENT_RX_SEARCH_TIMEOUT) {
// Search timeout // Search timeout
Log.w(MyTracksConstants.TAG, "Search timed out. Unassigning channel."); Log.w(TAG, "Search timed out. Unassigning channel.");
try { try {
getAntReceiver().ANTUnassignChannel(channel); getAntReceiver().ANTUnassignChannel(channel);
} catch (AntInterfaceException e) { } catch (AntInterfaceException e) {
Log.e(MyTracksConstants.TAG, "Failed to unassign ANT channel", e); Log.e(TAG, "Failed to unassign ANT channel", e);
} }
setSensorState(Sensor.SensorState.DISCONNECTED); setSensorState(Sensor.SensorState.DISCONNECTED);
} else if (antMessage[3] == AntMesg.MESG_UNASSIGN_CHANNEL_ID) { } else if (antMessage[3] == AntMesg.MESG_UNASSIGN_CHANNEL_ID) {
setSensorState(Sensor.SensorState.DISCONNECTED); setSensorState(Sensor.SensorState.DISCONNECTED);
Log.i(MyTracksConstants.TAG, Log.i(TAG, "Disconnected from the sensor: " + getSensorState());
"Disconnected from the sensor: " + getSensorState());
} }
} }
private void parseSensorData(byte[] antMessage) { private void parseSensorData(byte[] antMessage) {
if (antMessage.length != 11) { if (antMessage.length != 11) {
Log.e(MyTracksConstants.TAG, Log.e(TAG, "Unexpected ant message length: " + antMessage.length);
"Unexpected ant message length: " + antMessage.length);
return; return;
} }
int newMessageId = antMessage[MESSAGE_ID_INDEX] & 0xFF; int newMessageId = antMessage[MESSAGE_ID_INDEX] & 0xFF;
if (lastMessageId == newMessageId) { if (lastMessageId == newMessageId) {
// Repeated message. // Repeated message.
Log.i(MyTracksConstants.TAG, Log.i(TAG, String.format("SRM ignoring repeat: 0x%X", newMessageId));
String.format("SRM ignoring repeat: 0x%X", newMessageId));
return; return;
} }
if (newMessageId < lastMessageId) { if (newMessageId < lastMessageId) {
if (!(newMessageId < 20 && lastMessageId > 200)) { if (!(newMessageId < 20 && lastMessageId > 200)) {
Log.i(MyTracksConstants.TAG, Log.i(TAG, String.format("SRM ignoring repeat: 0x%X", newMessageId));
String.format("SRM ignoring repeat: 0x%X", newMessageId));
return; return;
} // else assume the byte overflowed to 0. } // else assume the byte overflowed to 0.
} }
@@ -15,16 +15,16 @@
*/ */
package com.google.android.apps.mytracks.services.sensors; package com.google.android.apps.mytracks.services.sensors;
import com.google.android.apps.mytracks.MyTracksConstants; import static com.google.android.apps.mytracks.MyTracksConstants.TAG;
import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.apps.mytracks.content.Sensor.SensorDataSet;
import com.google.android.apps.mytracks.services.sensors.ant.AntStartupMessage;
import com.dsi.ant.AntDefine; import com.dsi.ant.AntDefine;
import com.dsi.ant.AntInterface; import com.dsi.ant.AntInterface;
import com.dsi.ant.AntInterfaceIntent; import com.dsi.ant.AntInterfaceIntent;
import com.dsi.ant.AntMesg; import com.dsi.ant.AntMesg;
import com.dsi.ant.exception.AntInterfaceException; import com.dsi.ant.exception.AntInterfaceException;
import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.apps.mytracks.content.Sensor.SensorDataSet;
import com.google.android.apps.mytracks.services.sensors.ant.AntStartupMessage;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
@@ -84,7 +84,7 @@ public abstract class AntSensorManager extends SensorManager {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String antAction = intent.getAction(); String antAction = intent.getAction();
Log.i(MyTracksConstants.TAG, "enter status onReceive" + antAction); Log.i(TAG, "enter status onReceive" + antAction);
} }
}; };
@@ -93,12 +93,12 @@ public abstract class AntSensorManager extends SensorManager {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String antAction = intent.getAction(); String antAction = intent.getAction();
Log.i(MyTracksConstants.TAG, "enter data onReceive" + antAction); Log.i(TAG, "enter data onReceive" + antAction);
if (antAction.equals(AntInterfaceIntent.ANT_RX_MESSAGE_ACTION)) { if (antAction.equals(AntInterfaceIntent.ANT_RX_MESSAGE_ACTION)) {
byte[] antMessage = intent.getByteArrayExtra(AntInterfaceIntent.ANT_MESSAGE); byte[] antMessage = intent.getByteArrayExtra(AntInterfaceIntent.ANT_MESSAGE);
if (DEBUGGING) { if (DEBUGGING) {
Log.d(MyTracksConstants.TAG, "Received RX message " + messageToString(antMessage)); Log.d(TAG, "Received RX message " + messageToString(antMessage));
} }
handleMessage(antMessage); handleMessage(antMessage);
@@ -119,7 +119,7 @@ public abstract class AntSensorManager extends SensorManager {
@Override @Override
public void onServiceDisconnected() { public void onServiceDisconnected() {
Log.d(MyTracksConstants.TAG, "ANT interface reports disconnection"); Log.d(TAG, "ANT interface reports disconnection");
} }
}; };
@@ -129,14 +129,14 @@ public abstract class AntSensorManager extends SensorManager {
@Override @Override
public void onDestroy() { public void onDestroy() {
Log.i(MyTracksConstants.TAG, "destroying AntSensorManager"); Log.i(TAG, "destroying AntSensorManager");
context.unregisterReceiver(statusReceiver); context.unregisterReceiver(statusReceiver);
context.unregisterReceiver(dataReceiver); context.unregisterReceiver(dataReceiver);
try { try {
antReceiver.releaseInterface(); antReceiver.releaseInterface();
} catch (AntInterfaceException e) { } catch (AntInterfaceException e) {
Log.e(MyTracksConstants.TAG, "failed to release ANT interface", e); Log.e(TAG, "failed to release ANT interface", e);
} }
antReceiver.destroy(); antReceiver.destroy();
@@ -177,7 +177,7 @@ public abstract class AntSensorManager extends SensorManager {
antReceiver = AntInterface.getInstance(context, antServiceListener); antReceiver = AntInterface.getInstance(context, antServiceListener);
if (antReceiver == null) { if (antReceiver == null) {
Log.e(MyTracksConstants.TAG, "Failed to get ANT Receiver"); Log.e(TAG, "Failed to get ANT Receiver");
return; return;
} }
@@ -190,11 +190,11 @@ public abstract class AntSensorManager extends SensorManager {
* to initiate any ANT commands. * to initiate any ANT commands.
*/ */
private synchronized void serviceConnected() { private synchronized void serviceConnected() {
Log.d(MyTracksConstants.TAG, "ANT service connected"); Log.d(TAG, "ANT service connected");
try { try {
if (!antReceiver.claimInterface()) { if (!antReceiver.claimInterface()) {
Log.e(MyTracksConstants.TAG, "failed to claim ANT interface"); Log.e(TAG, "failed to claim ANT interface");
return; return;
} }
@@ -202,14 +202,14 @@ public abstract class AntSensorManager extends SensorManager {
// Make sure not to call AntInterface.enable() again, if it has been // Make sure not to call AntInterface.enable() again, if it has been
// already called before // already called before
if (antInterrupted == false) { if (antInterrupted == false) {
Log.i(MyTracksConstants.TAG, "Powering on Radio"); Log.i(TAG, "Powering on Radio");
antReceiver.enable(); antReceiver.enable();
} }
} else { } else {
Log.i(MyTracksConstants.TAG, "Radio already enabled"); Log.i(TAG, "Radio already enabled");
} }
} catch (AntInterfaceException e) { } catch (AntInterfaceException e) {
Log.e(MyTracksConstants.TAG, "failed to enable ANT", e); Log.e(TAG, "failed to enable ANT", e);
} }
try { try {
@@ -220,7 +220,7 @@ public abstract class AntSensorManager extends SensorManager {
// that message. // that message.
antReceiver.ANTResetSystem(); antReceiver.ANTResetSystem();
} catch (AntInterfaceException e) { } catch (AntInterfaceException e) {
Log.e(MyTracksConstants.TAG, "failed to reset ANT (expected exception)", e); Log.e(TAG, "failed to reset ANT (expected exception)", e);
} }
} }
@@ -232,7 +232,7 @@ public abstract class AntSensorManager extends SensorManager {
protected void handleMessage(byte[] antMessage) { protected void handleMessage(byte[] antMessage) {
int len = antMessage[0]; int len = antMessage[0];
if (len != antMessage.length - 2 || antMessage.length <= 2) { if (len != antMessage.length - 2 || antMessage.length <= 2) {
Log.e(MyTracksConstants.TAG, "Invalid message: " + messageToString(antMessage)); Log.e(TAG, "Invalid message: " + messageToString(antMessage));
return; return;
} }
@@ -253,8 +253,7 @@ public abstract class AntSensorManager extends SensorManager {
*/ */
protected boolean handleMessage(byte messageId, byte[] messageData) { protected boolean handleMessage(byte messageId, byte[] messageData) {
if (messageId == AntMesg.MESG_STARTUP_MESG_ID) { if (messageId == AntMesg.MESG_STARTUP_MESG_ID) {
Log.d(MyTracksConstants.TAG, String.format( Log.d(TAG, String.format("Received startup message (reason %02x); initializing channel",
"Received startup message (reason %02x); initializing channel",
new AntStartupMessage(messageData).getMessage())); new AntStartupMessage(messageData).getMessage()));
setupAntSensorChannels(); setupAntSensorChannels();
return true; return true;
@@ -305,13 +304,13 @@ public abstract class AntSensorManager extends SensorManager {
return true; return true;
} catch (AntInterfaceException e) { } catch (AntInterfaceException e) {
Log.e(MyTracksConstants.TAG, "failed to setup ANT channel", e); Log.e(TAG, "failed to setup ANT channel", e);
return false; return false;
} }
} }
private void registerForAntIntents() { private void registerForAntIntents() {
Log.i(MyTracksConstants.TAG, "Registering for ant intents."); Log.i(TAG, "Registering for ant intents.");
// Register for ANT intent broadcasts. // Register for ANT intent broadcasts.
IntentFilter statusIntentFilter = new IntentFilter(); IntentFilter statusIntentFilter = new IntentFilter();
statusIntentFilter.addAction(AntInterfaceIntent.ANT_ENABLED_ACTION); statusIntentFilter.addAction(AntInterfaceIntent.ANT_ENABLED_ACTION);
@@ -16,7 +16,8 @@
package com.google.android.apps.mytracks.services.sensors; package com.google.android.apps.mytracks.services.sensors;
import com.google.android.apps.mytracks.MyTracksConstants; import static com.google.android.apps.mytracks.MyTracksConstants.TAG;
import com.google.android.apps.mytracks.content.Sensor; import com.google.android.apps.mytracks.content.Sensor;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@@ -85,7 +86,7 @@ public class BluetoothConnectionManager {
*/ */
private synchronized void setState(Sensor.SensorState state) { private synchronized void setState(Sensor.SensorState state) {
// TODO pretty print this. // TODO pretty print this.
Log.d(MyTracksConstants.TAG, "setState(" + state + ")"); Log.d(TAG, "setState(" + state + ")");
this.state = state; this.state = state;
// Give the new state to the Handler so the UI Activity can update // Give the new state to the Handler so the UI Activity can update
@@ -104,7 +105,7 @@ public class BluetoothConnectionManager {
* in listening (server) mode. Called by the Activity onResume() * in listening (server) mode. Called by the Activity onResume()
*/ */
public synchronized void start() { public synchronized void start() {
Log.d(MyTracksConstants.TAG, "BluetoothConnectionManager.start()"); Log.d(TAG, "BluetoothConnectionManager.start()");
// Cancel any thread attempting to make a connection // Cancel any thread attempting to make a connection
if (connectThread != null) { if (connectThread != null) {
@@ -127,7 +128,7 @@ public class BluetoothConnectionManager {
* @param device The BluetoothDevice to connect * @param device The BluetoothDevice to connect
*/ */
public synchronized void connect(BluetoothDevice device) { public synchronized void connect(BluetoothDevice device) {
Log.d(MyTracksConstants.TAG, "connect to: " + device); Log.d(TAG, "connect to: " + device);
// Cancel any thread attempting to make a connection // Cancel any thread attempting to make a connection
if (state == Sensor.SensorState.CONNECTING) { if (state == Sensor.SensorState.CONNECTING) {
@@ -157,7 +158,7 @@ public class BluetoothConnectionManager {
*/ */
public synchronized void connected(BluetoothSocket socket, public synchronized void connected(BluetoothSocket socket,
BluetoothDevice device) { BluetoothDevice device) {
Log.d(MyTracksConstants.TAG, "connected"); Log.d(TAG, "connected");
// Cancel the thread that completed the connection // Cancel the thread that completed the connection
if (connectThread != null) { if (connectThread != null) {
@@ -189,7 +190,7 @@ public class BluetoothConnectionManager {
* Stop all threads * Stop all threads
*/ */
public synchronized void stop() { public synchronized void stop() {
Log.d(MyTracksConstants.TAG, "stop()"); Log.d(TAG, "stop()");
if (connectThread != null) { if (connectThread != null) {
connectThread.cancel(); connectThread.cancel();
connectThread = null; connectThread = null;
@@ -226,7 +227,7 @@ public class BluetoothConnectionManager {
*/ */
private void connectionFailed() { private void connectionFailed() {
setState(Sensor.SensorState.DISCONNECTED); setState(Sensor.SensorState.DISCONNECTED);
Log.i(MyTracksConstants.TAG, "Bluetooth connection failed."); Log.i(TAG, "Bluetooth connection failed.");
} }
/** /**
@@ -234,7 +235,7 @@ public class BluetoothConnectionManager {
*/ */
private void connectionLost() { private void connectionLost() {
setState(Sensor.SensorState.DISCONNECTED); setState(Sensor.SensorState.DISCONNECTED);
Log.i(MyTracksConstants.TAG, "Bluetooth connection lost."); Log.i(TAG, "Bluetooth connection lost.");
} }
/** /**
@@ -255,7 +256,7 @@ public class BluetoothConnectionManager {
try { try {
tmp = getSocket(); tmp = getSocket();
} catch (IOException e) { } catch (IOException e) {
Log.e(MyTracksConstants.TAG, "create() failed", e); Log.e(TAG, "create() failed", e);
} }
socket = tmp; socket = tmp;
} }
@@ -268,22 +269,22 @@ public class BluetoothConnectionManager {
insecure.setAccessible(true); insecure.setAccessible(true);
return (BluetoothSocket) insecure.invoke(device, 1); return (BluetoothSocket) insecure.invoke(device, 1);
} catch (SecurityException e) { } catch (SecurityException e) {
Log.e(MyTracksConstants.TAG, "Unable to get insecure connect.", e); Log.e(TAG, "Unable to get insecure connect.", e);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
Log.e(MyTracksConstants.TAG, "Unable to get insecure connect.", e); Log.e(TAG, "Unable to get insecure connect.", e);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
Log.e(MyTracksConstants.TAG, "Unable to get insecure connect.", e); Log.e(TAG, "Unable to get insecure connect.", e);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
Log.e(MyTracksConstants.TAG, "Unable to get insecure connect.", e); Log.e(TAG, "Unable to get insecure connect.", e);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
Log.e(MyTracksConstants.TAG, "Unable to get insecure connect.", e); Log.e(TAG, "Unable to get insecure connect.", e);
} }
return device.createRfcommSocketToServiceRecord(SPP_UUID); return device.createRfcommSocketToServiceRecord(SPP_UUID);
} }
@Override @Override
public void run() { public void run() {
Log.d(MyTracksConstants.TAG, "BEGIN mConnectThread"); Log.d(TAG, "BEGIN mConnectThread");
// Always cancel discovery because it will slow down a connection // Always cancel discovery because it will slow down a connection
adapter.cancelDiscovery(); adapter.cancelDiscovery();
@@ -299,8 +300,7 @@ public class BluetoothConnectionManager {
try { try {
socket.close(); socket.close();
} catch (IOException e2) { } catch (IOException e2) {
Log.e(MyTracksConstants.TAG, Log.e(TAG, "unable to close() socket during connection failure", e2);
"unable to close() socket during connection failure", e2);
} }
// Start the service over to restart listening mode // Start the service over to restart listening mode
BluetoothConnectionManager.this.start(); BluetoothConnectionManager.this.start();
@@ -320,7 +320,7 @@ public class BluetoothConnectionManager {
try { try {
socket.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
Log.e(MyTracksConstants.TAG, "close() of connect socket failed", e); Log.e(TAG, "close() of connect socket failed", e);
} }
} }
} }
@@ -335,7 +335,7 @@ public class BluetoothConnectionManager {
private final OutputStream mmOutStream; private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) { public ConnectedThread(BluetoothSocket socket) {
Log.d(MyTracksConstants.TAG, "create ConnectedThread"); Log.d(TAG, "create ConnectedThread");
btSocket = socket; btSocket = socket;
InputStream tmpIn = null; InputStream tmpIn = null;
OutputStream tmpOut = null; OutputStream tmpOut = null;
@@ -345,7 +345,7 @@ public class BluetoothConnectionManager {
tmpIn = socket.getInputStream(); tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream(); tmpOut = socket.getOutputStream();
} catch (IOException e) { } catch (IOException e) {
Log.e(MyTracksConstants.TAG, "temp sockets not created", e); Log.e(TAG, "temp sockets not created", e);
} }
mmInStream = tmpIn; mmInStream = tmpIn;
@@ -354,7 +354,7 @@ public class BluetoothConnectionManager {
@Override @Override
public void run() { public void run() {
Log.i(MyTracksConstants.TAG, "BEGIN mConnectedThread"); Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[parser.getFrameSize()]; byte[] buffer = new byte[parser.getFrameSize()];
int bytes; int bytes;
int offset = 0; int offset = 0;
@@ -383,11 +383,10 @@ public class BluetoothConnectionManager {
// re-align // re-align
offset = parser.getFrameSize() - index; offset = parser.getFrameSize() - index;
System.arraycopy(buffer, index, buffer, 0, offset); System.arraycopy(buffer, index, buffer, 0, offset);
Log.w(MyTracksConstants.TAG, "Misaligned data, found new message at " + Log.w(TAG, "Misaligned data, found new message at " + index + " recovering...");
index + " recovering...");
continue; continue;
} }
Log.w(MyTracksConstants.TAG, "Could not find valid data, dropping data"); Log.w(TAG, "Could not find valid data, dropping data");
offset = 0; offset = 0;
continue; continue;
} }
@@ -398,7 +397,7 @@ public class BluetoothConnectionManager {
handler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) handler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget(); .sendToTarget();
} catch (IOException e) { } catch (IOException e) {
Log.e(MyTracksConstants.TAG, "disconnected", e); Log.e(TAG, "disconnected", e);
connectionLost(); connectionLost();
break; break;
} }
@@ -417,7 +416,7 @@ public class BluetoothConnectionManager {
// Share the sent message back to the UI Activity // Share the sent message back to the UI Activity
handler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget(); handler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
} catch (IOException e) { } catch (IOException e) {
Log.e(MyTracksConstants.TAG, "Exception during write", e); Log.e(TAG, "Exception during write", e);
} }
} }
@@ -425,7 +424,7 @@ public class BluetoothConnectionManager {
try { try {
btSocket.close(); btSocket.close();
} catch (IOException e) { } catch (IOException e) {
Log.e(MyTracksConstants.TAG, "close() of connect socket failed", e); Log.e(TAG, "close() of connect socket failed", e);
} }
} }
} }
@@ -15,7 +15,8 @@
*/ */
package com.google.android.apps.mytracks.services.sensors; package com.google.android.apps.mytracks.services.sensors;
import com.google.android.apps.mytracks.MyTracksConstants; import static com.google.android.apps.mytracks.MyTracksConstants.TAG;
import com.google.android.apps.mytracks.MyTracksSettings; import com.google.android.apps.mytracks.MyTracksSettings;
import com.google.android.apps.mytracks.content.Sensor; import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
@@ -50,7 +51,7 @@ public class BluetoothSensorManager extends SensorManager {
private Sensor.SensorDataSet sensorDataSet = null; private Sensor.SensorDataSet sensorDataSet = null;
private MessageParser parser; private MessageParser parser;
public BluetoothSensorManager( public BluetoothSensorManager(
Context context, MessageParser parser) { Context context, MessageParser parser) {
this.context = context; this.context = context;
@@ -66,7 +67,7 @@ public class BluetoothSensorManager extends SensorManager {
} }
private void setupSensor() { private void setupSensor() {
Log.d(MyTracksConstants.TAG, "setupSensor()"); Log.d(TAG, "setupSensor()");
// Initialize the BluetoothSensorAdapter to perform bluetooth connections. // Initialize the BluetoothSensorAdapter to perform bluetooth connections.
connectionManager = new BluetoothConnectionManager(messageHandler, parser); connectionManager = new BluetoothConnectionManager(messageHandler, parser);
@@ -78,7 +79,7 @@ public class BluetoothSensorManager extends SensorManager {
public void setupChannel() { public void setupChannel() {
if (!isEnabled() || connectionManager == null) { if (!isEnabled() || connectionManager == null) {
Log.w(MyTracksConstants.TAG, "Disabled manager onStartTrack"); Log.w(TAG, "Disabled manager onStartTrack");
return; return;
} }
SharedPreferences prefs = SharedPreferences prefs =
@@ -88,7 +89,7 @@ public class BluetoothSensorManager extends SensorManager {
if (address == null) { if (address == null) {
return; return;
} }
Log.w(MyTracksConstants.TAG, "Connecting to bluetooth sensor: " + address); Log.w(TAG, "Connecting to bluetooth sensor: " + address);
// Get the BluetoothDevice object // Get the BluetoothDevice object
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
// Attempt to connect to the device // Attempt to connect to the device
@@ -102,7 +103,7 @@ public class BluetoothSensorManager extends SensorManager {
// already // already
if (connectionManager.getState() == Sensor.SensorState.NONE) { if (connectionManager.getState() == Sensor.SensorState.NONE) {
// Start the Bluetooth sensor services // Start the Bluetooth sensor services
Log.w(MyTracksConstants.TAG, "Disabled manager onStartTrack"); Log.w(TAG, "Disabled manager onStartTrack");
connectionManager.start(); connectionManager.start();
} }
} }
@@ -132,7 +133,7 @@ public class BluetoothSensorManager extends SensorManager {
switch (msg.what) { switch (msg.what) {
case BluetoothConnectionManager.MESSAGE_STATE_CHANGE: case BluetoothConnectionManager.MESSAGE_STATE_CHANGE:
// TODO should we update the SensorManager state var? // TODO should we update the SensorManager state var?
Log.i(MyTracksConstants.TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1); Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
break; break;
case BluetoothConnectionManager.MESSAGE_WRITE: case BluetoothConnectionManager.MESSAGE_WRITE:
break; break;
@@ -141,15 +142,14 @@ public class BluetoothSensorManager extends SensorManager {
try { try {
readBuf = (byte[]) msg.obj; readBuf = (byte[]) msg.obj;
sensorDataSet = parser.parseBuffer(readBuf); sensorDataSet = parser.parseBuffer(readBuf);
Log.d(MyTracksConstants.TAG, "MESSAGE_READ: " + sensorDataSet.toString()); Log.d(TAG, "MESSAGE_READ: " + sensorDataSet.toString());
} catch (IllegalArgumentException iae) { } catch (IllegalArgumentException iae) {
sensorDataSet = null; sensorDataSet = null;
Log.i(MyTracksConstants.TAG, Log.i(TAG, "Got bad sensor data: " + new String(readBuf, 0, readBuf.length),
"Got bad sensor data: " + new String(readBuf, 0, readBuf.length),
iae); iae);
} catch (RuntimeException re) { } catch (RuntimeException re) {
sensorDataSet = null; sensorDataSet = null;
Log.i(MyTracksConstants.TAG, "Unexpected exception on read.", re); Log.i(TAG, "Unexpected exception on read.", re);
} }
break; break;
case BluetoothConnectionManager.MESSAGE_DEVICE_NAME: case BluetoothConnectionManager.MESSAGE_DEVICE_NAME:
@@ -15,13 +15,14 @@
*/ */
package com.google.android.apps.mytracks.services.sensors; package com.google.android.apps.mytracks.services.sensors;
import java.util.Timer; import static com.google.android.apps.mytracks.MyTracksConstants.TAG;
import java.util.TimerTask;
import com.google.android.apps.mytracks.content.Sensor;
import android.util.Log; import android.util.Log;
import com.google.android.apps.mytracks.MyTracksConstants; import java.util.Timer;
import com.google.android.apps.mytracks.content.Sensor; import java.util.TimerTask;
/** /**
* Manage the connection to a sensor. * Manage the connection to a sensor.
@@ -50,19 +51,17 @@ public abstract class SensorManager {
private TimerTask checkSensorManager = new TimerTask() { private TimerTask checkSensorManager = new TimerTask() {
@Override @Override
public void run() { public void run() {
Log.i(MyTracksConstants.TAG, Log.i(TAG, "SensorManager state: " + getSensorState());
"SensorManager state: " + getSensorState());
switch (getSensorState()) { switch (getSensorState()) {
case CONNECTING: case CONNECTING:
long age = System.currentTimeMillis() - getSensorStateTimestamp(); long age = System.currentTimeMillis() - getSensorStateTimestamp();
if (age > 2 * RETRY_PERIOD) { if (age > 2 * RETRY_PERIOD) {
Log.i(MyTracksConstants.TAG, "Retrying connecting SensorManager."); Log.i(TAG, "Retrying connecting SensorManager.");
setupChannel(); setupChannel();
} }
break; break;
case DISCONNECTED: case DISCONNECTED:
Log.i(MyTracksConstants.TAG, Log.i(TAG, "Re-registering disconnected SensorManager.");
"Re-registering disconnected SensoManager.");
setupChannel(); setupChannel();
break; break;
} }
@@ -1,12 +1,12 @@
/* /*
* Copyright 2009 Google Inc. * Copyright 2009 Google Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not * Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of * use this file except in compliance with the License. You may obtain a copy of
* the License at * the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -15,7 +15,8 @@
*/ */
package com.google.android.apps.mytracks.services.sensors; package com.google.android.apps.mytracks.services.sensors;
import com.google.android.apps.mytracks.MyTracksConstants; import static com.google.android.apps.mytracks.MyTracksConstants.TAG;
import com.google.android.apps.mytracks.MyTracksSettings; import com.google.android.apps.mytracks.MyTracksSettings;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
@@ -29,9 +30,7 @@ import android.util.Log;
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public class SensorManagerFactory { public class SensorManagerFactory {
private SensorManagerFactory() {}
private SensorManagerFactory() {
}
/** /**
* Get a new sensor manager. * Get a new sensor manager.
@@ -44,9 +43,9 @@ public class SensorManagerFactory {
if (prefs == null) { if (prefs == null) {
return null; return null;
} }
String sensor = prefs.getString(context.getString(R.string.sensor_type_key), null); String sensor = prefs.getString(context.getString(R.string.sensor_type_key), null);
Log.i(MyTracksConstants.TAG, "Creating sensor of type: " + sensor); Log.i(TAG, "Creating sensor of type: " + sensor);
if (sensor == null) { if (sensor == null) {
return null; return null;
@@ -57,7 +56,7 @@ public class SensorManagerFactory {
} else if (sensor.equals(context.getString(R.string.zephyr_sensor_type))) { } else if (sensor.equals(context.getString(R.string.zephyr_sensor_type))) {
return new ZephyrSensorManager(context); return new ZephyrSensorManager(context);
} else { } else {
Log.w(MyTracksConstants.TAG, "Unable to find sensor type: " + sensor); Log.w(TAG, "Unable to find sensor type: " + sensor);
return null; return null;
} }
} }
@@ -15,11 +15,12 @@
*/ */
package com.google.android.apps.mytracks.services.sensors; package com.google.android.apps.mytracks.services.sensors;
import android.util.Log; import static com.google.android.apps.mytracks.MyTracksConstants.TAG;
import com.google.android.apps.mytracks.MyTracksConstants;
import com.google.android.apps.mytracks.content.Sensor; import com.google.android.apps.mytracks.content.Sensor;
import android.util.Log;
/** /**
* An implementation of a SensorData parser for Zephyr HRM. * An implementation of a SensorData parser for Zephyr HRM.
* *
@@ -33,7 +34,7 @@ public class ZephyrMessageParser implements MessageParser {
for (int i = 0; i < buffer.length; i++) { for (int i = 0; i < buffer.length; i++) {
sb.append(String.format("%02X", buffer[i])); sb.append(String.format("%02X", buffer[i]));
} }
Log.w(MyTracksConstants.TAG, "Got zephyr data: " + sb); Log.w(TAG, "Got zephyr data: " + sb);
// The provided units are 1/16 strides per minute. // The provided units are 1/16 strides per minute.
// TODO: Fix the cadence calculation. // TODO: Fix the cadence calculation.
// int cadence = SensorUtils.unsignedShortToInt(buffer, 56); // int cadence = SensorUtils.unsignedShortToInt(buffer, 56);
@@ -49,7 +50,7 @@ public class ZephyrMessageParser implements MessageParser {
.setCreationTime(System.currentTimeMillis()) .setCreationTime(System.currentTimeMillis())
.setHeartRate(b) .setHeartRate(b)
.build(); .build();
return sds; return sds;
} }