1.引包


   
   
  1. implementation 'com.thanosfisherman.wifiutils:wifiutils:1.6.4'
  2. implementation 'com.thanosfisherman.elvis:elvis:3.0'

2.使用

 WifiDialog(act).Builder().create().show()
   
   

3.wifiDialog


   
   
  1. import android.annotation.SuppressLint
  2. import android.app.AlertDialog
  3. import android.app.Dialog
  4. import android. content.Context
  5. import android.net.wifi.ScanResult
  6. import android.util.TypedValue
  7. import android.view.LayoutInflater
  8. import android.view.View
  9. import android.view.ViewGroup
  10. import android.widget.EditText
  11. import android.widget.LinearLayout
  12. import android.widget.Toast
  13. import androidx.appcompat.app.AppCompatActivity
  14. import androidx.recyclerview.widget.LinearLayoutManager
  15. import androidx.recyclerview.widget.RecyclerView
  16. import com.chad.library.adapter.base.BaseQuickAdapter
  17. import com.chad.library.adapter.base.viewholder.BaseViewHolder
  18. import com.kongzue.dialog.v 3.WaitDialog
  19. import com.snjk.recyclebox.R
  20. import com.snjk.recyclebox.offline.utils.dp 2pxI
  21. import com.snjk.recyclebox.offline.utils.toast
  22. import com.thanosfisherman.wifiutils.WifiUtils
  23. import com.thanosfisherman.wifiutils.wifiConnect.ConnectionErrorCode
  24. import com.thanosfisherman.wifiutils.wifiConnect.ConnectionSuccessListener
  25. import io.reactivex.Observable
  26. import io.reactivex.disposables.Disposable
  27. import io.reactivex.schedulers.Schedulers
  28. import java.util.concurrent.TimeUnit
  29. class WifiDialog(val activity: AppCompatActivity) : Dialog(activity) {
  30. inner class Builder {
  31. private lateinit var dialog: WifiDialog
  32. private lateinit var view: View
  33. private var wifiAdapter = WifiAdapter()
  34. private val wifiUtils = WifiUtils.withContext(context)
  35. private var disposable: Disposable? = null
  36. @SuppressLint( "InflateParams")
  37. private fun initView() {
  38. val inflater =
  39. context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
  40. view = inflater.inflate(R.layout.dialog_wifi, null)
  41. dialog = WifiDialog(activity)
  42. dialog.addContentView(
  43. view,
  44. ViewGroup.LayoutParams(
  45. LinearLayout.LayoutParams.WRAP_ CONTENT,
  46. 300.dp 2pxI(activity)
  47. )
  48. )
  49. dialog.setCancelable( true)
  50. dialog.setCanceledOnTouchOutside( true)
  51. }
  52. fun create(): Builder {
  53. initView()
  54. wifiAdapter.setOnItemClickListener { _, _, position - >
  55. shouPwdView(wifiAdapter.getItem(position))
  56. }
  57. view.findViewById <RecyclerView >(R.id.wifiRcv).apply {
  58. adapter = wifiAdapter
  59. layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
  60. }
  61. wifiUtils.enableWifi { if (!it) "wifi打开失败,请联系管理员".toast() }
  62. Observable.interval( 30, TimeUnit.SECONDS)
  63. .observeOn(Schedulers.io())
  64. .doOnSubscribe { d - > disposable = d }
  65. .doOnNext { scanWifi() }
  66. .subscribe()
  67. dialog.setOnDismissListener { disposable?.dispose() }
  68. scanWifi()
  69. return this
  70. }
  71. fun show() = dialog.show()
  72. private fun shouPwdView(sr: ScanResult) {
  73. val et = EditText(context)
  74. AlertDialog.Builder(context)
  75. .setTitle( "请输入密码")
  76. .setView(et)
  77. .setNegativeButton( "取消") { d, _ - > d.dismiss() }
  78. .setPositiveButton( "连接") { d, _ - >
  79. d.dismiss()
  80. showLoading()
  81. WifiUtils.withContext(context)
  82. .connectWith(sr.SSID, et.text.toString())
  83. .setTimeout( 10000)
  84. .onConnectionResult( object : ConnectionSuccessListener {
  85. override fun success() {
  86. "wifi连接成功".toast()
  87. hideLoading()
  88. dialog.dismiss()
  89. }
  90. override fun failed(errorCode: ConnectionErrorCode) {
  91. Toast.makeText(context, "EPIC FAIL!$errorCode", Toast. LENGTH_SHORT)
  92. .show()
  93. hideLoading()
  94. }
  95. })
  96. . start()
  97. }.show()
  98. }
  99. private fun scanWifi() {
  100. wifiUtils.scanWifi {
  101. if (it.isNullOrEmpty()) "附件没有可用的wifi".toast() else {
  102. wifiAdapter.setNewInstance(it)
  103. wifiAdapter.notifyDataSetChanged()
  104. / / / /以下为打印数据
  105. / / val sb = StringBuffer()
  106. / / it.map {
  107. / / sb.append( "设备名(SSID) ->${it.SSID}\t")
  108. / / .append( "信号强度 ->${it.level}\t")
  109. / / .append( "BSSID ->${it.BSSID}\t")
  110. / / .append( "level ->${it.level}\t")
  111. / / .append( "wifi -> ${it}")
  112. / / }
  113. / / Log.e( "TTT", sb.toString())
  114. }
  115. }. start()
  116. }
  117. private fun showLoading() {
  118. WaitDialog.show(activity, "wifi连接中...")
  119. .setOnBackClickListener { false }
  120. }
  121. private fun hideLoading() {
  122. WaitDialog.dismiss()
  123. }
  124. inner class WifiAdapter : BaseQuickAdapter <ScanResult, BaseViewHolder >(R.layout.item_wifi) {
  125. override fun convert(holder: BaseViewHolder, item: ScanResult) {
  126. holder.setImageResource(
  127. R.id.wifiIcon, when (item.level) {
  128. in 0 downTo - 50 - > R.drawable.icon_wifi_full
  129. in - 50 downTo - 70 - > R.drawable.icon_wifi_good
  130. in - 70 downTo - 90 - > R.drawable.icon_wifi_little
  131. else - > R.drawable.icon_wifi_weak
  132. }
  133. )
  134. .setText(R.id.wifiName, item.SSID)
  135. .setGone(R.id.wifiConn, !wifiUtils.isWifiConnected(item.SSID))
  136. .setGone(R.id.wifiToConn, wifiUtils.isWifiConnected(item.SSID))
  137. }
  138. }
  139. }
  140. }

4.wifiDialog布局


   
   
  1. <?xml version = "1.0" encoding = "utf-8"? >
  2. <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  3. xmlns:app = "http://schemas.android.com/apk/res-auto"
  4. xmlns:tools = "http://schemas.android.com/tools"
  5. android:layout_width = "260dp"
  6. android:layout_height = "320dp"
  7. android:layout_gravity = "center"
  8. android:background = "@drawable/ic_wifi_dialog"
  9. android:orientation = "vertical" >
  10. <TextView
  11. android:layout_width = "match_parent"
  12. android:layout_height = "46dp"
  13. android:gravity = "center"
  14. android:text = "wifi列表"
  15. android:textSize = "14sp" / >
  16. <View
  17. android:layout_width = "match_parent"
  18. android:layout_height = "1px"
  19. android:background = "#cfcfcf" / >
  20. <androidx.recyclerview.widget.RecyclerView
  21. android:id = "@+id/wifiRcv"
  22. android:layout_width = "match_parent"
  23. android:layout_height = "match_parent"
  24. app:layoutManager = "androidx.recyclerview.widget.LinearLayoutManager"
  25. tools:listitem = "@layout/item_wifi" / >
  26. < /LinearLayout >

5.wifi布局


   
   
  1. <?xml version = "1.0" encoding = "utf-8"? >
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  3. xmlns:app = "http://schemas.android.com/apk/res-auto"
  4. xmlns:tools = "http://schemas.android.com/tools"
  5. android:layout_width = "match_parent"
  6. android:layout_height = "46dp"
  7. android:orientation = "vertical" >
  8. <TextView
  9. android:id = "@+id/wifiName"
  10. android:layout_width = "wrap_content"
  11. android:layout_height = "match_parent"
  12. android:layout_gravity = "center_vertical"
  13. android:layout_weight = "1"
  14. android:gravity = "center_vertical"
  15. android:paddingStart = "10dp"
  16. android:paddingEnd = "10dp"
  17. android:textSize = "14sp"
  18. app:layout_constraintLeft_toLeftOf = "parent"
  19. app:layout_constraintTop_toTopOf = "parent"
  20. tools:text = "snkj" / >
  21. <ImageView
  22. android:id = "@+id/wifiIcon"
  23. android:layout_width = "16dp"
  24. android:layout_height = "16dp"
  25. android:src = "@drawable/icon_wifi_full"
  26. app:layout_constraintBottom_toBottomOf = "parent"
  27. app:layout_constraintLeft_toRightOf = "@+id/wifiName"
  28. app:layout_constraintTop_toTopOf = "parent"
  29. tools:ignore = "ContentDescription" / >
  30. <View
  31. android:layout_width = "match_parent"
  32. android:layout_height = "1px"
  33. android:background = "#efefef"
  34. app:layout_constraintBottom_toBottomOf = "parent"
  35. app:layout_constraintLeft_toLeftOf = "parent"
  36. app:layout_constraintRight_toRightOf = "parent" / >
  37. <TextView
  38. android:id = "@+id/wifiConn"
  39. android:layout_width = "wrap_content"
  40. android:layout_height = "match_parent"
  41. android:gravity = "center_vertical"
  42. android:paddingStart = "10dp"
  43. android:paddingEnd = "10dp"
  44. android:text = "已连接"
  45. android:textColor = "#009900"
  46. android:textSize = "12sp"
  47. android:visibility = "gone"
  48. app:layout_constraintBottom_toBottomOf = "parent"
  49. app:layout_constraintRight_toRightOf = "parent"
  50. app:layout_constraintTop_toTopOf = "parent" / >
  51. <TextView
  52. android:id = "@+id/wifiToConn"
  53. android:layout_width = "wrap_content"
  54. android:layout_height = "match_parent"
  55. android:gravity = "center_vertical"
  56. android:paddingStart = "10dp"
  57. android:paddingEnd = "10dp"
  58. android:text = "连接"
  59. android:textColor = "#0000cc"
  60. android:textSize = "12sp"
  61. app:layout_constraintBottom_toBottomOf = "parent"
  62. app:layout_constraintRight_toRightOf = "parent"
  63. app:layout_constraintTop_toTopOf = "parent" / >
  64. < /androidx.constraintlayout.widget.ConstraintLayout >

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐