You are here: Forum Home → ANT Developers Forums → ANT in Mobile Phones → Thread
package sdsu.sparta.arc_ant_demo;
import com.dsi.ant.AntInterface;
import com.dsi.ant.AntInterfaceIntent;
import com.dsi.ant.AntInterface.ServiceListener;
import com.dsi.ant.AntDefine;
import sdsu.sparta.arc_ant_demo.AntPlusManager;
import sdsu.sparta.arc_ant_demo.AntPlusManager.Callbacks;
import com.dsi.ant.exception.AntInterfaceException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Arc_ant_demo extends Activity //implements View.OnClickListener, AntPlusManager.Callbacks
{
Button ant_send;
AntInterface antInterface;
AntPlusManager antManager;
TextView sent_msg;
TextView rec_msg;
boolean ServConnected = false;
byte network = (byte)0x00;
byte channel = (byte)0x00;
short device = 33; // 33
byte deviceType = (byte)0x01; // 1
byte txType = (byte)0x01; // 1
byte radioFrequency = (byte)0xFF; // 255
short period = 8192; // 8192
byte channel_type = (byte)0x10; // 0x10 master 0x00 slave
/** Pair to any device. */
static final short WILDCARD = 0;
public static final String TAG = "arc_ant_demo";
/** Called when the activity is first created. */
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//ant_enable_disable = (Button) findViewById(R.id.ant_enable);
ant_send = (Button) findViewById(R.id.ant_send_msg);
sent_msg = (TextView) findViewById(R.id.sent_msg);
rec_msg = (TextView) findViewById(R.id.rec_msg);
// antManager = (AntPlusManager) getLastNonConfigurationInstance();
// antManager = null;
// if (antManager == null)
// {
// antManager = new AntPlusManager(this, savedInstanceState, this);
// }
// else
// {
// antManager.resetCallbacks(this, this);
// }
//
// antManager.start();
// antInterface = antManager.getAntReceiver();
ServiceListener listener = new ServiceListener()
{
@Override
public void onServiceConnected()
{
// TODO Auto-generated method stub
Log.d(TAG, "ServiceListener onServiceConnected()");
try
{
antInterface.enable();
boolean temp;
temp = antInterface.hasClaimedInterface();
if(temp)
{
// allow tx and rx of messages
ServConnected = true;
}
else
{
antInterface.requestForceClaimInterface("Arc Ant Demo");
}
}
catch (Throwable e)
{
error_message(e.toString());
}
}
@Override
public void onServiceDisconnected()
{
// TODO Auto-generated method stub
Log.d(TAG, "ServiceListener onServiceDisconnected()");
// do not allow tx and rx of messages
ServConnected = false;
}
};
antInterface = new AntInterface();
antInterface.initService(getApplicationContext(), listener);
try
{
Boolean state = antInterface.isServiceConnected();
error_message("isServiceConnected() returns " + state.toString());
}
catch (Throwable e)
{
error_message(e.toString());
}
Log.d(TAG, "On Create Exit");
}
public void ant_send(View view)
{
if(ServConnected)
{
try
{
byte[] txBuffer = {(byte) 0x00, (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66, (byte) 0x77, (byte) 0x8A};
// Tx Data
//antInterface.ANTTxMessage(txBuffer);
// Send Broadcast Data
antInterface.ANTSendBroadcastData(channel, txBuffer);
// Send Ack'd data
//antInterface.ANTSendAcknowledgedData(channel, txBuffer);
// Receive Broadcast data
}
catch (Throwable e)
{
error_message(e.toString());
}
}
else
{
error_message("Service not connected");
}
}
public void ant_channel_open(View view)
{
try
{
Boolean state;
state = antInterface.hasClaimedInterface();
//error_message(state.toString());
if(!state)
{
state = antInterface.claimInterface();
}
if(!state)
{
antInterface.requestForceClaimInterface("Arc Ant Demo");
}
// Assign Channel
antInterface.ANTAssignChannel(channel, channel_type, network);
// Set Channel ID
antInterface.ANTSetChannelId(channel, device, deviceType, txType);
// Set RF Frequency
antInterface.ANTSetChannelRFFreq(channel, radioFrequency);
// Set Channel Period
antInterface.ANTSetChannelPeriod(channel, period);
// Open Channel
antInterface.ANTOpenChannel(channel);
}
catch (Throwable e)
{
error_message(e.toString());
}
}
public void ant_channel_close (View view)
{
try
{
// Close Channel
antInterface.ANTCloseChannel(channel);
}
catch (Throwable e)
{
error_message(e.toString());
}
}
private void error_message(String msg)
{
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage(msg)
.setNeutralButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// do nothing
}
})
.show();
}
// @Override
// public void errorCallback()
// {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void notifyAntStateChanged()
// {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void notifyChannelStateChanged(byte channel)
// {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void notifyChannelDataChanged(byte channel)
// {
// // TODO Auto-generated method stub
//
// }
//
// @Override
// public void onClick(View v)
// {
// // TODO Auto-generated method stub
//
// }
}
package sdsu.sparta.arc_ant_demo;
import com.dsi.ant.AntInterface;
import com.dsi.ant.AntInterfaceIntent;
import com.dsi.ant.AntMesg;
import com.dsi.ant.AntInterface.ServiceListener;
import com.dsi.ant.AntDefine;
import com.dsi.ant.exception.AntInterfaceException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Arc_ant_demo extends Activity
{
Button ant_send;
AntInterface antInterface;
TextView sent_msg;
TextView rec_msg;
boolean ServConnected = false;
byte network = 0;
byte channel = 0;
short device = 33;
byte deviceType = 1;
byte txType = 1;
byte radioFrequency = 60;
short period = 6554; // = 5 Hz
byte channel_type = (byte)0x00; // 0x10 master 0x00 slave
/** Pair to any device. */
static final short WILDCARD = 0;
public static final String TAG = "arc_ant_demo";
/** Called when the activity is first created. */
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//ant_enable_disable = (Button) findViewById(R.id.ant_enable);
ant_send = (Button) findViewById(R.id.ant_send_msg);
sent_msg = (TextView) findViewById(R.id.sent_msg);
rec_msg = (TextView) findViewById(R.id.rec_msg);
ServiceListener listener = new ServiceListener()
{
@Override
public void onServiceConnected()
{
Log.d(TAG, "ServiceListener onServiceConnected()");
try
{
Boolean temp;
antInterface.enable();
temp = antInterface.claimInterface();
//error_message(temp.toString());
if(temp)
{
// allow tx and rx of messages
ServConnected = true;
}
else
{
antInterface.requestForceClaimInterface("Arc Ant Demo");
}
}
catch (Throwable e)
{
error_message(e.toString());
}
}
@Override
public void onServiceDisconnected()
{
Log.d(TAG, "ServiceListener onServiceDisconnected()");
// do not allow tx and rx of messages
ServConnected = false;
}
};
antInterface = new AntInterface();
antInterface.initService(getApplicationContext(), listener);
this.getApplicationContext().registerReceiver(AntMsgReceiver, new IntentFilter(AntInterfaceIntent.ANT_RX_MESSAGE_ACTION));
Log.d(TAG, "On Create Exit");
}
public void ant_send(View view)
{
if(ServConnected)
{
try
{
byte[] txBuffer = {(byte) 0x00, (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66, (byte) 0x77, (byte) 0x8A};
// Tx Data
//antInterface.ANTTxMessage(txBuffer);
//sent_msg.setText("Tx: " + getHexString(txBuffer));
// Send Broadcast Data
antInterface.ANTSendBroadcastData(channel, txBuffer);
sent_msg.setText("Broadcast: " + getHexString(txBuffer));
// Send Ack'd data
//antInterface.ANTSendAcknowledgedData(channel, txBuffer);
//sent_msg.setText("Acknowledged: " + getHexString(txBuffer));
// Receive Broadcast data
}
catch (Throwable e)
{
error_message(e.toString());
}
}
else
{
error_message("Service not connected");
}
}
public void ant_channel_open(View view)
{
try
{
Boolean state;
state = antInterface.hasClaimedInterface();
//error_message(state.toString());
if(!state)
{
state = antInterface.claimInterface();
}
if(!state)
{
antInterface.requestForceClaimInterface("Arc Ant Demo");
}
// Assign Channel
antInterface.ANTAssignChannel(channel, channel_type, network);
// Set Channel ID
antInterface.ANTSetChannelId(channel, device, deviceType, txType);
// Set RF Frequency
antInterface.ANTSetChannelRFFreq(channel, radioFrequency);
// Set Channel Period
antInterface.ANTSetChannelPeriod(channel, period);
// Open Channel
antInterface.ANTOpenChannel(channel);
}
catch (Throwable e)
{
error_message(e.toString());
}
}
public void ant_channel_close (View view)
{
try
{
// Close Channel
antInterface.ANTCloseChannel(channel);
}
catch (Throwable e)
{
error_message(e.toString());
}
}
private BroadcastReceiver AntMsgReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
//Context mContext = context;
String AntIntent = intent.getAction();
Log.d(TAG, "enter onReceive: " + AntIntent);
if (AntIntent.equals(AntInterfaceIntent.ANT_RX_MESSAGE_ACTION))
{
Log.d(TAG, "onReceive: ANT RX MESSAGE");
byte[] ANTRxMessage = intent.getByteArrayExtra(AntInterfaceIntent.ANT_MESSAGE);
Log.d(TAG, "Rx:"+ getHexString(ANTRxMessage));
switch(ANTRxMessage[AntMesg.MESG_ID_OFFSET])
{
case AntMesg.MESG_STARTUP_MESG_ID:
break;
case AntMesg.MESG_BROADCAST_DATA_ID:
rec_msg.setText("Broadcast: " + getHexString(ANTRxMessage));
break;
case AntMesg.MESG_ACKNOWLEDGED_DATA_ID:
rec_msg.setText("Acknowledged: " + getHexString(ANTRxMessage));
break;
case AntMesg.MESG_BURST_DATA_ID:
rec_msg.setText("Burst: " + getHexString(ANTRxMessage));
break;
case AntMesg.MESG_RESPONSE_EVENT_ID:
//rec_msg.setText("Msg Response Event: " + getHexString(ANTRxMessage));
break;
case AntMesg.MESG_CHANNEL_STATUS_ID:
break;
case AntMesg.MESG_CHANNEL_ID_ID:
break;
case AntMesg.MESG_VERSION_ID:
break;
case AntMesg.MESG_CAPABILITIES_ID:
break;
case AntMesg.MESG_GET_SERIAL_NUM_ID:
break;
case AntMesg.MESG_EXT_ACKNOWLEDGED_DATA_ID:
break;
case AntMesg.MESG_EXT_BROADCAST_DATA_ID:
break;
case AntMesg.MESG_EXT_BURST_DATA_ID:
break;
}
}
}
};
private void error_message(String msg)
{
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage(msg)
.setNeutralButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// do nothing
}
})
.show();
}
public static String getHexString(byte[] data)
{
if(null == data)
{
return "";
}
StringBuffer hexString = new StringBuffer();
for(int i = 0;i < data.length; i++)
{
hexString.append("[").append(String.format("%02X", data[i] & 0xFF)).append("]");
}
return hexString.toString();
}
}