請求告訴我:當設備處於WiFi共享模式時,是否可以獲取其他SSID列表(我只需要名稱,不再有)?當設備處於WiFi共享模式時獲取SSID
0
A
回答
1
你需要註冊一個BroadcastReceiver爲您的活動,但你需要添加權限之前,訪問網絡狀態,並修改它在你的清單:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
我們創建BroadcastReceiver
之前,我們需要建立一個幾件事情:
ArrayList<String> ssidList;
WifiManager wifiManager;
WifiScanReceiver wifiReciever;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
//Wifi manager will be used to request a scan
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
//Your boradcast receiver, it will be registered with the system to be notified of SSID's available
wifiReciever = new WifiScanReceiver();
//A list to add all the available SSID's
ssidList = new ArrayList<String>();
//Requesting a scan, results will be returned through the BroadcastReceiver
wifiManager.startScan();
}
這是您的BoradcastReceiver
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wifiManager.getScanResults();
ssidList.clear(); // clear to make sure we do not get duplicated entries
for (int i = 0; i < wifiScanList.size(); i++) {
String ssid = wifiScanList.get(i).SSID; //Get the SSID
ssidList.add(ssid); //add SSID to the list
Log.d("SSID", ssid);
}
//You can call this to keep scaning if you need to, or you can set up a Timer to scan every X seconds
//wifiManager.startScan();
}
}
您需要prefereably註冊的BroadcastReceiver當你的應用程序調用onResume
和註銷時,它調用:
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
這是完整的代碼:
public class WifiActivity extends AppCompatActivity {
ArrayList<String> ssidList;
WifiManager wifiManager;
WifiScanReceiver wifiReciever;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Wifi manager will be used to request a scan
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
//Your boradcast receiver, it will be registered with the system to be notified of SSID's available
wifiReciever = new WifiScanReceiver();
//A list to add all the available SSID's
ssidList = new ArrayList<String>();
//Requesting a scan, results will be returned through the BroadcastReceiver
wifiManager.startScan();
}
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wifiManager.getScanResults();
ssidList.clear(); // clear to make sure we do not get duplicated entries
for (int i = 0; i < wifiScanList.size(); i++) {
String ssid = wifiScanList.get(i).SSID; //Get the SSID
ssidList.add(ssid); //add SSID to the list
Log.d("SSID", ssid);
}
//You can call this to keep scaning if you need to, or you can set up a Timer to scan every X seconds
//wifiManager.startScan();
}
}
}
每次onReceive
運行你會得到所有可用SSID的列表,他們將被保存在ssidList
。
0
試試這個answer。
public class WiFiDemo extends Activity implements OnClickListener
{
WifiManager wifi;
ListView lv;
TextView textStatus;
Button buttonScan;
int size = 0;
List<ScanResult> results;
String ITEM_KEY = "key";
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textStatus = (TextView) findViewById(R.id.textStatus);
buttonScan = (Button) findViewById(R.id.buttonScan);
buttonScan.setOnClickListener(this);
lv = (ListView)findViewById(R.id.list);
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false)
{
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
this.adapter = new SimpleAdapter(WiFiDemo.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value });
lv.setAdapter(this.adapter);
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context c, Intent intent)
{
results = wifi.getScanResults();
size = results.size();
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
public void onClick(View view)
{
arraylist.clear();
wifi.startScan();
Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
try
{
size = size - 1;
while (size >= 0)
{
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY, results.get(size).SSID + " " + results.get(size).capabilities);
arraylist.add(item);
size--;
adapter.notifyDataSetChanged();
}
}
catch (Exception e)
{ }
}
}
相關問題
- 1. WIFI連接時獲取SSID?
- 2. 獲取連接設備的SSID和IP地址(連接前需要獲得權限)到Android熱點(Wifi共享)
- 3. 在Android 2.2上:處於共享模式並在同一時間掃描wifi
- 4. 當Android設備處於wifi狀態時,SSLSocket掛在getInputStream中
- 5. 當設備進入睡眠模式時WiFi鎖不起作用
- 6. 當設備處於低功耗模式時,SystemClock.elapsedRealtime()漂移
- 7. 當設備處於橫向模式時,MPAndroidChart PieChartRenderer崩潰
- 8. 當連接到WiFi時獲取ios設備的IP地址
- 9. javascript可以獲取當前WiFi連接的SSID嗎?
- 10. pushViewController: - 當設備處於橫向模式時,如何以縱向模式顯示?
- 11. MonoTouch WIFI SSID
- 12. Android,在設備處於睡眠模式時獲取強度信號(PhoneStateListener)
- 13. 通過WiFi將圖像從Android設備共享到其他設備
- 14. 設備處於測試模式Admob
- 15. 如何從iOS設備訪問WiFi網絡共享
- 16. 在Android設備之間使用WiFi共享文件
- 17. 如何在Android KitKat設備上啓用wifi數據共享?
- 18. Android中的對等設備上的WiFi直接圖像共享
- 19. 獲取我設備的wifi mac地址?
- 20. 獲取附近所有WiFi-Direct設備
- 21. 在一個WiFi網絡上獲取多個Android設備以共享信息的最佳方式
- 22. URLConnection當設備處於睡眠模式不起作用
- 23. Android:如何獲取當前設備WiFi-direct名稱
- 24. 設置wifi ssid與其中的空間
- 25. ALSA:在共享模式下打開PCM設備
- 26. 用於同步共享域模型的設計模式
- 27. 獲取WiFi啓用設備的Wifi可用性通知
- 28. 當設備處於睡眠模式時,AlarmManager.setInexactRepeating是否可以廣播意圖?
- 29. 當Android設備處於飛行模式時,打開位置提示失敗
- 30. 如何獲取局域網共享設備和文件?