介绍
今天就跟大家聊聊有关如何在Android中使用ibeacon实现一个蓝牙考勤功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
<强>一、添加静态权限(在AndroidManifest.xml文件中添加,需要蓝牙和定位权限)强>
& lt; uses-permission Android: name=癮ndroid.permission.ACCESS_COARSE_LOCATION",/比; & lt; uses-permission  android: name=癮ndroid.permission.ACCESS_FINE_LOCATION",/比; & lt; uses-permission  android: name=癮ndroid.permission.BLUETOOTH_ADMIN",/比; & lt; uses-permission  android: name=癮ndroid.permission.BLUETOOTH",/在
<强>二、检测与开启蓝牙,GPS
强>
1。是否支持蓝牙:
, if (! context.getPackageManager () .hasSystemFeature (PackageManager.FEATURE_BLUETOOTH_LE)), { ,,,,,ToastUtils.show(“本机不支持蓝牙功能,,无法蓝牙打卡“); ,,,,,((活动),上下文).finish (); ,,,,,return 假; ,,,} ,,,final BluetoothManager bm =, (BluetoothManager), context.getSystemService (Context.BLUETOOTH_SERVICE); ,,,if (Build.VERSION.SDK_INT 祝辞=,Build.VERSION_CODES.JELLY_BEAN_MR2), { ,,,,,mBleAdapter =, bm.getAdapter ();,//mBleAdapter为全局变量,为BluetoothAdapter对象 ,,,} ,,,if (bleAdapter ==, null), { ,,,,,ToastUtils.show(“本机不支持低功耗蓝牙功能,,无法蓝牙打卡“); ,,,,,((活动),上下文).finish (); ,,,,,return 假; ,,,} ,,,return 真实;
2。是否开启GPS:
LocationManager lm =, (LocationManager), context.getSystemService (Context.LOCATION_SERVICE); boolean gps =, lm.isProviderEnabled (LocationManager.GPS_PROVIDER); boolean network =, lm.isProviderEnabled (LocationManager.NETWORK_PROVIDER); if (gps | |,网络),{ ,,return 真实; } return 错误的;
3。开启GPS:
Intent Intent =, new 意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS); context.startActivityForResult(意图,ActivityCode.ACTIVITY_CODE_GPS);
4。开启蓝牙:
Intent enableBtIntent =, new 意图(BluetoothAdapter.ACTION_REQUEST_ENABLE); (活动),mContext) .startActivityForResult (enableBtIntent, ActivityCode.ACTIVITY_CODE_OPEN_BLE);
<强>三、动态申请蓝牙权限强>
private boolean 检查(Context 上下文,String 许可),{ ,,,return ContextCompat.checkSelfPermission(上下文,许可),==,PackageManager.PERMISSION_GRANTED; , ,,} , ,/* * ,,*,权限申请 ,,*/private 才能;void  searchBle () { ,,,if (Build.VERSION.SDK_INT 祝辞=,Build.VERSION_CODES.M), { ,,,,,if (!检查(mContext, Manifest.permission.ACCESS_FINE_LOCATION), | |, !检查(mContext, Manifest.permission.ACCESS_COARSE_LOCATION)), { ,,,,,,,ActivityCompat.requestPermissions (,, new String [] {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},, ACCESS_LOCATION); ,,,,,},{else ,,,,,,,//执行蓝牙搜索 ,,,,,} ,,,},{else ,,,,,//执行蓝牙搜索 ,,,} ,,} , @Override才能 public 才能;void  onRequestPermissionsResult (int requestCode, String[],权限,int [], grantResults), { ,,,switch (requestCode), { ,,,,,case ACCESS_LOCATION: ,,,,,,,if (hasAllPermissionsGranted (grantResults)), { ,,,,,,,,,//执行蓝牙搜索 ,,,,,,,},{else ,,,,,,,,,ToastUtils.show(“请开启权限“); ,,,,,,,} ,,,,,,,休息; ,,,} 以前,,}><强>四。搜索蓝牙强>
,/* * ,*搜索蓝牙 */public 才能;void  searchBle (), { ,,,if (Build.VERSION.SDK_INT 祝辞=,Build.VERSION_CODES.JELLY_BEAN_MR2), { null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null如何在Android中使用ibeacon实现一个蓝牙考勤功能