2

I've just updated my Nexus 5 and my app's feature has stopped working.

The reason is that the interface LeScanCallback is not calling the function onLeScan. On the below code, I'm using some deprecated code, but I need to do it since I'm testing some technology. This is how I start my bluetooth adapter:

@Override
protected void onStart()
{
    super.onStart();

    // Check if the device has BLE
    if (!this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
    {
        bleNotSupported();
        return;
    }

    // Initializes a Bluetooth adapter.
    final BluetoothManager bluetoothManager = (BluetoothManager)this.getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled())
    {
        bleNotEnabled();

        /*
        * Bluetooth can be enabled in app:
        *
        *    Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        *    btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        *    mAppContext.startActivity(btIntent);
        */
        return;
    }

    boolean bluetoothScanning = mBluetoothAdapter.startLeScan(mScanCallback); // this is true as well
    progressBar.setVisibility(View.VISIBLE);
}

@Override
protected void onStop() {
    super.onStop();

    if (mBluetoothAdapter != null)
    {
        mBluetoothAdapter.stopLeScan(mScanCallback);
    }
    progressBar.setVisibility(View.GONE);
    mDeviceAdapter.clearList();
}

BluetoothAdapter.LeScanCallback mScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord)
    {
//            if (previousBluetoothSelected == null)
        mDeviceAdapter.refreshList(device);

        if (device.getAddress().equalsIgnoreCase(previousBluetoothSelected)) {
            selectDeviceAndGo(device);
            mBluetoothAdapter.stopLeScan(this);
        }
    }
};

and it works in each device with API < 23, but not in API = 23.

What could be the reason?

Thank you very much in advance.

Regards.

Rafael.

Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92

1 Answers1

10

There is A bug on some of the Nexus 5 phones with Marshmallow. in some phones the scan wont start if your gps is off.

Maor Hadad
  • 1,850
  • 21
  • 36
  • 2
    It's not a bug of nexus 5, it's how marshmallow works. Bluetooth scan or WiFi scan require location services to be on and your app must declare access fine or coarse location. – greywolf82 Oct 21 '15 at 18:17
  • https://github.com/AltBeacon/android-beacon-library/issues/301. github altbeacon discussion about it. – Maor Hadad Oct 21 '15 at 18:21
  • @MaorHadad I can't see the point, as I said it's not a problem of nexus 5, even on nexus 9 with mra58k there is the same "problem". In the final version of MM you need location services on. – greywolf82 Oct 22 '15 at 16:45
  • That is crazy! It makes no sense that location services should be on to perform bluetooth scanning (it wastes battery) so whether or not is it intended I definitely count that as a bug. – Ben Clayton Nov 23 '15 at 10:39
  • 1
    You saved my very existence, I spent last 2 hours finding out why the heck I'm not getting any results. <3 – lookashc Aug 17 '17 at 17:04
  • GPS trick worked for my Xiomi phone also....even though I defined location permission in the manifest, the setting was OFF – anoop4real Mar 29 '18 at 10:25