The Way to Scan Wifi List
내 주변의 와이파이의 목록을 가져오는 법
1. WifiManager를 선언
wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
2. IntentFilter를 선언하여 Receiver를 등록
IntentFilter intentFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
getContext().registerReceiver(receiver, intentFilter);
3. Receiver의 구성
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
scanDatas = wifiManager.getScanResults();
adapter.setResults(scanDatas);
}else if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
getContext().sendBroadcast(new Intent("wifi.ON_NETWORK_STATE_CHANGED"));
}
}
};
4. 스캔시작
wifiManager.startScan();
5. class의 종료시 receiver해제
getContext().unregisterReceiver(receiver);
추가적으로 자동으로 와이파이에 연결시키려면?
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
}
전체적인 구성도
public class WifiScanFragment extends Fragment {
@Bind(R.id.widget_recyclerview) RecyclerView recyclerView;
private WifiScanAdapter adapter;
private WifiManager wifiManager;
private List<ScanResult> scanDatas; // ScanResult List
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
scanDatas = wifiManager.getScanResults();
adapter.setResults(scanDatas);
}else if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
getContext().sendBroadcast(new Intent("wifi.ON_NETWORK_STATE_CHANGED"));
}
}
};
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_wifi_scan, container, false);
ButterKnife.bind(this, view);
adapter = new WifiScanAdapter(getActivity());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
return view;
}
@Override
public void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
getContext().registerReceiver(receiver, intentFilter);
wifiManager.startScan();
}
@Override
public void onPause() {
super.onPause();
getContext().unregisterReceiver(receiver);
}
}
마지막으로 rxjava를 이용한 라이브러리도 있다.
https://github.com/pwittchen/ReactiveNetwork
'안드로이드' 카테고리의 다른 글
Goolgle map api 설정 (6) | 2016.02.21 |
---|---|
SHA-1 인증서 지문구하기 - The way getting the SHA1 (6) | 2016.02.21 |
Decompile - 디컴파일 (6) | 2016.02.10 |
Resource 배열처리 (6) | 2016.02.09 |
안드로이드 비디오 재생하기 (6) | 2016.02.06 |
댓글