Android 6个蓝牙
Android 6 bluetooth
我升级到 Android 6,但我使用蓝牙的应用程序无法使用这个新的 API 版本。 Play Store 上的应用程序也存在同样的问题:Bluetooth spp tools pro(查看蓝牙是否工作的好应用程序)不发现设备。
问题似乎出在蓝牙发现中:
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
我已经花了一些时间调查这个问题。
在 Android 错误跟踪器 here
上创建了错误报告
问题是系统不会将 BluetoothDevice.ACTION_FOUND
意图转发给已注册的 BroadcastReceiver
。 Logcat 显示这样的行:
10-16 07:34:09.147 786-802/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } to ProcessRecord{5ce2d92 21736:com.example.mvl.bluetoothtest/u0a74} (pid=21736, uid=10074) requires android.permission.ACCESS_COARSE_LOCATION due to sender com.android.bluetooth (uid 1002)
我的哪些主题应用程序需要 android.permission.ACCESS_COARSE_LOCATION
权限才能接收此意图。我个人不明白为什么我需要获得许可才能使用蓝牙设备。
因此,如果您将此权限添加到您的清单,那么它应该有一个前提条件 - 您必须设置目标 SDK 并使用不高于 22 的 SDK 进行编译。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
从 Android 6.0 开始,仅包含清单权限是不够的。
您必须明确询问用户有关被视为 "dangerous" 的每个权限。
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
是 "dangerous" 权限,因此您必须在进行实际发现之前使用 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;
}
}
}
要使用以前版本的 android,您应该使用兼容性库并使用 ActivityCompat
进行调用
查看GattService.java中的源代码时,会发现onScanResult方法中有一些代码comments:
// Do no report if location mode is OFF or the client has no location permission
// PEERS_MAC_ADDRESS permission holders always get results
if (hasScanResultPermission(client) && matchesFilters(client, result)) {
try {
ScanSettings settings = client.settings;
if ((settings.getCallbackType() &
ScanSettings.CALLBACK_TYPE_ALL_MATCHES) != 0) {
app.callback.onScanResult(result);
}
} catch (RemoteException e) {
Log.e(TAG, "Exception: " + e);
mClientMap.remove(client.clientIf);
mScanManager.stopScan(client);
}
}
这阐明了获得蓝牙 LE 广告报告需要什么。
我升级到 Android 6,但我使用蓝牙的应用程序无法使用这个新的 API 版本。 Play Store 上的应用程序也存在同样的问题:Bluetooth spp tools pro(查看蓝牙是否工作的好应用程序)不发现设备。
问题似乎出在蓝牙发现中:
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
我已经花了一些时间调查这个问题。
在 Android 错误跟踪器 here
上创建了错误报告
问题是系统不会将 BluetoothDevice.ACTION_FOUND
意图转发给已注册的 BroadcastReceiver
。 Logcat 显示这样的行:
10-16 07:34:09.147 786-802/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } to ProcessRecord{5ce2d92 21736:com.example.mvl.bluetoothtest/u0a74} (pid=21736, uid=10074) requires android.permission.ACCESS_COARSE_LOCATION due to sender com.android.bluetooth (uid 1002)
我的哪些主题应用程序需要 android.permission.ACCESS_COARSE_LOCATION
权限才能接收此意图。我个人不明白为什么我需要获得许可才能使用蓝牙设备。
因此,如果您将此权限添加到您的清单,那么它应该有一个前提条件 - 您必须设置目标 SDK 并使用不高于 22 的 SDK 进行编译。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
从 Android 6.0 开始,仅包含清单权限是不够的。 您必须明确询问用户有关被视为 "dangerous" 的每个权限。 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 是 "dangerous" 权限,因此您必须在进行实际发现之前使用 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;
}
}
}
要使用以前版本的 android,您应该使用兼容性库并使用 ActivityCompat
进行调用查看GattService.java中的源代码时,会发现onScanResult方法中有一些代码comments:
// Do no report if location mode is OFF or the client has no location permission
// PEERS_MAC_ADDRESS permission holders always get results
if (hasScanResultPermission(client) && matchesFilters(client, result)) {
try {
ScanSettings settings = client.settings;
if ((settings.getCallbackType() &
ScanSettings.CALLBACK_TYPE_ALL_MATCHES) != 0) {
app.callback.onScanResult(result);
}
} catch (RemoteException e) {
Log.e(TAG, "Exception: " + e);
mClientMap.remove(client.clientIf);
mScanManager.stopScan(client);
}
}
这阐明了获得蓝牙 LE 广告报告需要什么。