How to get wifi scan result on android marshmallow? -
i want wifi scan result on marshmallow device.
first, area have many wifi ap.
if wifi scan result on marshmallow device. add access_coarse_location
permission.
my source
@override public void onclick(view v) { if (build.version.sdk_int == build.version_codes.m) { int permissionresult = checkselfpermission(manifest.permission.access_coarse_location); if (permissionresult == packagemanager.permission_denied) { if (shouldshowrequestpermissionrationale(manifest.permission.access_coarse_location)) { alertdialog.builder dialog = new alertdialog.builder(mainactivity.this); dialog.settitle("need permission") .setmessage("permission grant?") .setpositivebutton("yes", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { if (build.version.sdk_int >= build.version_codes.m) { requestpermissions(new string[]{manifest.permission.access_coarse_location}, 1000); } } }).setnegativebutton("no", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(mainactivity.this, "cancel", toast.length_long).show(); } }).create().show(); } else { requestpermissions(new string[]{manifest.permission.access_coarse_location}, 1000); } } else { result = getscanwifiresult(); } } } @override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { if (requestcode == 1000) { if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) { if (activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { log.d(tag, "request permission"); result = mutils.getscanwifiresult(); } } else { toast.maketext(mainactivity.this, "permission denied", toast.length_long).show(); } } } public list<string> getscanwifiresult() { list<scanresult> result; mwifimanager.startscan(); // service register result = mwifimanager.getscanresults(); arraylist<string> ssid = new arraylist<string>(); if (result != null) { (scanresult scan : result) { ssid.add(scan.ssid); } } return ssid; }
with source on marshmallow device or gradle targetsdkversion
above 23. wifi scan result null, gradle targetsdkversion below 21 or kitkat device.
wifi scan result many wifi ap.
why wifi scan result null on marshmallow device?
because calling inside else
statement of if(permission == denied)
your result = getscanwifiresult()
should outside permissions if-else statement.
Comments
Post a Comment