Я хочу получить все доступные Wi-Fi в ResultList, используя метод переопределения broadcastReceiver onReceive (). Не знаю, почему я не могу go в свой метод переопределения (Не могу перехватить точку останова в println ("При получении активен"))
После этого я проверил resultList с помощью оператора "If", но это происходит, что resultList пуст. Что я делаю неправильно ?
Мой код ниже:
class AddNewDeviceFragment : Fragment() {
var resultList = ArrayList<ScanResult>()
private var adapter: NewDeviceAdapter = NewDeviceAdapter(arrayListOf())
private lateinit var model: NewDeviceViewModel
private lateinit var rootView: View
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
rootView = inflater.inflate(R.layout.fragment_addnewdevice, container, false)
return rootView
}
override fun onStart() {
super.onStart()
var newDevicesRecycledView = rootView.findViewById(R.id.newDevicesRecycledView) as RecyclerView
newDevicesRecycledView.layoutManager = LinearLayoutManager(activity)
newDevicesRecycledView.adapter = adapter
//ViewModel
model = ViewModelProviders.of(this).get(NewDeviceViewModel::class.java)
//LiveData
model.getNewDevices()
.observe(viewLifecycleOwner, Observer<MutableList<NewDevice>> { newDevices ->
adapter.addDevice(newDevices)
})
refreshButton.setOnClickListener { startScan() }
}
fun startScan() {
model.clearAllData()
val wifiManager = context?.getSystemService(Context.WIFI_SERVICE) as WifiManager
val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
resultList = wifiManager.scanResults as ArrayList<ScanResult>
println("On receive active")
for (result in resultList) {
model.addNewDevice(NewDevice(result.SSID.toString()))
}
}
}
if (!wifiManager.isWifiEnabled) {
Toast.makeText(activity, "Wifi is disable... We need to enable it", Toast.LENGTH_LONG).show()
wifiManager.setWifiEnabled(true)
}
context?.registerReceiver(broadcastReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
if(resultList.isEmpty())
Toast.makeText(activity,"ResultList empty",Toast.LENGTH_LONG).show()
wifiManager.startScan()
Toast.makeText(activity,"Scanning Wifi",Toast.LENGTH_LONG).show()
}
}