Question

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.

Was it helpful?

Solution 3

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 richardcloete@googlemail.com if you want.

Rich.

OTHER TIPS

The NXT's MAC Address can be found under the setting menu /NXT version. under this option, the ID number is the MAC Address .No need for USB!

My guess : a NullPointerException. Your btadapter variable is null and you try to call a method from it, which causes a NullPointerException.

But can't you provide a stacktrace or something ? Without a log it's hard to know what hapenned. If you use eclipse, go to Window/Show/Android/Logcat.

You can also run your application in debug mode and put a breakpoint just before the line where your app crash and see if btadapter has a value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top