欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何在Android中使用ibeacon實現(xiàn)一個藍牙考勤功能

今天就跟大家聊聊有關如何在Android中使用ibeacon實現(xiàn)一個藍牙考勤功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網營銷、網站重做改版、泗陽網站定制設計、自適應品牌網站建設、H5響應式網站商城開發(fā)、集團公司官網建設、成都外貿網站建設、高端網站制作、響應式網頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為泗陽等各大城市提供網站開發(fā)制作服務。

一、添加靜態(tài)權限(在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);

三、動態(tài)申請藍牙權限

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 {
        //執(zhí)行藍牙搜索
      }
    } else {
      //執(zhí)行藍牙搜索
    }
  }
 
  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
      case ACCESS_LOCATION:
        if (hasAllPermissionsGranted(grantResults)) {
          //執(zhí)行藍牙搜索
        } 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;
          //回調通知外部,界面更新可考勤狀態(tài)
          if (mListener != null) {
            mListener.onConnected();
          }
          //此處是延時調用stopLeScan關閉藍牙搜索
          beginTimer();
          break;
        }
      }
    }
  };

Android是什么

Android是一種基于Linux內核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯(lián)盟領導及開發(fā)。

看完上述內容,你們對如何在Android中使用ibeacon實現(xiàn)一個藍牙考勤功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網頁題目:如何在Android中使用ibeacon實現(xiàn)一個藍牙考勤功能
分享地址:http://www.chinadenli.net/article20/pgsdjo.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供外貿網站建設網站設計網站收錄定制網站虛擬主機企業(yè)網站制作

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化