-2

I want to use a BLUETOOTH I added this in manifest

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />

next I did this :

String uid = "0000111f-0000-1000-8000-00805f9b34fb";
    UUID uuid = UUID.fromString(uid.toUpperCase()); //Standard SerialPortService ID
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();

but here ;

mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);

I have this Error

va.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothSocket on a null object reference

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
tompok
  • 27
  • 6
  • 1
    https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Zun Feb 07 '19 at 09:36
  • what is mmsocket? if it is an object than initialize like this, for ex: Textview text; and refer id if exists. – S Ajith Feb 07 '19 at 09:43

1 Answers1

0

Your mmDevice is not instantiated properly, that's why you're getting NullPointerException. Prevent this with simple nullcheck: if(mmDevice!=null).

  1. Do you ask for permissions at runtime? Since Android M, you should check for permissions from your Activity:
private void checkPermissions() {
    ArrayList<String> permissions = ArrayList();
    if (checkSelfPermission(applicationContext, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        permissions.add(ACCESS_FINE_LOCATION);
    if (checkSelfPermission(applicationContext, BLUETOOTH) != PackageManager.PERMISSION_GRANTED)
        permissions.add(BLUETOOTH);
    if (checkSelfPermission(applicationContext, BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED)
        permissions.add(BLUETOOTH_ADMIN);
    if (permissions.isNotEmpty())
        requestPermissions(permissions.toTypedArray(), REQUEST_PERMISSIONS);
    else {
    startBluetoothService();
    }
}
  1. Make sure you instantiate your BluetoothDevice object properly. For that, you should either scan for devices using BluetoothManager:
BluetoothManager mBluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.adapter;
//check if bluetooth is enabled 
if (mBluetoothAdapter.isEnabled){
    mBluetoothAdapter.startDiscovery(); 
    //make sure you register this receiver to properly receive callbacks
    mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        //Finding devices                 
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            doThingsWithDevice();
    }
  }
}
};

or, if you know device address, you can use mBluetoothManager.adapter.getRemoteDevice("deviceAddress") method.

Kuba Pawłowski
  • 365
  • 2
  • 10