1/* 2 * Copyright (c) 2022 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 */ 15import bluetooth from '@ohos.bluetooth' 16import logger from '../Model/Logger' 17import { TitleBar } from '../Commom/TitleBar' 18import { PinDialog } from '../Commom/PinDialog' 19 20const TAG: string = 'Index' 21const TIME: number = 0 // 设备可被发现的持续时间 22 23@Entry 24@Component 25struct Index { 26 @State isOn: boolean = false 27 @State deviceList: Array<string> = [] 28 @State discoveryList: Array<string> = [] 29 private bluetoothState: boolean = false 30 private pinDialogController: CustomDialogController | null = null 31 private intervalId: number = -1 32 handlerClickButton = () => { 33 let context = getContext(this) as any 34 context.terminateSelf() 35 } 36 37 foundDevices() { 38 bluetooth.on('bluetoothDeviceFind', (data) => { 39 logger.info(TAG, `enter on bluetoothDeviceFind`) 40 if (data !== null && data.length > 0) { 41 if (this.discoveryList.indexOf(data[0]) === -1 && this.deviceList.indexOf(data[0]) === -1) { 42 this.discoveryList.push(data[0]) 43 } 44 logger.info(TAG, `discoveryList = ${JSON.stringify(this.discoveryList)}`) 45 } 46 let list = bluetooth.getPairedDevices() 47 if (list !== null && list.length > 0) { 48 this.deviceList = list 49 logger.info(TAG, `deviceList = ${JSON.stringify(this.deviceList)}`) 50 } 51 }) 52 bluetooth.startBluetoothDiscovery() 53 bluetooth.setBluetoothScanMode(bluetooth.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, TIME) 54 } 55 56 async aboutToAppear() { 57 let state = bluetooth.getState() 58 if (state === bluetooth.BluetoothState.STATE_ON) { 59 this.isOn = true 60 this.foundDevices() 61 } 62 if (state === bluetooth.BluetoothState.STATE_OFF) { 63 this.isOn = false 64 } 65 66 bluetooth.on('pinRequired', (data) => { 67 logger.info(TAG, `enter pinRequired`) 68 this.pinDialogController = new CustomDialogController({ 69 builder: PinDialog({ data: data }), 70 autoCancel: true, 71 alignment: DialogAlignment.Bottom 72 }) 73 this.pinDialogController.open() 74 logger.info(TAG, `onPinRequiredData = ${JSON.stringify(data)}`) 75 }) 76 77 bluetooth.on('bondStateChange', (data) => { 78 logger.info(TAG, `enter bondStateChange`) 79 logger.info(TAG, `data = ${JSON.stringify(data)}`) 80 if (data.state === bluetooth.BondState.BOND_STATE_BONDED) { 81 logger.info(TAG, `BOND_STATE_BONDED`) 82 let index = this.discoveryList.indexOf(data.deviceId) 83 this.discoveryList.splice(index, 1) 84 this.deviceList = bluetooth.getPairedDevices() 85 } 86 if (data.state === bluetooth.BondState.BOND_STATE_INVALID) { 87 logger.info(TAG, `BOND_STATE_INVALID`) 88 this.deviceList = bluetooth.getPairedDevices() 89 } 90 logger.info(TAG, `bondStateChange,data = ${JSON.stringify(data)}`) 91 }) 92 93 this.intervalId = setInterval(() => { 94 this.discoveryList = [] 95 this.foundDevices() 96 }, 30000) 97 98 } 99 100 initBluetooth() { 101 bluetooth.on('stateChange', (data) => { 102 logger.info(TAG, `enter on stateChange`) 103 if (data === bluetooth.BluetoothState.STATE_ON) { 104 logger.info(TAG, `enter BluetoothState.STATE_ON`) 105 this.foundDevices() 106 } 107 if (data === bluetooth.BluetoothState.STATE_OFF) { 108 logger.info(TAG, `enter BluetoothState.STATE_OFF`) 109 bluetooth.off('bluetoothDeviceFind', (data) => { 110 logger.info(TAG, `offBluetoothDeviceFindData = ${JSON.stringify(data)}`) 111 }) 112 bluetooth.stopBluetoothDiscovery() 113 this.discoveryList = [] 114 } 115 logger.info(TAG, `BluetoothState = ${JSON.stringify(data)}`) 116 }) 117 bluetooth.enableBluetooth() 118 } 119 120 build() { 121 Column() { 122 TitleBar({ handlerClickButton: this.handlerClickButton }) 123 Scroll() { 124 Column() { 125 Row() { 126 Column() { 127 Text($r('app.string.bluetooth')) 128 .fontSize(30) 129 .margin({ top: 20 }) 130 .alignSelf(ItemAlign.Start) 131 if (true === this.isOn) { 132 Text($r('app.string.discovery')) 133 .fontSize(20) 134 .alignSelf(ItemAlign.Start) 135 } 136 } 137 138 Blank() 139 140 Column() { 141 Toggle({ type: ToggleType.Switch, isOn: this.isOn }) 142 .selectedColor('#ff2982ea') 143 .key('toggleBtn') 144 .onChange((isOn: boolean) => { 145 if (isOn) { 146 this.isOn = true 147 this.initBluetooth() 148 } else { 149 this.isOn = false 150 bluetooth.disableBluetooth() 151 this.deviceList = [] 152 this.discoveryList = [] 153 } 154 }) 155 } 156 } 157 .width('90%') 158 159 if (this.isOn) { 160 Divider() 161 .vertical(false) 162 .strokeWidth(10) 163 .color('#ffece7e7') 164 .lineCap(LineCapStyle.Butt) 165 .margin('1%') 166 167 Text($r('app.string.paired_device')) 168 .fontSize(25) 169 .fontColor('#ff565555') 170 .margin({ left: '5%' }) 171 .alignSelf(ItemAlign.Start) 172 173 ForEach(this.deviceList, (item) => { 174 Row() { 175 Text(item) 176 .fontSize(20) 177 } 178 .alignSelf(ItemAlign.Start) 179 .width('100%') 180 .height(50) 181 .margin({ left: '5%', top: '1%' }) 182 .key('pairedDevice') 183 .onClick(() => { 184 AlertDialog.show({ 185 title: $r('app.string.disconnect'), 186 message: '此操作将会断开您与以下设备的连接:' + item, 187 primaryButton: { 188 value: $r('app.string.cancel'), 189 action: () => { 190 } 191 }, 192 secondaryButton: { 193 value: $r('app.string.confirm'), 194 action: () => { 195 let deleteStatus = bluetooth.cancelPairedDevice(item); 196 logger.info(TAG, `deleteStatus = ${deleteStatus}`) 197 if (deleteStatus === true) { 198 this.deviceList = bluetooth.getPairedDevices() 199 this.discoveryList = [] 200 bluetooth.startBluetoothDiscovery() 201 } 202 } 203 } 204 }) 205 }) 206 }) 207 208 Divider() 209 .vertical(false) 210 .strokeWidth(10) 211 .color('#ffece7e7') 212 .lineCap(LineCapStyle.Butt) 213 .margin('1%') 214 215 Text($r('app.string.available_device')) 216 .fontSize(25) 217 .fontColor('#ff565555') 218 .margin({ left: '5%', bottom: '2%' }) 219 .alignSelf(ItemAlign.Start) 220 221 ForEach(this.discoveryList, (item) => { 222 Row() { 223 Text(item) 224 .fontSize(20) 225 } 226 .alignSelf(ItemAlign.Start) 227 .width('100%') 228 .height(50) 229 .margin({ left: '5%', top: '1%' }) 230 .onClick(() => { 231 logger.info(TAG, `start bluetooth.pairDevice,item = ${item}`) 232 let pairStatus = bluetooth.pairDevice(item) 233 logger.info(TAG, `pairStatus = ${pairStatus}`) 234 }) 235 236 Divider() 237 .vertical(false) 238 .color('#ffece7e7') 239 .lineCap(LineCapStyle.Butt) 240 .margin('1%') 241 }) 242 } 243 } 244 } 245 .constraintSize({ maxHeight: '85%' }) 246 } 247 } 248}