wifi管理,解绑设备

This commit is contained in:
2026-05-20 09:48:18 +08:00
parent d98deb406f
commit e4c17969b7
9 changed files with 185 additions and 32 deletions

View File

@@ -0,0 +1,5 @@
package com.example.defenseapplication.bean
class DeleteBean {
var deviceId: String? = null
}

View File

@@ -74,7 +74,7 @@ class SettingsActivity : BaseActivity() {
.setPositiveText(getString(R.string.confirm))
.setShowInput(false)
dialog.setOnConfirmListener {
finish()
unbindDevice()
}
dialog.show(supportFragmentManager, "CustomDialogFragment")
}
@@ -134,6 +134,7 @@ class SettingsActivity : BaseActivity() {
}
binding.itemWifiManage.setOnClickListener {
val intent = Intent(this, WiFiManagementActivity::class.java)
intent.putExtra("wifi_name", binding.tvWifiName.text.toString())
startActivity(intent)
}
}
@@ -277,4 +278,36 @@ class SettingsActivity : BaseActivity() {
}
}
private fun unbindDevice() {
val deviceId = DeviceIdEvent.getDeviceId()
if (deviceId.isNullOrEmpty()) {
Toast.makeText(this, "设备ID为空", Toast.LENGTH_SHORT).show()
return
}
CoroutineScope(Dispatchers.IO).launch {
try {
val deleteBean = com.example.defenseapplication.bean.DeleteBean()
deleteBean.deviceId = deviceId
val response = RetrofitNetwork.getNetworkService().deleteDevice(deleteBean)
withContext(Dispatchers.Main) {
if (response.code == 200) {
Toast.makeText(this@SettingsActivity, "解绑成功", Toast.LENGTH_SHORT).show()
val intent = Intent(this@SettingsActivity, com.example.defenseapplication.home.HomeActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
finish()
} else {
Toast.makeText(this@SettingsActivity, response.msg, Toast.LENGTH_SHORT).show()
}
}
} catch (e: Exception) {
e.printStackTrace()
withContext(Dispatchers.Main) {
Toast.makeText(this@SettingsActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
}
}
}
}
}

View File

@@ -8,7 +8,7 @@ import com.example.defenseapplication.databinding.StrikePositionActivityBinding
import com.example.defenseapplication.view.CustomDialogFragment
import com.example.defenseapplication.view.showDefenseDialog
import com.gyf.immersionbar.ImmersionBar
//打击位置
class StrikePositionActivity : AppCompatActivity() {
private lateinit var binding: StrikePositionActivityBinding
private lateinit var markerLayoutParams: RelativeLayout.LayoutParams

View File

@@ -20,9 +20,17 @@ import androidx.core.app.ActivityCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.defenseapplication.R
import com.example.defenseapplication.adapter.WifiListAdapter
import com.example.defenseapplication.bean.DeviceBean
import com.example.defenseapplication.databinding.DialogWifiListBinding
import com.example.defenseapplication.databinding.WifiManagementActivityBinding
import com.example.defenseapplication.utils.DeviceIdEvent
import com.example.defenseapplication.utils.RetrofitNetwork
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
//WiFi管理
class WiFiManagementActivity : AppCompatActivity() {
@@ -30,6 +38,11 @@ class WiFiManagementActivity : AppCompatActivity() {
private lateinit var wifiManager: WifiManager
private lateinit var wifiScanReceiver: BroadcastReceiver
private var popupWindow: PopupWindow? = null
private var currentWifiName: String? = null
companion object {
const val EXTRA_WIFI_NAME = "wifi_name"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -39,6 +52,13 @@ class WiFiManagementActivity : AppCompatActivity() {
.statusBarDarkFont(true)
.titleBar(binding.topBar)
.init()
// 获取传递过来的WiFi名称
currentWifiName = intent.getStringExtra(EXTRA_WIFI_NAME)
if (!currentWifiName.isNullOrEmpty() && currentWifiName != getString(R.string.not_set)) {
binding.etWifiName.setText(currentWifiName)
}
binding.ivBack.setOnClickListener {
finish()
}
@@ -56,6 +76,17 @@ class WiFiManagementActivity : AppCompatActivity() {
updateBottomButtonVisibility()
}
})
binding.etWifiPassword.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
updateBottomButtonVisibility()
}
})
wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiScanReceiver = object : BroadcastReceiver() {
@@ -66,14 +97,61 @@ class WiFiManagementActivity : AppCompatActivity() {
}
}
}
//点击确定成功失败吐司
binding.btnConfirm.setOnClickListener {
// if(){
// ToastUtils.showSuccess(this, getString(R.string.wifi_connection_success))
// }else{
// ToastUtils.showError(this, getString(R.string.wifi_connection_failed))
// }
// 点击确定按钮修改WiFi信息
binding.btnConfirm.setOnClickListener {
updateWifiInfo()
}
updateBottomButtonVisibility()
}
private fun updateWifiInfo() {
val wifiName = binding.etWifiName.text.toString().trim()
val wifiPassword = binding.etWifiPassword.text.toString().trim()
if (wifiName.isEmpty()) {
ToastUtils.showError(this, getString(R.string.enter_wifi_name))
return
}
if (wifiPassword.isEmpty()) {
ToastUtils.showError(this, getString(R.string.enter_wifi_password))
return
}
val deviceId = DeviceIdEvent.getDeviceId()
if (deviceId.isNullOrEmpty()) {
ToastUtils.showError(this, getString(R.string.device_id_empty))
return
}
binding.btnConfirm.isEnabled = false
CoroutineScope(Dispatchers.IO).launch {
try {
val deviceBean = DeviceBean(
id = deviceId,
wifiName = wifiName,
wifiPassword = wifiPassword
)
val response = RetrofitNetwork.getNetworkService().updateDeviceInfo(deviceBean)
withContext(Dispatchers.Main) {
binding.btnConfirm.isEnabled = true
if (response.code == 200) {
ToastUtils.showSuccess(this@WiFiManagementActivity, getString(R.string.modify_success))
finish()
} else {
ToastUtils.showError(this@WiFiManagementActivity, response.msg ?: getString(R.string.modify_failed))
}
}
} catch (e: Exception) {
e.printStackTrace()
withContext(Dispatchers.Main) {
binding.btnConfirm.isEnabled = true
ToastUtils.showError(this@WiFiManagementActivity, getString(R.string.network_request_failed))
}
}
}
}
@@ -95,10 +173,15 @@ class WiFiManagementActivity : AppCompatActivity() {
wifiManager.startScan()
val dialogBinding = DialogWifiListBinding.inflate(LayoutInflater.from(this))
// 设置弹窗高度为屏幕的2/3
val screenHeight = resources.displayMetrics.heightPixels
val popupHeight = (screenHeight * 2 / 3)
popupWindow = PopupWindow(
dialogBinding.root,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
popupHeight
).apply {
isFocusable = true
setBackgroundDrawable(null)
@@ -113,16 +196,27 @@ class WiFiManagementActivity : AppCompatActivity() {
dialogBinding.rvWifiList.layoutManager = LinearLayoutManager(this@WiFiManagementActivity)
val wifiList = getWifiList()
dialogBinding.rvWifiList.adapter = WifiListAdapter(wifiList) { wifiName ->
val adapter = WifiListAdapter(wifiList) { wifiName ->
binding.etWifiName.setText(wifiName)
popupWindow?.dismiss()
}
dialogBinding.rvWifiList.adapter = adapter
// 添加下拉刷新功能
dialogBinding.swipeRefreshLayout.setOnRefreshListener {
wifiManager.startScan()
// 延迟1秒后停止刷新动画
android.os.Handler().postDelayed({
dialogBinding.swipeRefreshLayout.isRefreshing = false
}, 1000)
}
}
private fun updateBottomButtonVisibility() {
val wifiName = binding.etWifiName.text.toString().trim()
val wifiPassword = binding.etWifiPassword.text.toString().trim()
binding.bottomButtonLayout.visibility =
if (wifiName.isNotEmpty()) android.view.View.VISIBLE else android.view.View.GONE
if (wifiName.isNotEmpty() && wifiPassword.isNotEmpty()) android.view.View.VISIBLE else android.view.View.GONE
}
private fun getWifiList(): List<String> {
@@ -135,6 +229,9 @@ class WiFiManagementActivity : AppCompatActivity() {
}
private fun updateWifiList() {
popupWindow?.contentView?.findViewById<androidx.swiperefreshlayout.widget.SwipeRefreshLayout>(R.id.swipeRefreshLayout)?.let { swipeRefreshLayout ->
swipeRefreshLayout.isRefreshing = false
}
popupWindow?.contentView?.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.rvWifiList)?.let { recyclerView ->
val wifiList = getWifiList()
recyclerView.adapter = WifiListAdapter(wifiList) { wifiName ->
@@ -156,4 +253,4 @@ class WiFiManagementActivity : AppCompatActivity() {
unregisterReceiver(wifiScanReceiver)
popupWindow?.dismiss()
}
}
}

View File

@@ -7,6 +7,7 @@ import com.example.defenseapplication.bean.GetCodeBean
import bean.LoginBean
import bean.LoginRequest
import bean.RegisterRequest
import com.example.defenseapplication.bean.DeleteBean
import com.example.defenseapplication.bean.MessageGroup
import com.example.defenseapplication.bean.SwitchFamilyRequest
import retrofit2.http.Body
@@ -75,7 +76,11 @@ interface ApiService {
@GET("/app/deviceInfo/{id}")
suspend fun getDeviceInfo(@retrofit2.http.Path("id") id: String): GetCodeBean<DeviceBean>
/**
* 解绑设备
*/
@POST("/app/deleteDevice")
suspend fun deleteDevice(@Body deleteBean: DeleteBean): GetCodeBean<DeleteBean>
}