Estoy teniendo una serie de problemas que espero me podáis resolver. Soy muy novato en Android y estoy buscando ejemplos válidos para escanear las redes WiFi con el móvil. En todos los ejemplos que le he cargado, surge el mismo problema, no encuentra nunca ninguna red, cuando no es cierto.
Ahora mismo estoy usando este código de ejemplo, que en teoría debería funcionar pero no lo hace. ¿Qué puede ser?
public class MainActivity extends AppCompatActivity {
private Element[] nets;
private WifiManager wifiManager;
private List<ScanResult> wifiList;
private TextView tv;
Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv = (TextView) findViewById(R.id.textView);
tv.setText("");
detectWifi();
Snackbar.make(view, "Scanning wifi ...", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
//******************** HAM DO TIM WIFI *********************
public void detectWifi() {
this.wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
this.wifiManager.startScan();
this.wifiList = this.wifiManager.getScanResults();
Toast.makeText(this, "So wifi tim thay: " + wifiList.size(), Toast.LENGTH_SHORT).show();
this.nets = new Element[wifiList.size()];//khoi tao mang nets voi so phan tu = so phan tu cua mang wifiList.
for (int i = 0; i < wifiList.size(); i++) {
String item = wifiList.get(i).toString();
//Toast.makeText(this, item, Toast.LENGTH_SHORT).show();
String[] mang_item = item.split(",");//cat chuoi nhan duoc thanh roi luu vao mang_item.
String item_ssid = mang_item[0]; //lay ra chuoi "SSID: name wifi"
String ssid = item_ssid.split(": ")[1]; //cat chuoi "SSID: name wifi" => tao thanh mang 2 phan tu la SSID va name wifi|| String ssid la ten => lay o phan tu thu 1 cua mang
nets[i] = new Element(ssid);
}
AdapterElements adapterElements = new AdapterElements(this);
ListView netList = (ListView) findViewById(R.id.lv_ssid);
netList.setAdapter(adapterElements);
netList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Toast.makeText(getApplicationContext(), nets[i].getTitle() + "", Toast.LENGTH_SHORT).show();
showdialow(nets[i].getTitle());
}
});
//showdialow();
}
//******************** SHOW DIALOG *********************
public void showdialow(final String ten_wifi) {
dialog = new Dialog(MainActivity.this);
dialog.setTitle("Nhập mật khẩu wifi");
dialog.setCancelable(true);
dialog.setContentView(R.layout.dialog_layout);
Button btn_DongY = (Button) dialog.findViewById(R.id.btn_dongy);
Button btn_Huy = (Button) dialog.findViewById(R.id.btn_huy);
final EditText edt_Password = (EditText) dialog.findViewById(R.id.edt_password);
CheckBox checkBox = (CheckBox) dialog.findViewById(R.id.cb_show);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked){
edt_Password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
else {
edt_Password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
});
btn_DongY.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String matkhau = edt_Password.getText().toString();
if (TextUtils.isEmpty(matkhau)) {
edt_Password.setError("chưa nhập password");
}
else{
Toast.makeText(MainActivity.this, "Tên wifi: " + ten_wifi + " Mat khau: " + matkhau, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
});
btn_Huy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
class AdapterElements extends ArrayAdapter<Object> {
Activity context;
public AdapterElements(Activity context) {
super(context, R.layout.items, nets);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View item = inflater.inflate(R.layout.items, null);
TextView tvSsid = (TextView) item.findViewById(R.id.tvSSID);
tvSsid.setText(nets[position].getTitle());
return item;
}
}
Muchas gracias a todos de antemano.