温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Android中使用ibeacon实现一个蓝牙考勤功能

发布时间:2021-05-14 17:56:26 来源:亿速云 阅读:208 作者:Leah 栏目:移动开发

今天就跟大家聊聊有关如何在Android中使用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;
        }
      }
    }
  };

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

看完上述内容,你们对如何在Android中使用ibeacon实现一个蓝牙考勤功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI