我在扫描wifi网络
WifiWizard2.scan().then(function (results) { console.log("SCANRESULTS", results); }, function () { console.log("TOUGH LUCK"); });
但每次我得到一个空数组扫描结果[]我怎么做错了我可以确定我周围至少有20个网络。我用的是android 7设备,如果有区别的话。
k4emjkb11#
高等级
public class WifiFunction { private final String tag = WifiFunction.class.getSimpleName(); private WifiManager wifiManager; public List<WifiDetail> getListofWifi(Context context) { List<WifiDetail> wifiDetails = new ArrayList<>(); List<ScanResult> results = wifiManager.getScanResults(); Log.d(tag,"Wifi Details " + wifiManager.getScanResults().size()); for (ScanResult result : results) { wifiDetails.add(new WifiDetail(result.BSSID, result.SSID)); Log.d(tag, result.BSSID + result.SSID); } return wifiDetails; } public void startScan(Context context) { wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); wifiManager.startScan(); IntentFilter filter = new IntentFilter(); filter.addAction(SCAN_RESULTS_AVAILABLE_ACTION); context.registerReceiver(new resultReciever(this),filter); } }
接收器类
public class resultReciever extends BroadcastReceiver { private WifiFunction wifiFunction; resultReciever(WifiFunction wifiFunction) { this.wifiFunction = wifiFunction; } @Override public void onReceive(Context context, Intent intent) { Log.d("Receiver","started"); wifiFunction.getListofWifi(context); } }
主要活动我只是打电话:
(new WifiFunction()).startScan(this);
清单
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permisiion.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permisiion.ACCESS_FINE_LOCATION" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".resultReciever"/> </application>
用户权限:
private boolean checkPermission() { List<String> permissionsList = new ArrayList<String>(); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) { permissionsList.add(Manifest.permission.ACCESS_WIFI_STATE); } if (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) { permissionsList.add(Manifest.permission.CHANGE_WIFI_STATE); } if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { permissionsList.add(Manifest.permission.ACCESS_COARSE_LOCATION); } if (permissionsList.size() > 0) { ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]), 1); return false; } return true; } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 1: (new WifiFunction()).startScan(this); break; } }
这应该工作。尝试了完整的代码。
fxnxkyjh2#
在后来的Android版本中,你必须启用GPS。我通常会关闭我的GPS,希望能防止无良应用程序的跟踪。只有当我在移动的手机上打开GPS时,类似于上面的代码才对我有用。
2条答案
按热度按时间k4emjkb11#
高等级
接收器类
主要活动我只是打电话:
清单
用户权限:
这应该工作。尝试了完整的代码。
fxnxkyjh2#
在后来的Android版本中,你必须启用GPS。我通常会关闭我的GPS,希望能防止无良应用程序的跟踪。只有当我在移动的手机上打开GPS时,类似于上面的代码才对我有用。