蓝牙4.0无法扫描到Ble设备问题

 1.在AndroidManifest里面加上了权限

    <!-- 蓝牙相关 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

2.开启位置权限

安卓从6.0开始,连接BLE设备不仅需要位置权限,还需要打开位置服务,需要把系统位置(Location)打开。

出于对用户体验性的考虑,可加入检测GPS开关是否打开,以及未打开时提示用户并打开相关页面的操作

 // gps是否可用  
       public static final boolean isGpsEnable(final Context context) {  
           LocationManager locationManager  
                   = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);  
           boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);  
           boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);  
           if (gps || network) {  
               return true;  
           }  
           return false;  
       }  
    //跳转到gps设置页
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
    activity.startActivityForResult(intent,requestCode);  

注意:部分机型打开GPS开关才可以搜索到蓝牙设备,如oppo手机。

猜你喜欢

转载自blog.csdn.net/m0_37796683/article/details/82380758