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)) .setPositiveText(getString(R.string.confirm))
.setShowInput(false) .setShowInput(false)
dialog.setOnConfirmListener { dialog.setOnConfirmListener {
finish() unbindDevice()
} }
dialog.show(supportFragmentManager, "CustomDialogFragment") dialog.show(supportFragmentManager, "CustomDialogFragment")
} }
@@ -134,6 +134,7 @@ class SettingsActivity : BaseActivity() {
} }
binding.itemWifiManage.setOnClickListener { binding.itemWifiManage.setOnClickListener {
val intent = Intent(this, WiFiManagementActivity::class.java) val intent = Intent(this, WiFiManagementActivity::class.java)
intent.putExtra("wifi_name", binding.tvWifiName.text.toString())
startActivity(intent) 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.CustomDialogFragment
import com.example.defenseapplication.view.showDefenseDialog import com.example.defenseapplication.view.showDefenseDialog
import com.gyf.immersionbar.ImmersionBar import com.gyf.immersionbar.ImmersionBar
//打击位置
class StrikePositionActivity : AppCompatActivity() { class StrikePositionActivity : AppCompatActivity() {
private lateinit var binding: StrikePositionActivityBinding private lateinit var binding: StrikePositionActivityBinding
private lateinit var markerLayoutParams: RelativeLayout.LayoutParams private lateinit var markerLayoutParams: RelativeLayout.LayoutParams

View File

@@ -20,9 +20,17 @@ import androidx.core.app.ActivityCompat
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.example.defenseapplication.R import com.example.defenseapplication.R
import com.example.defenseapplication.adapter.WifiListAdapter import com.example.defenseapplication.adapter.WifiListAdapter
import com.example.defenseapplication.bean.DeviceBean
import com.example.defenseapplication.databinding.DialogWifiListBinding import com.example.defenseapplication.databinding.DialogWifiListBinding
import com.example.defenseapplication.databinding.WifiManagementActivityBinding import com.example.defenseapplication.databinding.WifiManagementActivityBinding
import com.example.defenseapplication.utils.DeviceIdEvent
import com.example.defenseapplication.utils.RetrofitNetwork
import com.gyf.immersionbar.ImmersionBar import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
//WiFi管理 //WiFi管理
class WiFiManagementActivity : AppCompatActivity() { class WiFiManagementActivity : AppCompatActivity() {
@@ -30,6 +38,11 @@ class WiFiManagementActivity : AppCompatActivity() {
private lateinit var wifiManager: WifiManager private lateinit var wifiManager: WifiManager
private lateinit var wifiScanReceiver: BroadcastReceiver private lateinit var wifiScanReceiver: BroadcastReceiver
private var popupWindow: PopupWindow? = null private var popupWindow: PopupWindow? = null
private var currentWifiName: String? = null
companion object {
const val EXTRA_WIFI_NAME = "wifi_name"
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@@ -39,6 +52,13 @@ class WiFiManagementActivity : AppCompatActivity() {
.statusBarDarkFont(true) .statusBarDarkFont(true)
.titleBar(binding.topBar) .titleBar(binding.topBar)
.init() .init()
// 获取传递过来的WiFi名称
currentWifiName = intent.getStringExtra(EXTRA_WIFI_NAME)
if (!currentWifiName.isNullOrEmpty() && currentWifiName != getString(R.string.not_set)) {
binding.etWifiName.setText(currentWifiName)
}
binding.ivBack.setOnClickListener { binding.ivBack.setOnClickListener {
finish() finish()
} }
@@ -56,6 +76,17 @@ class WiFiManagementActivity : AppCompatActivity() {
updateBottomButtonVisibility() 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 wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiScanReceiver = object : BroadcastReceiver() { 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() wifiManager.startScan()
val dialogBinding = DialogWifiListBinding.inflate(LayoutInflater.from(this)) val dialogBinding = DialogWifiListBinding.inflate(LayoutInflater.from(this))
// 设置弹窗高度为屏幕的2/3
val screenHeight = resources.displayMetrics.heightPixels
val popupHeight = (screenHeight * 2 / 3)
popupWindow = PopupWindow( popupWindow = PopupWindow(
dialogBinding.root, dialogBinding.root,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT popupHeight
).apply { ).apply {
isFocusable = true isFocusable = true
setBackgroundDrawable(null) setBackgroundDrawable(null)
@@ -113,16 +196,27 @@ class WiFiManagementActivity : AppCompatActivity() {
dialogBinding.rvWifiList.layoutManager = LinearLayoutManager(this@WiFiManagementActivity) dialogBinding.rvWifiList.layoutManager = LinearLayoutManager(this@WiFiManagementActivity)
val wifiList = getWifiList() val wifiList = getWifiList()
dialogBinding.rvWifiList.adapter = WifiListAdapter(wifiList) { wifiName -> val adapter = WifiListAdapter(wifiList) { wifiName ->
binding.etWifiName.setText(wifiName) binding.etWifiName.setText(wifiName)
popupWindow?.dismiss() popupWindow?.dismiss()
} }
dialogBinding.rvWifiList.adapter = adapter
// 添加下拉刷新功能
dialogBinding.swipeRefreshLayout.setOnRefreshListener {
wifiManager.startScan()
// 延迟1秒后停止刷新动画
android.os.Handler().postDelayed({
dialogBinding.swipeRefreshLayout.isRefreshing = false
}, 1000)
}
} }
private fun updateBottomButtonVisibility() { private fun updateBottomButtonVisibility() {
val wifiName = binding.etWifiName.text.toString().trim() val wifiName = binding.etWifiName.text.toString().trim()
val wifiPassword = binding.etWifiPassword.text.toString().trim()
binding.bottomButtonLayout.visibility = 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> { private fun getWifiList(): List<String> {
@@ -135,6 +229,9 @@ class WiFiManagementActivity : AppCompatActivity() {
} }
private fun updateWifiList() { 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 -> popupWindow?.contentView?.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.rvWifiList)?.let { recyclerView ->
val wifiList = getWifiList() val wifiList = getWifiList()
recyclerView.adapter = WifiListAdapter(wifiList) { wifiName -> recyclerView.adapter = WifiListAdapter(wifiList) { wifiName ->

View File

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

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:background="@drawable/rounded_top_corner" android:background="@drawable/rounded_top_corner"
android:orientation="vertical"> android:orientation="vertical">
@@ -10,7 +10,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="56dp" android:layout_height="56dp"
android:gravity="center" android:gravity="center"
android:text="@string/select_wifi" android:text="@string/select_wifi"
android:textColor="@color/black" android:textColor="@color/black"
android:textSize="18sp" android:textSize="18sp"
android:textStyle="bold" /> android:textStyle="bold" />
@@ -20,11 +20,18 @@
android:layout_height="1dp" android:layout_height="1dp"
android:background="@color/dialog_view" /> android:background="@color/dialog_view" />
<androidx.recyclerview.widget.RecyclerView <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/rvWifiList" android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="0dp"
android:maxHeight="400dp" android:layout_weight="1">
android:overScrollMode="never" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvWifiList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout> </LinearLayout>

View File

@@ -47,22 +47,16 @@
<!-- 人体模型区域 --> <!-- 人体模型区域 -->
<RelativeLayout <RelativeLayout
android:id="@+id/bodyLayout" android:id="@+id/bodyLayout"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1" android:layout_weight="1"
android:layout_margin="16dp" android:layout_margin="16dp">
android:padding="16dp">
<!-- 虚线边框 android:background="@drawable/bg_scan_frame"-->
<View
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- 人体模型图片 --> <!-- 人体模型图片 -->
<ImageView <ImageView
android:id="@+id/ivHumanBody" android:id="@+id/ivHumanBody"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="439dp"
android:scaleType="fitCenter" android:scaleType="fitCenter"
android:src="@drawable/human_body" /> android:src="@drawable/human_body" />
@@ -72,7 +66,6 @@
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="100dp" android:layout_height="100dp"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:scaleType="fitCenter" android:scaleType="fitCenter"
android:src="@drawable/dajitubiao" /> android:src="@drawable/dajitubiao" />
@@ -84,7 +77,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="40dp" android:layout_height="40dp"
android:layout_margin="16dp" android:layout_margin="16dp"
android:layout_marginBottom="40dp"
android:background="@drawable/rounded_blue_bg" android:background="@drawable/rounded_blue_bg"
android:text="@string/submit" android:text="@string/submit"
android:textColor="@color/white" android:textColor="@color/white"

View File

@@ -427,4 +427,11 @@
<string name="no_share_app">暂无可用分享应用</string> <string name="no_share_app">暂无可用分享应用</string>
<string name="device_no">设备编号</string> <string name="device_no">设备编号</string>
<string name="fw_version">固件版本</string> <string name="fw_version">固件版本</string>
<!-- WiFi管理 -->
<string name="not_set">未设置</string>
<string name="device_id_empty">设备ID为空</string>
<string name="modify_success">修改成功</string>
<string name="modify_failed">修改失败</string>
<string name="network_request_failed">网络请求失败</string>
</resources> </resources>

View File

@@ -432,4 +432,11 @@
<string name="invite_friend">Come and join me! Scan the QR code to download the app</string> <string name="invite_friend">Come and join me! Scan the QR code to download the app</string>
<string name="share_to">Share to</string> <string name="share_to">Share to</string>
<string name="no_share_app">No sharing application available</string> <string name="no_share_app">No sharing application available</string>
<!-- WiFi Management -->
<string name="not_set">Not set</string>
<string name="device_id_empty">Device ID is empty</string>
<string name="modify_success">Modified successfully</string>
<string name="modify_failed">Modification failed</string>
<string name="network_request_failed">Network request failed</string>
</resources> </resources>