java - Android: Programmatically Turn on WiFi hotspot -
i trying turn on portable wifi hotspot on, referring link:
how set advanced settings of android wifihotspot
this working on samsung galaxy s3 android v4.4.2.(no issues)
on other devices same or lower android version, application crashes , restarts device.
code follows:
package com.android.startwifi; import java.lang.reflect.invocationtargetexception; import java.lang.reflect.method; import android.app.activity; import android.content.context; import android.net.wifi.wificonfiguration; import android.net.wifi.wifimanager; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.widget.toast; public class main extends activity { public wifimanager wifimanager; public context context; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); log.i("hi1","hi"); createwifiaccesspoint(); } private void createwifiaccesspoint() { wifimanager wifimanager = (wifimanager)getbasecontext().getsystemservice(context.wifi_service); if(wifimanager.iswifienabled()) { wifimanager.setwifienabled(false); } method[] wmmethods = wifimanager.getclass().getdeclaredmethods(); boolean methodfound=false; for(method method: wmmethods){ if(method.getname().equals("setwifiapenabled")){ methodfound=true; wificonfiguration netconfig = new wificonfiguration(); netconfig.allowedauthalgorithms.set(wificonfiguration.authalgorithm.open); netconfig.allowedprotocols.set(wificonfiguration.protocol.rsn); netconfig.allowedprotocols.set(wificonfiguration.protocol.wpa); netconfig.allowedkeymanagement.set(wificonfiguration.keymgmt.wpa_psk); netconfig.allowedpairwiseciphers.set(wificonfiguration.pairwisecipher.ccmp); netconfig.allowedpairwiseciphers.set(wificonfiguration.pairwisecipher.tkip); netconfig.allowedgroupciphers.set(wificonfiguration.groupcipher.ccmp); netconfig.allowedgroupciphers.set(wificonfiguration.groupcipher.tkip); try { boolean apstatus=(boolean) method.invoke(wifimanager, netconfig,true); //statusview.settext("creating wi-fi network \""+netconfig.ssid+"\""); (method iswifiapenabledmethod: wmmethods) { if(iswifiapenabledmethod.getname().equals("iswifiapenabled")){ while(!(boolean)iswifiapenabledmethod.invoke(wifimanager)){ }; for(method method1: wmmethods){ if(method1.getname().equals("getwifiapstate")){ int apstate; apstate=(integer)method1.invoke(wifimanager); // netconfig=(wificonfiguration)method1.invoke(wifi); //statusview.append("\nssid:"+netconfig.ssid+"\npassword:"+netconfig.presharedkey+"\n"); } } } } if(apstatus) { system.out.println("successdddd"); //statusview.append("\naccess point created!"); //finish(); //intent searchsensorsintent = new intent(this,searchsensors.class); //startactivity(searchsensorsintent); }else { system.out.println("failed"); //statusview.append("\naccess point creation failed!"); } } catch (illegalargumentexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } catch (invocationtargetexception e) { e.printstacktrace(); } } } if(!methodfound){ //statusview.settext("your phone's api not contain setwifiapenabled method configure access point"); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
permissions have defined in manifest file are:
<uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_network_state"/> <uses-permission android:name="android.permission.access_wifi_state"/> <uses-permission android:name="android.permission.change_wifi_state"/>
log file generated follows:
09-10 18:35:01.644: d/jdwp(29752): prepping jdwp on adb 09-10 18:35:01.644: d/jdwp(29752): adb transport startup 09-10 18:35:01.644: d/dalvikvm(29752): elevating priority 0 -8 09-10 18:35:01.645: d/jdwp(29752): jdwp: thread running 09-10 18:35:01.645: d/jdwp(29752): acceptconnection 09-10 18:35:01.646: d/jdwp(29752): trying receive file descriptor adb 09-10 18:35:01.646: d/dalvikvm(29752): zygote thread init done 09-10 18:35:01.653: d/jdwp(29752): received file descriptor 34 adb 09-10 18:35:01.658: d/jdwp(29752): processincoming 09-10 18:35:01.659: d/jdwp(29752): processincoming 09-10 18:35:01.659: d/jdwp(29752): handlepacket : cmd=0x1, cmdset=0xc7, len=0x13, id=0x40000040, flags=0x0, datalen=0x8 09-10 18:35:01.661: d/jdwp(29752): processincoming 09-10 18:35:01.661: d/jdwp(29752): handlepacket : cmd=0x1, cmdset=0xc7, len=0x17, id=0x40000041, flags=0x0, datalen=0xc 09-10 18:35:01.663: d/jdwp(29752): processincoming 09-10 18:35:01.663: d/jdwp(29752): handlepacket : cmd=0x1, cmdset=0xc7, len=0x13, id=0x40000042, flags=0x0, datalen=0x8 09-10 18:35:01.665: d/jdwp(29752): processincoming 09-10 18:35:01.665: d/jdwp(29752): handlepacket : cmd=0x1, cmdset=0xc7, len=0x13, id=0x40000043, flags=0x0, datalen=0x8 09-10 18:35:01.676: d/jdwp(29752): sendbufferedrequest : len=0x3d 09-10 18:35:01.736: d/jdwp(29752): sendbufferedrequest : len=0x45 09-10 18:35:01.754: w/asset(29752): assetmanager-->adddefaultassets cip path not exsit! 09-10 18:35:02.219: i/hi1(29752): hi 09-10 18:35:02.261: d/wifimanager(29752): enter init, sthreadrefcount:0 09-10 18:35:02.268: d/wifimanager(29752): create wifimanager handlerthread 09-10 18:35:03.599: i/system.out(29752): successdddd 09-10 18:35:03.634: v/phonewindow(29752): decorview setvisiblity: visibility = 4 09-10 18:35:03.745: v/phonewindow(29752): decorview setvisiblity: visibility = 0 09-10 18:35:03.922: d/libegl(29752): loaded /system/lib/egl/libegl_mali.so 09-10 18:35:03.924: d/libegl(29752): loaded /system/lib/egl/libglesv1_cm_mali.so 09-10 18:35:03.927: d/libegl(29752): loaded /system/lib/egl/libglesv2_mali.so 09-10 18:35:04.111: d/openglrenderer(29752): enabling debug mode 0 09-10 18:35:10.610: e/inputeventreceiver(29752): channel '41f21f48 com.android.startwifi/com.android.startwifi.main (client)' ~ publisher closed input channel or error occurred. events=0x9
thus have following questions:
- i know why app behaving on samsung s3 not on other devices?
- how can fix issue?
wificonfiguration netconfig = new wificonfiguration(); netconfig.ssid = "myap"; netconfig.allowedauthalgorithms.set(wificonfiguration.authalgorithm.open); netconfig.allowedprotocols.set(wificonfiguration.protocol.rsn); netconfig.allowedprotocols.set(wificonfiguration.protocol.wpa); netconfig.allowedkeymanagement.set(wificonfiguration.keymgmt.none); try { method setwifiapmethod = wifimanager.getclass().getmethod("setwifiapenabled", wificonfiguration.class, boolean.class); boolean apstatus=(boolean) setwifiapmethod.invoke(wifimanager, netconfig,true); method iswifiapenabledmethod = wifimanager.getclass().getmethod("iswifiapenabled"); while(!(boolean)iswifiapenabledmethod.invoke(wifimanager)){}; method getwifiapstatemethod = wifimanager.getclass().getmethod("getwifiapstate"); int apstate=(integer)getwifiapstatemethod.invoke(wifimanager); method getwifiapconfigurationmethod = wifimanager.getclass().getmethod("getwifiapconfiguration"); netconfig=(wificonfiguration)getwifiapconfigurationmethod.invoke(wifimanager); log.e("client", "\nssid:"+netconfig.ssid+"\npassword:"+netconfig.presharedkey+"\n"); } catch (exception e) { log.e(this.getclass().tostring(), "", e); }
Comments
Post a Comment