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

有些话、适合烂在心里 提交于 2019-11-29 04:35:41

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!