For details about the request API, please refer to the API reference.
For details, click SafetyDetect-WifiDetect-Android-Sample.zip to download the sample code.
This API checks characteristics of the Wi-Fi and router to be connected, analyzes the Wi-Fi information, and returns the Wi-Fi detection results after classification, helping you prevent possible attacks to your app from malicious Wi-Fi.
After you integrate WifiDetect into your app, your app can detect ARP attacks, man-in-the-middle attacks, DNS hijacking attacks, and other attacks on the Wi-Fi to be connected. If attacks are detected, your app can interrupt the user operation or ask users to confirm whether to continue the operation.
For details, please refer to Development Process.
Before using the WifiDetect API of Safety Detect, you must ensure that Huawei Mobile Services (APK) of the required version has been installed on the user device. If the installed version is not the required one, your app may stop responding after calling the API. If your app detects that the Huawei Mobile Services (APK) version installed on user devices is not the required one, it should ask the user to update Huawei Mobile Services (APK).
To check whether the installed Huawei Mobile Services (APK) version is compatible with your Android SDK version, call the isHuaweiMobileServicesAvailable() method. The sample code is as follows:
- if (HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(context) == ConnectionResult.SUCCESS){
- // The SafetyDetect SysIntegrity API is available.
- } else {
- // Prompt user to update HMS.
- }
- /*
- * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.huawei.hms.safetydetect.sample;
- import com.huawei.hmf.tasks.OnFailureListener;
- import com.huawei.hmf.tasks.OnSuccessListener;
- import com.huawei.hms.common.ApiException;
- import com.huawei.hms.support.api.entity.safetydetect.WifiDetectResponse;
- import com.huawei.hms.support.api.safetydetect.SafetyDetect;
- import com.huawei.hms.support.api.safetydetect.SafetyDetectStatusCodes;
- import android.app.Fragment;
- import android.util.Log;
- import android.view.View;
- /**
- * An example of how to use AppsCheck Service API.
- * Note that you have to configure an AppId for SafetyDetect Service first.
- */
- public class SafetyDetectWifiDetectAPIFragment extends Fragment implements View.OnClickListener {
- private void invokeGetWifiDetectStatus() {
- Log.i(TAG, "Start to getWifiDetectStatus!");
- SafetyDetectClient wifidetectClient = SafetyDetect.getClient(MainActivity.this);
- Task task = wifidetectClient.getWifiDetectStatus();
- task.addOnSuccessListener(new OnSuccessListener<WifiDetectResponse>() {
- @Override
- public void onSuccess(WifiDetectResponse wifiDetectResponse) {
- int wifiDetectStatus = wifiDetectResponse.getWifiDetectStatus();
- Log.i(TAG, "\n-1 :获取WiFi状态失败\n" + "0 :未连接WIFI\n" + "1 :当前连接的WIFI安全\n" + "2 :当前连接的WIFI不安全.");
- Log.i(TAG, "wifiDetectStatus is: " + wifiDetectStatus);
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- if (e instanceof ApiException) {
- ApiException apiException = (ApiException) e;
- Log.e(TAG,
- "Error: " + apiException.getStatusCode() + ":"
- + SafetyDetectStatusCodes.getStatusCodeString(apiException.getStatusCode()) + ": "
- + apiException.getStatusMessage());
- } else {
- Log.e(TAG, "ERROR! " + e.getMessage());
- }
- }
- });
- }
- }
For details about the request API, please refer to the API reference.
For details, click SafetyDetect-WifiDetect-Android-Sample.zip to download the sample code.