[2.13.0 优化美化模式显示,添加魔方显示状态,重构蓝牙连接功能]

This commit is contained in:
lixiaopeng
2022-12-07 20:18:05 +08:00
parent cba41e5415
commit f58df1636d
13 changed files with 145 additions and 122 deletions

View File

@@ -7,12 +7,16 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import com.mogo.commons.context.ContextHolderUtil.getContext
import com.mogo.eagle.core.data.mofang.MfConstants
import com.mogo.eagle.core.function.call.hmi.CallerHmiManager.updateMfStatusView
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger
import com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant.Companion.M_F
import com.mogo.eagle.core.utilcode.mogo.storage.SharedPrefsMgr
/**
* 魔方连接状态和设备管理
*/
@SuppressLint("StaticFieldLeak")
class MoFangManager private constructor() {
@@ -24,58 +28,61 @@ class MoFangManager private constructor() {
private var mContext: Context? = null
private val TAG = "MoFangManager"
private val mBluetoothAdapter: BluetoothAdapter? = null
private var deviceName = ""
//添加状态判断
private var isMfConnect: Boolean = false
private lateinit var mBluetoothAdapter: BluetoothAdapter
private var isMfConnect: Boolean = false //添加状态判断
fun init(context: Context) {
// initBluetooth()
// mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
// if (!mBluetoothAdapter.isEnabled) {
// mBluetoothAdapter.enable()
// }
// mBluetoothAdapter.startDiscovery()
// showBondedDevice(mBluetoothAdapter)
mContext = context
initBluetooth(context)
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (!mBluetoothAdapter.isEnabled) {
mBluetoothAdapter.enable()
}
mBluetoothAdapter.startDiscovery()
showBondedDevice(mBluetoothAdapter)
}
/**
* 初始化蓝牙广播
*/
private fun initBluetooth() {
private fun initBluetooth(context: Context) {
val intentFilter = IntentFilter();
// 监视蓝牙关闭和打开的状态
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED)
// 监视蓝牙设备与APP连接的状态
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED)
// 注册广播
getContext().registerReceiver(bluetoothMonitorReceiver, intentFilter);
context.registerReceiver(bluetoothMonitorReceiver, intentFilter)
}
/**
* 查找蓝牙连接过的蓝牙设备
*/
private fun showBondedDevice(bluetoothAdapter: BluetoothAdapter) {
// val deviceList = bluetoothAdapter.bondedDevices
// for (device in deviceList) {
// try {
// //使用反射调用获取设备连接状态方法
// val isConnectedMethod = BluetoothDevice::class.java.getDeclaredMethod(
// "isConnected",
// *null as Array<Class<*>?>?
// )
// isConnectedMethod.isAccessible = true
//// boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
// Log.d(TAG, "-- device.getName() = " + device.name) //device.getAddress()
// if (device.name == "MINI_KEYBOARD") { //并且连接 TODO
// SharedPrefsMgr.getInstance(getContext()).putString("BLUETOOTH", device.name)
// }
// } catch (e: NoSuchMethodException) {
// e.printStackTrace()
// }
// }
if (bluetoothAdapter != null) {
val deviceList = bluetoothAdapter.bondedDevices
for (device in deviceList) {
try {
//使用反射调用获取设备连接状态方
val isConnectedMethod = BluetoothDevice::class.java.getDeclaredMethod("isConnected")
isConnectedMethod.isAccessible = true
val isConnected = isConnectedMethod.invoke(device) as Boolean
CallerLogger.d("$M_F${TAG}"," showBondedDevice name = ${device.name} ---address = ${device.address}----isMfConnect = $isMfConnect ---isConnected = $isConnected")
if (device.name == "MINI_KEYBOARD") { //并且连接
mContext?.let { SharedPrefsMgr.getInstance(it).putString(MfConstants.BLUETOOTH_NAME, device.name) }
}
mContext?.let { SharedPrefsMgr.getInstance(it).putBoolean(MfConstants.BLUETOOTH_STATUS, isConnected) }
if (isConnected) {
isMfConnect = true
}
} catch (e: NoSuchMethodException) {
e.printStackTrace()
}
}
}
}
@@ -83,27 +90,30 @@ class MoFangManager private constructor() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
BluetoothAdapter.ACTION_STATE_CHANGED -> { //中间状态
CallerLogger.d("$M_F${TAG}","bluetoothMonitorReceiver ACTION_STATE_CHANGED action = ${intent.action}")
}
BluetoothDevice.ACTION_ACL_CONNECTED -> { //蓝牙设备已连接
if (!isMfConnect) {
updateMfStatusView("mofang", true)
// showBondedDevice(mBluetoothAdapter)
updateMfStatusView(TAG, true)
isMfConnect = true
mContext?.let { SharedPrefsMgr.getInstance(it).putBoolean(MfConstants.BLUETOOTH_STATUS, true) }
}
CallerLogger.d("$M_F${TAG}","bluetoothMonitorReceiver ACTION_ACL_CONNECTED ----- isMfConnect = $isMfConnect")
}
BluetoothDevice.ACTION_ACL_DISCONNECTED -> { //蓝牙设备已断开 主动更新 TODO
BluetoothDevice.ACTION_ACL_DISCONNECTED -> { //蓝牙设备已断开 主动更新
CallerLogger.d("$M_F${TAG}","bluetoothMonitorReceiver ACTION_ACL_DISCONNECTED ----- isMfConnect = $isMfConnect ")
if (isMfConnect) {
updateMfStatusView("mofang", false)
updateMfStatusView(TAG, false)
isMfConnect = false
mContext?.let { SharedPrefsMgr.getInstance(it).putBoolean(MfConstants.BLUETOOTH_STATUS, false) }
}
}
}
}
}
}
}