與Android 6.0盯着它是不足以包含清單的權限。 您必須明確詢問用戶每個被認爲「危險」的權限。 BluetoothDevice.ACTION_FOUND需要藍牙以及ACCESS_COARSE_LOCATION權限 http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_FOUND
的ACCESS_COARSE_LOCATION http://developer.android.com/reference/android/Manifest.permission.html#ACCESS_COARSE_LOCATION 是一個「危險」的許可,因此,你要問使用做實際的發現之前它requestPermission了。
public void doDiscovery() {
int hasPermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);
if (hasPermission == PackageManager.PERMISSION_GRANTED) {
continueDoDiscovery();
return;
}
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
android.Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_COARSE_LOCATION_PERMISSIONS);
}
然後你會得到用戶的答案上onRequestPermissionsResult
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_COARSE_LOCATION_PERMISSIONS: {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
continueDoDiscovery();
} else {
Toast.makeText(this,
getResources().getString(R.string.permission_failure),
Toast.LENGTH_LONG).show();
cancelOperation();
}
return;
}
}
}
要使用的機器人,你應該使用兼容庫,並使用ActivityCompat
哪些設備遇到此問題?哪個連結?我使用Nexus 5 –
這是一個聯繫5 – eldina