Search code examples
javaandroideclipsebluetoothnxt

Android - Connecting Samsung Galaxy Ace VIA Bluetooth to NXT


Ok so I've added the permission to the manifest file and paired my devices but I am getting a crash right here: Set pairedDevices = btAdapter.getBondedDevices();

I attempt to connect via a button click:

private OnClickListener myListener = new OnClickListener() {
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.main_btnYes:
            connectToNXT(); // connect to NXT
                myIntent = new Intent(v.getContext(), SelectSession.class);
                startActivityForResult(myIntent, 0);
            break;
        case R.id.main_btnNo:
            myIntent = new Intent(v.getContext(), ExitScreen.class);
            startActivityForResult(myIntent, 0);
            break;
        }
    }
};

Here is the connectToNXT() method: The crash occurs here: Set bondedDevices = btAdapter.getBondedDevices(); private void connectToNXT() {

        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

        **Set<BluetoothDevice> bondedDevices = btAdapter.getBondedDevices();**

        BluetoothDevice nxtDevice = null;   

}

Anyone know why this would cause a crash?

Also, since I'm pretty new to android and bluetooth(2 days :D), could someone be kind enough to let me know of a good tutorial for android bluetooth?

Thanks,

Rich.


Solution

  • Well, after trying out various pieces of code(none of which worked...), I managed to take bits from each of them and get it to work on my NXT.

    I'm using the Samsung Galaxy Ace(android OS) smartphone on firmware 2.2.1

    Here is the connect method that works. Feel free to use it if you want.

    Declarations:

        // This is the NXT Mac Address. Each device has a specific Mac. Find it in the Build output when uploading
        // your NXT app to the brick using a USB cable. MUST USE USB CABLE TO SEE MAC ADDRESS!
        final String nxtMac = "00:16:53:05:3C:F5";
        //Important: This is the data stream used to communicate with the NXT.
        private DataOutputStream nxtDos = null;
        BluetoothAdapter localAdapter;
        BluetoothSocket nxtSocket;
        boolean success = false;
    

    Connect Method

        //Connect to NXT
        public boolean connectToNXT() {         
            // get the BluetoothDevice of the NXT
            BluetoothDevice nxt = localAdapter.getRemoteDevice(nxtMac);
            //Try to connect to the nxt
            try {
                nxtSocket = nxt.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
                nxtSocket.connect();
                //Get the Data stream
                nxtDos = new DataOutputStream(nxtSocket.getOutputStream());
                success = true;
            } catch (IOException e) {
                Log.d("Bluetooth", "Err: Device not found or cannot connect");
                success = false;
            }
            return success;
        }
    

    Email me at [email protected] if you want.

    Rich.