js-apis-wifi.md 4.0 KB
Newer Older
W
wusongqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
# WLAN

> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.

##  Modules to Import

```
import wifi from '@ohos.wifi_native_js';
```

## wifi.isWifiActive

isWifiActive(): boolean

Checks whether WLAN is activated.

**Return values**

| Type| Description|
| -------- | ---------------------------- |
| boolean  | Returns **true** if WLAN is activated; returns **false** otherwise.|

## wifi.getSignalLevel

getSignalLevel(rssi: number, band: number): number

Obtains the WLAN signal strength.

**Parameters**

| Name| Type| Mandatory| Description|
| ---------- | -------- | -------- | --------------------- |
| rssi       | number   | Yes| Signal strength (in dBm) of the hotspot.|
| band       | number   | Yes| Frequency band of the WLAN access point (AP).|

**Return values**

| Type| Description|
| -------- | ---------------------------- |
| number   | Signal strength obtained. The value range is 0 to 4.|

## wifi.scan

scan(): boolean

Starts a scan for WLAN.

**Return values**

| Type| Description|
| -------- | -------------------------------------------- |
| boolean  | Returns **true** if the scan is successful; returns **false** otherwise.|

## wifi.getScanInfos

getScanInfos(): Promise<Array\<WifiScanInfo>>

Obtains the scan result. This method uses a promise to return the result.

**Return values**

| Type| Description|
| ------------------------------------------------ | ---------------------- |
| Promise< Array\<[WifiScanInfo](#wifiscaninfo)> > | Promise used to return the scan result, which is a list of hotspots detected.|

## wifi.getScanInfos

getScanInfos(callback: AsyncCallback<Array\<WifiScanInfo>>): void

Obtains the scan result. This method uses an asynchronous callback to return the result.

| Name| Type| Mandatory| Description|
| ---------- | ----------------------------------------------------- | -------- | ------------------------------ |
| callback   | AsyncCallback< Array\<[WifiScanInfo](#wifiscaninfo)>> | Yes| Callback invoked to return the result, which is a list of hotspots detected.|

**Example**

```
import wifi from '@ohos.wifi_native_js';


wifi.getScanInfos(result => {
    var len = Object.keys(result).length;
    console.log("received scan info size: " + len);
    for (var i = 0; i < len; ++j) {
        console.info("ssid: " + result[i].ssid);
        console.info("bssid: " + result[i].bssid);
        console.info("securityType: " + result[i].securityType);
        console.info("rssi: " + result[i].rssi);
        console.info("band: " + result[i].band);
        console.info("frequency: " + result[i].frequency);
        console.info("timestamp: " + result[i].timestamp);
    }
});

wifi.getScanInfos().then(result => {
    var len = Object.keys(result).length;
    console.log("received scan info size: " + len);
    for (var i = 0; i < len; ++i) {
        console.info("ssid: " + result[i].ssid);
        console.info("bssid: " + result[i].bssid);
        console.info("securityType: " + result[i].securityType);
        console.info("rssi: " + result[i].rssi);
        console.info("band: " + result[i].band);
        console.info("frequency: " + result[i].frequency);
        console.info("timestamp: " + result[i].timestamp);
    }
});
```

## WifiScanInfo

Defines WLAN hotspot information.

| Name| Type| Readable/Writable| Description|
| ------------ | ---------------- | ------------ | ----------------------------- |
| ssid         | string           | Read-only| Hotspot service set identifier (SSID), in UTF-8 format.|
| bssid        | string           | Read-only| Basic service set identifier (BSSID) of the hotspot.|
| securityType | WifiSecurityType | Read-only| WLAN encryption type.|
| rssi         | number           | Read-only| Signal strength (in dBm) of the hotspot.|
| band         | number           | Read-only| Frequency band of the WLAN AP.|
| frequency    | number           | Read-only| Frequency of the WLAN AP.|
| timestamp    | number           | Read-only| Timestamp.|