我已经写了code创建Android设备的接入点。我已经在两个模拟器和真正的device.But这是行不通的测试。在哪里我拿错?
公共类MainWAP延伸活动{
WifiManager wifiManager;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main_wap);
}
公共无效openWifi(视图v){
createWifiAccessPoint();
}
私人无效createWifiAccessPoint(){
如果(wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(假);
}
方法[] wmMethods = wifiManager.getClass()getDeclaredMethods()。
布尔methodFound = FALSE;
对于(方法方法:wmMethods){
如果(method.getName()。等于(setWifiApEnabled)){
methodFound = TRUE;
WifiConfiguration用netconfig =新WifiConfiguration();
netConfig.SSID =存取点;
netConfig.allowedAuthAlgorithms.set(
WifiConfiguration.AuthAlgorithm.OPEN);
尝试 {
布尔apstatus =(布尔)method.invoke(
wifiManager,用netconfig,真正的);
对于(方法isWifiApEnabledmethod:wmMethods){
如果(isWifiApEnabledmethod.getName()。等于(
isWifiApEnabled)){
而(!(布尔)isWifiApEnabledmethod.invoke(
wifiManager)){};
对于(方法方法一:wmMethods){
如果(method1.getName()。等于(
getWifiApState)){
INT apstate;
apstate =(整数)method1.invoke(
wifiManager);
}
}
}
}
如果(apstatus){
Log.d(泼水节活动,
接入点创建);
} 其他 {
Log.d(泼水节活动,
接入点创建失败);
}
}赶上(抛出:IllegalArgumentException E){
e.printStackTrace();
}赶上(IllegalAccessException E){
e.printStackTrace();
}赶上(的InvocationTargetException E){
e.printStackTrace();
}
}
}
如果(!methodFound){
Log.d(泼水节活动,
不能配置接入点);
}
}
}
解决方案
您 WiFiManager
是肯定不会被初始化。
在你的的onCreate
添加方法如下:
wifiManager =(WiFiManager)getSystemService(Context.WIFI_SERVICE);
I've written the code to create an access point for android devices. I've tested on both emulator and real device.But it doesn't work. Where did i get wrong?
public class MainWAP extends Activity {
WifiManager wifiManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_wap);
}
public void openWifi(View v) {
createWifiAccessPoint();
}
private void createWifiAccessPoint() {
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.SSID = "AccessPoint";
netConfig.allowedAuthAlgorithms.set(
WifiConfiguration.AuthAlgorithm.OPEN);
try {
boolean apstatus = (Boolean) method.invoke(
wifiManager, netConfig, true);
for (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);
}
}
}
}
if (apstatus) {
Log.d("Splash Activity",
"Access Point created");
} else {
Log.d("Splash Activity",
"Access Point creation failed");
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
if (!methodFound) {
Log.d("Splash Activity",
"cannot configure an access point");
}
}
}
解决方案
Your WiFiManager
is definately not initialized.
In your onCreate
method add this:
wifiManager = (WiFiManager) getSystemService(Context.WIFI_SERVICE);