我升级到Android 6,使用蓝牙的应用程序无法使用这个新的API版本.这与Play商店中的应用程序存在同样的问题:蓝牙spp工具专业版(如果蓝牙工作则可以查看的良好应用程序),它不会发现设备.
问题似乎出现在蓝牙发现中:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery()
Log.i("BLUETOOTH", String.valueOf(mBluetoothAdapter.isDiscovering())); // Return false
Run Code Online (Sandbox Code Playgroud)
我的应用程序适用于Android 4/5,我遵循:http://developer.android.com/guide/topics/connectivity/bluetooth.html
Luc*_*uci 20
盯着Android 6.0,仅包含对清单的权限是不够的.您必须明确询问用户有关被视为"危险"的每个权限.BluetoothDevice.ACTION_FOUND需要BLUETOOTH和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);
}
Run Code Online (Sandbox Code Playgroud)
然后你会得到用户对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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
要使用以前版本的android,您应该使用兼容性库并使用ActivityCompat进行调用