Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – wifiManager.startScan() returns false

Posted on November 11, 2022 By

Solution 1 :

In app permissions denied, You try accept location permission.

Problem :

Don’t know why my code copied one to one from from android.developer.com doesn’t work.
While getting result of receivingList of avaiable Wifi I got toast communitating error from scanFailure() function. My Api level requires permissions which I declared so I meet all the requirements.

AddNewDeviceFragment

class AddNewDeviceFragment : Fragment() {

    private lateinit var wifiManager: WifiManager
    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)
            })
        swipeRefreshLayout.setOnRefreshListener { startScan() }
    }

    fun startScan() {
        swipeRefreshLayout.isRefreshing = true

        wifiManager = context?.getSystemService(Context.WIFI_SERVICE) as WifiManager


        if (!wifiManager.isWifiEnabled) {
            Toast.makeText(activity, "Wifi is disable... We need to enable it", Toast.LENGTH_LONG).show()
            wifiManager.setWifiEnabled(true)
        }

        val wifiScanReceiver = object : BroadcastReceiver() {

            override fun onReceive(context: Context, intent: Intent) {
                val success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false)
                if (success) {
                    scanSuccess()
                } else {
                    scanFailure()
                }
            }
        }

        val intentFilter = IntentFilter()
        intentFilter.addAction("android.net.wifi.SCAN_RESULTS")
        context?.registerReceiver(wifiScanReceiver, intentFilter)

        val success = wifiManager.startScan()
        if (!success) {
            // scan failure handling
            scanFailure()
        }
        swipeRefreshLayout.isRefreshing = false
    }

    private fun scanSuccess() {
        model.clearAllData()

        val results = wifiManager.scanResults
        for (result in results) {
            model.addNewDevice(NewDevice(result.SSID.toString()))
        }
    }

    private fun scanFailure() {
        Toast.makeText(activity,"Error while scanning WiFi",Toast.LENGTH_LONG).show()
    }
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns_android="http://schemas.android.com/apk/res/android"
        xmlns_tools="http://schemas.android.com/tools"
        package="com.example.lightmeup">

    <uses-permission android_name="android.permission.INTERNET" />
    <uses-permission android_name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android_name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
    <uses-permission android_name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android_name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android_name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android_name="android.permission.CHANGE_WIFI_STATE"/>

    <application
            android_allowBackup="true"
            android_icon="@mipmap/ic_launcher"
            android_label="@string/app_name"
            android_roundIcon="@mipmap/ic_launcher"
            android_supportsRtl="true"
            android_theme="@style/AppTheme"
            tools_ignore="GoogleAppIndexingWarning">
        <activity android_name=".MainActivity"
        android_theme="@style/AppTheme.NoActionBar"
        android_screenOrientation="portrait">

        </activity>
        <activity
                android_name=".LoginActivity"
                android_screenOrientation="portrait">
            <intent-filter>
                <action android_name="android.intent.action.MAIN"/>

                <category android_name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Comments

Comment posted by developer.android.com/guide/topics/connectivity/…

check you have location enabled or not from Settings and also permissions are granted. In the link you have shared, also read in same article about this

READ  [FIXED] android - Error none of following function Intent in cardview
Powered by Inline Related Posts

Comment posted by Paweł Bocian

I checked. Location is enabled and permissions are granted :/

Comment posted by Waqar UlHaq

can you see any logs Logcat?

Android Tags:android, android-wifi, kotlin, wifi, wifimanager

Post navigation

Previous Post: [FIXED] java – MVVM with Room and LiveData – How to get List from LiveData
Next Post: [FIXED] android – Xamarin.Forms.Droid Resource, Ambiguous Terms/ Build Action’s Randomly Changing Missing Resources

Related Posts

[FIXED] android – How to create Binding Adapter for material.Slider view? Android
[FIXED] android – backstack does not clear while navigating from fragment to activity Android
[FIXED] android – How to trust all my self-signed certificates in my app Android
[FIXED] Android – How to generate onClick code for ScrollView items? Android
[FIXED] android – Create array of object in retrofit2 using put request Android
[FIXED] android – Using DrawerLayout with BottomSheetBehavior Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme