android蓝牙门禁,BleAgent蓝牙门禁SDK文档

个人笔记-蓝牙门禁sdk文档

导入包

//以下是在主module的build

implementation 'com.zhy.core:BleLib:1.2.3' //此版本已经引用基础包

//以下是在项目根目录的build

allprojects {

repositories {

...

maven { url 'http://*.*.*.*:*/nexus/content/repositories/zhy' }

}

}

在Application进行初始化

Application需要继承CoreLib的ApplicationCore

初始化方法init

@Override

public void onCreate() {

super.onCreate();

if (shouldInit()) {

...

//初始化蓝牙门禁的api

BleAgent.init(this,appid,bleBaseUrl);

}

}

防止多次初始化

/**

* 防止多次初始化

*

* @return

*/

public boolean shouldInit() {

ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));

List processInfos = am.getRunningAppProcesses();

String mainProcessName = getPackageName();

int myPid = android.os.Process.myPid();

for (ActivityManager.RunningAppProcessInfo info : processInfos) {

if (info.pid == myPid && mainProcessName.equals(info.processName)) {

return true;

}

}

return false;

}

页面是BluetoothDoorNewFragment

这里请自行引入页面到Activity即可

示例代码:

/**

* Desc:

* Author: YJG

* Email: ye.jg@outlook.com

* Date: 2018/8/6

*/

public class BluetoothNewActivity extends BaseActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.common_content);

initView();

}

private void initView() {

setTitle("蓝牙开门");

FragmentManager fm = getSupportFragmentManager();

FragmentTransaction ft=fm.beginTransaction();

BluetoothDoorNewFragment bluetoothDoorNewFragment =new BluetoothDoorNewFragment();

ft.replace(R.id.content, bluetoothDoorNewFragment);

ft.commit();

}

}

登陆后需要执行以下操作

LoginActivity页面的操作

//设置用户token到蓝牙门禁模块

BleAgent.setLoginToken(token);

BleAgent.setLoginMobile(mobile);//数字蓝牙使用到

MainActivity页面onResume的操作

if (!Utils.isBackground()) {

//这边是未了兼容升级app的时候,用户已经登录的情况,没有走登录的逻辑,没有设置token,导致请求已 经授权的设备列表没有token

BleAgent.setLoginToken(token);

//上报开门记录用到

BleAgent.setAccountId(accountId);

//数字蓝牙使用到

BleAgent.setLoginMobile(mobile);

//请求已授权的蓝牙设备列表

BleServer.reqAuthDevices();

//补上传蓝牙门禁未上传的开门记录

BleServer.postUnPostRecordList();

}

这里需要注意不是每次onResume都执行上面代码,app不在后台,即重新启动才需要,在诸如返回MainActivity时的OnResume情况是不需要的。

APP账号退出重新登录的时候

执行sp文件清除,调用下面方法

BleAgent.logoutBle();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值