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