欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android基于ibeacon实现蓝牙考勤功能

程序员文章站 2024-02-07 09:18:40
说明: ibeacon设备会主动发射蓝牙信号,当手机打开蓝牙靠近ibeacon设备时,就会收到设备发送的蓝牙信号,这时只需要根据ibeacon设备的uuid、major、...

说明:

ibeacon设备会主动发射蓝牙信号,当手机打开蓝牙靠近ibeacon设备时,就会收到设备发送的蓝牙信号,这时只需要根据ibeacon设备的uuid、major、minor、mac这四个值,就可以确认是哪一台ibeacon设备,然后调用服务端考勤接口(ibeacon设备只为了确认手机在考勤机边上,不需要发送考勤数据到ibeacon设备上),即可实现蓝牙考勤。

一、添加静态权限(在androidmanifest.xml文件中添加,需要蓝牙和定位权限)

<uses-permission android:name="android.permission.access_coarse_location" />
<uses-permission android:name="android.permission.access_fine_location" />
<uses-permission android:name="android.permission.bluetooth_admin" />
<uses-permission android:name="android.permission.bluetooth" />

二、检测与开启蓝牙、gps

1.是否支持蓝牙:

 if (!context.getpackagemanager().hassystemfeature(packagemanager.feature_bluetooth_le)) {
      toastutils.show("本机不支持蓝牙功能, 无法蓝牙打卡");
      ((activity) context).finish();
      return false;
    }
    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("本机不支持低功耗蓝牙功能, 无法蓝牙打卡");
      ((activity) context).finish();
      return false;
    }
    return true;

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 || network) {
   return true;
}
return false;

3.开启gps:

intent intent = new intent(settings.action_location_source_settings);
context.startactivityforresult(intent, activitycode.activity_code_gps);

4.开启蓝牙:

intent enablebtintent = new intent(bluetoothadapter.action_request_enable);
((activity) mcontext).startactivityforresult(enablebtintent, activitycode.activity_code_open_ble);

三、动态申请蓝牙权限

private boolean check(context context, string permission) {
    return contextcompat.checkselfpermission(context, permission) == packagemanager.permission_granted;
 
  }
 
  /**
   * 权限申请
   */
  private void searchble(){
    if (build.version.sdk_int >= build.version_codes.m) {
      if (!check(mcontext, manifest.permission.access_fine_location) || !check(mcontext, manifest.permission.access_coarse_location)) {
        activitycompat.requestpermissions(this, new string[]{manifest.permission.access_fine_location, manifest.permission.access_coarse_location}, access_location);
      } else {
        //执行蓝牙搜索
      }
    } else {
      //执行蓝牙搜索
    }
  }
 
  @override
  public void onrequestpermissionsresult(int requestcode, string[] permissions, int[] grantresults) {
    switch (requestcode) {
      case access_location:
        if (hasallpermissionsgranted(grantresults)) {
          //执行蓝牙搜索
        } else {
          toastutils.show("请开启权限");
        }
        break;
    }
  }

四.搜索蓝牙

 /**
 * 搜索蓝牙
*/
  public void searchble() {
    if (build.version.sdk_int >= build.version_codes.jelly_bean_mr2) {
      mbleadapter.startlescan(mlescancallback);
    }
  }
 
  /**
   * 搜索结果回调
   */
  private bluetoothadapter.lescancallback mlescancallback = new bluetoothadapter.lescancallback() {
 
    @override
    public void onlescan(final bluetoothdevice device, int rssi, byte[] scanrecord) {
      //fromscandata方法将ibeacon数据转换为实体对象,内部包括了uuid、major、minor、mac、distance等信息
      final bleutil.deviceinfo info = bleutil.fromscandata(device, rssi, scanrecord);
      if (info == null || textutils.isempty(info.uuid) || info.major <= 0 || info.minor <= 0 || textutils.isempty(info.mac)) {
        return;
      }
      if (muuids == null || muuids.isempty()) {
        //此处关闭蓝牙搜索
        mbleadapter.stoplescan(mlescancallback);
        return;
      }
      for (machineinfo machineinfo : muuids) {
        if (info.uuid.equalsignorecase(machineinfo.uuid) &&
            (!textutils.isempty(machineinfo.major) && info.major == integer.parseint(machineinfo.major)) &&
            (!textutils.isempty(machineinfo.minor) && info.minor == integer.parseint(machineinfo.minor)) &&
            info.mac.equalsignorecase(machineinfo.mac) && info.distance <= max_distance) {
          mconnected = true;
          //回调通知外部,界面更新可考勤状态
          if (mlistener != null) {
            mlistener.onconnected();
          }
          //此处是延时调用stoplescan关闭蓝牙搜索
          begintimer();
          break;
        }
      }
    }
  };

五、考勤

此步调用服务端提供的api增加考勤记录

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇: VBS教程:函数-LBound 函数

下一篇: