javaandroidandroid-studiobluetooth

Android bluetooth cannot enter BroadcastReceiver


I am trying to search for bluetooth device in Android. I am following the tutorial in this page. I test by printing log but it can only print 1 but not 2. It means it cannot enter the BroadcastReceiver. I tried to move the receiver inside registerReceiver with calling function but it does not work. May I ask why?

public class ScanList extends AppCompatActivity {

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BroadcastReceiver mReceiver;
int REQUEST_ENABLE_BT = 1;
ArrayAdapter<String> mArrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan_list);


    if (mBluetoothAdapter != null) {
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        this,
                        new String[]{Manifest.permission.BLUETOOTH_CONNECT}, REQUEST_ENABLE_BT
                );
            }
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(receiver, filter);
    mBluetoothAdapter.startDiscovery();
    Log.i("devi", "1");   // can only print this line
}

// Create a BroadcastReceiver for ACTION_FOUND.
private BroadcastReceiver receiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        Log.i("devi", "2");
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Discovery has found a device. Get the BluetoothDevice
            // object and its info from the Intent.
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (ActivityCompat.checkSelfPermission(ScanList.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        ScanList.this,
                        new String[]{Manifest.permission.BLUETOOTH_CONNECT}, REQUEST_ENABLE_BT
                );
            }
            String deviceName = device.getName();
            String deviceHardwareAddress = device.getAddress(); // MAC address
            Log.i("devi", deviceName);
            Log.i("devi", deviceHardwareAddress);


            Intent reintent = new Intent(ScanList.this, MainActivity.class);
            Bundle extras = new Bundle();
            extras.putString("name",deviceName);
            extras.putString("address",deviceHardwareAddress);
            reintent.putExtras(extras);
            startActivity(reintent);
            finish();
        }
    }
};

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);
}
}

Edit

I can get device now by adding

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                            MainActivity.this,
                            new String[]{Manifest.permission.BLUETOOTH_CONNECT}, 1001
                );

     }

in front of mBluetoothAdapter.startDiscovery(); and String deviceName = device.getName();. However, it is very unstable that clicking scan is not a must to enter BroadcastReceiver. Also, it crash when enter BroadcastReceiver. It said

Bluetooth keeps stopping

Bluetooth is my app name. How can I solve this?

Edit2

I have solved it. The problem is that not every device has a name. If it scans a device without name and shows in logcat, it gives null and crash. To solve it just add if statement and check whether getName is null.


Solution

  • The problem is that not every device has a name. If it scans a device without name and shows in logcat, it gives null and crash. To solve it just add if statement and check whether getName is null.