0

アプリケーションでの WiFiManger の使用について質問があります。コードは非常に単純です。ネットワークをスキャンして利用可能な WiFi を取得するこのサービスを作成しました。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    mapName = intent.getExtras().getString("mapName");
    sendNotification();
    mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    // Check for wifi is disabled
    if (!mainWifi.isWifiEnabled()) {
        // If wifi disabled then enable it
        Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled",
                Toast.LENGTH_LONG).show();

        mainWifi.setWifiEnabled(true);
    }
    mainWifi.startScan();
    List<ScanResult> scanResults=mainWifi.getScanResults();
    readAps=getAps(scanResults);
    //List of AP received in current position
    String result=compareRP();
    if(!result.isEmpty())
        Toast.makeText(this, "Localized in RP: "+result, Toast.LENGTH_LONG).show();
    else
        Toast.makeText(this, "Unable to localize you in this map", Toast.LENGTH_LONG).show();
    return START_NOT_STICKY;
}

private ArrayList<AccessPoint> getAps(List<ScanResult> scanResults) {
    ArrayList<AccessPoint> temp = new ArrayList<AccessPoint>();
    for(ScanResult s:scanResults){
        temp.add(new AccessPoint(s.SSID,s.level,s.frequency));
    }
    return temp;
}

この条件を追加するにはどうすればよいですか: 「新しい」WiFi リストを待ちたいのですが、それは次の場合です:

    mainwifi.startScan();

メソッドが戻ります。

現在、私のサービスは引き続きクラスター マッチング アルゴリズムを使用していますが、それは必要ありません。私がしなければならないことは何ですか?

助けてくれてありがとう!

フレッド

4

1 に答える 1

0

アクティビティ内で a を使用する必要がBroadcastReceiverあります。

private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
        // This condition is not necessary if you listen to only one action
        if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            List<ScanResult> mScanResults = mWifiManager.getScanResults();
            Toast.makeText(getApplicationContext(), "Scan results are available", Toast.LENGTH_LONG).show();
            // Do what you want
        }
    }
};

private WifiManager mWifiManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    // You can add multiple actions... 
    registerReceiver(mWifiScanReceiver, intentFilter);
    // ...
    mWifiManager.startScan();

}
于 2016-03-07T10:24:23.490 に答える