You are here: Forum Home → ANT Developers Forums → ANT in Mobile Phones → Thread
Thats not the problem. The Channel is assign with network 1.
The smartLAB walk+ is an ANT+ Device so the should be no problem to connect to it.
But even with a wildcard I cant find the device. The walk+ acts as an master an sends a beacon with 4Hz.
I should get the beacon with the app with wildcard configuration, but it cant find anything.
import com.dsi.ant.AntDefine;
import com.dsi.ant.AntInterface;
import com.dsi.ant.AntInterfaceIntent;
import com.dsi.ant.AntMesg;
import com.dsi.ant.AntInterface.ServiceListener;
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 ANTsmartlab extends Activity {
    public static final String TAG = "ANTsmartlab";
    /** Called when the activity is first created. */
    
    static final byte SLAVE = AntDefine.PARAMETER_RX_NOT_TX;
    static final byte MASTER = AntDefine.PARAMETER_TX_NOT_RX;
    static final byte BOTH = AntDefine.PARAMETER_SHARED_CHANNEL;
    static final byte WILDCARD_CHANNEL = AntDefine.PARAMETER_ALWAYS_RX_WILD_CARD_SEARCH_ID;
    boolean ServConnected = false;
    
    /** Layout */
    TextView sent_msg;
    TextView rec_msg;
    Button ant_send;
    Button ant_open;
    Button ant_close;
    
    
    AntInterface antInterface;
    /**Channel Configuration */
    byte channel = 0;
    byte channel_type = SLAVE; // 0x10 master 0x00 slave
    byte network = (byte) 0x01; // 0 ANT, 1 ANT+, 2 ANT-FS
    byte radioFrequency = 57; // Device Profil Pedo Meter 1.3
    byte txType = 0; //
    byte deviceType = (byte) 200; // 100 for defined device, 200 for pairing
    short device = 0;
    short period = 8192; // = 4 Hz = 8192 / 32768
    /*Pair to any device. */
    static final short WILDCARD = 0;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ant_send = (Button) findViewById(R.id.ant_send);
        ant_open = (Button) findViewById(R.id.ant_open);
        ant_close = (Button) findViewById(R.id.ant_close);
        sent_msg = (TextView) findViewById(R.id.sent_msg);
        rec_msg = (TextView) findViewById(R.id.rec_msg);
        Log.d(TAG, "Layout done");
        
        
        antInterface = new AntInterface();
        Log.d(TAG, "new Interface");    
        
        antInterface.initService(getApplicationContext(), listener);
        Log.d(TAG, "initService");
        
        
        this.getApplicationContext().registerReceiver(AntMsgReceiver,
                new IntentFilter(AntInterfaceIntent.ANT_RX_MESSAGE_ACTION));
        
        
    }
    
    /**BUtton Functions------------------------------------------------------------------*/
    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 };
                antInterface.ANTSendBroadcastData(channel, txBuffer);
                antInterface.ANTTxMessage(txBuffer);
                sent_msg.setText("Broadcast: " + getHexString(txBuffer));
            } 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();
            if (!state) {
                state = antInterface.claimInterface();
            }
            if (!state) {
                antInterface.requestForceClaimInterface("ANTsmartlab");
            }
            // 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());
        }
        return;
    }
    public void ant_channel_close(View view) {
        try {
            // Close Channel
            antInterface.ANTCloseChannel(channel);
        } catch (Throwable e) {
            error_message(e.toString());
        }
        return;
    }
    
    /** ANT-------------------------------------------------------------------*/
    private 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());antInterface.toString();
                if (temp) {
                    // allow tx and rx of messages
                    ServConnected = true;
                    Log.d(TAG, "ANT service connected");
                } else {
                    antInterface.requestForceClaimInterface("ANTsmartlab");
                }
            } 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;
        }
    };
    
    
    private BroadcastReceiver AntMsgReceiver = new BroadcastReceiver() {
        
        @Override
        public void onReceive(Context context, Intent intent) {
            // Context mContext = context;
            String AntIntent = intent.getAction();
            byte[] ANTRxMessage = intent
                    .getByteArrayExtra(AntInterfaceIntent.ANT_MESSAGE);
            Log.d(TAG, "enter onReceive: " + AntIntent);
            if (AntIntent.equals(AntInterfaceIntent.ANT_RX_MESSAGE_ACTION)) {
                Log.d(TAG, "onReceive: ANT RX MESSAGE");
                Log.d(TAG, "Rx:" + getHexString(ANTRxMessage));
                switch (ANTRxMessage[AntMesg.MESG_ID_OFFSET]) {
                case AntMesg.MESG_STARTUP_MESG_ID:
                    rec_msg.setText("Message Startup:"
                            + ANTRxMessage.toString());
                    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:
                    rec_msg.setText("Channel Status: "
                            + getHexString(ANTRxMessage));
                    break;
                case AntMesg.MESG_CHANNEL_ID_ID:
                    rec_msg.setText("Mesg. Channel ID: "
                            + getHexString(ANTRxMessage));
                    break;
                case AntMesg.MESG_VERSION_ID:
                    rec_msg.setText("Msg Version ID: "
                            + getHexString(ANTRxMessage));
                    break;
                case AntMesg.MESG_CAPABILITIES_ID:
                    rec_msg.setText("Msg Capabilities: "
                            + getHexString(ANTRxMessage));
                    break;
                case AntMesg.MESG_GET_SERIAL_NUM_ID:
                    rec_msg.setText("Msg Serial Num: "
                            + getHexString(ANTRxMessage));
                    break;
                case AntMesg.MESG_EXT_ACKNOWLEDGED_DATA_ID:
                    rec_msg.setText("Msg Ext Acknowledged Data: "
                            + getHexString(ANTRxMessage));
                    break;
                case AntMesg.MESG_EXT_BROADCAST_DATA_ID:
                    rec_msg.setText("Msg Ext Broadcast Data: "
                            + getHexString(ANTRxMessage));
                    break;
                case AntMesg.MESG_EXT_BURST_DATA_ID:
                    rec_msg.setText("Msg Ext Burst Data: "
                            + getHexString(ANTRxMessage));
                    break;
                default:
                    rec_msg.setText("Dont Know:" + getHexString(ANTRxMessage));
                    break;
                }
            } else {
                Log.w(TAG, getHexString(ANTRxMessage));
            }
        }
    };
    
    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();
    }
}