告知:devifがWi-Fi共有モードになっているときに他のSSIDのリストを取得できますか?デバイスがWi-Fi共有モードになっているときに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
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
実行あなたは、あなたのアプリがonResume
を呼び出し、それがを呼び出したときに登録解除するときprefereably BroadcastReceiverを登録する必要があり
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();
}
}
利用可能なすべての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)
{ }
}
}
関連する問題