1/* 2 * Copyright (c) 2021 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 { paramMock } from "../utils" 17 18export function mockOhosBluetooth() { 19 const ArrayStringMock = ["[PC preview] unknown array string"] 20 const connectedBLEDevicesMock = [ 21 { 22 connectedBLEDevice: "[PC Preview] unknow connectedBLEDevice", 23 } 24 ] 25 const gattServiceMock = { 26 serviceUuid: "[PC preview] unknown serviceUuid", 27 isPrimary: "[PC preview] unknown isPrimary", 28 characteristics: [ 29 BLECharacteristicMock 30 ], 31 includeServices: [ 32 gattServiceMock 33 ] 34 } 35 const BLECharacteristicMock = { 36 serviceUuid: "[PC preview] unknown serviceUuid", 37 characteristicUuid: "[PC preview] unknown characteristicUuid", 38 characteristicValue: "[PC preview] unknown characteristicValue", 39 descriptors: [ 40 BLEDescriptorMock 41 ] 42 } 43 const BLEDescriptorMock = { 44 serviceUuid: "[PC preview] unknown serviceUuid", 45 characteristicUuid: "[PC preview] unknown characteristicUuid", 46 descriptorUuid: "[PC preview] unknown descriptorUuid", 47 descriptorValue: "[PC preview] unknown descriptorValue" 48 } 49 const gattServicesMock = [ 50 gattServiceMock 51 ] 52 const scanResultMock = { 53 deviceId: "[PC preview] unknown deviceId", 54 rssi: "[PC preview] unknown rssi", 55 data: "[PC preview] unknown data" 56 } 57 const characteristicReadReqMock = { 58 deviceId: "[PC preview] unknown deviceId", 59 transId: "[PC preview] unknown transId", 60 offset: "[PC preview] unknown offset", 61 characteristicUuid: "[PC preview] unknown characteristicUuid", 62 serviceUuid: "[PC preview] unknown serviceUuid" 63 } 64 const characteristicWriteReqMock = { 65 deviceId: "[PC preview] unknown deviceId", 66 transId: "[PC preview] unknown transId", 67 offset: "[PC preview] unknown offset", 68 isPrep: "[PC preview] unknown isPrep", 69 needRsp: "[PC preview] unknown needRsp", 70 value: "[PC preview] unknown value", 71 characteristicUuid: "[PC preview] unknown characteristicUuid", 72 serviceUuid: "[PC preview] unknown serviceUuid" 73 } 74 const descriptorReadReqMock = { 75 deviceId: "[PC preview] unknown v", 76 transId: "[PC preview] unknown transId", 77 offset: "[PC preview] unknown offset", 78 descriptorUuid: "[PC preview] unknown descriptorUuid", 79 characteristicUuid: "[PC preview] unknown characteristicUuid", 80 serviceUuid: "[PC preview] unknown serviceUuid" 81 } 82 const descriptorWriteReqMock = { 83 deviceId: "[PC preview] unknown deviceId", 84 transId: "[PC preview] unknown transId", 85 offset: "[PC preview] unknown offset", 86 isPrep: "[PC preview] unknown isPrep", 87 needRsp: "[PC preview] unknown needRsp", 88 value: "[PC preview] unknown value", 89 descriptorUuid: "[PC preview] unknown descriptorUuid", 90 characteristicUuid: "[PC preview] unknown characteristicUuid", 91 serviceUuid: "[PC preview] unknown serviceUuid" 92 } 93 const BLEConnectChangedStateMock = { 94 deviceId: "[PC preview] unknown deviceId", 95 state: "[PC preview] unknown state" 96 } 97 const pinRequiredParamMock = { 98 deviceId: "[PC preview] unknown deviceId", 99 pinCode: "[PC preview] unknown pinCode" 100 } 101 const sppReadMock = { 102 ArrayBuffer: "[PC preview] unknown ArrayBuffer" 103 } 104 const GattServerMock = { 105 startAdvertising: function (...args) { 106 console.warn("GattServer.startAdvertising interface mocked in the Previewer." + 107 "How this interface works on the Previewer may be different from that on a real device.") 108 }, 109 stopAdvertising: function () { 110 console.warn("GattServer.stopAdvertising interface mocked in the Previewer." + 111 "How this interface works on the Previewer may be different from that on a real device.") 112 }, 113 addService: function (...args) { 114 console.warn("GattServer.addService interface mocked in the Previewer." + 115 "How this interface works on the Previewer may be different from that on a real device.") 116 return paramMock.paramBooleanMock 117 }, 118 removeService: function (...args) { 119 console.warn("GattServer.removeService interface mocked in the Previewer." + 120 "How this interface works on the Previewer may be different from that on a real device.") 121 return paramMock.paramBooleanMock 122 }, 123 close: function () { 124 console.warn("GattServer.close interface mocked in the Previewer." + 125 "How this interface works on the Previewer may be different from that on a real device.") 126 }, 127 notifyCharacteristicChanged: function (...args) { 128 console.warn("GattServer.notifyCharacteristicChanged interface mocked in the Previewer." + 129 "How this interface works on the Previewer may be different from that on a real device.") 130 return paramMock.paramBooleanMock 131 }, 132 sendResponse: function (...args) { 133 console.warn("GattServer.sendResponse interface mocked in the Previewer." + 134 "How this interface works on the Previewer may be different from that on a real device.") 135 return paramMock.paramBooleanMock 136 }, 137 on: function (...args) { 138 console.warn("GattServer.on interface mocked in the Previewer." + 139 "How this interface works on the Previewer may be different from that on a real device.") 140 const len = args.length 141 if (typeof args[len - 1] === 'function') { 142 if (args[0] == 'characteristicRead') { 143 args[len - 1].call(this, characteristicReadReqMock); 144 } else if (args[0] == 'characteristicWrite') { 145 args[len - 1].call(this, characteristicWriteReqMock); 146 } else if (args[0] == 'descriptorRead') { 147 args[len - 1].call(this, descriptorReadReqMock); 148 } else if (args[0] == 'descriptorWrite') { 149 args[len - 1].call(this, descriptorWriteReqMock); 150 } else if (args[0] == 'connectStateChange') { 151 args[len - 1].call(this, BLEConnectChangedStateMock); 152 } 153 } 154 }, 155 off: function (...args) { 156 console.warn("GattServer.off interface mocked in the Previewer." + 157 "How this interface works on the Previewer may be different from that on a real device.") 158 const len = args.length 159 if (typeof args[len - 1] === 'function') { 160 if (args[0] == 'characteristicRead') { 161 args[len - 1].call(this, characteristicReadReqMock); 162 } else if (args[0] == 'characteristicWrite') { 163 args[len - 1].call(this, characteristicWriteReqMock); 164 } else if (args[0] == 'descriptorRead') { 165 args[len - 1].call(this, descriptorReadReqMock); 166 } else if (args[0] == 'descriptorWrite') { 167 args[len - 1].call(this, descriptorWriteReqMock); 168 } else if (args[0] == 'connectStateChange') { 169 args[len - 1].call(this, BLEConnectChangedStateMock); 170 } 171 } 172 }, 173 } 174 const GattClientDeviceMock = { 175 connect: function () { 176 console.warn("GattClientDevice.connect interface mocked in the Previewer." + 177 "How this interface works on the Previewer may be different from that on a real device.") 178 return paramMock.paramBooleanMock 179 }, 180 disconnect: function () { 181 console.warn("GattClientDevice.disconnect interface mocked in the Previewer." + 182 "How this interface works on the Previewer may be different from that on a real device.") 183 return paramMock.paramBooleanMock 184 }, 185 close: function () { 186 console.warn("GattClientDevice.close interface mocked in the Previewer." + 187 "How this interface works on the Previewer may be different from that on a real device.") 188 return paramMock.paramBooleanMock 189 }, 190 getDeviceName: function (...args) { 191 console.warn("GattClientDevice.getDeviceName interface mocked in the Previewer." + 192 "How this interface works on the Previewer may be different from that on a real device.") 193 const len = args.length 194 if (typeof args[len - 1] === 'function') { 195 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramStringMock); 196 } else { 197 return new Promise((resolve, reject) => { 198 resolve(paramMock.paramStringMock); 199 }) 200 } 201 }, 202 getServices: function (...args) { 203 console.warn("GattClientDevice.getServices interface mocked in the Previewer." + 204 "How this interface works on the Previewer may be different from that on a real device.") 205 const len = args.length 206 if (typeof args[len - 1] === 'function') { 207 args[len - 1].call(this, paramMock.businessErrorMock, gattServicesMock); 208 } else { 209 return new Promise((resolve, reject) => { 210 resolve(gattServicesMock); 211 }) 212 } 213 }, 214 readCharacteristicValue: function (...args) { 215 console.warn("GattClientDevice.readCharacteristicValue interface mocked in the Previewer." + 216 "How this interface works on the Previewer may be different from that on a real device.") 217 const len = args.length 218 if (typeof args[len - 1] === 'function') { 219 args[len - 1].call(this, paramMock.businessErrorMock, BLECharacteristicMock); 220 } else { 221 return new Promise((resolve, reject) => { 222 resolve(BLECharacteristicMock); 223 }) 224 } 225 }, 226 readDescriptorValue: function (...args) { 227 console.warn("GattClientDevice.readDescriptorValue interface mocked in the Previewer." + 228 "How this interface works on the Previewer may be different from that on a real device.") 229 const len = args.length 230 if (typeof args[len - 1] === 'function') { 231 args[len - 1].call(this, paramMock.businessErrorMock, BLEDescriptorMock); 232 } else { 233 return new Promise((resolve, reject) => { 234 resolve(BLEDescriptorMock); 235 }) 236 } 237 }, 238 writeCharacteristicValue: function (...args) { 239 console.warn("GattClientDevice.writeCharacteristicValue interface mocked in the Previewer." + 240 "How this interface works on the Previewer may be different from that on a real device.") 241 return paramMock.paramBooleanMock 242 }, 243 writeDescriptorValue: function (...args) { 244 console.warn("GattClientDevice.writeDescriptorValue interface mocked in the Previewer." + 245 "How this interface works on the Previewer may be different from that on a real device.") 246 return paramMock.paramBooleanMock 247 }, 248 getRssiValue: function (...args) { 249 console.warn("GattClientDevice.getRssiValue interface mocked in the Previewer." + 250 "How this interface works on the Previewer may be different from that on a real device.") 251 const len = args.length 252 if (typeof args[len - 1] === 'function') { 253 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 254 } else { 255 return new Promise((resolve, reject) => { 256 resolve(paramMock.paramNumberMock); 257 }) 258 } 259 }, 260 setBLEMtuSize: function (...args) { 261 console.warn("GattClientDevice.setBLEMtuSize interface mocked in the Previewer." + 262 "How this interface works on the Previewer may be different from that on a real device.") 263 return paramMock.paramBooleanMock 264 }, 265 setNotifyCharacteristicChanged: function (...args) { 266 console.warn("GattClientDevice.setNotifyCharacteristicChanged interface mocked in the Previewer." + 267 "How this interface works on the Previewer may be different from that on a real device.") 268 return paramMock.paramBooleanMock 269 }, 270 on: function (...args) { 271 console.warn("GattClientDevice.on interface mocked in the Previewer." + 272 "How this interface works on the Previewer may be different from that on a real device.") 273 const len = args.length 274 if (typeof args[len - 1] === 'function') { 275 if (args[0] == 'BLECharacteristicChange') { 276 args[len - 1].call(this, BLECharacteristicMock); 277 } else if (args[0] == 'BLEConnectionStateChange') { 278 args[len - 1].call(this, BLEConnectChangedStateMock); 279 } 280 } 281 }, 282 off: function (...args) { 283 console.warn("GattClientDevice.off interface mocked in the Previewer." + 284 "How this interface works on the Previewer may be different from that on a real device.") 285 const len = args.length 286 if (typeof args[len - 1] === 'function') { 287 if (args[0] == 'BLECharacteristicChange') { 288 args[len - 1].call(this, BLECharacteristicMock); 289 } else if (args[0] == 'BLEConnectionStateChange') { 290 args[len - 1].call(this, BLEConnectChangedStateMock); 291 } 292 } 293 }, 294 } 295 global.ohosplugin.bluetooth = { 296 getState: function () { 297 console.warn("bluetooth.getState interface mocked in the Previewer." + 298 "How this interface works on the Previewer may be different from that on a real device.") 299 return paramMock.paramNumberMock 300 }, 301 getBtConnectionState: function () { 302 console.warn("bluetooth.getBtConnectionState interface mocked in the Previewer." + 303 "How this interface works on the Previewer may be different from that on a real device.") 304 return paramMock.paramNumberMock 305 }, 306 pairDevice: function (...args) { 307 console.warn("bluetooth.pairDevice interface mocked in the Previewer." + 308 "How this interface works on the Previewer may be different from that on a real device.") 309 return paramMock.paramBooleanMock 310 }, 311 enableBluetooth: function () { 312 console.warn("bluetooth.enableBluetooth interface mocked in the Previewer." + 313 "How this interface works on the Previewer may be different from that on a real device.") 314 return paramMock.paramBooleanMock; 315 }, 316 disableBluetooth: function () { 317 console.warn("bluetooth.disableBluetooth interface mocked in the Previewer." + 318 "How this interface works on the Previewer may be different from that on a real device.") 319 return paramMock.paramBooleanMock; 320 }, 321 getLocalName: function () { 322 console.warn("bluetooth.getLocalName interface mocked in the Previewer." + 323 "How this interface works on the Previewer may be different from that on a real device.") 324 return paramMock.paramStringMock; 325 }, 326 getPairedDevices: function () { 327 console.warn("bluetooth.getPairedDevices interface mocked in the Previewer." + 328 "How this interface works on the Previewer may be different from that on a real device.") 329 return ArrayStringMock; 330 }, 331 setDevicePairingConfirmation: function (...args) { 332 console.warn("bluetooth.setDevicePairingConfirmation interface mocked in the Previewer." + 333 "How this interface works on the Previewer may be different from that on a real device.") 334 return paramMock.paramBooleanMock; 335 }, 336 setLocalName: function (...args) { 337 console.warn("bluetooth.setLocalName interface mocked in the Previewer." + 338 "How this interface works on the Previewer may be different from that on a real device.") 339 return paramMock.paramBooleanMock; 340 }, 341 setBluetoothScanMode: function (...args) { 342 console.warn("bluetooth.setBluetoothScanMode interface mocked in the Previewer." + 343 "How this interface works on the Previewer may be different from that on a real device.") 344 return paramMock.paramBooleanMock; 345 }, 346 getBluetoothScanMode: function () { 347 console.warn("bluetooth.getBluetoothScanMode interface mocked in the Previewer." + 348 "How this interface works on the Previewer may be different from that on a real device.") 349 return paramMock.paramNumberMock; 350 }, 351 startBluetoothDiscovery: function () { 352 console.warn("bluetooth.startBluetoothDiscovery interface mocked in the Previewer." + 353 "How this interface works on the Previewer may be different from that on a real device.") 354 return paramMock.paramBooleanMock; 355 }, 356 stopBluetoothDiscovery: function () { 357 console.warn("bluetooth.stopBluetoothDiscovery interface mocked in the Previewer." + 358 "How this interface works on the Previewer may be different from that on a real device.") 359 return paramMock.paramBooleanMock; 360 }, 361 on: function (...args) { 362 console.warn("bluetooth.on interface mocked in the Previewer." + 363 "How this interface works on the Previewer may be different from that on a real device.") 364 const len = args.length 365 if (typeof args[len - 1] === 'function') { 366 if (args[0] == 'bluetoothDeviceFind') { 367 args[len - 1].call(this, ArrayStringMock); 368 } else if (args[0] == 'bondStateChange') { 369 args[len - 1].call(this, paramMock.paramNumberMock); 370 } else if (args[0] == 'pinRequired') { 371 args[len - 1].call(this, pinRequiredParamMock); 372 } else if (args[0] == 'stateChange') { 373 args[len - 1].call(this, paramMock.paramNumberMock); 374 } else if (args[0] == 'sppRead') { 375 args[len - 1].call(this, sppReadMock.ArrayBuffer); 376 } 377 } 378 }, 379 off: function (...args) { 380 console.warn("bluetooth.off interface mocked in the Previewer." + 381 "How this interface works on the Previewer may be different from that on a real device.") 382 const len = args.length 383 if (typeof args[len - 1] === 'function') { 384 if (args[0] == 'bluetoothDeviceFind') { 385 args[len - 1].call(this, ArrayStringMock); 386 } else if (args[0] == 'bondStateChange') { 387 args[len - 1].call(this, paramMock.paramNumberMock); 388 } else if (args[0] == 'pinRequired') { 389 args[len - 1].call(this, pinRequiredParamMock); 390 } else if (args[0] == 'stateChange') { 391 args[len - 1].call(this, paramMock.paramNumberMock); 392 } else if (args[0] == 'sppRead') { 393 args[len - 1].call(this, sppReadMock.ArrayBuffer); 394 } 395 } 396 }, 397 sppListen: function (...args) { 398 console.warn("bluetooth.sppListen interface mocked in the Previewer." + 399 "How this interface works on the Previewer may be different from that on a real device.") 400 const len = args.length 401 if (typeof args[len - 1] === 'function') { 402 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 403 } 404 }, 405 sppAccept: function (...args) { 406 console.warn("bluetooth.sppAccept interface mocked in the Previewer." + 407 "How this interface works on the Previewer may be different from that on a real device.") 408 const len = args.length 409 if (typeof args[len - 1] === 'function') { 410 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 411 } 412 }, 413 sppConnect: function (...args) { 414 console.warn("bluetooth.sppConnect interface mocked in the Previewer." + 415 "How this interface works on the Previewer may be different from that on a real device.") 416 const len = args.length 417 if (typeof args[len - 1] === 'function') { 418 args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock); 419 } 420 }, 421 sppCloseServerSocket: function (...args) { 422 console.warn("bluetooth.sppCloseServerSocket interface mocked in the Previewer." + 423 "How this interface works on the Previewer may be different from that on a real device.") 424 }, 425 sppCloseClientSocket: function (...args) { 426 console.warn("bluetooth.sppCloseClientSocket interface mocked in the Previewer." + 427 "How this interface works on the Previewer may be different from that on a real device.") 428 }, 429 sppWrite: function (...args) { 430 console.warn("bluetooth.sppWrite interface mocked in the Previewer." + 431 "How this interface works on the Previewer may be different from that on a real device.") 432 return paramMock.paramBooleanMock; 433 }, 434 ScanDuty: { 435 SCAN_MODE_LOW_POWER: 0, 436 SCAN_MODE_BALANCED: 1, 437 SCAN_MODE_LOW_LATENCY: 2 438 }, 439 MatchMode: { 440 MATCH_MODE_AGGRESSIVE: 1, 441 MATCH_MODE_STICKY: 2 442 }, 443 ScanMode: { 444 SCAN_MODE_NONE: 0, 445 SCAN_MODE_CONNECTABLE: 1, 446 SCAN_MODE_GENERAL_DISCOVERABLE: 2, 447 SCAN_MODE_LIMITED_DISCOVERABLE: 3, 448 SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE: 4, 449 SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE: 5 450 }, 451 SppType: { 452 SPP_RFCOMM: 0 453 } 454 } 455 global.ohosplugin.bluetooth.BLE = { 456 createGattServer: function () { 457 console.warn("bluetooth.BLE.createGattServer interface mocked in the Previewer." + 458 "How this interface works on the Previewer may be different from that on a real device.") 459 return GattServerMock 460 }, 461 createGattClientDevice: function (...args) { 462 console.warn("bluetooth.BLE.createGattClientDevice interface mocked in the Previewer." + 463 "How this interface works on the Previewer may be different from that on a real device.") 464 return GattClientDeviceMock 465 }, 466 getConnectedBLEDevices: function () { 467 console.warn("bluetooth.BLE.getConnectedBLEDevices interface mocked in the Previewer." + 468 "How this interface works on the Previewer may be different from that on a real device.") 469 return connectedBLEDevicesMock 470 }, 471 startBLEScan: function (...args) { 472 console.warn("bluetooth.BLE.startBLEScan interface mocked in the Previewer." + 473 "How this interface works on the Previewer may be different from that on a real device.") 474 }, 475 stopBLEScan: function () { 476 console.warn("bluetooth.BLE.stopBLEScan interface mocked in the Previewer." + 477 "How this interface works on the Previewer may be different from that on a real device.") 478 }, 479 on: function (...args) { 480 console.warn("bluetooth.BLE.on interface mocked in the Previewer." + 481 "How this interface works on the Previewer may be different from that on a real device.") 482 const len = args.length 483 if (typeof args[len - 1] === 'function') { 484 if (args[0] == 'BLEDeviceFind') { 485 args[len - 1].call(this, [scanResultMock]); 486 } 487 } 488 }, 489 off: function (...args) { 490 console.warn("bluetooth.BLE.off interface mocked in the Previewer." + 491 "How this interface works on the Previewer may be different from that on a real device.") 492 const len = args.length 493 if (typeof args[len - 1] === 'function') { 494 if (args[0] == 'BLEDeviceFind') { 495 args[len - 1].call(this, [scanResultMock]); 496 } 497 } 498 }, 499 } 500}