android6.0 去设置---位置 里面 开启定位 加入以下代码 蓝牙就可以扫描了
1 在配置文件加入以下代码
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
2 在代码加入android6.0运行权限
private static final int REQUEST_FINE_LOCATION=0;
private void mayRequestLocation() {
if (Build.VERSION.SDK_INT >= 23) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(context,Manifest.permission.ACCESS_COARSE_LOCATION);
if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
//判断是否需要 向用户解释,为什么要申请该权限
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION))
Toast.makeText(context,R.string.ble_need, 1).show();
ActivityCompat.requestPermissions(this ,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},REQUEST_FINE_LOCATION);
return;
}else{
}
} else {
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_FINE_LOCATION:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// The requested permission is granted.
if (mScanning == false) {
scanLeDevice(true);
}
} else{
// The user disallowed the requested permission.
}
break;
}
}
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startLeScan(mLEScanCallback);
private BluetoothAdapter.LeScanCallback mLEScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan( BluetoothDevice device, int rssi, byte[] scanRecord) {
}
};
mBluetoothAdapter.stopLeScan(mLEScanCallback);