mobile-freak
mobile-freak

Reputation: 1

Android 7.0 Bluetooth discovering does not start

I am trying to build a Bluetoothscanner. But the startDiscovery() is not starting. I already include the permissions.

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

My Code is:

final BluetoothManager mBluetoothManager =   
(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothAdapter.disable();
onoff.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
            if (!mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.enable();
                onoff.setChecked(true);
            }
            else {
                mBluetoothAdapter.disable();
                onoff.setChecked(false);
            }
            if(!mBluetoothAdapter.isDiscovering()){
                Log.e("Searching?: ", "is not searching");
                mBluetoothAdapter.startDiscovery();
                Log.d("SearchingStatus: ", "startDiscovery() started!" );
                Log.e("Searching?", String.valueOf(mBluetoothAdapter.isDiscovering()));
            }
        }
    });

My last log looks like this:

Searching?: false

Upvotes: 0

Views: 523

Answers (1)

TobiasMelin
TobiasMelin

Reputation: 231

BluetoothAdapter.enable is an asynchronous call: it will return immediately and clients should listen for ACTION_STATE_CHANGED to be notified of subsequent adapter state change, see more at https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#enable()

If you comment out all calls to BluetoothAdapter.enable / BluetoothAdapter.disable and make sure that Bluetooth is enabled (from Settings -> Bluetooth) before you start the app does it work then?

I would check the return value of mBluetoothAdapter.startDiscovery() and have a Log.e if it returns false.

Upvotes: 1

Related Questions