0

I am a beginner working on a BLE scanner using Google's sample code: https://github.com/googlesamples/android-BluetoothLeGatt

Currently, the code is not able to detect any devices. This post says that this is because of location services being disabled or not being explicitly asked for on runtime. I have already included the required permissions in my Manifest.

I have added a few lines of code, which I think should work.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBar().setTitle(R.string.title_devices);
        mHandler = new Handler();


        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
            finish();
        }

        // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
        // BluetoothAdapter through BluetoothManager.
        final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

        // Checks if Bluetooth is supported on the device.
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }



    // Quick permission check
        int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
        permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
        if (permissionCheck != 0) {

            this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number
        }

    }

I think the quick permission check I have added should work, but the app still doesn't seem to be detecting any devices.

2 Answers2

1

I have met the same case that you are facing. If you are using android 6.0 or greater than. You must request location permission at runtime. After get Bluetooth Adapter, let's insert a code line as below to request location permission. When your app run, a dialog will be showed to ask whether you agree to shared your location.

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);

Let response my answer if this is not correct answer for you question, I will delete immediately.

TaQuangTu
  • 2,155
  • 2
  • 16
  • 30
0

onscannerregistered status =133

This might be because of the below reason.

1)Location Permission not given in Appication.

2)When Connecting to the Devies stop the scan and then connect to devices.

3)Scanning should not happen when we are reading/writing Data to Devices. Make sure the scan is stopped

4)If Frist scan is started should wait until the scan is finished.Then start the second scan.

5)Scanning multiple times without waiting for the previous scan to finish might result in couldn't find the scan call-back.

Try fast BLE Library it s more effective the Google BLE library available.

vinay shetty
  • 895
  • 1
  • 9
  • 15