Android连接WIFI

效果图

打开WIFI并获取WIFI列表

打开WIFI并获取WIFI列表

连接到指定WIFI

连接到指定WIFI

直接连接配置过的WIFI

直接连接配置过的WIFI

密码错误

密码错误

源码

KqwWifiManagerDemo

WIFI的获取、连接状态等等的信息,都是通过广播回调的.

下面介绍了主要的方法,更多请查看KqwWifiManager

注册广播接收者

<!-- 监听网络状态的广播接收者 -->
<receiver android:name=".KqwWifiManager$NetworkBroadcastReceiver">
    <intent-filter>
        <!-- AP扫描完成,客户端得到可用的结果集 -->
        <action android:name="android.net.wifi.SCAN_RESULTS" />
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        <action android:name="android.net.wifi.STATE_CHANGE" />
        <action android:name="android.net.wifi.supplicant.STATE_CHANGE" />
    </intent-filter>
</receiver>

广播接收者

/**
 * 广播接收者
 */
public static class NetworkBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String action = intent.getAction();
            Log.i(TAG, "onReceive: action = " + action);
            // 监听WIFI的启用状态
            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
                int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
                switch (wifiState) {
                    case WifiManager.WIFI_STATE_ENABLING:
                        Log.i(TAG, "onReceive: 正在打开 WIFI...");
                        break;
                    case WifiManager.WIFI_STATE_ENABLED:
                        Log.i(TAG, "onReceive: WIFI 已打开");
                        if (null != mOnWifiEnabledListener) {
                            mOnWifiEnabledListener.onWifiEnabled(true);
                            mOnWifiEnabledListener.onFinish();
                        }
                        break;
                    case WifiManager.WIFI_STATE_DISABLING:
                        Log.i(TAG, "onReceive: 正在关闭 WIFI...");
                        break;
                    case WifiManager.WIFI_STATE_DISABLED:
                        Log.i(TAG, "onReceive: WIFI 已关闭");
                        if (null != mOnWifiEnabledListener) {
                            mOnWifiEnabledListener.onWifiEnabled(false);
                            mOnWifiEnabledListener.onFinish();
                        }
                        break;
                    case WifiManager.WIFI_STATE_UNKNOWN:
                        Log.i(TAG, "onReceive: WIFI 状态未知!");
                        break;
                    default:
                        break;
                }
            }

            // WIFI扫描完成
            if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
                if (null != mOnWifiScanResultsListener) {
                    mOnWifiScanResultsListener.onScanResults(getScanResults());
                    mOnWifiScanResultsListener.onFinish();
                }
            }

            // WIFI 连接状态的监听(只有WIFI可用的时候,监听才会有效)
            if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
                NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if (null != networkInfo && networkInfo.isConnected()) {
                    WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
                    if (null != wifiInfo && String.format("\"%s\"", mConnectingSSID).equals(wifiInfo.getSSID()) && null != mOnWifiConnectListener) {
                        // WIFI连接成功
                        mOnWifiConnectListener.onSuccess(wifiInfo.getSSID());
                        mOnWifiConnectListener.onFinish();
                        mOnWifiConnectListener = null;
                    }
                }
            }

            // WIFI连接过程的监听
            if (intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
                WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
                SupplicantState state = wifiInfo.getSupplicantState();
                switch (state) {
                    case INTERFACE_DISABLED: // 接口禁用
                        Log.i(TAG, "onReceive: INTERFACE_DISABLED 接口禁用");
                        break;
                    case DISCONNECTED:// 断开连接
                    case INACTIVE: // 不活跃的
                        Log.i(TAG, "onReceive: INACTIVE 不活跃的 DISCONNECTED:// 断开连接");
                        if (null != mOnWifiConnectListener) {
                            // 断开当前网络失败
                            mOnWifiConnectListener.onFailure();
                            // 连接完成
                            mOnWifiConnectListener.onFinish();
                            mOnWifiConnectListener = null;
                            mConnectingSSID = null;
                        }
                        break;
                    case SCANNING: // 正在扫描
                        Log.i(TAG, "onReceive: SCANNING 正在扫描");
                        break;
                    case AUTHENTICATING: // 正在验证
                        Log.i(TAG, "onReceive: AUTHENTICATING: // 正在验证");
                        if (null != mOnWifiConnectListener) {
                            mOnWifiConnectListener.onConnectingMessage("正在验证");
                        }
                        break;
                    case ASSOCIATING: // 正在关联
                        Log.i(TAG, "onReceive: ASSOCIATING: // 正在关联");
                        if (null != mOnWifiConnectListener) {
                            mOnWifiConnectListener.onConnectingMessage("正在关联");
                        }
                        break;
                    case ASSOCIATED: // 已经关联
                        Log.i(TAG, "onReceive: ASSOCIATED: // 已经关联");
                        if (null != mOnWifiConnectListener) {
                            mOnWifiConnectListener.onConnectingMessage("已经关联");
                        }
                        break;
                    case FOUR_WAY_HANDSHAKE:
                        Log.i(TAG, "onReceive: FOUR_WAY_HANDSHAKE:");
                        break;
                    case GROUP_HANDSHAKE:
                        Log.i(TAG, "onReceive: GROUP_HANDSHAKE:");
                        break;
                    case COMPLETED: // 完成
                        Log.i(TAG, "onReceive: COMPLETED: // 完成");
                        if (null != mOnWifiConnectListener) {
                            mOnWifiConnectListener.onConnectingMessage("正在连接...");
                        }
                        break;
                    case DORMANT:
                        Log.i(TAG, "onReceive: DORMANT:");
                        break;
                    case UNINITIALIZED: // 未初始化
                        Log.i(TAG, "onReceive: UNINITIALIZED: // 未初始化");
                        break;
                    case INVALID: // 无效的
                        Log.i(TAG, "onReceive: INVALID: // 无效的");
                        break;
                    default:
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

打开WIFI

/**
 * 打开Wifi
 */
public void openWifi(@NonNull OnWifiEnabledListener listener) {
    if (!mWifiManager.isWifiEnabled()) {
        mOnWifiEnabledListener = listener;
        mOnWifiEnabledListener.onStart(true);
        mWifiManager.setWifiEnabled(true);
    }
}

关闭WIFI

/**
 * 关闭Wifi
 */
public void closeWifi(@NonNull OnWifiEnabledListener listener) {
    if (mWifiManager.isWifiEnabled()) {
        mOnWifiEnabledListener = listener;
        mOnWifiEnabledListener.onStart(false);
        mWifiManager.setWifiEnabled(false);
    }
}

扫描附近的WIFI

/**
 * 扫描附近的WIFI
 *
 * @param listener 扫描完成的回调接口
 */
public void startScan(@NonNull OnWifiScanResultsListener listener) {
    try {
        mOnWifiScanResultsListener = listener;
        mOnWifiScanResultsListener.onStart();
        // 先返回缓存
        mOnWifiScanResultsListener.onScanResults(getScanResults());
        // 重新开始扫描
        mWifiManager.startScan();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

获取WIFI列表

/**
 * 获取Wifi列表
 *
 * @return Wifi列表
 */
private static List<ScanResult> getScanResults() {
    try {
        // 得到扫描结果
        return mWifiManager.getScanResults();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

通过密码连接到WIFI

/**
 * 通过密码连接到WIFI
 *
 * @param scanResult 要连接的WIFI
 * @param pwd        密码
 * @param listener   连接的监听
 */
public void connectionWifiByPassword(@NonNull ScanResult scanResult, @Nullable String pwd, @NonNull OnWifiConnectListener listener) {
    // SSID
    String SSID = scanResult.SSID;
    // 加密方式
    SecurityMode securityMode = getSecurityMode(scanResult);

    // 生成配置文件
    WifiConfiguration addConfig = createWifiConfiguration(SSID, pwd, securityMode);
    int netId;
    // 判断当前配置是否存在
    WifiConfiguration updateConfig = isExists(addConfig);
    if (null != updateConfig) {
        // 更新配置
        netId = mWifiManager.updateNetwork(updateConfig);
    } else {
        // 添加配置
        netId = mWifiManager.addNetwork(addConfig);
    }
    // 通过NetworkID连接到WIFI
    connectionWifiByNetworkId(SSID, netId, listener);
}

直接连接配置过的WIFI

/**
 * 通过NetworkId连接到WIFI (配置过的网络可以直接获取到NetworkID,从而不用再输入密码)
 *
 * @param SSID      WIFI名字
 * @param networkId NetworkId
 * @param listener  连接的监听
 */
public void connectionWifiByNetworkId(@NonNull String SSID, int networkId, @NonNull OnWifiConnectListener listener) {
    // 正要连接的SSID
    mConnectingSSID = SSID;
    // 连接的回调监听
    mOnWifiConnectListener = listener;
    // 连接开始的回调
    mOnWifiConnectListener.onStart(SSID);
    /*
     * 判断 NetworkId 是否有效
     * -1 表示配置参数不正确
     */
    if (-1 == networkId) {
        // 连接WIFI失败
        if (null != mOnWifiConnectListener) {
            // 配置错误
            mOnWifiConnectListener.onFailure();
            // 连接完成
            mOnWifiConnectListener.onFinish();
            mOnWifiConnectListener = null;
            mConnectingSSID = null;
        }
        return;
    }
    // 获取当前的网络连接
    WifiInfo wifiInfo = getConnectionInfo();
    if (null != wifiInfo) {
        // 断开当前连接
        boolean isDisconnect = disconnectWifi(wifiInfo.getNetworkId());
        if (!isDisconnect) {
            // 断开当前网络失败
            if (null != mOnWifiConnectListener) {
                // 断开当前网络失败
                mOnWifiConnectListener.onFailure();
                // 连接完成
                mOnWifiConnectListener.onFinish();
                mOnWifiConnectListener = null;
                mConnectingSSID = null;
            }
            return;
        }
    }

    // 连接WIFI
    boolean isEnable = mWifiManager.enableNetwork(networkId, true);
    if (!isEnable) {
        // 连接失败
        if (null != mOnWifiConnectListener) {
            // 连接失败
            mOnWifiConnectListener.onFailure();
            // 连接完成
            mOnWifiConnectListener.onFinish();
            mOnWifiConnectListener = null;
            mConnectingSSID = null;
        }
    }
}

断开指定WIFI

/**
 * 断开WIFI
 *
 * @param netId netId
 * @return 是否断开
 */
public boolean disconnectWifi(int netId) {
    boolean isDisable = mWifiManager.disableNetwork(netId);
    boolean isDisconnect = mWifiManager.disconnect();
    return isDisable && isDisconnect;
}

删除配置

/**
 * 删除配置
 *
 * @param netId netId
 * @return 是否删除成功
 */
private boolean deleteConfig(int netId) {
    boolean isDisable = mWifiManager.disableNetwork(netId);
    boolean isRemove = mWifiManager.removeNetwork(netId);
    boolean isSave = mWifiManager.saveConfiguration();
    return isDisable && isRemove && isSave;
}

生成配置信息

/**
 * 生成新的配置信息 用于连接Wifi
 *
 * @param SSID     WIFI名字
 * @param password WIFI密码
 * @param mode     WIFI加密类型
 * @return 配置
 */
private WifiConfiguration createWifiConfiguration(@NonNull String SSID, @Nullable String password, @NonNull SecurityMode mode) {
    WifiConfiguration config = new WifiConfiguration();
    config.allowedAuthAlgorithms.clear();
    config.allowedGroupCiphers.clear();
    config.allowedKeyManagement.clear();
    config.allowedPairwiseCiphers.clear();
    config.allowedProtocols.clear();
    config.SSID = "\"" + SSID + "\"";

    if (mode == SecurityMode.OPEN) {
        //WIFICIPHER_NOPASS
        // config.wepKeys[0] = "";
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        // config.wepTxKeyIndex = 0;
    } else if (mode == SecurityMode.WEP) {
        //WIFICIPHER_WEP
        config.hiddenSSID = true;
        config.wepKeys[0] = "\"" + password + "\"";
        config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        config.wepTxKeyIndex = 0;
    } else if (mode == SecurityMode.WPA) {
        //WIFICIPHER_WPA
        config.preSharedKey = "\"" + password + "\"";
        config.hiddenSSID = true;
        config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        //config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        config.status = WifiConfiguration.Status.ENABLED;
    }
    return config;
}

目前存在的问题

目前存在的问题是,当删除配置以后,再连接WIFI,调用addNetwork或者updateNetwork都会返回-1,需要重启一下WIFI才会正常,有知道原因还请指点一下。

Android连接WiFi摄像头通常需要通过一些第三方库或者SDK来实现,因为直接操作摄像头通常受限于系统的权限。以下是大致步骤: 1. **添加依赖**:如果你打算使用Java或Kotlin编写应用,可以使用开源库如`CameraX` (Google官方推荐) 或 `OpenCV` 来处理摄像头功能。 ```java // 使用CameraX implementation "androidx.camera:camera-core:1.0.0" implementation "androidx.camera:camera-camera2:1.0.0" ``` 2. **设备扫描**:首先检测并获取可用的WiFi网络列表,包括已连接WiFi摄像头设备。这需要网络管理权限。 3. **建立连接**:识别出特定的WiFi摄像头设备后,利用相应的API(比如Bonjour或UPnP)建立网络连接。 4. **打开相机预览**:一旦连接成功,你可以创建CameraCaptureSession并开始显示实时视频流。 ```java // CameraX示例 CaptureSession session = cameraProvider.bindToLifecycle(lifecycleOwner); try { ImageCapture imageCapture = new ImageCapture(cameraId); ImageAnalysis imageAnalysis = new ImageAnalysis.Builder() .setTargetAspectRatio(aspectRatio) .build(); session.setRepeatingRequest(imageCapture.captureRequestBuilder().build(), imageAnalysis::onImageCaptured, null); } catch (CameraAccessException e) { e.printStackTrace(); } ``` 5. **权限管理和用户界面**:确保在AndroidManifest.xml中添加了必要的权限,并设计一个友好的用户界面让用户选择连接的摄像头。 **相关问题--:** 1. 如果摄像头连接失败,应该检查哪些方面的问题? 2. Android系统如何保证用户隐私,防止非法访问WiFi摄像头? 3. 是否可以直接从Android设备控制远程WiFi摄像头?
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值