Jac*_*osi 21 android bluetooth-lowenergy android-bluetooth android-6.0-marshmallow
我正在尝试使用BluatoothLeScanner.startScan功能而不是弃用的BluetoothAdapter.startLeScan.昨天我将我的Nexus 5更新到Android 6.0,从那时起我的应用程序就不再起作用了.我首先添加了此处所需的ACCESS_COARSE_LOCATION首选项, https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id.然后我添加了此处所述的权限:https://developer.android.com/training/permissions/requesting.html.但最后它似乎无法正常工作,它不会发送回设备.
这是我的代码:
表现
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stm.sensitronapp">
<uses-sdk android:maxSdkVersion="23"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>`
Run Code Online (Sandbox Code Playgroud)
DeviceScanActivity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_COARSE);
}
}
// Device scan callback.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
mLeDeviceListAdapter.addDevice(result.getDevice());
mLeDeviceListAdapter.notifyDataSetChanged();
}
};
}
}
}
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
mSwipeRefreshLayout.setRefreshing(true);
mLeDeviceListAdapter.clear();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION ) == PackageManager.PERMISSION_GRANTED) {
mBluetoothLeScanner.startScan(mScanCallback);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
你是应用程序在启动时提示输入位置权限吗?如果不是,请在其他地方处理代码,以便提示它.
此外,您可以检查此项以测试您的应用是否正常运行:
打开设置>应用> YourApplication>权限并启用位置,然后尝试扫描结果.
仅当您在清单上提供了ACCESS_COARSE_LOCATION时,才会在权限下列出位置.
| 归档时间: |
|
| 查看次数: |
17586 次 |
| 最近记录: |