2017-04-27 6 views
0

wifi가 켜져 있지만 wmi가 켜져있을 때 wifi를 모두 검색하고 싶지만 Android 7.0에서는 작동하지 않습니다. 테스트 용으로이 휴대 전화 만 있습니다. (이건 안드로이드라고 생각합니다. 버전 문제).Wifi 스캐너가 android nougat (7.0)와 작동하지 않습니다.

문제를 어떻게 해결할 수 있습니까?

코드 :

import android.app.Activity; 
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.WifiManager; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 



public class DetectWifi extends Activity implements View.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.detectwifi_page); 

    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(DetectWifi.this, arraylist, R.layout.dialog_wifi, 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

권한을 요청하셨습니까? –

+0

가능한 복제본은 [SCAN \ _RESULTS \ _AVAILABLE \ _ACTION] 안드로이드 6.0에서 빈 목록을 반환] (http://stackoverflow.com/questions/32151603/scan-results-available-action-return-empty-list-in-android- 6-0) –

답변

0

Android 7의 경우 Wi-Fi 검색 결과를 얻으려면 ACCESS_COARSE_LOCATION 및 ACCESS_FINE_LOCATION 권한이 필요합니다. 또한 기기에서 위치를 켜야합니다.

0

WiFi를 사용하는 것 외에도 검색 결과에 액세스하려면 위치 서비스가 활성화되어 있어야합니다. Wi-Fi 만 사용하는 경우 빈 스캔 결과 목록이 표시됩니다.