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 type { P2PDiscoveryListener } from './Discovery'; 17import type wifi from '@ohos.wifi'; 18import DiscoveredPrinter from '../discovery/DiscoveredPrinter'; 19import { Log } from '@ohos/common'; 20import type { PrintServiceAdapter } from '../PrintServiceAdapter'; 21import Discovery from './Discovery'; 22import CheckEmptyUtils from '@ohos/common'; 23 24const TAG = 'P2pDiscovery'; 25 26/** 27 * Discover previously-added P2P devices, if any. 28 */ 29export class P2PDiscovery extends Discovery implements P2PDiscoveryListener { 30 /** 31 * SCHEME P2P 32 */ 33 public static readonly schemeP2p: string = 'p2p'; 34 35 private mDiscoveringPeers: boolean = false; 36 private mPrintServiceAdapter: PrintServiceAdapter; 37 38 constructor(printServiceAdapter: PrintServiceAdapter) { 39 super(); 40 this.mPrintServiceAdapter = printServiceAdapter; 41 } 42 43 /** 44 * 把发现的p2p打印机转换为DiscoveredPrinter 45 * 46 * @param device 47 */ 48 public static toPrinter(device: wifi.WifiP2pDevice): DiscoveredPrinter { 49 let path: string = this.toPath(device); 50 let deviceName: string = <string>device.deviceName; 51 if (deviceName.trim() === '') { 52 deviceName = device.deviceAddress; 53 } 54 return new DiscoveredPrinter(deviceName, path, <number>device.deviceStatus, <string>device.deviceAddress); 55 } 56 57 /** 58 * path 添加 p2p发现标记 59 * 60 * @param device 61 */ 62 public static toPath(device: wifi.WifiP2pDevice): string { 63 return this.schemeP2p + '://' + device.deviceAddress.replace(/:/g, '-'); 64 } 65 66 /** 67 * 开始发现p2打印机设备 68 */ 69 onStartDiscovery(): void { 70 if (this.mDiscoveringPeers) { 71 Log.error(TAG, 'p2p discovery is already start'); 72 return; 73 } 74 75 this.mDiscoveringPeers = true; 76 Log.info(TAG, `onStartDiscovery ${this.mDiscoveringPeers}`); 77 //开始发现 78 this.mPrintServiceAdapter.p2pMonitor.discovery(this); 79 } 80 81 /** 82 * 停止发现打印机设备 83 */ 84 onStopDiscovery(): void { 85 if (this.mDiscoveringPeers) { 86 this.mDiscoveringPeers = false; 87 //停止发现打印机 88 this.mPrintServiceAdapter.p2pMonitor.stopDiscover(this); 89 this.clearPrinterMap(false); 90 } 91 } 92 93 onPeerFound(p2pDevice): void { 94 if (CheckEmptyUtils.isEmpty(p2pDevice)) { 95 Log.error(TAG, 'p2p devices is null'); 96 return; 97 } 98 const printer = P2PDiscovery.toPrinter(p2pDevice); 99 this.printerFound(printer); 100 } 101 102 onPeerLost(p2pDevice): void { 103 if (CheckEmptyUtils.isEmpty(p2pDevice)) { 104 Log.error(TAG, 'p2p devices is null'); 105 return; 106 } 107 this.printerLost(P2PDiscovery.toPath(p2pDevice)); 108 } 109} 110