diff --git a/README_zh.md b/README_zh.md index 4e6e20560f4e4ea75717adc6f577eb2766d428f5..f5b378bfccfbbd3eaf11486d432d596af33dcda8 100755 --- a/README_zh.md +++ b/README_zh.md @@ -27,12 +27,14 @@ - [`Flashlight:`手电筒(JS)](https://gitee.com/openharmony/app_samples/tree/master/common/Flashlight) - [`JsHelloWorld:`你好世界(JS)](https://gitee.com/openharmony/app_samples/tree/master/common/JsHelloWorld) - [`eTSUtilBase64Codec:`Base64编解码(eTS)](https://gitee.com/openharmony/app_samples/tree/master/common/eTSUtilBase64Codec) - - data - [`eTSFileIo:`文件管理(eTS)](https://gitee.com/openharmony/app_samples/tree/master/data/eTSFileIo) - [`eTSKvStore:`分布式数据库(eTS)](https://gitee.com/openharmony/app_samples/tree/master/data/eTSKvStore) - [`eTSLiteStorage:`轻量级存储(eTS)](https://gitee.com/openharmony/app_samples/tree/master/data/eTSLiteStorage) - [`eTSRdb:`关系型数据库(eTS)](https://gitee.com/openharmony/app_samples/tree/master/data/eTSRdb) +- device + - [`eTSDeviceManager:`设备管理(eTS)](https://gitee.com/openharmony/app_samples/tree/master/device/eTSDeviceManager) + - [`eTSWlan:`WLAN(eTS)](https://gitee.com/openharmony/app_samples/tree/master/device/eTSWlan) - media - [`JsAudioPlayer:`音频播放和管理(JS)](https://gitee.com/openharmony/app_samples/tree/master/media/JsAudioPlayer) - [`JsVideo:`视频播放(JS)](https://gitee.com/openharmony/app_samples/tree/master/media/JsVideo) @@ -85,6 +87,7 @@ - Util - [`eTSUtilScope:`范围判断(eTS)](https://gitee.com/openharmony/app_samples/tree/master/Util/eTSUtilScope) - [`eTSUtilLruBuffer:`缓冲区(eTS)](https://gitee.com/openharmony/app_samples/tree/master/Util/eTSUtilLruBuffer) + - [`eTSUtilRationalNumber:`有理数(eTS)](https://gitee.com/openharmony/app_samples/tree/master/Util/eTSUtilRationalNumber) ## 使用说明 diff --git a/device/eTSWlan/README_zh.md b/device/eTSWlan/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..866aff96afaf81c39693a27b001b761f609f2c38 --- /dev/null +++ b/device/eTSWlan/README_zh.md @@ -0,0 +1,20 @@ +# 网络与连接-WLAN + +### 简介 + +本示例展示了在eTS中WLAN的基本使用,包括禁用和启用WLAN、WLAN扫描和获取扫描结果、WLAN状态监听、WiFi连接状态监听、获取IP信息、获取国家码、判断设备是否支持WLAN相关特性。 + +### 使用说明 + +1、启动应用后会判断WLAN是否激活,如果是激活状态,会扫描并展示可用WiFi列表,同时获取已连接WiFi信息并展示。 + +2、点击界面的Switch开关可以禁用和激活WLAN,界面会监听WLAN状态扫描可用WiFi列表,也会监听WiFi连接状态展示已连接WiFi。 + +3、点击可用WLAN列表中的WLAN信息,可以连接WiFi,如果是加密类型,会弹窗输入密码后连接。 + +4、点击首页右上角的**关于**图标,进入关于界面,展示获取的IP信息、国家码和支持WLAN相关特性信息。 + +### 约束与限制 + +1、本示例仅支持标准系统上运行。 + diff --git a/device/eTSWlan/build.gradle b/device/eTSWlan/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..40d7a3d7c8ec041dbbcbce83990eb3897047d276 --- /dev/null +++ b/device/eTSWlan/build.gradle @@ -0,0 +1,34 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +// For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 8 + supportSystem "standard" +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } + dependencies { + classpath 'com.huawei.ohos:hap:3.0.5.2' + classpath 'com.huawei.ohos:decctest:1.2.7.2' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } +} diff --git a/device/eTSWlan/entry/build.gradle b/device/eTSWlan/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..30b7ce4d32977e84afa220954ce819137aefd049 --- /dev/null +++ b/device/eTSWlan/entry/build.gradle @@ -0,0 +1,21 @@ +apply plugin: 'com.huawei.ohos.hap' +// For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 8 + defaultConfig { + compatibleSdkVersion 7 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) + testImplementation 'junit:junit:4.13.1' +} diff --git a/device/eTSWlan/entry/src/main/config.json b/device/eTSWlan/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..852440dd100a4574e5ab01fdac00d153b0dda305 --- /dev/null +++ b/device/eTSWlan/entry/src/main/config.json @@ -0,0 +1,68 @@ +{ + "app": { + "bundleName": "ohos.samples.etswlan", + "vendor": "samples", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.etswlan", + "name": ".MyApplication", + "mainAbility": ".MainAbility", + "srcPath": "", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "visible": true, + "srcPath": "MainAbility", + "name": ".MainAbility", + "srcLanguage": "ets", + "icon": "$media:icon", + "description": "$string:description_mainability", + "formsEnabled": false, + "label": "$string:entry_MainAbility", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "mode": { + "syntax": "ets", + "type": "pageAbility" + }, + "pages": [ + "pages/index", + "pages/about" + ], + "name": ".MainAbility", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/ets/MainAbility/app.ets b/device/eTSWlan/entry/src/main/ets/MainAbility/app.ets new file mode 100644 index 0000000000000000000000000000000000000000..bf28e58b36cf11db491d3f00768ef2c9e41d05cf --- /dev/null +++ b/device/eTSWlan/entry/src/main/ets/MainAbility/app.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +export default { + onCreate() { + console.info('Application onCreate') + }, + onDestroy() { + console.info('Application onDestroy') + }, +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/ets/MainAbility/component/availableWifi.ets b/device/eTSWlan/entry/src/main/ets/MainAbility/component/availableWifi.ets new file mode 100644 index 0000000000000000000000000000000000000000..d20e3b6135da0386bd51357c38457e1bbcfd6cc6 --- /dev/null +++ b/device/eTSWlan/entry/src/main/ets/MainAbility/component/availableWifi.ets @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +import prompt from '@system.prompt' +import { WifiModel } from '../model/wifiModel' +import { WifiView } from '../component/wifiView' +import { PasswordDialog } from '../component/pswDialog' + +const TAG = '[AvailableWiFi]' +let self = null + +@Component +export struct AvailableWifi { + private wifiModel = new WifiModel() + @State wifiList: Array = [] + private linkedInfo: any = null + private selectIndex: number = -1 + private pswDialogController: CustomDialogController = new CustomDialogController({ + builder: PasswordDialog({ scanInfo: this.wifiList[this.selectIndex], action: this.onAccept }), + autoCancel: true + }) + + onAccept(scanInfo, psw) { + console.info(`${TAG} connect wifi`) + self.wifiModel.connectNetwork(scanInfo, psw) + } + + aboutToAppear() { + self = this + } + + build() { + Column() { + Row() { + Text($r('app.string.available_wlan')) + .fontSize(22) + .layoutWeight(1) + } + .width('95%') + + List() { + ForEach(this.wifiList, (item, index) => { + ListItem() { + WifiView({ wifi: item }) + } + .onClick(() => { + console.info(TAG + 'connect wifi') + this.selectIndex = index + if (this.linkedInfo !== null && item.ssid === this.linkedInfo.ssid) { + prompt.showToast({ message: 'this wifi is connected' }) + return + } + if (item.securityType === 0 || item.securityType === 1) { + this.wifiModel.connectNetwork(item, '') + return + } + this.pswDialogController.open() + }) + }, item => JSON.stringify(item)) + } + .layoutWeight(1) + .divider({ strokeWidth: 1, color: Color.Gray, startMargin: 10, endMargin: 10 }) + .margin(10) + } + .margin({ top: 15 }) + } +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/ets/MainAbility/component/infoView.ets b/device/eTSWlan/entry/src/main/ets/MainAbility/component/infoView.ets new file mode 100644 index 0000000000000000000000000000000000000000..d2ddb37e3b68188ef1f201bc32fd37621ce28664 --- /dev/null +++ b/device/eTSWlan/entry/src/main/ets/MainAbility/component/infoView.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +@Component +export struct InfoView { + private infoList: Array = [] + + build() { + Column() { + ForEach(this.infoList, (item, index) => { + Column() { + Text(item.key) + .fontColor(Color.Black) + .fontSize(20) + .width('100%') + Text(item.value) + .fontColor(Color.Black) + .fontSize(20) + .width('100%') + .margin({ top: 5, bottom: 5 }) + if (index < this.infoList.length - 1) { + Divider().color(Color.Grey) + } + } + .padding(5) + }, item => JSON.stringify(item)) + } + .margin(10) + .border({ width: 1, color: Color.Gray, radius: 15 }) + } +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/ets/MainAbility/component/pswDialog.ets b/device/eTSWlan/entry/src/main/ets/MainAbility/component/pswDialog.ets new file mode 100644 index 0000000000000000000000000000000000000000..efdfd8f9750d515fb1a7592a59ef8beb3b99c46a --- /dev/null +++ b/device/eTSWlan/entry/src/main/ets/MainAbility/component/pswDialog.ets @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +@CustomDialog +export struct PasswordDialog { + private scanInfo: any = null + private psw: string = '' + private controller: CustomDialogController + private action: (scanInfo, psw) => void + + build() { + Column() { + Text(this.scanInfo.ssid) + .fontSize(20) + .width('95%') + TextInput({ placeholder: 'input password' }) + .type(InputType.Password) + .placeholderColor(Color.Gray) + .fontSize(19) + .margin({ top: 15 }) + .width('95%') + .onChange((value: string) => { + this.psw = value + }) + Row() { + Button() { + Text($r('app.string.sure')) + .fontColor(Color.Blue) + .fontSize(17) + } + .layoutWeight(7) + .backgroundColor(Color.White) + .margin(5) + .onClick(() => { + this.controller.close() + this.action(this.scanInfo, this.psw) + }) + + Text() + .width(1).height(35) + .backgroundColor('#8F8F8F') + Button() { + Text($r('app.string.cancel')) + .fontColor(Color.Red) + .fontSize(17) + } + .layoutWeight(7) + .backgroundColor(Color.White) + .margin(5) + .onClick(() => { + this.controller.close() + }) + } + .width('100%') + .margin({ top: '3%' }) + } + .padding(15) + } +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/ets/MainAbility/component/titleBar.ets b/device/eTSWlan/entry/src/main/ets/MainAbility/component/titleBar.ets new file mode 100644 index 0000000000000000000000000000000000000000..cd91e1db061c5d905798a4295350a48faf34e229 --- /dev/null +++ b/device/eTSWlan/entry/src/main/ets/MainAbility/component/titleBar.ets @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ +import router from '@system.router'; + +@Component +export struct TitleBar { + build() { + Row() { + Text($r('app.string.entry_MainAbility')) + .fontColor(Color.White) + .fontSize(25) + .layoutWeight(1) + Button() { + Image($r('app.media.about')) + .size({ width: 60, height: '95%' }) + .objectFit(ImageFit.Contain) + }.type(ButtonType.Normal) + .height('90%') + .backgroundColor('#0D9FFB') + .onClick(() => { + router.push({ uri: 'pages/about' }) + }) + } + .width('100%') + .height('8%') + .constraintSize({ minHeight: 50 }) + .padding({ left: 15 }) + .backgroundColor('#0D9FFB') + } +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/ets/MainAbility/component/wifiView.ets b/device/eTSWlan/entry/src/main/ets/MainAbility/component/wifiView.ets new file mode 100644 index 0000000000000000000000000000000000000000..bc3692865926a03d7c0a7e6410de31907928e03d --- /dev/null +++ b/device/eTSWlan/entry/src/main/ets/MainAbility/component/wifiView.ets @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +@Component +export struct WifiView { + private wifi: any = null + private securityString: Resource = $r('app.string.encryption') + @State isLock: boolean = true + + aboutToAppear() { + if (this.wifi.securityType === 0 || this.wifi.securityType === 1) { + this.securityString = $r('app.string.open') + this.isLock = false + } + } + + build() { + Row() { + Column() { + Text(this.wifi.ssid) + .fontSize(20) + .width('100%') + Text(this.securityString) + .fontSize(18) + .fontColor(Color.Gray) + .width('100%') + } + .layoutWeight(1) + + Stack({ alignContent: Alignment.BottomEnd }) { + Image($r('app.media.wifi')) + .height(30).width(30) + .objectFit(ImageFit.Contain) + if (this.isLock) { + Image($r("app.media.lock")) + .objectFit(ImageFit.Contain) + .width(15).height(15) + } + } + .width(40).height(40) + .margin({ right: 10 }) + } + .backgroundColor(Color.White) + .width('100%') + .padding(10) + } +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/ets/MainAbility/model/wifiModel.ets b/device/eTSWlan/entry/src/main/ets/MainAbility/model/wifiModel.ets new file mode 100644 index 0000000000000000000000000000000000000000..44bee75aa73628e21ffb5d5fd6ed21a6f06dcd16 --- /dev/null +++ b/device/eTSWlan/entry/src/main/ets/MainAbility/model/wifiModel.ets @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +import wifi from '@ohos.wifi'; +import prompt from '@system.prompt'; + +const TAG = '[WiFiModel]' + +export class WifiModel { + private eventListener = null + + constructor() { + this.eventListener = new wifi.EventListener() + } + + addConnectListener(callback) { + this.eventListener.on('wifiConnectionChange', (state) => { + console.info(`${TAG} wifiConnectionChange ${state}`) + callback(state) + }) + } + + addWifiStateListener(callback) { + this.eventListener.on('wifiStateChange', (state) => { + console.info(`${TAG} wifiStateChange ${state}`) + callback(state) + }) + } + + getScanInfos(callback) { + wifi.scan() + console.info(`${TAG} scanWifi`) + wifi.getScanInfos((result) => { + let wifiList = [] + console.info(`${TAG} wifi received scan info call back:${JSON.stringify(result)}`) + if (result === null || result.length === 0) { + callback(wifiList) + return + } + for (var i = 0; i < result.length; ++i) { + wifiList.push({ + ssid: result[i].ssid, + bssid: result[i].bssid, + securityType: result[i].securityType, + rssi: result[i].rssi, + band: result[i].band, + frequency: result[i].frequency, + timestamp: result[i].timestamp, + }) + } + callback(wifiList) + }) + } + + connectNetwork(scanInfo, psw) { + prompt.showToast({ message: 'connecting' }) + console.info(`${TAG} connectNetwork bssid=${scanInfo.bssid}`) + let deviceConfigs = wifi.getDeviceConfigs() + console.info(TAG + 'getDeviceConfigs:' + JSON.stringify(deviceConfigs)) + for (let i = 0;i < deviceConfigs.length; i++) { + if (deviceConfigs[i].bssid === scanInfo.bssid) { + if (deviceConfigs[i].preSharedKey !== psw) { + prompt.showToast({ message: 'err password' }) + return + } + prompt.showToast({ message: `connect result = ${wifi.connectToDevice(deviceConfigs[i])}` }) + return + } + } + let deviceConfig = { + ssid: scanInfo.ssid, + bssid: scanInfo.bssid, + preSharedKey: psw, + isHiddenSsid: false, + securityType: scanInfo.securityType + } + if (wifi.connectToDevice(deviceConfig)) { + prompt.showToast({ message: 'connect success' }) + wifi.addDeviceConfig(deviceConfig) + } else { + prompt.showToast({ message: 'connect fail' }) + } + } + + resolveIP(ip) { + let address: string = ip.toString() + if (address === '0') { + return '00:00:000:000' + } + address.substring(0, 2) + return `${address.substring(0, 2)}:${address.substring(2, 4)}:${address.substring(4, 7)}:${address.substring(7, 10)}` + } + + getIpInfo() { + let ipInfoList = [] + let ipInfo = wifi.getIpInfo() + console.info(`${TAG} getIpInfo=${JSON.stringify(ipInfo)}`) + ipInfoList.push({ key: $r('app.string.ip_address'), value: this.resolveIP(ipInfo.ipAddress) }) + ipInfoList.push({ key: $r('app.string.gate_way'), value: this.resolveIP(ipInfo.gateway) }) + ipInfoList.push({ key: $r('app.string.net_mask'), value: this.resolveIP(ipInfo.netmask) }) + ipInfoList.push({ key: $r('app.string.primary_dns'), value: this.resolveIP(ipInfo.primaryDns) }) + ipInfoList.push({ key: $r('app.string.second_dns'), value: this.resolveIP(ipInfo.secondDns) }) + ipInfoList.push({ key: $r('app.string.server_ip'), value: this.resolveIP(ipInfo.serverIp) }) + ipInfoList.push({ key: $r('app.string.lease_duration'), value: ipInfo.leaseDuration.toString() }) + return ipInfoList + } + + getCountryCode() { + let countryCodeList = [] + let countryCode = wifi.getCountryCode() + countryCodeList.push({ key: $r('app.string.country_code'), value: 'CH' }) + return countryCodeList + } + + getFeatureSupport() { + let featureSupportedList = [] + featureSupportedList.push({ + key: $r('app.string.infrastructure_feature'), + value: wifi.isFeatureSupported(0x0001).toString() + }) + featureSupportedList.push({ key: $r('app.string.ghz_feature'), value: wifi.isFeatureSupported(0x0002).toString() }) + featureSupportedList.push({ + key: $r('app.string.gas_anqp_feature'), + value: wifi.isFeatureSupported(0x0004).toString() + }) + featureSupportedList.push({ key: $r('app.string.wifi_direct'), value: wifi.isFeatureSupported(0x0008).toString() }) + featureSupportedList.push({ key: $r('app.string.soft_ap'), value: wifi.isFeatureSupported(0x0010).toString() }) + featureSupportedList.push({ key: $r('app.string.wifi_aware'), value: wifi.isFeatureSupported(0x0040).toString() }) + return featureSupportedList + } +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/ets/MainAbility/pages/about.ets b/device/eTSWlan/entry/src/main/ets/MainAbility/pages/about.ets new file mode 100644 index 0000000000000000000000000000000000000000..8b2e394955c909327a97d2b53ff81b941eacf64d --- /dev/null +++ b/device/eTSWlan/entry/src/main/ets/MainAbility/pages/about.ets @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +import router from '@system.router' +import { WifiModel } from '../model/wifiModel' +import { InfoView } from '../component/infoView.ets' + +@Entry +@Component +struct About { + private wifiModel: WifiModel = new WifiModel() + + build() { + Column() { + Row() { + Image($r('app.media.ic_back')) + .size({ width: 50, height: '100%' }) + .objectFit(ImageFit.Contain) + .onClick(() => { + router.back() + }) + Text($r('app.string.about')) + .fontColor(Color.White) + .fontSize(25) + .layoutWeight(1) + } + .width('100%').height('8%') + .constraintSize({ minHeight: 50 }) + .backgroundColor('#0D9FFB') + + Scroll() { + Column() { + InfoView({ infoList: this.wifiModel.getIpInfo() }) + InfoView({ infoList: this.wifiModel.getCountryCode() }) + InfoView({ infoList: this.wifiModel.getFeatureSupport() }) + } + } + .layoutWeight(1) + } + } +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/ets/MainAbility/pages/index.ets b/device/eTSWlan/entry/src/main/ets/MainAbility/pages/index.ets new file mode 100644 index 0000000000000000000000000000000000000000..7f7086cb37039fbef24bcf701e7718ef67df61e9 --- /dev/null +++ b/device/eTSWlan/entry/src/main/ets/MainAbility/pages/index.ets @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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. + */ + +import wifi from '@ohos.wifi'; +import { WifiModel } from '../model/wifiModel' +import { TitleBar } from '../component/titleBar' +import { AvailableWifi } from '../component/availableWifi' + +const TAG = '[eTSWiFi]' +let self = null + +@Entry +@Component +struct Index { + private wifiModel: WifiModel = new WifiModel() + private linkedInfo: any = { ssid: "", bssid: "", networkId: 0, rssi: 0, band: 0 } + @State wifiList: Array = [] + @State isLinked: boolean = false + @State isSwitchOn: boolean = false + + scan() { + this.wifiModel.getScanInfos((result) => { + if (self.isSwitchOn) { + self.wifiList = result + if (result.length === 0) { + setTimeout(() => { + self.scan() + }, 1000) + } + } + }) + } + + getLinkedInfo() { + wifi.getLinkedInfo((linkedInfo) => { + console.info(`${TAG} linkedInfo ${JSON.stringify(linkedInfo)}`) + if (linkedInfo === null) { + self.isLinked = false + self.linkedInfo = { ssid: "", bssid: "", networkId: 0, rssi: 0, band: 0 } + return + } + self.linkedInfo = linkedInfo + self.isLinked = self.linkedInfo.bssid.length > 0 ? true : false + }) + } + + addListener() { + this.wifiModel.addConnectListener((state) => { + console.info(`${TAG} connectLisener state=' ${state}`) + if (state === 1 || state === 4) { + self.isLinked = true + self.getLinkedInfo() + return + } + self.isLinked = false + }) + this.wifiModel.addWifiStateListener((state) => { + console.info(`${TAG} wifiStateLisener state=' ${state}`) + self.wifiList = [] + if (state === 2 || state === 3) { + self.scan() + } + }) + } + + aboutToAppear() { + self = this + if (wifi.isWifiActive()) { + console.info(`${TAG} wifi active`) + this.isSwitchOn = true + this.scan() + if (wifi.isConnected()) { + this.getLinkedInfo() + } + } + this.addListener() + } + + build() { + Column() { + TitleBar() + Row() { + Text($r('app.string.wlan')) + .fontSize(22) + .fontWeight(FontWeight.Bold) + .layoutWeight(1) + Column() { + Toggle({ type: ToggleType.Switch, isOn: this.isSwitchOn }) + .onChange((isOn: boolean) => { + console.info(`${TAG} wifi swtich is ${isOn}`) + if (isOn) { + this.isSwitchOn = true + wifi.enableWifi() + return + } + this.isSwitchOn = false + this.isLinked = false + wifi.disconnect() + wifi.disableWifi() + }) + } + } + .width('95%') + + if (this.isLinked) { + Text($r('app.string.connected')) + .fontSize(22) + .width('95%') + Row() { + Text(this.linkedInfo.ssid) + .fontSize(20) + .fontColor(Color.Black) + .layoutWeight(1) + Text($r('app.string.connected')) + .fontSize(18) + .fontColor(Color.Black) + } + .width('95%') + .padding(10) + .margin(10) + .border({ radius: 15, color: Color.Gray, width: 1 }) + .backgroundColor(Color.White) + } + AvailableWifi({ wifiList: this.wifiList, linkedInfo: this.linkedInfo }) + } + .size({ width: '100%', height: '100%' }) + .backgroundColor('#F5F5F5') + } +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/resources/base/element/string.json b/device/eTSWlan/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..0fa7e414a8f85f80364ce97fdb1b6d9df2b78278 --- /dev/null +++ b/device/eTSWlan/entry/src/main/resources/base/element/string.json @@ -0,0 +1,100 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "eTSWLAN" + }, + { + "name": "description_mainability", + "value": "eTSWLAN Ability" + }, + { + "name": "cancel", + "value": "Cancel" + }, + { + "name": "sure", + "value": "Sure" + }, + { + "name": "wlan", + "value": "WLAN" + }, + { + "name": "available_wlan", + "value": "Available WLAN" + }, + { + "name": "connected", + "value": "Connected" + }, + { + "name": "about", + "value": "About" + }, + { + "name": "ip_address", + "value": "IP address:" + }, + { + "name": "gate_way", + "value": "Gateway:" + }, + { + "name": "net_mask", + "value": "Netmask:" + }, + { + "name": "primary_dns", + "value": "Primary Dns IP address:" + }, + { + "name": "second_dns", + "value": "Second DNS IP address:" + }, + { + "name": "server_ip", + "value": "DHCP server IP:" + }, + { + "name": "lease_duration", + "value": "IP address lease duration:" + }, + { + "name": "country_code", + "value": "Country code:" + }, + { + "name": "infrastructure_feature", + "value": "Infrastructure Feature:" + }, + { + "name": "ghz_feature", + "value": "5 GHz Feature:" + }, + { + "name": "gas_anqp_feature", + "value": "GAS/ANQP Feature:" + }, + { + "name": "wifi_direct", + "value": "WiFi-Direct Feature:" + }, + { + "name": "soft_ap", + "value": "Soft AP Feature:" + }, + { + "name": "wifi_aware", + "value": "WiFi Aware Feature:" + }, + { + "name": "encryption", + "value": "encrypted" + }, + { + "name": "open", + "value": "open" + } + ] +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/resources/base/media/about.png b/device/eTSWlan/entry/src/main/resources/base/media/about.png new file mode 100644 index 0000000000000000000000000000000000000000..2613fc352850e45f0582fa825183e3928cef3378 Binary files /dev/null and b/device/eTSWlan/entry/src/main/resources/base/media/about.png differ diff --git a/device/eTSWlan/entry/src/main/resources/base/media/ic_back.png b/device/eTSWlan/entry/src/main/resources/base/media/ic_back.png new file mode 100644 index 0000000000000000000000000000000000000000..9f793557fd1ba7a4941e2ba4dea6fc2316eb5f24 Binary files /dev/null and b/device/eTSWlan/entry/src/main/resources/base/media/ic_back.png differ diff --git a/device/eTSWlan/entry/src/main/resources/base/media/icon.png b/device/eTSWlan/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/device/eTSWlan/entry/src/main/resources/base/media/icon.png differ diff --git a/device/eTSWlan/entry/src/main/resources/base/media/lock.png b/device/eTSWlan/entry/src/main/resources/base/media/lock.png new file mode 100644 index 0000000000000000000000000000000000000000..b78d86d2fc3afcc542f069b75ada2338fcf1a71a Binary files /dev/null and b/device/eTSWlan/entry/src/main/resources/base/media/lock.png differ diff --git a/device/eTSWlan/entry/src/main/resources/base/media/wifi.png b/device/eTSWlan/entry/src/main/resources/base/media/wifi.png new file mode 100644 index 0000000000000000000000000000000000000000..7017ea920f2683095b49d9f8221e40d7697f9943 Binary files /dev/null and b/device/eTSWlan/entry/src/main/resources/base/media/wifi.png differ diff --git a/device/eTSWlan/entry/src/main/resources/en/element/string.json b/device/eTSWlan/entry/src/main/resources/en/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..55c25ef852b8c14e302d2f4963ecb4764776cf8c --- /dev/null +++ b/device/eTSWlan/entry/src/main/resources/en/element/string.json @@ -0,0 +1,100 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "eTSWLAN" + }, + { + "name": "description_mainability", + "value": "eTSWLAN Ability" + }, + { + "name": "cancel", + "value": "Cancel" + }, + { + "name": "wlan", + "value": "WLAN" + }, + { + "name": "sure", + "value": "Sure" + }, + { + "name": "available_wlan", + "value": "Available WLAN" + }, + { + "name": "connected", + "value": "Connected" + }, + { + "name": "about", + "value": "About" + }, + { + "name": "ip_address", + "value": "IP address:" + }, + { + "name": "gate_way", + "value": "Gateway:" + }, + { + "name": "net_mask", + "value": "Netmask:" + }, + { + "name": "primary_dns", + "value": "Primary Dns IP address:" + }, + { + "name": "second_dns", + "value": "Second DNS IP address:" + }, + { + "name": "server_ip", + "value": "DHCP server IP:" + }, + { + "name": "lease_duration", + "value": "IP address lease duration:" + }, + { + "name": "country_code", + "value": "Country code:" + }, + { + "name": "infrastructure_feature", + "value": "Infrastructure Feature:" + }, + { + "name": "ghz_feature", + "value": "5 GHz Feature:" + }, + { + "name": "gas_anqp_feature", + "value": "GAS/ANQP Feature:" + }, + { + "name": "wifi_direct", + "value": "WiFi-Direct Feature:" + }, + { + "name": "soft_ap", + "value": "Soft AP Feature:" + }, + { + "name": "wifi_aware", + "value": "WiFi Aware Feature:" + }, + { + "name": "encryption", + "value": "encrypted" + }, + { + "name": "open", + "value": "open" + } + ] +} \ No newline at end of file diff --git a/device/eTSWlan/entry/src/main/resources/zh/element/string.json b/device/eTSWlan/entry/src/main/resources/zh/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..104950ec13dbe6e6c3731bd756f64e9ea2547da3 --- /dev/null +++ b/device/eTSWlan/entry/src/main/resources/zh/element/string.json @@ -0,0 +1,100 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "eTSWLAN" + }, + { + "name": "description_mainability", + "value": "eTSWLAN Ability" + }, + { + "name": "cancel", + "value": "取消" + }, + { + "name": "sure", + "value": "确定" + }, + { + "name": "wlan", + "value": "WLAN" + }, + { + "name": "available_wlan", + "value": "可用WLAN" + }, + { + "name": "connected", + "value": "已连接" + }, + { + "name": "about", + "value": "关于" + }, + { + "name": "ip_address", + "value": "IP地址:" + }, + { + "name": "gate_way", + "value": "网关:" + }, + { + "name": "net_mask", + "value": "掩码:" + }, + { + "name": "primary_dns", + "value": "主DNS服务器:" + }, + { + "name": "second_dns", + "value": "备DNS服务器:" + }, + { + "name": "server_ip", + "value": "DHCP服务端ip:" + }, + { + "name": "lease_duration", + "value": "IP地址租用时长:" + }, + { + "name": "country_code", + "value": "国家码:" + }, + { + "name": "infrastructure_feature", + "value": "基础结构模式特性:" + }, + { + "name": "ghz_feature", + "value": "5 GHz 宽带特性:" + }, + { + "name": "gas_anqp_feature", + "value": "GAS/ANQP特性:" + }, + { + "name": "wifi_direct", + "value": "WiFi-Direct特性:" + }, + { + "name": "soft_ap", + "value": "Soft AP特性:" + }, + { + "name": "wifi_aware", + "value": "WiFi Aware组网特性:" + }, + { + "name": "encryption", + "value": "加密" + }, + { + "name": "open", + "value": "开放" + } + ] +} \ No newline at end of file diff --git a/device/eTSWlan/screenshots/device/about.png b/device/eTSWlan/screenshots/device/about.png new file mode 100644 index 0000000000000000000000000000000000000000..0bd29a963b7d150e90fee835d3c358e85374a6ed Binary files /dev/null and b/device/eTSWlan/screenshots/device/about.png differ diff --git a/device/eTSWlan/screenshots/device/main.png b/device/eTSWlan/screenshots/device/main.png new file mode 100644 index 0000000000000000000000000000000000000000..89be60c2bc65f363f802fda83dafa103818e7586 Binary files /dev/null and b/device/eTSWlan/screenshots/device/main.png differ diff --git a/device/eTSWlan/screenshots/device/password.png b/device/eTSWlan/screenshots/device/password.png new file mode 100644 index 0000000000000000000000000000000000000000..a5c8b4fee5109a1858eb211fcbea178596ed18c8 Binary files /dev/null and b/device/eTSWlan/screenshots/device/password.png differ diff --git a/device/eTSWlan/settings.gradle b/device/eTSWlan/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..4773db73233a570c2d0c01a22e75321acfbf7a07 --- /dev/null +++ b/device/eTSWlan/settings.gradle @@ -0,0 +1 @@ +include ':entry'