Android:基于ibeacon实现蓝牙考勤

来源:互联网 发布:机器手编程语言 编辑:程序博客网 时间:2024/06/05 23:29

说明:

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增加考勤记录


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 大冒险告白被接受了怎么办剧透微博 大冒险被告白了怎么办 腐书网 大冒险告白被接受了怎么办 006 大冒险告白被接受了怎么办广播剧 coolpad手机解锁图案忘了怎么办 装死兔严重掉毛怎么办 兔子嘴巴磕破了怎么办 嘴巴里面摔烂了怎么办 兔子的鼻子损了怎么办 两个人嘴巴被粘牙糖粘住怎么办 小鸟被老鼠粘粘住了怎么办 小猫被老鼠粘粘住怎么办 羊子嘴巴烂了怎么办 小孩突然嘴肿了怎么办 电视机图像颜色变了 怎么办 冰沙床垫结块了怎么办 冰沙床垫融化了怎么办 脚真菌感染肿了怎么办 水雾魔珠被吃了怎么办 怀孕40天出血了怎么办 联璧金融里的钱怎么办 提现的钱不对怎么办 究极日月蛋太多怎么办 人脸识别不匹配怎么办 地下城没有属强怎么办 传奇属性点错了怎么办 龙之谷技能太多怎么办 龙之谷pk卡怎么办 为什么访问别人空间没有记录怎么办 被蚊孑咬了有小红包怎么办 微信建了个打牌群找不客源怎么办 三星手机一直停在开机画面怎么办 微信猜拳被骗了怎么办 组队领金币的码怎么办 cad画图时鼠标飘怎么办 玩迷你世界很卡怎么办 玩迷你世界太卡怎么办 迷你世界太卡了怎么办 把线稿画在背景里了怎么办 蜗牛在家里跑了怎么办 小孩总是跟大人犟嘴怎么办