0

The app (an OBDII reader app) crashes when trying to connect to a device. I had the BTHandler class in my MainActivity where BTHandler btHandler = new BTHandler(); btHandler.btConnect();are now, but I was told I should make a class and then create an Instance of it in MainActivity.

So this code:

BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//AA:BB:CC:11:22:33");
        try {
            BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
            socket.connect();
            Log.v("connect", "connect");
        }
        catch (IOException e) {
            e.printStackTrace();
            Log.v("exception","e");
        }

used to be in MainActivity, but was told that it should be in another class by my college who's helping me with this.

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private BluetoothAdapter mAdapter;
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.connect);
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        //init();

        if (mAdapter == null) {
            Toast.makeText(getApplicationContext(), "Device does not Support Bluetooth", Toast.LENGTH_LONG).show();
            finish();
        } else {
            if (!mAdapter.isEnabled()) {
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(intent, 1);
            }
        }
    }

    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.connect:
                onConnect(); //Operation
                Log.v("Log", "Pressed onClick");
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Bluetooth must be Enabled", Toast.LENGTH_SHORT).show();
            finish();
        }
    }



    private void onConnect() {
        ArrayList deviceStrs = new ArrayList();
        final ArrayList<String> devices = new ArrayList();

        BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
        Set pairedDevices = mAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (Object device : pairedDevices) {
                BluetoothDevice bdevice = (BluetoothDevice) device;
                deviceStrs.add(bdevice.getName() + "\n" + bdevice.getAddress());
                devices.add(bdevice.getAddress());
            }
        }

        // show list
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice,
                deviceStrs.toArray(new String[deviceStrs.size()]));

        alertDialog.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                int position = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
                String deviceAddress = devices.get(position);

                BTHandler btHandler = new BTHandler();
                btHandler.btConnect();

            }
        });


        alertDialog.setTitle("Paired devices");
        alertDialog.show();


    }
}

BTHandler.java

public class BTHandler {
final ArrayList<String> devices = new ArrayList();
BluetoothAdapter mAdapter;
BluetoothDevice device;
String deviceAddress;

public BTHandler(Context context, Handler handler) {
    mAdapter = BluetoothAdapter.getDefaultAdapter();
}

public void btConnect(String deviceAdress) {
    mAdapter = BluetoothAdapter.getDefaultAdapter();
    device = mAdapter.getRemoteDevice(deviceAddress);

    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//AA:BB:CC:11:22:33");
    try {
        BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
        socket.connect();
        Log.v("connect", "connect");
    }
    catch (IOException e) {
        e.printStackTrace();
        Log.v("exception","e");
    }
}
}

Logcat:

Process: com.example.asabanov.powersupplytool, PID: 14113 java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String)' on a null object reference
at com.example.asabanov.powersupplytool.BTHandler.btConnect(BTHandler.java:19)
at com.example.asabanov.powersupplytool.MainActivity$1.onClick(MainActivity.java:93)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1108)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1214)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3139)
at android.widget.AbsListView$3.run(AbsListView.java:3981)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
swess
  • 171
  • 19
  • here is the problem you have declared BluetoothAdapter mAdapter; but never initialized it in BTHandler.java – Shreenivas Chikati Mar 02 '16 at 12:17
  • Aaah gotcha! I edited the OP, check to see if I did this right. And since i'm going to need a handler. `BTHandler btHandler = new BTHandler();` `btHandler.btConnect();` Are now saying "(Context, Handler)" in BTHandler and "(String)" in BTHandler cannot be applied. – swess Mar 02 '16 at 12:43

1 Answers1

1

You are not using the constructor in which mAdapter is being initialized, causing it to be null so a NullPointerException

Change

BTHandler btHandler = new BTHandler();

with

BTHandler btHandler = new BTHandler(MainActivity.this, null); 

in onClick method of alert dialog.

Note: You are not using second parameter so null shouldn't worry you or may be you should remove the second parameter all together.

Update Create BTHandler instance like this

BTHandler btHandler = new BTHandler(MainActivity.this, new Handler() {
    public void handleMessage(Message msg) {
        String response = msg.getData().getString("message");
        // TODO: take your action here
    }
});

use this to send message inside BTHandler. Create a global variable mHandler

Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", "Hello");
msg.setData(b);
mHandler.sendMessage(msg);

Change your BTHandler constructor like this

Handler mHandler;
public BTHandler(Context context, Handler handler) {
    mAdapter = BluetoothAdapter.getDefaultAdapter();
    mHandler = handler;
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • Yes! This worked, thank you. Do you have any idea how I could also have the second parameter to work (`btHandler.btConnect();`)? – swess Mar 02 '16 at 13:16
  • Sorry for my ignorance, how should I use the code inside `BTHandler` and creating a global var `mHandler` – swess Mar 02 '16 at 14:00
  • Updated to put more context into it. The second code block should be used when you need to send some message from `BTHandler` to `MainActitivy`. May be you should post a new question and please do sate in that why do you want to use the handler and for what purpose. – Rohit5k2 Mar 02 '16 at 14:04