import deviceManager from '@ohos.distributedDeviceManager'; import common from '@ohos.app.ability.common'; import { KvStoreModel } from '../model/KvStoreModel'; import { BUNDLE_NAME, RemoteDeviceModel } from '../model/RemoteDeviceModel'; import Want from '@ohos.app.ability.Want'; import { DeviceDialog } from '../common/DeviceDialog'; import router from '@ohos.router'; import { class1 } from './Storage'; const TAG: string = 'testTag' const DATA_CHANGE: string = 'dataChange' const DATA_CHANGE1: number = 0 const EXIT: string = 'exit' @Entry @Component struct DistributedPage{ // @StorageLink('deviceList') deviceList: Array = AppStorage.get('deviceList')!; //private selectedIndex: number | undefined = 0; // private onSelectedIndexChange: (selectedIndex: number | undefined) => void = () => { //} @State deviceDialogWidth: number = 0 @State @Watch('dataChange') click: number = 0; @State click1: number = 0; @State @Watch('dataChange') message: string = 'Hello World'; @State inputMsg: string = ''; @State isDistributed: boolean = false @StorageLink('deviceList') deviceList: Array = [] @State selectedIndex: number | undefined = -1 private remoteDeviceModel: RemoteDeviceModel = new RemoteDeviceModel() private kvStoreModel: KvStoreModel = new KvStoreModel() private dialogController: CustomDialogController | null = null onSelectedIndexChange = async (index: number | undefined) => { console.info(TAG, `selectedIndexChange`) this.selectedIndex = index this.selectDevice() } async aboutToAppear() { this.showDeviceDialog() } // aboutToAppear(){ // this.showDeviceDialog() // } aboutToDisappear() { this.kvStoreModel.deleteKvStore() } startAbilityCallBack = (key: string) => { if (DATA_CHANGE === key) { this.kvStoreModel.put(DATA_CHANGE, this.message) } if (EXIT === key) { this.kvStoreModel.put(DATA_CHANGE, EXIT) } } selectDevice() { this.isDistributed = true if (this.selectedIndex !== undefined && (this.remoteDeviceModel === null || this.remoteDeviceModel.discoverList.length <= 0)) { console.info(TAG, `continue unauthed device: ${JSON.stringify(this.deviceList)}`) this.startAbility(this.deviceList[this.selectedIndex].networkId as string) this.clearSelectState() return } if (this.selectedIndex !== undefined) { this.remoteDeviceModel.authenticateDevice(this.deviceList[this.selectedIndex], () => { if (this.remoteDeviceModel !== null && this.remoteDeviceModel.deviceList !== null && this.selectedIndex !== undefined) { for (let i = 0; i < this.remoteDeviceModel.deviceList!.length; i++) { if (this.remoteDeviceModel.deviceList![i].deviceName === this.deviceList[this.selectedIndex].deviceName) { this.startAbility(this.remoteDeviceModel.deviceList![i].networkId); } } } }) } this.clearSelectState() } clearSelectState() { this.deviceList = [] if (this.dialogController !== null) { this.dialogController.close() } console.info(TAG, `cancelDialog`) if (this.remoteDeviceModel === undefined) { return } this.remoteDeviceModel.unregisterDeviceListCallback() } async startAbility(deviceId: string | undefined) { console.debug(TAG, `startAbility deviceId: ${deviceId}`) let context = getContext(this) as common.UIAbilityContext let choicePages:class1={} // console.log("获取当前页面"+choicePages.choicePages?.toString()) let want: Want = { bundleName: BUNDLE_NAME, abilityName: 'EntryAbility', // abilityName: choicePages.choicePages===2?'':'firstAbility', deviceId: deviceId, parameters: { isRemote: 'isRemote' } } context.startAbility(want).then((data) => { console.info(TAG, `start ability finished: ${JSON.stringify(data)}`) this.startAbilityCallBack(DATA_CHANGE) }) } dataChange() { console.info(TAG, `dataChange, message = ${this.message},`) this.kvStoreModel.put(DATA_CHANGE1.toString(), this.click.toString()) this.kvStoreModel.put(DATA_CHANGE, this.message,) } showDeviceDialog() { this.deviceList = [] // 注册监听回调,发现设备或查找到已认证设备会弹窗显示 this.remoteDeviceModel.registerDeviceListCallback(() => { this.deviceList = [] let deviceTempList = this.remoteDeviceModel.discoverList.length > 0 ? this.remoteDeviceModel.discoverList : this.remoteDeviceModel.deviceList; if (deviceTempList !== null) { for (let i = 0; i < deviceTempList!.length; i++) { console.debug(TAG, `device ${i}/${deviceTempList!.length} deviceId= ${deviceTempList![i].deviceId}, deviceName= ${deviceTempList![i].deviceName}, deviceType= ${deviceTempList![i].deviceType}`); if (deviceTempList !== null) { this.deviceList.push({ deviceId: deviceTempList![i].deviceId, deviceName: deviceTempList![i].deviceName, deviceType: deviceTempList![i].deviceType, networkId: deviceTempList![i].networkId, }) AppStorage.set('deviceList', this.deviceList) } } } }) if (this.dialogController === null) { this.dialogController = new CustomDialogController({ builder: DeviceDialog({ selectedIndex: this.selectedIndex, onSelectedIndexChange: this.onSelectedIndexChange }), cancel: () => { this.clearSelectState() }, autoCancel: true, alignment: DialogAlignment.Center, customStyle: false }) } if (this.dialogController !== null) { this.dialogController.open() } } build(){ Column(){ }.width('100%') .backgroundColor('#ccfdfcfc') .height('100%') .justifyContent(FlexAlign.Center) .padding({ left: 18, right: 32 }) .margin({ bottom: 15 }) .onAreaChange((oldArea: Area, newArea: Area) => { this.deviceDialogWidth = (newArea.width > newArea.height ? newArea.height : newArea.width) as number * 0.1 //percentage }) .onClick(()=>{ router.pushUrl({ url:'pages/Index' }) }) } }