Android 6.0 - Bluetooth - No code exists for Action_Found broadcast intent

前端 未结 1 1084
情话喂你
情话喂你 2020-12-17 01:35

UPDATE

I tried many codes, also from examples shown on the internet. each of them follows my approach. After many hours of testing, i came to the co

相关标签:
1条回答
  • 2020-12-17 02:03

    Very simple solution:

    1. Add FINE_LOCATION permission to manifest:

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

    2. Request FINE_LOCATION permission at runtime:

    //since i was working with appcompat, i used ActivityCompat method, but this method can be called easily from Activity subclassess.
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSION_REQUEST_CONSTANT);
    

    3. Implement onRequestPermissionsResult method:

    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    
    switch (requestCode) {
        case MY_PERMISSION_REQUEST_CONSTANT: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                //permission granted!
            }
            return;
        }
    }
    }
    

    All this because Marshmallows requires this permission in order to use bluetooth for discovery.

    Since this permission belongs to the Dangerous group of permissions, simply declaring it in the manifest doesn't work, we need the user's explicit consent to use the position (even if we don't need the position actually).

    0 讨论(0)
提交回复
热议问题