目录

1.系统特性

检测尝试连接的WIFI及路由器特征,分析当前尝试访问的网络情况,通过聚类分析,实时反馈Wifi检测结果,帮助开发者防范来自恶意Wifi的恶意行为攻击。

2.典型场景

开发者集成WifiDetect接口,当应用获取尝试连接的WIFI存在ARP攻击、中间人攻击、DNS劫持等攻击时,开发者可以阻断操作或者进一步让用户认证确认。

3.开发流程

请参考:开发流程

4.应用开发

使用 Safety Detect WifiDetect API 之前,必须确保在用户设备上安装了正确版本的 HMS 服务。如果安装了不正确的版本,则您的应用可能会在调用 API 后停止响应。如果您的应用检测到安装了错误的版本,则应要求用户在其设备上更新 HMS。

要检查已安装的 HMS 版本是否与您使用的Android SDK版本兼容,请调用 isHuaweiMobileServicesAvailable() 方法,如以下代码片段所示:

  1. if (HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context) == ConnectionResult.SUCCESS){
  2. // The SafetyDetect SysIntegrity API is available.
  3. } else {
  4. // Prompt user to update HMS.
  5. }

4.1调用 WifiDetect API :

  1. /*
  2. * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. *  you may not use this file except in compliance with the License.
  5. *  You may obtain a copy of the License at
  6. *
  7. *      http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. *  Unless required by applicable law or agreed to in writing, software
  10. *  distributed under the License is distributed on an "AS IS" BASIS,
  11. *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. *  See the License for the specific language governing permissions and
  13. *  limitations under the License.
  14. */
  15. package com.huawei.hms.safetydetect.sample;
  16. import com.huawei.hmf.tasks.OnFailureListener;
  17. import com.huawei.hmf.tasks.OnSuccessListener;
  18. import com.huawei.hms.common.ApiException;
  19. import com.huawei.hms.support.api.entity.safetydetect.WifiDetectResponse;
  20. import com.huawei.hms.support.api.safetydetect.SafetyDetect;
  21. import com.huawei.hms.support.api.safetydetect.SafetyDetectStatusCodes;
  22. import android.app.Fragment;
  23. import android.util.Log;
  24. import android.view.View;
  25. /**
  26. * An example of how to use AppsCheck Service API.
  27. * Note that you have to configure an AppId for SafetyDetect Service first.
  28. */
  29. public class SafetyDetectWifiDetectAPIFragment extends Fragment implements View.OnClickListener {
  30.    private void invokeGetWifiDetectStatus() {
  31.        Log.i(TAG, "Start to getWifiDetectStatus!");
  32.        SafetyDetectClient wifidetectClient = SafetyDetect.getClient(MainActivity.this);
  33.        Task task = wifidetectClient.getWifiDetectStatus();
  34.        task.addOnSuccessListener(new OnSuccessListener<WifiDetectResponse>() {
  35.            @Override
  36.            public void onSuccess(WifiDetectResponse wifiDetectResponse) {
  37.                int wifiDetectStatus = wifiDetectResponse.getWifiDetectStatus();
  38.                Log.i(TAG, "\n-1 :获取WiFi状态失败\n" + "0 :未连接WIFI\n" + "1 :当前连接的WIFI安全\n" + "2 :当前连接的WIFI不安全.");
  39.                Log.i(TAG, "wifiDetectStatus is: " + wifiDetectStatus);
  40.            }
  41.        }).addOnFailureListener(new OnFailureListener() {
  42.            @Override
  43.            public void onFailure(Exception e) {
  44.                if (e instanceof ApiException) {
  45.                    ApiException apiException = (ApiException) e;
  46.                    Log.e(TAG,
  47.                        "Error: " + apiException.getStatusCode() + ":"
  48.                            + SafetyDetectStatusCodes.getStatusCodeString(apiException.getStatusCode()) + ": "
  49.                            + apiException.getStatusMessage());
  50.                } else {
  51.                    Log.e(TAG, "ERROR! " + e.getMessage());
  52.                }
  53.            }
  54.        });
  55.    }
  56. }
说明:

更多关于请求接口的详细信息可以参考API参考。

具体代码逻辑请见:SafetyDetect-WifiDetect-Android-Sample.zip。