转一段使用C与wpa设置wifi的代码



/******************************************************************
 Name:        iSTCClientOpen
 Description: open a socket for wifi
 ******************************************************************/
int iSTCClientOpen(void)
    {
    Client_t *pClient = (Client_t *)calloc(1, sizeof(Client_t));


    if (pClient != NULL)
        {
        pClient->sockFd = socket(AF_INET, SOCK_STREAM, 0);
        if (pClient->sockFd < 0)
            {
            printf("@err: socket() for Send\n");
            goto fail;
            }


        pClient->thisAddr.sin_family = AF_INET;
        pClient->thisAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        pClient->thisAddr.sin_port = ntohs(INADDR_ANY);


        if (bind(pClient->sockFd, (struct sockaddr *) &pClient->thisAddr, sizeof(pClient->thisAddr)) < 0)
            {
            printf("@err: bind error(%x).\n", errno);
            goto fail;
            }


        pClient->peerAddr.sin_family = AF_INET;
        pClient->peerAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        pClient->peerAddr.sin_port = ntohs(DEFAULT_PORT);


        if (connect(pClient->sockFd, (struct sockaddr *)&pClient->peerAddr, sizeof(pClient->peerAddr)) < 0)
            {
            printf("@err: connect %d:%s\n", errno,strerror(errno));
            goto fail;
            }
        }


    return (int)pClient;


fail:
    if (pClient)
        {
        if (pClient->sockFd >= 0)
            close(pClient->sockFd);
        free(pClient);
        }


    return 0;
    }


int iSTC_WIFI_Connect(char *ESSID, char *Password,int Encryptiontype, char *ifname, int timeout)
    {
    int ret = -1;
    int len;
    char buf[256];
    int cid = iSTCClientOpen();
    if(cid == 0)
    {
        return -1;
    }
    Client_t *pClient = (Client_t *)cid;


    iSTCHead_t *pHead = (iSTCHead_t *)buf;
    iSTCWifiConnect_t *pConnect = (iSTCWifiConnect_t *)pHead->data;


    strncpy(pConnect->ESSID, ESSID,sizeof(pConnect->ESSID));
    strncpy(pConnect->Password, Password,sizeof(pConnect->Password));
#ifndef USEISTCDAEMON
    strncpy(pConnect->ifname,ifname,sizeof(pConnect->ifname));
    pConnect->Encryption_type = Encryptiontype;
#endif
    pConnect->timeout = timeout;


    pHead->version  = iSTC_VERSION;
    pHead->type     = iSTC_TYPE_REQUEST;
    pHead->length   = sizeof(iSTCWifiConnect_t);
    pHead->seq      = 0;
    pHead->command  = iSTC_CMD_WIFI_CONNECT;
    pHead->checksum = 0;
    printf("Encryptiontype %d ,%d,%d\n",Encryptiontype,sizeof(iSTCHead_t),pHead->length);
    len = send(pClient->sockFd, pHead, sizeof(iSTCHead_t) + pHead->length, 0);
    if (len != sizeof(iSTCHead_t) + pHead->length) goto bye;
    printf("send over\n");
    len = read(pClient->sockFd, pHead, sizeof(iSTCHead_t));
    if (len != sizeof(iSTCHead_t)) goto bye;
printf("read over\n");
    if (pHead->response == iSTC_OK)
        ret = 0;
    else
        ret = -1;
if(pHead->response == iSTC_ERROR_SYSTEM)
{
printf("----------------wpa error ---------\n");
system("killall wpa_cli\n");
system("killall wpa_supplicant\n");
sleep(1);
system("wpa_supplicant -B -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlan0\n");
sleep(1);
    system("wpa_cli &\n");
}
bye:
    iSTCClientClose(cid);
    return ret;
    }





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我作为一个语言模型,无法提供完整的可执行代码。但是,我可以给出一些代码示例,供您参考: 1. 获取WIFI管理器对象 ``` WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); ``` 2. 设置WIFI网络的参数 ``` WifiConfiguration wifiConfig = new WifiConfiguration(); wifiConfig.SSID = "\"SSID name\""; wifiConfig.preSharedKey = "\"password\""; wifiConfig.status = WifiConfiguration.Status.ENABLED; wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); ``` 3. 连接WIFI网络 ``` int networkId = wifiManager.addNetwork(wifiConfig); wifiManager.disconnect(); wifiManager.enableNetwork(networkId, true); wifiManager.reconnect(); ``` 4. 检查WIFI连接状态 ``` ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiInfo.isConnected()) { // WIFI连接成功 } else { // WIFI连接失败 } ``` 请注意,上述代码示例可能需要根据您的具体需求进行修改。此外,连接WIFI网络需要获取相应的权限,例如: ``` <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值