1/* 2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 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 16import wifi from '@ohos.wifiManager' 17import { Log } from '@ohos/common'; 18 19const TAG = 'WifiP2pHelper'; 20 21export default class WifiP2pHelper { 22 async checkConnectState(printerId: string) { 23 Log.info(TAG, `checkConnectState enter...`); 24 let printerDevices = this.parsePrinterId(printerId); 25 Log.info(TAG, `printerDevices is ${printerDevices}`); 26 let connectInfo = await wifi.getP2pLinkedInfo(); 27 Log.info(TAG, `connectInfo is ${connectInfo.connectState}`); 28 if (connectInfo.connectState === P2pConnectState.CONNECTED) { 29 let groupInfo = await wifi.getCurrentGroup(); 30 if (groupInfo.ownerInfo.deviceAddress === printerDevices) { 31 return true; 32 } 33 } 34 return false; 35 } 36 37 public static checkWifiActive(): boolean { 38 let wifiState: boolean = wifi.isWifiActive(); 39 return wifiState; 40 } 41 42 private parsePrinterId(printerId: string) { 43 let split = printerId.split('//'); 44 return split[split.length - 1].replace(/-/g, ':'); 45 } 46} 47 48enum P2pConnectState { 49 DISCONNECTED = 0, 50 CONNECTED = 1, 51 UNKNOWN_ERROR = -1 52}