2014-06-05 249 views
0

我正在嘗試爲我的項目製作無線掃描儀,當我點擊掃描按鈕時掃描20次。當我運行代碼掃描,但我不知道它掃描20次或沒有,RSSI值/ wifi級別不斷變化。在日誌中還說,你需要取消註冊回收。掃描20次的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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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

您的無線掃描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()

+0

我只想掃描20次,得到Wifi信號的平均RSSI值。除了在onClick – user3659612

+0

上移動wifi.startScan(),我還應該做些什麼其他修正?你能告訴我如何在主要活動中初始化一個數組嗎?之後,我想我可以弄清楚。 – user3659612

+0

它的方式不正確,因爲每次掃描結束並調用onReceive()時,你會運行一個循環20次,所以這會發生像20 * 20次也許,checksoon我會編輯我的答案 – Yazan

0

更新:您可以使用Android設備,在那裏你可以看看我的演示的RF測量發現了幾種方法,在這裏:

https://github.com/panosvas/Measurements

您可以在其中找到一個接一個地執行WiFi測量。我還創建了一個服務器,用於存儲這些測量以及使用UDP數據包,您可以在這裏找到一個遠程觸發應用:

https://github.com/panosvas/IndoorPositioningServer

有可能獲得多次測量。您的方法是錯誤的,因爲for循環可能會以毫秒執行,Android中的每個wifi測量都會根據設備保持大約5秒。實現你的目標的一種方法如下:

  • 創建一個非同步任務或調用startScan()
  • 保持一個計數器和每個onRecieve觸發
  • 如果計數器時間更新任務沒有達到上限,則onRecieve再次調用任務
  • 內如果計數器達到極限,則onRecieve裏面你不必再調用任務

請記住,爲了要獲得正確的測量結果,您必須等待每個測量結束,否則您將採取相同測量的重複。