Android 6蓝牙 [英] Android 6 bluetooth
问题描述
我已升级到Android 6,并且使用蓝牙的应用程序无法与此新API版本一起使用. Play商店上的应用程序也存在同样的问题:蓝牙spp工具专业版(用于查看蓝牙是否正常的好应用程序)无法发现设备.
I upgraded to Android 6 and my applications who use Bluetooth doesn't work with this new API version. It's the same problem with application on Play Store: Bluetooth spp tools pro (good application to view if bluetooth works) which doesn't discovery of devices.
问题似乎出在蓝牙发现中:
The problem seems to be in Bluetooth discovery:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery()
Log.i("BLUETOOTH", String.valueOf(mBluetoothAdapter.isDiscovering())); // Return false
我的应用程序在Android 4/5上运行良好,我遵循: http://developer.android. com/guide/topics/connectivity/bluetooth.html
My applications work well with Android 4/5 and I followed : http://developer.android.com/guide/topics/connectivity/bluetooth.html
推荐答案
盯着Android 6.0,仅在清单上添加权限是不够的. 您必须明确询问用户有关被视为危险"的每个权限. BluetoothDevice.ACTION_FOUND需要BLUETOOTH和ACCESS_COARSE_LOCATION权限 http://developer.android.com/reference/android/bluetooth/BluetoothDevice .html#ACTION_FOUND
Staring with Android 6.0 it is not enough to include permissions on manifest. You have to ask the user explicitly about each permission that is considered "dangerous". BluetoothDevice.ACTION_FOUND requires BLUETOOTH and ACCESS_COARSE_LOCATION permissions 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进行请求.
The ACCESS_COARSE_LOCATION http://developer.android.com/reference/android/Manifest.permission.html#ACCESS_COARSE_LOCATION is a "dangerous" permission and therefore you have to ask for it using requestPermission before doing actual discovery.
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上获得用户答案
then on you will get the user answer on 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;
}
}
}
要使用早期版本的android,您应该使用兼容性库,并使用ActivityCompat进行调用
To work with previous versions of android you should use compatibility libraries and make the calls using ActivityCompat
这篇关于Android 6蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!