我正在做一个我必须通过热点连接两部手机的地方。
将有 2 个按钮(发送和接收)。如果有人按下 RECEIVE 按钮,他们的热点将被激活。如果有人按下发送按钮,他们应该连接到该热点。
我已经在这里做了很多研究,它对我构建代码有很大帮助,但它仍然无法正常工作。addNetwork 方法返回一个有效的 id,enableNetwork 返回 true,但它仍然没有连接到热点网络。它禁用其他的,但它不连接到热点。
热点创建得很好,因为如果我通过设置、wifi 等,我可以加入它。
这是我的代码:
以下是按钮的侦听器:
protected void setUpButtonListeners(){
Log.i("SETUP BUTTON LISTENERS", "SETTING UP THE BUTTON LISTENERS");
wificonfiguration=new WifiConfiguration();
setUpTheWifiConfiguration(wificonfiguration);
wifimanager = (WifiManager) HighwayActivity.this.getSystemService(HighwayActivity.this.WIFI_SERVICE);
wifimanager.setWifiEnabled(true);
Button sendButton= (Button) findViewById(R.id.send);
Button receiveButton = (Button) findViewById(R.id.receive);
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
netID = wifimanager.addNetwork(wificonfiguration);
Log.e("NET ID ::: ", "O net id is : "+ netID);
boolean asad=wifimanager.disconnect();
Log.e("wifi.manager disconnect"," bool is "+asad );
boolean aux = wifimanager.enableNetwork(netID, true);
Log.e("enableNetwork", "bool aux : " + aux);
boolean reconnect=wifimanager.reconnect();
Log.e("Recconect ::: ", " recnect is : "+ reconnect);
wifireceiver=new WifiReceiver();
registerReceiver(wifireceiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
}
});
receiveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
try
{
wifimanager.setWifiEnabled(false);
//USING REFLECTION TO GET METHOD "SetWifiAPEnabled"
Method method=wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifimanager, wificonfiguration, true);
Toast.makeText(HighwayActivity.this, "Craeted a hotspot with the SSID : " + wificonfiguration.SSID, Toast.LENGTH_SHORT).show();
}
catch (NoSuchMethodException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InvocationTargetException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
这是我的wifi配置:(我都试过了)
protected void setUpTheWifiConfiguration(WifiConfiguration wificonfiguration){
wificonfiguration.SSID = "\"" + mySSID + "\"";
wificonfiguration.preSharedKey = "\"" + "pwteste123A"+ "\"";
/*
wificonfiguration.status = WifiConfiguration.Status.ENABLED;
wificonfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
// wificonfiguration.hiddenSSID = true;
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wificonfiguration.priority = 40;
*/
// No security
wificonfiguration.status = WifiConfiguration.Status.DISABLED;
wificonfiguration.priority = 40;
wificonfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wificonfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wificonfiguration.allowedAuthAlgorithms.clear();
wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wificonfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wificonfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
}