java - How can I connect to a specific wifi network? -
i have devlope project list out wifi connection available near device.now, want connect 1 specific network connection. can do? have got networks in listview. if example available post comment link.
private void getwifinetworkslist() { intentfilter filter = new intentfilter(); filter.addaction(wifimanager.scan_results_available_action); final wifimanager wifimanager = (wifimanager) getapplicationcontext() .getsystemservice(context.wifi_service); ; registerreceiver(new broadcastreceiver() { @suppresslint("usevalueof") @override public void onreceive(context context, intent intent) { sb = new stringbuilder(); scanlist = wifimanager.getscanresults(); sb.append("\n number of wifi connections :" + " " + scanlist.size() + "\n\n"); adapter = new arrayadapter<string>(draginoactivity.this, android.r.layout.simple_list_item_1, list); adapter.clear(); (int = 0; < scanlist.size(); i++) { listwifidevice.setadapter(adapter); // list.add(new integer(i + 1).tostring() + ". "); list.add((new integer(i + 1).tostring() + ". ") + (scanlist.get(i)).tostring().substring(5, (scanlist.get(i)).tostring().indexof(","))); // list.add("\n\n"); listwifidevice.setadapter(adapter); adapter.notifydatasetchanged(); listwifidevice .setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { textview tv = (textview) arg1; // todo auto-generated method stub toast.maketext(draginoactivity.this, "" + tv.gettext().tostring(), 1000) .show(); } }); } // listwifidevice.setadapter(adapter); } }, filter); wifimanager.startscan(); }
private void setnetworkslist() { networkslist = (listview) findviewbyid(r.id.networklist); networkslist.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, final int position, long id) { final int chosenposition = position; wifiitem wifiitem = new wifiitem(); wifiitem = (wifiitem) parent.getitematposition(position); networkssid = wifiitem.getssid(); networkpass = ""; showpassworddialog(connectactivity.this); setpasswordlistener(new passwordtypedlistener() { @override public void onpasswordtyped() { connecthelper = new connecthelper(); if(scanresult.get(chosenposition).capabilities.contains("wep")) { connecthelper.setupconnection(networkssid, networkpass, "wep"); } //jezeli siec wpa else if(scanresult.get(chosenposition).capabilities.contains("wpa")) { connecthelper.setupconnection(networkssid, networkpass, "wpa"); } //inne przypadki (open network) else { connecthelper.setupconnection(networkssid, networkpass, "else"); } wifimanager.addnetwork(connecthelper.getconfiguration()); list<wificonfiguration> list = wifimanager.getconfigurednetworks(); for( wificonfiguration : list ) { if(i.ssid != null && i.ssid.equals("\"" + networkssid + "\"")) { wifimanager.disconnect(); wifimanager.enablenetwork(i.networkid, true); if(wifimanager.reconnect()) { for(int j=0;j<listwifi.size();j++){ listwifi.get(j).setconnected(false); } runnablecounter=0; handler.postdelayed(ifconnected,1000); } break; } } } }); } }); }
and dialog
private void showpassworddialog(context context) { final edittext input = new edittext(context); input.setsingleline(); alertdialog.builder alert = new alertdialog.builder(context); alert.settitle("password"); alert.setview(input); alert.setpositivebutton("confirm", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { networkpass = input.gettext().tostring(); if(connectactivity.this.passwordlistener != null) connectactivity.this.passwordlistener.onpasswordtyped(); } }); alert.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } }); alert.show(); }
and in oncreate skip creating wifiitems , store ssids
arraylist<wifiitem> listwifi = new arraylist<wifiitem>(); wifimanager.startscan(); scanresult = wifimanager.getscanresults(); for(scanresult sc : scanresult) { wifiitem wifiitem = new wifiitem(); wifiitem.setssid(sc.ssid); wifiitem.setbssid(sc.bssid); listwifi.add(wifiitem); }
Comments
Post a Comment