Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 4.x专用网络静态IP地址(不连接)_Android_Networking_Adhoc_Static Ip Address - Fatal编程技术网

Android 4.x专用网络静态IP地址(不连接)

Android 4.x专用网络静态IP地址(不连接),android,networking,adhoc,static-ip-address,Android,Networking,Adhoc,Static Ip Address,我正在开发一款Android应用程序,通过网络连接到定制硬件 如果您将其设置为使用无线接入点,则可以正常工作。 如果您将其设置为使用设备上的接入点(也称为栓系),则其工作正常。 如果您将其设置为使用自组织网络,它将不起作用。Android在“获取IP地址”步骤失败 我们的工作是使用一个静态IP地址,低,看它的工作!如果您将平板电脑连接到我们的自定义硬件并强制使用自定义子网掩码和IP地址,它会立即连接 然而,这太令人困惑了。DHCP或GTFO。我们的解决方案使用一个自定义网络管理器,如果它是一个自

我正在开发一款Android应用程序,通过网络连接到定制硬件

如果您将其设置为使用无线接入点,则可以正常工作。 如果您将其设置为使用设备上的接入点(也称为栓系),则其工作正常。 如果您将其设置为使用自组织网络,它将不起作用。Android在“获取IP地址”步骤失败

我们的工作是使用一个静态IP地址,低,看它的工作!如果您将平板电脑连接到我们的自定义硬件并强制使用自定义子网掩码和IP地址,它会立即连接

然而,这太令人困惑了。DHCP或GTFO。我们的解决方案使用一个自定义网络管理器,如果它是一个自组织网络,则自动强制使用一个静态IP地址。这应该行得通

我已经按照(经过一些修改)的说明进行了操作,效果非常好。您可以选择网络,为其分配一个静态IP地址,就这样。android平板电脑永远不会连接到网络。但是,如果您在“real”wifi管理器下查看网络并选择“modify settings”(修改设置),您可以看到所有静态IP地址信息的保存位置。它就是无法连接

更让我痛苦的是,如果你移除网络,转到android wifi管理器并手动输入信息,它就会工作。但如果您以编程方式执行相同的步骤,则不会

有人知道这是为什么,或者如何修复它吗?这是一个有根的Nexus7

提前谢谢

public void connectToWiFiNetwork(String networkSSID, String networkPass, String type)
{
    //Make sure a password is set
    if(networkPass != null && networkPass.length() == 0)
        return;

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain ssid in quotes
    conf.status = WifiConfiguration.Status.ENABLED;
    int id = -1;

    if(type.contains("WPA")) {

        conf.preSharedKey = "\""+ networkPass +"\"";
        //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        System.out.println("Contains WPA");

    } else if (type.contains("WEP")) {

        conf.wepKeys[0] = "\"" + networkPass + "\"";
        conf.wepTxKeyIndex = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        System.out.println("Contains WEP");

    } else {
        //open network?
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        System.out.println("Open WiFi");
    }



    if(type.contains("IBSS"))
    {
        //Adhoc network. Provide an IP address to prevent... stuff.

        //For Android 2.x
        //final ContentResolver cr = this.context.getContentResolver();
        //Settings.System.putInt(cr, Settings.System.WIFI_USE_STATIC_IP, 1);
        //Settings.System.putString(cr, Settings.System.WIFI_STATIC_IP, "169.254.1.1");
        //Settings.System.putString(cr, Settings.System.WIFI_STATIC_NETMASK, "255.255.0.0");



        try{
            //Some code here isn't used... 
            WifiConfiguration wifiConf = null;
            WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);

            WifiInfo connectionInfo = wifiManager.getConnectionInfo();
            List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
            for (WifiConfiguration conf2 : configuredNetworks){
                if (conf2.networkId == connectionInfo.getNetworkId()){
                    wifiConf = conf2;
                    break;
                }
            }

            wifiConf = conf;

            setIpAssignment("STATIC", wifiConf); //or "DHCP" for dynamic setting
            Log.d("CONNECTION", "Connecting now using a static IP address");
            setIpAddress(InetAddress.getByName("169.254.1.1"), 24, wifiConf);
            Log.d("CONNECTION", "Connecting now using a static IP address");
            setGateway(InetAddress.getByName("255.255.0.0"), wifiConf);
            Log.d("CONNECTION", "Connecting now using a static IP address");
            setDNS(InetAddress.getByName("8.8.8.8"), wifiConf);
            Log.d("CONNECTION", "Connecting now using a static IP address");
            //wifiManager.updateNetwork(wifiConf); //apply the setting
            Log.d("CONNECTION", "Connecting now using a static IP address");

        }catch(Exception e){
            e.printStackTrace();
        }
    }

    id = wifi.addNetwork(conf);
    wifi.saveConfiguration();

    //Add the network

    //And enable it

    List<WifiConfiguration> list = wifi.getConfiguredNetworks();
    for( WifiConfiguration i : list ) {
        if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
            wifi.disconnect();
            wifi.enableNetwork(i.networkId, true);
            wifi.reconnect();
            break;
        }
    }




    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("auto_start_tethering", false);
    editor.commit();

    Log.d("CONNECTION", "Connecting using " + networkSSID + " and " + networkPass + ".");
}
public void connectToWiFiNetwork(字符串networkSSID、字符串networkPass、字符串类型)
{
//确保设置了密码
if(networkPass!=null&&networkPass.length()=0)
返回;
WifiConfiguration conf=新的WifiConfiguration();
conf.SSID=“\”“+networkSSID+”\”“;//请注意引号。字符串应在引号中包含SSID
conf.status=WifiConfiguration.status.ENABLED;
int id=-1;
如果(类型包含(“WPA”)){
conf.preSharedKey=“\”+网络通行证+“\”;
//conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA\u PSK);
System.out.println(“包含WPA”);
}else if(type.contains(“WEP”)){
conf.wepKeys[0]=“\”“+networkPass+”\”;
conf.wepTxKeyIndex=0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.alloweGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
System.out.println(“包含WEP”);
}否则{
//开放网络?
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
System.out.println(“开放式WiFi”);
}
if(type.contains(“IBSS”))
{
//临时网络。提供一个IP地址来防止…的东西。
//适用于Android 2.x
//final ContentResolver cr=this.context.getContentResolver();
//Settings.System.putInt(cr,Settings.System.WIFI\u USE\u STATIC\u IP,1);
//Settings.System.putString(cr,Settings.System.WIFI_STATIC_IP,“169.254.1.1”);
//Settings.System.putString(cr,Settings.System.WIFI_STATIC_NETMASK,“255.255.0.0”);
试一试{
//这里有些代码没有使用。。。
WifiConfiguration wifiConf=null;
WifiManager WifiManager=(WifiManager)this.context.getSystemService(context.WIFI_服务);
WifiInfo connectionInfo=wifiManager.getConnectionInfo();
List configuredNetworks=wifiManager.getConfiguredNetworks();
用于(线路配置会议2:配置的数据网络){
如果(conf2.networkId==connectionInfo.getNetworkId()){
wifiConf=conf2;
打破
}
}
wifiConf=conf;
setIpAssignment(“STATIC”,wifiConf);//或“DHCP”用于动态设置
Log.d(“连接”,“现在使用静态IP地址连接”);
setIpAddress(InetAddress.getByName(“169.254.1.1”),24,wifiConf;
Log.d(“连接”,“现在使用静态IP地址连接”);
setGateway(InetAddress.getByName(“255.255.0.0”),wifiConf);
Log.d(“连接”,“现在使用静态IP地址连接”);
setDNS(InetAddress.getByName(“8.8.8.8”),wifiConf);
Log.d(“连接”,“现在使用静态IP地址连接”);
//wifiManager.updateNetwork(wifiConf);//应用设置
Log.d(“连接”,“现在使用静态IP地址连接”);
}捕获(例外e){
e、 printStackTrace();
}
}
id=wifi.addNetwork(conf);
wifi.saveConfiguration();
//添加网络
//并启用它
List List=wifi.getConfiguredNetworks();
用于(无线配置i:列表){
如果(i.SSID!=null&&i.SSID.equals(“\”+networkSSID+“\”)){
wifi.disconnect();
wifi.enableNetwork(即networkId,true);
wifi.reconnect();
打破
}
}
SharedReferences设置=PreferenceManager.GetDefaultSharedReferences(上下文);
SharedReferences.Editor=settings.edit();
编辑器.putBoolean(“自动启动链接”,false);
commit();
Log.d(“连接”、“使用“+networkSSID+”和“+networkPass+”进行连接”);
}