refactor(app): 优化设备ID管理和秤SDK兼容性

- 移除BaseApp中的deviceId全局变量
- 添加对x86_64架构支持以用于Windows模拟器调试
- 增加BuildConfig字段IS_TEST_DEVICE控制测试设备逻辑
- 启用buildConfig功能支持编译时配置
- 实现测试设备使用固定DEVICE_ID_2替代真实UDID
- 将基础URL从测试环境切换到生产环境
- 重构WeightUtil支持多架构并添加错误处理
- 添加秤SDK可用性检查避免非ARM设备崩溃
This commit is contained in:
2026-05-22 08:59:19 +08:00
parent 30f6c74a99
commit e82fffb40a
6 changed files with 65 additions and 31 deletions
+7 -4
View File
@@ -18,9 +18,8 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
ndk {
//设置支持的SO库架构
abiFilters.addAll(listOf("armeabi-v7a"))
//, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
//设置支持的SO库架构x86_64 仅用于在 Windows 模拟器上调试(秤功能不可用)
abiFilters.addAll(listOf("armeabi-v7a", "x86_64"))
}
// 设置输出APK文件名格式
@@ -42,10 +41,13 @@ android {
"proguard-rules.pro"
)
signingConfig = signingConfigs.getByName("debug_507")
buildConfigField("Boolean", "IS_TEST_DEVICE", "false")
}
debug {
//507扫码秤主板在android.buildTypes{}内增加
signingConfig = signingConfigs.getByName("debug_507")
// 调试构建视为测试设备,使用固定的 DEVICE_ID_1 代替真实 UDID
buildConfigField("Boolean", "IS_TEST_DEVICE", "false")
}
}
compileOptions {
@@ -57,10 +59,11 @@ android {
}
buildFeatures {
viewBinding = true
buildConfig = true
}
packagingOptions {
pickFirst("lib/armeabi-v7a/libserial_port.so")
pickFirst("lib/arme64-v8a/libserial_port.so")
pickFirst("lib/arm64-v8a/libserial_port.so")
}
sourceSets {
named("main") {
@@ -29,7 +29,6 @@ class BaseApp : Application() {
var configUrl = ""
var token: String? = null
var deviceId: String? = null
var appVersion: String = "1"
@Volatile
private var sharedPref: SharedPreferences? = null
@@ -16,6 +16,10 @@ object GlobalData {
const val TEST_BASE_URL = "http://192.168.1.201:14801"
const val UAT_BASE_URL = "https://dev.yixiong-tech.com:8083"
const val PROD_BASE_URL = "https://api.dm.yixiong-tech.com:8443"
//临时用于测试
const val DEVICE_ID_2 = "8fc2ab34-2137-3112-acca-f884ea8736d4"
/**
* 设备id
*/
@@ -5,6 +5,7 @@ import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.shuwei.dish.match.BuildConfig
import com.shuwei.dish.match.base.BaseActivity
import com.shuwei.dish.match.base.BaseApp
import com.shuwei.dish.match.databinding.ActivityInitBinding
@@ -40,12 +41,14 @@ class InitActivity : BaseActivity() {
BaseApp.appVersion = AppUtil.getAppVersionCode(this).toString()
// 获取 ANDROID_ID
var androidId =
Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
androidId = "39a7abdd06b3c7ab"
BaseApp.deviceId = androidId
SpTool.put(SpTool.DEVICE_ID, androidId)
GlobalData.appBaseUrl = GlobalData.TEST_BASE_URL
// var androidId =
// Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
// androidId = "39a7abdd06b3c7ab"
val deviceId = if (BuildConfig.IS_TEST_DEVICE) GlobalData.DEVICE_ID_2 else AppUtil.getUDID(this)
GlobalData.deviceId = deviceId
SpTool.put(SpTool.DEVICE_ID, deviceId)
// GlobalData.appBaseUrl = GlobalData.TEST_BASE_URL
GlobalData.appBaseUrl = GlobalData.PROD_BASE_URL
BaseApp.canteenId = "0"
// // TODO: 以上保存deviceId用于临时使用,后续改为下面注释方式
@@ -1,33 +1,58 @@
package com.shuwei.dish.match.utils
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
import com.shuwei.dish.match.utils.ext.roundedOneDecimalPlace
//import com.aithings.Weigher
import com.wabon.wbintelligenthardwaresdk.api.SensorScale
import java.util.concurrent.ConcurrentHashMap
typealias WeightCallback = (address: Int, state: Int, weight: Double) -> Unit
object WeightUtil {
private const val TAG = "WeightUtil"
const val STATE_STABLE = SensorScale.STATE_STABLE
const val STATE_UNSTABLE = SensorScale.STATE_UNSTABLE
const val STATE_OVER_WEIGHT = SensorScale.STATE_OVER_WEIGHT
// 使用字面量常量,避免在非 ARM 设备上触发 SensorScale 类加载
const val STATE_STABLE = 1
const val STATE_UNSTABLE = 0
const val STATE_OVER_WEIGHT = 2
var isConnected = false
var weightFuncMap: MutableMap<String, WeightCallback?>? =
null
/** 秤 SDK 是否可用;非 ARM 设备上为 false,所有秤操作将静默跳过 */
var isAvailable = false
val weightFuncMap: ConcurrentHashMap<String, WeightCallback?> = ConcurrentHashMap()
fun init() {
Weigher2.init()
val abi = Build.SUPPORTED_ABIS.firstOrNull() ?: ""
if (!abi.startsWith("arm")) {
Log.w(TAG, "当前架构 $abi 不支持秤 SDK,已跳过初始化")
return
}
try {
SensorScale.isLog = true
Weigher2.init()
} catch (e: UnsatisfiedLinkError) {
Log.w(TAG, "秤 SDK 加载失败(非 ARM 设备),已跳过: ${e.message}")
return
} catch (e: Exception) {
Log.w(TAG, "秤 SDK 初始化异常,已跳过: ${e.message}")
return
}
isAvailable = true
Weigher2.setListener(object : WeightListenerImpl() {
override fun onInit(connect: Boolean) {
super.onInit(connect)
isConnected = connect
}
override fun onTare() {
super.onTare()
Log.d(TAG, "tareTwo,onTare 回调触发")
}
override fun onZero() {
super.onZero()
Log.d(TAG, "tareTwo,onZero 回调触发")
}
override fun onGetWeight(address: Int, state: Int, weight: Double) {
super.onGetWeight(address, state, weight)
val stateStr = when (state) {
@@ -38,7 +63,7 @@ object WeightUtil {
}
val useWeight = (weight*1000).roundedOneDecimalPlace()
Log.d(TAG, "readWeight, address=$address,state=$stateStr, weight=$useWeight")
weightFuncMap?.forEach { (key, value) ->
weightFuncMap.forEach { (key, value) ->
value?.invoke(address, state, useWeight)
}
@@ -55,10 +80,12 @@ object WeightUtil {
}
fun startContinuousRead() {
if (!isAvailable) return
Weigher2.startContinuousRead()
}
fun stopContinuousRead() {
if (!isAvailable) return
try {
Weigher2.stopContinuousRead()
Weigher2.unInit()
@@ -69,26 +96,24 @@ object WeightUtil {
fun tareTwo(address: Int) {
try {
Weigher2.tareTwo(address)
Log.d(TAG, "tareTwo,执行清零操作: address=$address")
// SDK 内部自动管理总线,直接发送清零命令
Weigher2.zeroTwo(address)
} catch (e: Exception) {
e.printStackTrace()
Log.e(TAG, "tareTwo,执行清零异常: address=$address, error=${e.message}")
}
}
private fun runOnUiThread(action: () -> Unit) {
Handler(Looper.getMainLooper()).post {
action()
}
fun removeWeightListener(weightKey: String) {
weightFuncMap.remove(weightKey)
}
fun addWeightListener(
weightKey: String,
getWeight: WeightCallback = { _, _, _ -> }
) {
if (weightFuncMap == null) {
weightFuncMap = mutableMapOf()
}
weightFuncMap?.put(weightKey, getWeight)
weightFuncMap[weightKey] = getWeight
}
}
@@ -98,7 +123,7 @@ open class WeightListenerImpl : Weigher2.Listener {
}
override fun onZero() {
Log.d("WeightUtil", "zero ok")
Log.d("WeightUtil", "tareTwo,zero ok")
}
override fun onTare() {
Binary file not shown.