我升级到了Android 6,我的使用蓝牙的应用程序不能在这个新的API版本上工作。Play Store上的应用程序也存在同样的问题: Bluetooth spp tools pro (如果蓝牙正常工作,可以查看的很好的应用程序),它不能发现设备。
问题似乎出在蓝牙发现中:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery()
Log.i("BLUETOOTH", String.valueOf(mBluetoothAdapter.isDiscovering())); // Return false我的应用程序在Android4/5上运行得很好,我关注了一下:http://developer.android.com/guide/topics/connectivity/bluetooth.html
发布于 2015-10-28 02:43:26
从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进行调用
发布于 2015-10-16 19:13:08
我花了一些时间调查这个问题。
已在安卓bug跟踪器here上创建bug报告
问题是系统不会将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权限才能接收此意图。我个人不明白为什么我需要得到许可才能使用蓝牙设备。
因此,如果您将此权限添加到您的Manifest,那么它应该在另一个前提条件下工作-您必须设置目标SDK并使用不高于22的SDK进行编译。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>发布于 2016-08-10 00:27:40
当在GattService.java,you中检查源代码时,会在方法onScanResult中找到一些代码注释:
// 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广告报告所需的内容。
https://stackoverflow.com/questions/33142034
复制相似问题