uni-app 获取 android 手机 IMEI码

1、需求来源

最近项目上需要获取手机的IMEI码,并且在更换手机号登录后,需要提示重新更新IMEI码。

2、需求拆分

2.1 获取 IMEI 码

查阅 uni-app 官网发现在android 10 已经无法获取imei码,所以对于这个需求拆分成两种情况。

第一种情况: android 10 以上 选择用户手动输入的方式。

第二种情况:android 10 以下,通过 plus.device.getInfo 自动获取的方式。

2.2 获取android 手机的唯一编码

2.2.1 通过 uni.getSystemInfoSync 获取

let { deviceId } = uni.getSystemInfoSync();

可以看出 deviceId 在清理缓存等情况下,会存在不唯一的情况。

2.2.2 通过 plus.device.getOAID 获取

可以发现该方法只会在android 10 以上的手机手机有效。

2.2.3 通过 plus.device.getInfo 获取

plus.device.getInfo 方法的使用方法同 2.1 过去 IMEI码

3、完整示例

3.1 获取 IMEI码,deviceId

/** * 获取系统设备信息 */ export function getSystemDeviceInfo() { return new Promise((resolve, reject) => { // 返回结果 let resolveResult = (deviceId, imei) => { resolve({ deviceId, imei }) }; let sysInfo = uni.getSystemInfoSync(); // #ifdef APP-PLUS if (sysInfo.osName.toLowerCase() == 'android' && sysInfo.osVersion >= 10) { plus.device.getOAID({ success: ({ oaid }) => { resolveResult(oaid); }, fail: (e) => { resolveResult(sysInfo.deviceId); } }); } else { requestPermissions('android.permission.READ_PHONE_STATE').then(res => { plus.device.getInfo({ success: ({ uuid, imei }) => { let oaid = uuid?.split(",") || \[\] resolveResult(oaid || sysInfo.deviceId, imei) }, fail: (error) => { resolveResult(sysInfo.deviceId); }, }); }).catch((err) => { resolveResult(sysInfo.deviceId); }); } // #endif // #ifndef APP-PLUS resolveResult(sysInfo.deviceId); // #endif }); }

3.2 申请系统权限

/** * 申请权限 * * @param {Object} permissions */ export function requestPermissions(permissions) { return new Promise((resolve, reject) => { plus.android.requestPermissions(permissions, function(e) { if (e.deniedAlways.length > 0) { //权限被永久拒绝 // 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启 console.log('Always Denied!!! ' + e.deniedAlways.toString()); return reject({ code: -1, message: '权限被永久拒绝', deniedAlways: e.deniedAlways }); } if (e.deniedPresent.length > 0) { //权限被临时拒绝 // 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限 console.log('Present Denied!!! ' + e.deniedPresent.toString()); return reject({ code: -2, message: '权限被临时拒绝', deniedAlways: e.deniedPresent }); } if (e.granted.length > 0) { // 权限被允许 // 调用依赖获取定位权限的代码 console.log('Granted!!! ' + e.granted.toString()); return resolve(); } }, function(e) { console.log('Request Permissions error:' + JSON.stringify(e)); reject(e); }); }); }

4、注意

1、plus.device.getInfo 在用户拒绝权限后success, fail 回调都不会响应,所以增加手动获取权限方法,避免用户拒绝权限后无法正确的回调

2、plus.device.getOAID 只在 android 10 以上有用。

3、uni.getSystemInfoSync 获取的 deviceId 存在不是唯一的情况。

相关推荐
千里马学框架1 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
码兄科技2 天前
24小时自助健身房系统软件开发实战指南:功能设计与部署方案
java·spring boot·uni-app
2501_916007472 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
2501_916008892 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
开开心心就好2 天前
安卓手写文字生成工具,多种纸张一直免费
网络·网络协议·tcp/ip·leetcode·智能手机·电脑·模拟退火算法
2501_916008892 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
开开心心就好2 天前
批量提取PDF中的图片,直接导出原图
前端·javascript·支持向量机·智能手机·pdf·html·启发式算法
郑州光合科技余经理2 天前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
终端安全笔记3 天前
iOS 27 之后「策略空转」:设备升级不报错,但旧策略不再管它
android·网络·安全·ios·智能手机
ToDesk_Daas3 天前
随时随地手搓app!手机远程 Vibe Coding 实操分享
智能手机·电脑·iphone