android6.0 ble scan android6.0蓝牙扫描

来源:互联网 发布:mac图片放大快捷键 编辑:程序博客网 时间:2024/05/21 06:12

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);


3 1
原创粉丝点击