0

I'm currently programming an app which is a bluetooth controller for an Arduino robotic arm.

When I press the button 'Start' (b7), it is supposed to enable bluetooth. But the app stops and in the logcat, i get:

"Attempt to invoke virtual method 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()' on a null object reference".

here is the code:

package trombertlabs.essai1;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import java.util.Set;

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

public class MainActivity extends ActionBarActivity {

    ImageButton b, b0, b1, b2, b3, b4;
    Button b5, b6, b7;


    BluetoothAdapter bA;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b = (ImageButton)findViewById(R.id.upbb);
        b0 = (ImageButton)findViewById(R.id.upba);
        b1 = (ImageButton) findViewById(R.id.downba);
        b2 = (ImageButton)findViewById(R.id.downbb);
        b3 = (ImageButton)findViewById(R.id.leftb);
        b4 = (ImageButton)findViewById(R.id.rightb);
        b5 = (Button)findViewById(R.id.closeb);
        b6 = (Button)findViewById(R.id.openb);
        b7 = (Button)findViewById(R.id.startb);

        b7.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                 BtInterface();
            }
        });

    }

        public void BtInterface () {

        if (!bA.isEnabled()) {
            bA.enable();
        } 
        else {
        }
      }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

Actually, there are 'imports' and buttons that are unused, but this is for the rest of the project.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Hugo Trombert
  • 25
  • 1
  • 8

1 Answers1

0

You should initialize your BluetoothAdapter bA;, e.g.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ba = BluetoothAdapter.getDefaultAdapter();
Divers
  • 9,531
  • 7
  • 45
  • 88
  • Thanks Divers! I initialized it 'final BluetoothAdapter bA = BluetoothAdapter.getDefaultAdapter();' Thanks also Tunaki, I'll study the duplicate in depth – Hugo Trombert Jul 29 '16 at 09:07