0

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.

richard
  • 87
  • 1
  • 2
  • 7

3 Answers3

1

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!

Yousef
  • 11
  • 1
0

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.

Anthony Chatellier
  • 594
  • 1
  • 4
  • 15
  • As you can see, I was right, it's a NullPointerException. I don't know what causes the problem, I'm not a bluetooth expert in Android, but did you put the permissions in the manifest ? Is BT activated on the phone ? Are you using the emulator ? Did you try the bluetooth chat made by the Android team and does it work on your device ? – Anthony Chatellier Jul 25 '11 at 15:06
  • Yeha it is a NPE and I will post the log in 7hours... I cant get it all into a comment and I cant post an answer to my own post :/ I have added the permissions to the manifest and bluetooth is activated on both dvices and both devices are paired. I've not tried the chat app - good suggestion, I'll give it a go. Thanks. – richard Jul 25 '11 at 15:08
  • I made changes to my first comment. You don't need to post all the stacktrace, it won't help. – Anthony Chatellier Jul 25 '11 at 15:10
  • Ok Thanks - Question is still open to anyone who may see the solution. – richard Jul 25 '11 at 15:11
  • From the android doc : getDefaultAdapter () returns the default local adapter, or null if Bluetooth is not supported on this hardware platform. – Anthony Chatellier Jul 25 '11 at 15:18
  • I have now tried using the BluetoothChat app from android and it still crashes on startup. It appears that the bluetooth app is using at least android 3.0 as it's using Actionbar, yet my mobile is on 2.2.1. I have modified the bluetooth app to work without the action bar, changed the min version to 2.2 and it still crashes but also slows my mobile down so much that I need to restart it. Any ideas as to wtf is going on? I'm on a Samsung Galaxy Ace. – richard Jul 25 '11 at 17:58
  • You said you had two devices. Did you try it on the other one ? (or is it a Galaxy Ace too ?) – Anthony Chatellier Jul 25 '11 at 19:21
  • Both devices as in the droid and the NXT brick as it is the device I am attempting to connect to. I don't know what to do with this now - it's driving me nuts. – richard Jul 25 '11 at 21:09
0

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.

richard
  • 87
  • 1
  • 2
  • 7