0

I'm trying to develop this app that discovers & connects to nearby Bluetooth devices. I ran into a NULLPOINTEREXCEPTION where my app crashes. Figured out it was due to my UUID not being assigned any value. Now I'm brand new to Android development & I don't know what value is to be assigned to my UUID so that it doesn't crash.

Following is my ConnectedBTDevice.java that handles connection to other Bluetooth devices.

package vertex2016.mvjce.edu.bluealert;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.IOException;
import java.util.UUID;

public class ConnectedBTDevice extends AppCompatActivity {

    public BluetoothDevice btd;
    public BluetoothSocket btSocket, tempSocket;
    private UUID myUUID;
    ArrayAdapter arr;
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connected_btdevice);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);


        arr  = new ArrayAdapter(this, android.R.layout.simple_list_item_2);

        btd = getIntent().getParcelableExtra("BluetoothDevice");

        connectBT();
        displayStuff();

    }

    public void connectBT() {
        Thread myThread = new Thread() {

            public void run() {
                tempSocket = null;
                btSocket = null;

                try {
                    tempSocket = btd.createRfcommSocketToServiceRecord(myUUID);

                } catch (IOException e) {
                    e.printStackTrace();
                }

                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

                try {
                    tempSocket.connect();
                    arr.add("CONNECTED TO-->" + btd.getName());
                } catch (IOException e) {
                    e.printStackTrace();
                    try {
                       tempSocket.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }


            }

        };
        myThread.start();
    }




    public void displayStuff()
    {
        lv = (ListView)findViewById(R.id.connectedBTlistView);
        lv.setAdapter(arr);
    }

}

And this is logcat that shows the error

03-24 00:32:11.254 5772-5772/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@17abdde2 time:66442377
03-24 00:32:11.594 5772-5772/vertex2016.mvjce.edu.bluealert E/ActivityThread: Performing stop of activity that is not resumed: {vertex2016.mvjce.edu.bluealert/vertex2016.mvjce.edu.bluealert.MainActivity}
                                                                              java.lang.RuntimeException: Performing stop of activity that is not resumed: {vertex2016.mvjce.edu.bluealert/vertex2016.mvjce.edu.bluealert.MainActivity}
                                                                                  at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3377)
                                                                                  at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3458)
                                                                                  at android.app.ActivityThread.access$1200(ActivityThread.java:154)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
03-24 00:32:16.677 5772-5772/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@13b7f542 time:66447800
03-24 00:32:17.811 5772-8519/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_launch_request id:vertex2016.mvjce.edu.bluealert time:66448935
03-24 00:32:18.420 5772-5772/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@313c32f2 time:66449543
03-24 00:32:24.308 5772-5772/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_launch_request id:vertex2016.mvjce.edu.bluealert time:66455431
03-24 00:32:24.593 5772-9062/vertex2016.mvjce.edu.bluealert W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
03-24 00:32:24.611 5772-9062/vertex2016.mvjce.edu.bluealert E/AndroidRuntime: FATAL EXCEPTION: Thread-39196
                                                                              Process: vertex2016.mvjce.edu.bluealert, PID: 5772
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.UUID.getMostSignificantBits()' on a null object reference
                                                                                  at android.os.ParcelUuid.writeToParcel(ParcelUuid.java:129)
                                                                                  at android.bluetooth.IBluetooth$Stub$Proxy.connectSocket(IBluetooth.java:1767)
                                                                                  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:309)
                                                                                  at vertex2016.mvjce.edu.bluealert.ConnectedBTDevice$1.run(ConnectedBTDevice.java:63)
03-24 00:32:25.172 5772-5772/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@13b7f542 time:66456295
03-24 00:32:25.945 5772-9062/vertex2016.mvjce.edu.bluealert I/Process: Sending signal. PID: 5772 SIG: 9

My question is: what value do I assign to myUUID such that it connects to any device I want? I'm sorry if this question sounds stupid, but I'm really trying to get a clear picture of what role UUID plays & how it is used (as in what values are to be assigned).

I have gone through posts like Android: How do bluetooth UUIDs work?, but didn't seem to answer the main question. What value do I assign to my UUID?

Thank you for your time!!

Community
  • 1
  • 1
Auro
  • 1,578
  • 3
  • 31
  • 62
  • Possible duplicate of [Android: How do bluetooth UUIDs work?](http://stackoverflow.com/questions/13964342/android-how-do-bluetooth-uuids-work) – Morrison Chang Mar 23 '16 at 20:07
  • When you say `what value do I assign to myUUID such that it connects to any device I want?` is kind of like saying I want to talk with every person I meet and call them 'Bob'. The UUID is a name that you use to identify what device / app you are talking to. Now how to get the UUID of the device you are trying to communicate with depends on the manufacturer to tell you (or attempt reverse-engineering techniques to find out what it is) – Morrison Chang Mar 23 '16 at 20:51
  • This is a bit of simplification - it may help to re-read http://developer.android.com/guide/topics/connectivity/bluetooth.html and specifically http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingDevices – Morrison Chang Mar 23 '16 at 20:57

1 Answers1

0

The device(s) you want to connect to should have a UUID, and you'll need to discover it.

Here's a link to an example app (not mine) that discovers Bluetooth devices and services.

The author makes use of the function BluetoothDevice.getUuids().

Matt Brown
  • 288
  • 2
  • 7