内容说明
1. OOP调用例子(推荐)
- 使用
XfxPlugin.lua的面向对象封装 - 展示通过
call方法和直接获取deviceOps对象两种方式 - 获取所有设备信息并显示在悬浮日志窗口
2. 直接调用例子
- 直接调用
Main类的方式 - 适合了解底层实现
3. 实际应用示例
包含 5 个实用场景:
- 设备指纹识别:生成唯一设备指纹
- 环境检测:检测 Root、模拟器、ADB 等(防作弊)
- 设备兼容性检查:检查 Android 版本、设备类型、CPU 架构
- 设备唯一标识管理:获取或生成并保存设备 ID
- 设备信息汇总报告:生成完整的设备信息报告
获取的设备信息包括
- 权限和调试:Root 状态、ADB 状态、开发者选项
- Android 版本:SDK 版本名称和代码
- 设备识别:Android ID、MAC 地址、制造商、型号
- 设备类型:是否平板、是否模拟器
- CPU 架构:支持的 ABI 列表
- 唯一设备标识:多种方式获取唯一设备 ID
OOP调用例子
-- ============================================
-- 设备信息获取示例
-- 演示如何使用 XfxPlugin.lua 调用 DeviceOps 获取设备信息
-- ============================================
-- 方式一:使用 XfxPlugin.lua 面向对象封装(推荐)
-- ============================================
print('========== 方式一:使用 XfxPlugin.lua 封装 ==========')
-- 加载 XfxPlugin.lua 类
local xfxModule = require('lib/XfxPlugin')
-- 创建 XFX 对象实例
-- 注意:new 方法会自动初始化 Main.init(context),所以可以直接使用 DeviceOps
local xfx = xfxModule:new({
apkName = 'xfxPlugin-release.apk', -- APK 文件名
})
-- 方法 1:通过 call 方法调用 DeviceOps 的方法
print('\n--- 通过 call 方法调用 ---')
local ok1, isRooted = pcall(function()
return xfx:call('deviceOps', 'isDeviceRooted')
end)
local ok2, manufacturer = pcall(function()
return xfx:call('deviceOps', 'getManufacturer')
end)
if ok1 and ok2 then
print('设备是否 Root: ' .. tostring(isRooted))
print('设备制造商: ' .. tostring(manufacturer))
else
print('获取设备信息失败')
end
-- 方法 2:直接获取 deviceOps 对象,然后调用方法(更直观,推荐)
print('\n--- 直接获取 deviceOps 对象 ---')
local deviceOps = xfx:getOps('deviceOps')
if deviceOps then
-- A. 判断 Root / ADB
print('\n【权限和调试信息】')
local isRooted = deviceOps.isDeviceRooted()
local isAdbEnabled = deviceOps.isAdbEnabled()
local isDevSettingsEnabled = deviceOps.isDevelopmentSettingsEnabled()
print('是否 Root: ' .. tostring(isRooted))
print('ADB 是否开启: ' .. tostring(isAdbEnabled))
print('开发者选项是否开启: ' .. tostring(isDevSettingsEnabled))
-- B. Android 版本信息
print('\n【Android 版本信息】')
local sdkName = deviceOps.getSDKVersionName()
local sdkCode = deviceOps.getSDKVersionCode()
print('SDK 版本名称: ' .. tostring(sdkName))
print('SDK 版本代码: ' .. tostring(sdkCode))
-- C. 基本识别信息
print('\n【设备识别信息】')
local androidId = deviceOps.getAndroidID()
local macAddr = deviceOps.getMacAddress()
local manufacturer = deviceOps.getManufacturer()
local model = deviceOps.getModel()
print('Android ID: ' .. tostring(androidId))
print('MAC 地址: ' .. tostring(macAddr))
print('制造商: ' .. tostring(manufacturer))
print('型号: ' .. tostring(model))
-- D. 设备类型判断
print('\n【设备类型】')
local isTablet = deviceOps.isTablet()
local isEmulator = deviceOps.isEmulator()
print('是否平板: ' .. tostring(isTablet))
print('是否模拟器: ' .. tostring(isEmulator))
-- E. ABI 信息(CPU 架构)
print('\n【CPU 架构信息】')
local abisJson = deviceOps.getABIsAsJson()
print('ABI JSON: ' .. abisJson) -- 输出: ["arm64-v8a", "armeabi-v7a"]
local abis = jsonLib.decode(abisJson)
for i, abi in ipairs(abis) do
print(i .. '. ' .. abi)
end
-- F. 唯一设备 ID(默认策略)
print('\n【唯一设备标识】')
local uniqueId = deviceOps.getUniqueDeviceId()
print('唯一设备 ID(默认): ' .. tostring(uniqueId))
-- G. 唯一设备 ID(自定义前缀)
local uniqueIdWithPrefix = deviceOps.getUniqueDeviceId('my_app_')
print('唯一设备 ID(带前缀): ' .. tostring(uniqueIdWithPrefix))
-- H. 唯一设备 ID(强制重新生成)
local uniqueIdForce = deviceOps.getUniqueDeviceId(true)
print('唯一设备 ID(强制重新生成): ' .. tostring(uniqueIdForce))
-- I. 唯一设备 ID(前缀 + 强制重新生成)
local uniqueIdBoth = deviceOps.getUniqueDeviceId('my_app_', true)
print('唯一设备 ID(前缀+强制): ' .. tostring(uniqueIdBoth))
-- J. 判断是否与指定 ID 为同一台设备
local sameDevice = deviceOps.isSameDevice(uniqueId)
print('与 uniqueId 是否同一设备: ' .. tostring(sameDevice))
-- 方法 3:结合悬浮日志窗口显示设备信息(更友好)
print('\n--- 使用悬浮日志窗口显示设备信息 ---')
local screenWidth, screenHeight = getDisplaySize()
xfx:showFloatLogWindow({
title = '设备信息',
width = math.floor(screenWidth / 2.5),
height = math.floor(screenHeight / 2),
})
xfx:setFloatLogTitle('设备信息概览')
xfx:logi('制造商: ' .. manufacturer)
xfx:logi('型号: ' .. model)
xfx:logi('Android: ' .. sdkName .. ' (SDK ' .. sdkCode .. ')')
xfx:logi('是否 Root: ' .. tostring(isRooted))
xfx:logi('是否模拟器: ' .. tostring(isEmulator))
xfx:logi('是否平板: ' .. tostring(isTablet))
xfx:logi('Android ID: ' .. androidId)
xfx:logi('唯一设备 ID: ' .. uniqueId)
sleep(5000) -- 显示 5 秒
xfx:closeFloatLog()
else
print('无法获取 deviceOps 对象')
end
直接调用例子
import('com.nx.assist.lua.LuaEngine')
-- ============================================
-- 方式二:直接调用方式(不推荐,但可以了解底层实现)
-- ============================================
print('========== 方式二:直接调用方式 ==========')
local loader = LuaEngine.loadApk('xfxPlugin-release.apk')
if not loader then
print('Failed to load APK')
return
end
local context = LuaEngine.getContext()
local UtilCodeMain = loader.loadClass('com.xfx.plugin.Main')
if not UtilCodeMain then
print('Failed to load Class')
return
end
-- 初始化插件
UtilCodeMain.init(context)
-- 获取 DeviceOps
local deviceOps = UtilCodeMain.deviceOps()
-- 获取设备信息
print('\n【设备基本信息】')
local manufacturer = deviceOps.getManufacturer()
local model = deviceOps.getModel()
local androidId = deviceOps.getAndroidID()
local sdkName = deviceOps.getSDKVersionName()
local sdkCode = deviceOps.getSDKVersionCode()
print('制造商: ' .. tostring(manufacturer))
print('型号: ' .. tostring(model))
print('Android ID: ' .. tostring(androidId))
print('Android 版本: ' .. tostring(sdkName) .. ' (SDK ' .. tostring(sdkCode) .. ')')
print('\n【设备状态】')
print('是否 Root: ' .. tostring(deviceOps.isDeviceRooted()))
print('ADB 是否开启: ' .. tostring(deviceOps.isAdbEnabled()))
print('是否模拟器: ' .. tostring(deviceOps.isEmulator()))
print('是否平板: ' .. tostring(deviceOps.isTablet()))
print('\n【唯一设备标识】')
local uniqueId = deviceOps.getUniqueDeviceId()
print('唯一设备 ID: ' .. tostring(uniqueId))
实际应用示例
import('com.nx.assist.lua.LuaEngine')
local xfxModule = require('lib/XfxPlugin')
local xfx = xfxModule:new({apkName = 'xfxPlugin-release.apk'})
local deviceOps = xfx:getOps('deviceOps')
-- ============================================
-- 示例 1:设备指纹识别
-- ============================================
print('========== 示例 1:设备指纹识别 ==========')
-- 生成设备指纹(用于设备识别、防作弊等)
local function generateDeviceFingerprint()
local manufacturer = deviceOps.getManufacturer()
local model = deviceOps.getModel()
local androidId = deviceOps.getAndroidID()
local uniqueId = deviceOps.getUniqueDeviceId('app_')
-- 组合生成设备指纹
local fingerprint = manufacturer .. '_' .. model .. '_' .. androidId .. '_' .. uniqueId
return fingerprint
end
local fingerprint = generateDeviceFingerprint()
print('设备指纹: ' .. fingerprint)
-- ============================================
-- 示例 2:环境检测(防作弊)
-- ============================================
print('\n========== 示例 2:环境检测 ==========')
local function checkEnvironment()
local isRooted = deviceOps.isDeviceRooted()
local isEmulator = deviceOps.isEmulator()
local isAdbEnabled = deviceOps.isAdbEnabled()
local warnings = {}
if isRooted then
table.insert(warnings, '设备已 Root')
end
if isEmulator then
table.insert(warnings, '运行在模拟器上')
end
if isAdbEnabled then
table.insert(warnings, 'ADB 调试已开启')
end
if #warnings > 0 then
print('⚠️ 环境警告:')
for i, warning in ipairs(warnings) do
print(' ' .. i .. '. ' .. warning)
end
return false
else
print('✓ 环境检测通过')
return true
end
end
local envOk = checkEnvironment()
-- ============================================
-- 示例 3:设备兼容性检查
-- ============================================
print('\n========== 示例 3:设备兼容性检查 ==========')
local function checkCompatibility()
local sdkCode = deviceOps.getSDKVersionCode()
local isTablet = deviceOps.isTablet()
-- 检查 Android 版本(例如:要求 Android 5.0 以上,SDK 21+)
local minSdk = 21
if sdkCode < minSdk then
print('❌ Android 版本过低,需要 Android 5.0 (SDK ' .. minSdk .. ') 以上')
return false
end
-- 检查设备类型
if isTablet then
print('ℹ️ 检测到平板设备,可能需要特殊适配')
end
-- 检查 CPU 架构(使用 JSON 方式)
local abisJson = deviceOps.getABIsAsJson()
if abisJson then
print('支持的 CPU 架构 (JSON): ' .. abisJson)
if jsonLib then
local abis = jsonLib.decode(abisJson)
if abis then
print('支持的 CPU 架构:')
for i, abi in ipairs(abis) do
print(' ' .. i .. '. ' .. tostring(abi))
end
end
end
end
print('✓ 设备兼容性检查通过')
return true
end
local compatible = checkCompatibility()
-- ============================================
-- 示例 4:设备唯一标识管理
-- ============================================
print('\n========== 示例 4:设备唯一标识管理 ==========')
-- 获取或生成设备唯一标识(用于用户识别、数据统计等)
local function getOrCreateDeviceId()
-- 尝试获取已保存的设备 ID
local fileOps = xfx:getOps('fileOps')
local deviceIdFile = '/sdcard/.device_id.txt'
if fileOps.isFileExists(deviceIdFile) then
-- 读取已保存的设备 ID
local savedId = fileOps.readFile2String(deviceIdFile)
if savedId and string.len(savedId) > 0 then
print('读取已保存的设备 ID: ' .. savedId)
return savedId
end
end
-- 生成新的设备 ID
local newId = deviceOps.getUniqueDeviceId('app_user_')
print('生成新的设备 ID: ' .. newId)
-- 保存设备 ID
fileOps.createOrExistsFile(deviceIdFile)
fileOps.writeFileFromString(deviceIdFile, newId)
print('设备 ID 已保存到: ' .. deviceIdFile)
return newId
end
local deviceId = getOrCreateDeviceId()
print('当前设备 ID: ' .. deviceId)
-- ============================================
-- 示例 5:设备信息汇总报告
-- ============================================
print('\n========== 示例 5:设备信息汇总报告 ==========')
local function generateDeviceReport()
local report = {}
report.manufacturer = deviceOps.getManufacturer()
report.model = deviceOps.getModel()
report.androidVersion = deviceOps.getSDKVersionName()
report.sdkCode = deviceOps.getSDKVersionCode()
report.androidId = deviceOps.getAndroidID()
report.uniqueId = deviceOps.getUniqueDeviceId()
report.isRooted = deviceOps.isDeviceRooted()
report.isEmulator = deviceOps.isEmulator()
report.isTablet = deviceOps.isTablet()
report.isAdbEnabled = deviceOps.isAdbEnabled()
-- 获取屏幕信息(需要 screenOps)
local screenOps = xfx:getOps('screenOps')
if screenOps then
report.screenWidth = screenOps.getScreenWidth()
report.screenHeight = screenOps.getScreenHeight()
report.screenDpi = screenOps.getScreenDensityDpi()
end
return report
end
local report = generateDeviceReport()
print('\n【设备信息报告】')
print('制造商: ' .. report.manufacturer)
print('型号: ' .. report.model)
print('Android 版本: ' .. report.androidVersion .. ' (SDK ' .. report.sdkCode .. ')')
print('屏幕尺寸: ' .. (report.screenWidth or 'N/A') .. ' x ' .. (report.screenHeight or 'N/A'))
print('屏幕 DPI: ' .. (report.screenDpi or 'N/A'))
print('是否 Root: ' .. tostring(report.isRooted))
print('是否模拟器: ' .. tostring(report.isEmulator))
print('是否平板: ' .. tostring(report.isTablet))
print('\n示例执行完成!')
1. 官方交流QQ群,添加多个不批。建议使用安卓手机或电脑申请。
飞云脚本圈: 586333520
Auto.js学习交流③群:286635606
Auto.js学习交流②群:712194666(满员)
IOS免越狱自动化测试群:691997586
2. 盗版,破解有损他人权益和违法作为,请各位会员支持正版。
3. 本站部分资源来源于用户上传和网络搜集,如有侵权请提供版权证明并联系站长删除。
4.如未特别申明,本站的技术性文章均为原创,未经授权,禁止转载/搬运等侵权行为。
5.全站所有付费服务均为虚拟商品,购买后自动发货。售出后概不接受任何理由的退、换。注册即为接受此条款。
6.如果站内内容侵犯了您的权益,请联系站长删除。
飞云脚本 » 【懒人精灵】小飞侠插件专题——获取安卓设备的设备信息
飞云脚本圈: 586333520

Auto.js学习交流③群:286635606
Auto.js学习交流②群:712194666(满员)
IOS免越狱自动化测试群:691997586
2. 盗版,破解有损他人权益和违法作为,请各位会员支持正版。
3. 本站部分资源来源于用户上传和网络搜集,如有侵权请提供版权证明并联系站长删除。
4.如未特别申明,本站的技术性文章均为原创,未经授权,禁止转载/搬运等侵权行为。
5.全站所有付费服务均为虚拟商品,购买后自动发货。售出后概不接受任何理由的退、换。注册即为接受此条款。
6.如果站内内容侵犯了您的权益,请联系站长删除。
飞云脚本 » 【懒人精灵】小飞侠插件专题——获取安卓设备的设备信息