我需要通過掃描最強的可用Wifi來啓動活動來查找我在建築物中的當前位置。我想在該建築物中只找到3個地方。因此,如果我目前在這些位置附近,我會點擊一個按鈕掃描最強的可用Wifi以啓動一項活動。掃描可用wifi,檢測特定最強的Wifi SSID以啓動XML活動佈局
- 我需要掃描建築物中可用的Wifi。
- 獲取3個最強信號。
- 然後檢測這3個最強信號的SSID中的任何一個是否與我想要定位的特定SSID相同。
- 如果我的SSID是最強的3個信號之一,那麼應用程序將啓動一個XML佈局活動,
到目前爲止,我已經有編碼掃描最強可用的WiFi。但是我不知道如何在最強的特定Wifi SSID檢測到時啓動一項活動。我不需要有關Wifi的信息,只需啓動我的信息的活動佈局即可。
目前我使用下面的代碼位掃描,我從here得到了最強的WIFI信號:
WiFiDemo.java
public class WiFiDemo extends Activity implements OnClickListener {
private static final String TAG = "WiFiDemo";
WifiManager wifi;
BroadcastReceiver receiver;
TextView textStatus;
Button buttonScan;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup UI
textStatus = (TextView) findViewById(R.id.textStatus);
buttonScan = (Button) findViewById(R.id.buttonScan);
buttonScan.setOnClickListener(this);
// Setup WiFi
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// Get WiFi status
WifiInfo info = wifi.getConnectionInfo();
textStatus.append("\n\nWiFi Status: " + info.toString());
// List available networks
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
textStatus.append("\n\n" + config.toString());
}
// Register Broadcast Receiver
if (receiver == null)
receiver = new WiFiScanReceiver(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d(TAG, "onCreate()");
}
@Override
public void onStop() {
unregisterReceiver(receiver);
}
public void onClick(View view) {
Toast.makeText(this, "On Click Clicked. Toast to that!!!",
Toast.LENGTH_LONG).show();
if (view.getId() == R.id.buttonScan) {
Log.d(TAG, "onClick() wifi.startScan()");
wifi.startScan();
}
} }
WiFiScanReceiver.java
public class WiFiScanReceiver extends BroadcastReceiver {
private static final String TAG = "WiFiScanReceiver";
WiFiDemo wifiDemo;
public WiFiScanReceiver(WiFiDemo wifiDemo) {
super();
this.wifiDemo = wifiDemo;
}
@Override
public void onReceive(Context c, Intent intent) {
List<ScanResult> results = wifiDemo.wifi.getScanResults();
ScanResult bestSignal = null;
for (ScanResult result : results) {
if (bestSignal == null
|| WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
bestSignal = result;
}
String message = String.format("%s networks found. %s is the strongest.",
results.size(), bestSignal.SSID);
Toast.makeText(wifiDemo, message, Toast.LENGTH_LONG).show();
Log.d(TAG, "onReceive() message: " + message);
}
}
是不是最強的可用wifi檢測到相同的最強WiFi ssid檢測???換句話說,你的解決方案不是正確的嗎? –
是的。但我想檢測我目前是否在特定的無線接入點附近找到我的位置。所以我需要檢測最強的wifi SSID是否與我自己的特定wifi SSID相同。 – fadzlinaz