我正在尝试为我的项目制作wifi扫描程序,当我按下扫描按钮时会进行20次扫描。当我运行代码扫描,但我不知道它扫描20次,RSSI值/ wifi级别不断变化。在日志中它还表示您需要取消注册记录。
我的MainActivity.java
package com.example.scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
WifiManager wifi;
WifiScanReceiver wifireciever;
WifiInfo info;
Button scan, save;
List<ScanResult> wifilist;
ListView list;
String wifis[];
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
list=(ListView)findViewById(R.id.listView1);
scan=(Button)findViewById(R.id.button1);
save=(Button)findViewById(R.id.button2);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()==false){
wifi.setWifiEnabled(true);
}
wifireciever = new WifiScanReceiver();
registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savedata();
}
});
}
protected void savedata() {
// TODO Auto-generated method stub
try {
File sdcard = Environment.getExternalStorageDirectory();
File directory = new File(sdcard.getAbsolutePath() + "/WIFI_RESULT");
directory.mkdirs();
name = new SimpleDateFormat("yyyy-MM-dd HH mm ss").format(new Date());
File file = new File(directory,name + "wifi_data.txt");
FileOutputStream fou = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fou);
try {
for (int i =0; i < list.getCount(); i++){
osw.append(list.getItemAtPosition(i).toString());
}
osw.flush();
osw.close();
Toast.makeText(getBaseContext(), "Saved", Toast.LENGTH_LONG).show();
} catch (IOException e){
e.printStackTrace();
}
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
public class scan_data {
String ssid;
String bssid;
int lvl;
int count = 0;
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
scan_data[] data = new scan_data[100];
for (int i=0;i<100;i++){
scan_data sdata = new scan_data();
data[i]=sdata;
}
int a =0;
for (int i=0;i<20;i++){
wifi.startScan();
List<ScanResult> wifilist = wifi.getScanResults();
if (a<wifilist.size()){
a=wifilist.size();
}
if (i==0){
for(int j=0;j<wifilist.size();j++){
data[j].ssid=wifilist.get(j).SSID.toString();
data[j].bssid=wifilist.get(j).BSSID.toString();
data[j].lvl=wifilist.get(j).level;
data[j].count++;
}
}
else if (i==19){
for (int j=0;j<wifilist.size();j++){
if(data[j].bssid.equals(wifilist.get(j).BSSID)){
data[j].lvl=data[j].lvl + wifilist.get(j).level;
data[j].count++;
}
data[j].lvl=data[j].lvl/data[j].count;
}
}
else {
for (int j=0;j<wifilist.size();j++){
if(data[j].bssid.equals(wifilist.get(j).BSSID)){
data[j].lvl=data[j].lvl + wifilist.get(j).level;
data[j].count++;
}
}
}
}
wifis = new String[a];
for (int i =0; i<a; i++){
wifis[i] = ("\n" + data[i].ssid + "\n AP Address" + data[i].bssid + "\n Signal Strength:" + data[i].lvl).toString();
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,wifis));
}
}
protected void onPause() {
unregisterReceiver(wifireciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifireciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
并且日志猫是:
06-05 17:42:43.262: D/dalvikvm(32156): GC_FOR_ALLOC freed 117K, 11% free 6899K/7687K, paused 35ms, total 35ms
06-05 17:42:43.312: I/dalvikvm-heap(32156): Grow heap (frag case) to 13.478MB for 6400016-byte allocation
06-05 17:42:43.412: D/dalvikvm(32156): GC_CONCURRENT freed 1K, 6% free 13148K/13959K, paused 33ms+2ms, total 103ms
06-05 17:42:43.412: D/dalvikvm(32156): WAIT_FOR_CONCURRENT_GC blocked 8ms
06-05 17:42:43.532: D/AbsListView(32156): Get MotionRecognitionManager
06-05 17:42:43.642: D/libEGL(32156): loaded /system/lib/egl/libGLES_rhea.so
06-05 17:42:43.662: D/BRCM_EGL(32156): eglCreateContext() config: 19 context: 0x4a12ca38, VC context 1, Thread 32156
06-05 17:42:43.662: D/BRCM_EGL(32156): eglCreateWindowSurface() surface: 0x4a240960, VC surface: 1, Thread: 32156
06-05 17:42:43.662: D/BRCM_EGL(32156): eglMakeCurrent(0x4a12ca38, 0x4a240960, 0x4a240960) Thread: 32156
06-05 17:42:43.672: D/OpenGLRenderer(32156): Enabling debug mode 0
06-05 17:43:39.937: D/BRCM_EGL(32156): eglMakeCurrent(NULL) Thread: 32156
06-05 17:43:39.937: D/BRCM_EGL(32156): eglDestroySurface() surface: 0x4a240960, android window 0x4908d370, Thread: 32156
06-05 17:43:40.457: D/BRCM_EGL(32156): eglMakeCurrent(0x4a12ca38, 0x4a2cbf60, 0x4a2cbf60) Thread: 32156
06-05 17:43:40.478: D/BRCM_EGL(32156): eglMakeCurrent(NULL) Thread: 32156
06-05 17:43:40.488: D/BRCM_EGL(32156): eglMakeCurrent(0x4a12ca38, 0x4a2cbf60, 0x4a2cbf60) Thread: 32156
06-05 17:43:40.488: D/BRCM_EGL(32156): eglMakeCurrent(NULL) Thread: 32156
06-05 17:43:40.508: E/ActivityThread(32156): Activity com.example.scanner.MainActivity has leaked IntentReceiver com.example.scanner.MainActivity$WifiScanReceiver@41640c78 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.508: E/ActivityThread(32156): android.app.IntentReceiverLeaked: Activity com.example.scanner.MainActivity has leaked IntentReceiver com.example.scanner.MainActivity$WifiScanReceiver@41640c78 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1314)
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1301)
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1295)
06-05 17:43:40.508: E/ActivityThread(32156): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:394)
06-05 17:43:40.508: E/ActivityThread(32156): at com.example.scanner.MainActivity$1.onClick(MainActivity.java:64)
06-05 17:43:40.508: E/ActivityThread(32156): at android.view.View.performClick(View.java:4262)
06-05 17:43:40.508: E/ActivityThread(32156): at android.view.View$PerformClick.run(View.java:17421)
06-05 17:43:40.508: E/ActivityThread(32156): at android.os.Handler.handleCallback(Handler.java:615)
06-05 17:43:40.508: E/ActivityThread(32156): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:43:40.508: E/ActivityThread(32156): at android.os.Looper.loop(Looper.java:137)
06-05 17:43:40.508: E/ActivityThread(32156): at android.app.ActivityThread.main(ActivityThread.java:4947)
06-05 17:43:40.508: E/ActivityThread(32156): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:43:40.508: E/ActivityThread(32156): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:43:40.508: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-05 17:43:40.508: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-05 17:43:40.508: E/ActivityThread(32156): at dalvik.system.NativeStart.main(Native Method)
06-05 17:43:40.518: E/ActivityThread(32156): Activity com.example.scanner.MainActivity has leaked IntentReceiver com.example.scanner.MainActivity$WifiScanReceiver@4167dca8 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.518: E/ActivityThread(32156): android.app.IntentReceiverLeaked: Activity com.example.scanner.MainActivity has leaked IntentReceiver com.example.scanner.MainActivity$WifiScanReceiver@4167dca8 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1314)
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1301)
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1295)
06-05 17:43:40.518: E/ActivityThread(32156): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:394)
06-05 17:43:40.518: E/ActivityThread(32156): at com.example.scanner.MainActivity$1.onClick(MainActivity.java:64)
06-05 17:43:40.518: E/ActivityThread(32156): at android.view.View.performClick(View.java:4262)
06-05 17:43:40.518: E/ActivityThread(32156): at android.view.View$PerformClick.run(View.java:17421)
06-05 17:43:40.518: E/ActivityThread(32156): at android.os.Handler.handleCallback(Handler.java:615)
06-05 17:43:40.518: E/ActivityThread(32156): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:43:40.518: E/ActivityThread(32156): at android.os.Looper.loop(Looper.java:137)
06-05 17:43:40.518: E/ActivityThread(32156): at android.app.ActivityThread.main(ActivityThread.java:4947)
06-05 17:43:40.518: E/ActivityThread(32156): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:43:40.518: E/ActivityThread(32156): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:43:40.518: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-05 17:43:40.518: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-05 17:43:40.518: E/ActivityThread(32156): at dalvik.system.NativeStart.main(Native Method)
06-05 17:43:40.528: E/ActivityThread(32156): Activity com.example.scanner.MainActivity has leaked IntentReceiver com.example.scanner.MainActivity$WifiScanReceiver@416416e0 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.528: E/ActivityThread(32156): android.app.IntentReceiverLeaked: Activity com.example.scanner.MainActivity has leaked IntentReceiver com.example.scanner.MainActivity$WifiScanReceiver@416416e0 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1314)
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1301)
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1295)
06-05 17:43:40.528: E/ActivityThread(32156): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:394)
06-05 17:43:40.528: E/ActivityThread(32156): at com.example.scanner.MainActivity$1.onClick(MainActivity.java:64)
06-05 17:43:40.528: E/ActivityThread(32156): at android.view.View.performClick(View.java:4262)
06-05 17:43:40.528: E/ActivityThread(32156): at android.view.View$PerformClick.run(View.java:17421)
06-05 17:43:40.528: E/ActivityThread(32156): at android.os.Handler.handleCallback(Handler.java:615)
06-05 17:43:40.528: E/ActivityThread(32156): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:43:40.528: E/ActivityThread(32156): at android.os.Looper.loop(Looper.java:137)
06-05 17:43:40.528: E/ActivityThread(32156): at android.app.ActivityThread.main(ActivityThread.java:4947)
06-05 17:43:40.528: E/ActivityThread(32156): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:43:40.528: E/ActivityThread(32156): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:43:40.528: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-05 17:43:40.528: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-05 17:43:40.528: E/ActivityThread(32156): at dalvik.system.NativeStart.main(Native Method)
06-05 17:43:40.538: E/ActivityThread(32156): Activity com.example.scanner.MainActivity has leaked IntentReceiver com.example.scanner.MainActivity$WifiScanReceiver@41641368 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.538: E/ActivityThread(32156): android.app.IntentReceiverLeaked: Activity com.example.scanner.MainActivity has leaked IntentReceiver com.example.scanner.MainActivity$WifiScanReceiver@41641368 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1314)
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1301)
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1295)
06-05 17:43:40.538: E/ActivityThread(32156): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:394)
06-05 17:43:40.538: E/ActivityThread(32156): at com.example.scanner.MainActivity$1.onClick(MainActivity.java:64)
06-05 17:43:40.538: E/ActivityThread(32156): at android.view.View.performClick(View.java:4262)
06-05 17:43:40.538: E/ActivityThread(32156): at android.view.View$PerformClick.run(View.java:17421)
06-05 17:43:40.538: E/ActivityThread(32156): at android.os.Handler.handleCallback(Handler.java:615)
06-05 17:43:40.538: E/ActivityThread(32156): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:43:40.538: E/ActivityThread(32156): at android.os.Looper.loop(Looper.java:137)
06-05 17:43:40.538: E/ActivityThread(32156): at android.app.ActivityThread.main(ActivityThread.java:4947)
06-05 17:43:40.538: E/ActivityThread(32156): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:43:40.538: E/ActivityThread(32156): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:43:40.538: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-05 17:43:40.538: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-05 17:43:40.538: E/ActivityThread(32156): at dalvik.system.NativeStart.main(Native Method)
06-05 17:43:40.548: E/ActivityThread(32156): Activity com.example.scanner.MainActivity has leaked IntentReceiver com.example.scanner.MainActivity$WifiScanReceiver@41640ff0 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.548: E/ActivityThread(32156): android.app.IntentReceiverLeaked: Activity com.example.scanner.MainActivity has leaked IntentReceiver com.example.scanner.MainActivity$WifiScanReceiver@41640ff0 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:793)
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1314)
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1301)
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1295)
06-05 17:43:40.548: E/ActivityThread(32156): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:394)
06-05 17:43:40.548: E/ActivityThread(32156): at com.example.scanner.MainActivity$1.onClick(MainActivity.java:64)
06-05 17:43:40.548: E/ActivityThread(32156): at android.view.View.performClick(View.java:4262)
06-05 17:43:40.548: E/ActivityThread(32156): at android.view.View$PerformClick.run(View.java:17421)
06-05 17:43:40.548: E/ActivityThread(32156): at android.os.Handler.handleCallback(Handler.java:615)
06-05 17:43:40.548: E/ActivityThread(32156): at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:43:40.548: E/ActivityThread(32156): at android.os.Looper.loop(Looper.java:137)
06-05 17:43:40.548: E/ActivityThread(32156): at android.app.ActivityThread.main(ActivityThread.java:4947)
06-05 17:43:40.548: E/ActivityThread(32156): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:43:40.548: E/ActivityThread(32156): at java.lang.reflect.Method.invoke(Method.java:511)
06-05 17:43:40.548: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-05 17:43:40.548: E/ActivityThread(32156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-05 17:43:40.548: E/ActivityThread(32156): at dalvik.system.NativeStart.main(Native Method)
我知道我错过了unregistorReciever的事情,但我应该在哪里添加
答案 0 :(得分:0)
你的wifi扫描20次,显然是因为这个
for (int i=0;i<20;i++){
wifi.startScan();
你在for循环中开始扫描20次,为什么?
关于取消注册接收者,
它可以放在onDestroy()
还有一个注意事项,
行wifi.startScan();
不应放在onReceive()
内
它应该放在扫描按钮的onClick()上
将调用onReceive()
,以便您可以获取数据并保存
因此,如果您调用startScan,它将再次触发扫描
并且您使用的硬编码太多,100和20这些是什么?
编辑:
scan_data[] data = null;
int TOTAL_SCANS = 0;
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifilist = wifi.getScanResults();
data = new scan_data[wifilist .size()];
int a =0;
// for (int i=0;i<20;i++){ //DELETE THE LOOP
if (a<wifilist.size()){
a=wifilist.size();
}
for(int r=0;r<wifilist.size();r++){
data[r] = new scan_date();
data[r].ssd=wifilist.get(r).ssd();//check syntax here ... just sample
}
//at end of onReceive()
if(TOTAL_SCANS < 20){
++TOTAL_SCANS;
wifi.startScan();
}else{
System.out.println(" Scan Completed [" + TOTAL_SCANS + "] times.");
//you can unregisterReceiver here too
}
:
:
检查this sample,只需在if(TOTAL_SCANS<20)
onReceive()
答案 1 :(得分:0)
更新:您可以使用Android设备查看我的演示进行RF测量,您可以在此处找到几种方法:
https://github.com/panosvas/Measurements
您可以在其中一个接一个地找到WiFi测量的实现。我还创建了一个用于存储这些测量的服务器以及一个使用UDP数据包的远程触发器应用程序,您可以在此处找到:
https://github.com/panosvas/IndoorPositioningServer
可以获得多次测量。您的方法是错误的,因为for循环可能会在几秒钟内执行,而Android中的每个wifi测量都会根据设备保持大约5秒。实现目标的一种方法如下:
请记住,为了获得正确的测量结果,您必须等到每次测量完成,否则您将重复进行相同的测量。