btManagerSwitchOff003.test.js 34.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import bluetoothManager from '@ohos.bluetoothManager';
import bluetooth from '@ohos.bluetooth';
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'

Q
quanli 已提交
20 21
export default function btManagerError003Test() {
describe('btManagerError003Test', function() {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    let gattServer = null;
    let gattClient = null;
    function sleep(delay) {
        return new Promise(resovle => setTimeout(resovle, delay))
    }

    async function tryToDisableBt() {
        let sta = bluetoothManager.getState();
        switch(sta){
            case 0:
                console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta));
                break;
            case 1:
                console.info('[bluetooth_js] bt turning on:'+ JSON.stringify(sta));
                await sleep(3000);
                break;
            case 2:
                console.info('[bluetooth_js] bt turn on:'+ JSON.stringify(sta));
                bluetoothManager. disableBluetooth();
                await sleep(3000);
                break;
            case 3:
                console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta));
                break;
            default:
                console.info('[bluetooth_js] enable success');
        }
    }
    beforeAll(function () {
        console.info('beforeAll called')
        gattServer = bluetoothManager.BLE.createGattServer();
        gattClient = bluetoothManager.BLE.createGattClientDevice("11:22:33:44:55:66");
    })
    beforeEach(async function(done) {
        console.info('beforeEach called')
        await tryToDisableBt()
        done()
    })
    afterEach(function () {
        console.info('afterEach called')
    })
    afterAll(async function (done) {
        console.info('afterAll called')
        await sleep(6000);
        gattClient.close();
        done()
    })

    /**
Q
quanli 已提交
71
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0100
72 73 74 75 76
     * @tc.name test bluetooth Profile ConnectionState
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
77
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0100', 0, async function (done) {
78 79
        await sleep(3000);
        try {
Q
quanli 已提交
80 81
            let sta = bluetoothManager.getState();
            console.info('[bluetooth_js] bt getState:'+ JSON.stringify(sta));
82 83
            let connState = bluetoothManager.getBtConnectionState();
            console.info('[bluetooth_js] get bt connection state result' 
Q
quanli 已提交
84
                         + JSON.stringify(connState));
85 86 87 88 89 90 91 92 93 94 95
            expect(true).assertFalse();
            done();
        } catch (error) {
            console.error('[bluetooth_js]getBtConnectionState error.code:'
            +JSON.stringify(error.code)+ 'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }  
    })

    /**
Q
quanli 已提交
96
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0200
97 98 99 100 101
     * @tc.name Test pairDevice api
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
102
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0200', 0, async function (done) {
103 104 105 106 107 108 109 110 111 112 113 114 115
        try {
            bluetoothManager.pairDevice("11:22:55:66:33:44");
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]pairDevice error.code:'+JSON.stringify(error.code)+
                   'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }  
    })

    /**
Q
quanli 已提交
116
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0300
117 118 119 120 121
     * @tc.name test getRemoteDeviceName
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
122
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0300', 0, async function (done) {
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        try {
            let ret = bluetoothManager.getRemoteDeviceName("00:00:00:00:00:00");
            console.info('[bluetooth_js] getRemoteDeviceName ret2:' + JSON.stringify(ret));
            expect(ret.length).assertEqual(0);
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]getRemoteDeviceName error.code:'+JSON.stringify(error.code)+
                   'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }  
    })

    /**
Q
quanli 已提交
138
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0400
139 140 141 142 143
     * @tc.name test getRemoteDeviceClass
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
144
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0400', 0, async function (done) {
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        try {
            let DeviceClass = bluetoothManager.getRemoteDeviceClass("00:00:00:00:00:00");
            console.info('[bluetooth_js] getRemoteDeviceClass ret2 :' + JSON.stringify(DeviceClass) 
            + 'majorClass:' +DeviceClass.majorClass + 'majorMinorClass:'+ DeviceClass.majorMinorClass 
            + 'classOfDevice:' + DeviceClass.classOfDevice);
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]getRemoteDeviceClass error.code:'+JSON.stringify(error.code)+
                   'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
161
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0500
162 163 164 165 166
     * @tc.name test get PairedDevices
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
167
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0500', 0, async function (done) {
168 169 170 171 172 173 174 175 176 177 178 179 180 181
        try {
            let ret = bluetoothManager.getPairedDevices();
            console.info('[bluetooth_js] getPairedDevices ret2:' + JSON.stringify(ret));
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]getPairedDevices error.code:'+JSON.stringify(error.code)+
                   'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
182
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0600
183 184 185 186 187
     * @tc.name test Get A2DP ConnectionState
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
188
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0600', 0, async function (done) {
189 190 191 192 193 194 195 196 197 198 199 200 201 202
        try {
            let a2dpSrcConn = bluetoothManager.getProfileConnectionState(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE);
            console.info('[bluetooth_js]get a2dp result:' + JSON.stringify(a2dpSrcConn));
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]getProfileConnState error.code:'+JSON.stringify(error.code)+
                   'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
203
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0700
204 205 206 207 208
     * @tc.name Test setDevicePairing
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
209
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0700', 0, async function (done) {
210 211 212 213 214 215 216 217 218 219 220 221 222
        try {
            bluetoothManager.setDevicePairingConfirmation("11:22:55:66:33:44",false);
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]setDevicePairingConfirmation error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
223
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0800
224 225 226 227 228
     * @tc.name setLocalName
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
229
     it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0800', 0, async function (done) {
230 231 232 233 234 235 236 237 238 239 240 241 242 243
        try {
            let newName = 'my bluetooth';
            bluetoothManager.setLocalName(newName);
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]setLocalName error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
244
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0900
245 246 247 248 249
     * @tc.name TEST setBluetoothScanMode
     * @tc.desc TEST 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
250
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_0900', 0, async function (done) {
251 252 253 254 255 256 257 258 259 260 261 262 263
        try {
            bluetoothManager.setBluetoothScanMode(bluetoothManager.ScanMode.SCAN_MODE_LIMITED_DISCOVERABLE,0);
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]setBluetoothScanMode error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

     /**
Q
quanli 已提交
264
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1000
265 266 267 268 269
     * @tc.name TEST getBluetoothScanMode
     * @tc.desc TEST 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
270
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1000', 0, async function (done) {
271 272 273 274 275 276 277 278 279 280 281 282 283 284
        try {
            let oldScanMode = bluetoothManager.getBluetoothScanMode();
            console.info('[bluetooth_js] getBluetoothScanMode = '+ JSON.stringify(oldScanMode));
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]getBluetoothScanMode error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
285
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1100
286 287 288 289 290
     * @tc.name TEST startBluetoothDiscovery
     * @tc.desc TEST 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
291
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1100', 0, async function (done) {
292 293 294 295 296 297 298 299 300 301 302 303 304
        try {
            bluetoothManager.startBluetoothDiscovery();
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]startBluetoothDiscovery error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
305
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1200
306 307 308 309 310
     * @tc.name TEST stopBluetoothDiscovery
     * @tc.desc TEST 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
311
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1200', 0, async function (done) {
312 313 314 315 316 317 318 319 320 321 322 323 324
        try {
            bluetoothManager.stopBluetoothDiscovery();
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]stopBluetoothDiscovery error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
325
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1600
326 327 328 329 330
     * @tc.name test getDevice HFP State.
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 1
     */
Q
quanli 已提交
331
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1600', 0, async function (done) {
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        try {
            let hfpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
            let retArray = hfpSrc.getConnectionDevices();
            console.info('[bluetooth_js]hfp getConnectionDevices:' + JSON.stringify(retArray));
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]getConnectionDevices error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
347
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1700
348 349 350 351 352
     * @tc.name test getDeviceState.
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 1
     */
Q
quanli 已提交
353
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1700', 0, async function (done) {
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
        try {
            let hfpSrc = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
            let ret = hfpSrc.getDeviceState('11:22:33:44:55:66');
            console.info('[bluetooth_js]hfp getDeviceState:' + JSON.stringify(ret));
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]getDeviceState error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
369
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1800
370 371 372 373 374
     * @tc.name test A2DP Connect
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 1
     */
Q
quanli 已提交
375
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1800', 0, async function (done) {
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
        try {
            let a2dpSrc = bluetoothManager.getProfile(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE);
            a2dpSrc.connect('11:22:33:44:55:77');
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]A2DPconnect error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

   
    /**
Q
quanli 已提交
391
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1900
392 393 394 395 396
     * @tc.name test A2DP disconnect
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
397
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_1900', 0, async function (done) {
398 399 400 401 402 403 404 405 406 407 408 409 410 411
        try {
            let a2dpSrc = bluetoothManager.getProfile(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE);
            a2dpSrc.disconnect('11:22:33:44:55:77');
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]A2DPdisconnect error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
412
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_2000
413 414 415 416 417
     * @tc.name test  get A2DP Playing State
     * @tc.desc Test  2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
418
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_2000', 0, async function (done) {
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
        try {
            let a2dpSrc = bluetoothManager.getProfile(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE);
            console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
            let state = a2dpSrc.getPlayingState('11:22:33:44:55:66');
            console.info('[bluetooth_js]a2dp getPlayingState result:' + JSON.stringify(state));
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]getPlayingState error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
435
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_2100
436 437 438 439 440
     * @tc.name test HFP Connect
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 1
     */
Q
quanli 已提交
441
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_2100', 0, async function (done) {
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
        try {
            let hfpSrc = 
                bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
            hfpSrc.connect('11:22:33:44:55:77');
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]HFPconnect error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })
   
    /**
Q
quanli 已提交
457
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_2200
458 459 460 461 462
     * @tc.name test HFP disconnect
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
463
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_2200', 0, async function (done) {
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
        try {
            let hfpSrc = 
               bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
            hfpSrc.disconnect('11:22:33:44:55:77');
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]HFPdisconnect error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
479
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_2800
480 481 482 483 484
     * @tc.name testClassicStartBLEScan
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
485
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_2800', 0, async function (done) {
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
        try {
            bluetoothManager.BLE.startBLEScan(
                [{
                    deviceId:"11:22:33:44:55:66",
                    name:"test",
                    serviceUuid:"00001888-0000-1000-8000-00805f9b34fb"
                }],
                {
                    interval: 500,
                    dutyMode: bluetoothManager.ScanDuty.SCAN_MODE_LOW_POWER,
                    matchMode: bluetoothManager.MatchMode.MATCH_MODE_AGGRESSIVE,
                }
            );
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]startBLEScan error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
510
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_2900
511 512 513 514 515
     * @tc.name testClassicStartBLEScan
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
516
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_2900', 0, async function (done) {
517 518 519 520 521 522 523 524 525 526 527 528 529
        try {
            bluetoothManager.BLE.stopBLEScan();
            expect(true).assertFalse();
            done()
        } catch (error) {
            console.error('[bluetooth_js]startBLEScan error.code:'
               +JSON.stringify(error.code)+'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
Q
quanli 已提交
530
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3000
531 532 533 534 535
     * @tc.name testStartAdvertising
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 0
     */
Q
quanli 已提交
536
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3000', 0, async function (done) {
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
          let manufactureValueBuffer = new Uint8Array(4);
          manufactureValueBuffer[0] = 1;
          manufactureValueBuffer[1] = 2;
          manufactureValueBuffer[2] = 3;
          manufactureValueBuffer[3] = 4;
          let serviceValueBuffer = new Uint8Array(4);
          serviceValueBuffer[0] = 4;
          serviceValueBuffer[1] = 6;
          serviceValueBuffer[2] = 7;
          serviceValueBuffer[3] = 8;
          let setting={
            interval:20,
            txPower:-10,
            connectable:true,
            }
          let advData={
               serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"],
               manufactureData:[{
                    manufactureId:4567,
                    manufactureValue:manufactureValueBuffer.buffer
               }],
               serviceData:[{
                    serviceUuid:"00001888-0000-1000-8000-00805f9b34fb",
                    serviceValue:serviceValueBuffer.buffer
               }],
          }
          let advResponse ={
               serviceUuids:["00001889-0000-1000-8000-00805f9b34fb"],
               manufactureData:[{
                    manufactureId:1789,
                    manufactureValue:manufactureValueBuffer.buffer
               }],
               serviceData:[{
                    serviceUuid:"00001889-0000-1000-8000-00805f9b34fb",
                    serviceValue:serviceValueBuffer.buffer
               }],
          }
          try {
               gattServer.startAdvertising(setting,advData,advResponse);
               expect(true).assertFalse();
          }catch(error) {
               console.error('[bluetooth_js]startAdvertising error.code:'+JSON.stringify(error.code)+
                   'error.message:'+JSON.stringify(error.message));
               expect(error.code).assertEqual('2900003');
          }
          await sleep(2000);
          done();
     })

       /**
Q
quanli 已提交
587
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3100
588 589 590 591 592
     * @tc.name teststopAdvertising
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 0
     */
Q
quanli 已提交
593
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3100', 0, async function (done) {
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
          let manufactureValueBuffer = new Uint8Array(4);
          manufactureValueBuffer[0] = 1;
          manufactureValueBuffer[1] = 2;
          manufactureValueBuffer[2] = 3;
          manufactureValueBuffer[3] = 4;
          let serviceValueBuffer = new Uint8Array(4);
          serviceValueBuffer[0] = 4;
          serviceValueBuffer[1] = 6;
          serviceValueBuffer[2] = 7;
          serviceValueBuffer[3] = 8;
          let advData={
               serviceUuids:["00001888-0000-1000-8000-00805f9b34fb"],
               manufactureData:[{
                    manufactureId:4567,
                    manufactureValue:manufactureValueBuffer.buffer
               }],
               serviceData:[{
                    serviceUuid:"00001888-0000-1000-8000-00805f9b34fb",
                    serviceValue:serviceValueBuffer.buffer
               }],
          }
          let advResponse ={
               serviceUuids:["00001889-0000-1000-8000-00805f9b34fb"],
               manufactureData:[{
                    manufactureId:1789,
                    manufactureValue:manufactureValueBuffer.buffer
               }],
               serviceData:[{
                    serviceUuid:"00001889-0000-1000-8000-00805f9b34fb",
                    serviceValue:serviceValueBuffer.buffer
               }],
          }
          try {
               gattServer.stopAdvertising();
               expect(true).assertFalse();
          }catch(error) {
               console.error('[bluetooth_js]stopAdvertising error.code:'+JSON.stringify(error.code)+
                   'error.message:'+JSON.stringify(error.message));
               expect(error.code).assertEqual('2900003');
          }
          done();
     })

    /**
Q
quanli 已提交
638
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3200
639 640 641 642 643
     * @tc.name testAddService
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
644
    it('SUB_COMMUNICATION_BTMANAGER_SwitchOff_3200', 0, async function (done) {
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
        try {
            let descriptors = [];
            let arrayBuffer = new ArrayBuffer(8);
            let descV = new Uint8Array(arrayBuffer);
            descV[0] = 11;
            let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
            characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB',
            descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer};
            descriptors[0] = descriptor;
            let characteristics = [];
            let arrayBufferC = new ArrayBuffer(8);
            let cccV = new Uint8Array(arrayBufferC);
            cccV[0] = 1;
            let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB',
            characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 
            characteristicValue: arrayBufferC, descriptors:descriptors};
            characteristics[0] = characteristic;
            let gattService = {serviceUuid:'00001810-0000-1000-8000-00805F9B34FB', isPrimary: true,
            characteristics:characteristics, includeServices:[]};
            gattServer.addService(gattService);
            expect(true).assertFalse();
            done();
        } catch (error) {
            console.error(`[bluetooth_js]AddService failed, code is ${error.code},
                  message is ${error.message}`);
            expect(error.code).assertEqual('2900003');
            done()
        }       
    })

    /**
Q
quanli 已提交
676
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3300
677 678 679 680 681
     * @tc.name test removeService
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 2
     */
Q
quanli 已提交
682
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3300', 0, async function (done) {
683 684 685 686 687 688 689 690 691 692 693 694 695
        try {
            gattServer.removeService('00001810-0000-1000-8000-00805F9B34FB');
            expect(true).assertFalse();
            done();
        } catch (error) {
            console.error(`[bluetooth_js]removeService failed, code is ${error.code},
                  message is ${error.message}`);
            expect(error.code).assertEqual('401');
            done()
        }       
    })

    /**
Q
quanli 已提交
696
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3400
697 698 699 700 701
     * @tc.name test removeService
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
702
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3400', 0, async function (done) {
703 704 705 706 707 708 709 710 711 712 713 714 715
        try {
            gattServer.close();
            expect(true).assertFalse();
            done();
        } catch (error) {
            console.error(`[bluetooth_js]close failed, code is ${error.code},
                  message is ${error.message}`);
            expect(error.code).assertEqual('2900003');
            done()
        }       
    })

    /**
Q
quanli 已提交
716
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3600
717 718 719 720 721
     * @tc.name testSendResponse success
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.type Function
     * @tc.level Level 1
     */
Q
quanli 已提交
722
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3600', 0, async function (done) {
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
        try {
            let arrayBuffer = new ArrayBuffer(8);
            let value =  new Uint8Array(arrayBuffer);
            value[0] = 1;
            let ServerResponse = {deviceId: '00:11:22:33:44:55', transId: 1,
                status: 0, offset: 0, value: arrayBuffer};
            gattServer.sendResponse(ServerResponse);
            expect(true).assertFalse();
            done();
        } catch (error) {
            console.error(`[bluetooth_js]sendResponse failed, code is ${error.code},
            message is ${error.message}`);
            expect(error.code).assertEqual('2900003');
            done()
        }
        
    })

    /**
Q
quanli 已提交
742
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3700
743 744 745 746 747
     * @tc.name test gatt connect 
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
748
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3700', 0, async function (done) { 
749 750 751 752 753 754 755 756 757 758 759 760 761
        try {
            gattClient.connect();
            await sleep(2000);
            expect(true).assertFalse();
        } catch(error) {
            console.error(`[bluetooth_js]connect failed, code is ${error.code}, 
            message is ${error.message}`);
            expect(error.code).assertEqual('2900003');
        }
        done()
    })

    /**
Q
quanli 已提交
762
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3800
763 764 765 766 767
     * @tc.name test gatt disconnect
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
768
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3800', 0, async function (done) {
769 770 771 772 773 774 775 776 777 778 779 780
        try {
            gattClient.disconnect();
            expect(true).assertFalse();
          } catch(error) {
            console.error(`[bluetooth_js]disconnect failed, code is ${error.code}, 
            message is ${error.message}`);
            expect(error.code).assertEqual('2900003');
        }
        done()
    })

    /**
Q
quanli 已提交
781
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3900
782 783 784 785 786
     * @tc.name test gatt close
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
787
    it('SUB_COMMUNICATION_BTMANAGER_SWITCHOFF_3900', 0, async function (done) {
788 789 790 791 792 793 794 795 796 797 798 799
        try {
            gattClient.close();
            expect(true).assertFalse();
          } catch(error) {
            console.error(`[bluetooth_js]gattClient close failed, code is ${error.code}, 
            message is ${error.message}`);
            expect(error.code).assertEqual('2900003');
        }
        done()
    })

    /**
Q
quanli 已提交
800
    * @tc.number SUB_COMMUNICATION_BTMANAGER_BLESCAN_0800
801 802 803 804 805
    * @tc.name testClassicStartBLEScan
    * @tc.desc Test ClassicStartBLEScan api.
    * @tc.type Function
    * @tc.level Level 3
    */
Q
quanli 已提交
806
    it('SUB_COMMUNICATION_BTMANAGER_BLESCAN_0800', 0, async function (done) {
Q
quanli 已提交
807 808 809 810 811
        function onReceiveEvent(data)
        {
            console.info('[bluetooth_js] BLEscan device result8'+JSON.stringify(data));
            expect(true).assertTrue(data.length=0);
        }
812 813 814 815 816 817 818 819 820 821
        try {
            let state = bluetoothManager.getState();
            console.info('[bluetooth_js] bt turn off1:'+ JSON.stringify(state));
            if(state == bluetoothManager.BluetoothState.STATE_ON) {
                let result2= bluetoothManager.disableBluetooth();
                console.info('[bluetooth_js]disable result1'+ JSON.stringify(result2));
                let state1 = bluetoothManager.getState();
                console.info('[bluetooth_js] getState4 off = '+ JSON.stringify(state1));
                expect(state1).assertEqual(bluetooth.BluetoothState.STATE_OFF);
            }
Q
quanli 已提交
822
            console.info('[bluetooth_js] BLE BLEDeviceFind on start!');
823 824 825 826 827 828 829 830 831 832
            bluetoothManager.BLE.on("BLEDeviceFind", onReceiveEvent)
            bluetoothManager.BLE.startBLEScan(
                [{}],
                {
                    interval: 100,
                    dutyMode: bluetoothManager.ScanDuty.SCAN_MODE_LOW_POWER,
                    matchMode: bluetoothManager.MatchMode.MATCH_MODE_AGGRESSIVE,
                }
            );
            await sleep(1000);
Q
quanli 已提交
833
            // expect(true).assertFalse();
834 835 836 837 838 839 840 841
            console.info('[bluetooth_js] BLE scan off8');
            bluetoothManager.BLE.stopBLEScan();
            done();
        } catch (error) {
            console.error('[bluetooth_js]Scan_0800 error.code:'+JSON.stringify(error.code)+
            'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
        }
Q
quanli 已提交
842 843 844 845
        console.info('[bluetooth_js] BLE BLEDeviceFind off start!');
        bluetoothManager.BLE.off('BLEDeviceFind', onReceiveEvent);
        await sleep(2000);
        done();
846 847 848
    })

    /**
Q
quanli 已提交
849
         * @tc.number SUB_COMMUNICATION_BTMANAGER_BLESCAN_0900
850 851 852 853 854
         * @tc.name testClassicStartBLEScan
         * @tc.desc Test ClassicStartBLEScan api.
         * @tc.type Function
         * @tc.level Level 3
         */
Q
quanli 已提交
855
    it('SUB_COMMUNICATION_BTMANAGER_BLESCAN_0900', 0, async function (done) {
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
        bluetooth.disableBluetooth();
        await sleep(3000);
        let state = bluetooth.getState();
        console.info('[bluetooth_js] bt turn off1:'+ JSON.stringify(state));
        function onReceiveEvent(data)
        {
            console.info('[bluetooth_js] BLEscan device result8'+JSON.stringify(data));
            expect(true).assertTrue(data.length=0);
        }
        bluetooth.BLE.on("BLEDeviceFind", onReceiveEvent)
        bluetooth.BLE.startBLEScan(
            [{}],
            {
                interval: 100,
                dutyMode: bluetooth.ScanDuty.SCAN_MODE_LOW_POWER,
                matchMode: bluetooth.MatchMode.MATCH_MODE_AGGRESSIVE,
            }
        );
        await sleep(1000);
        console.info('[bluetooth_js] BLE scan off8');
        bluetooth.BLE.off('BLEDeviceFind', onReceiveEvent);
        bluetooth.BLE.stopBLEScan();
        done();
    })

    /**
Q
quanli 已提交
882
     * @tc.number SUB_COMMUNICATION_BTMANAGER_SWITCH_0400
883 884 885 886 887
     * @tc.name testEnableBluetooth
     * @tc.desc Test EnableBluetooth api by promise.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
888
    it('SUB_COMMUNICATION_BTMANAGER_SWITCH_0400', 0, async function (done) {
889 890 891 892 893 894 895 896 897 898 899 900 901
        let state = bluetooth.getState();
        console.info('[bluetooth_js] getState1 off = '+ JSON.stringify(state));
        if(state == bluetooth.BluetoothState.STATE_OFF) {
            let result2= bluetooth.disableBluetooth();
            console.info('[bluetooth_js]disable result1'+ JSON.stringify(result2));
            expect(result2).assertFalse();
            let state1 = bluetooth.getState();
            console.info('[bluetooth_js] getState4 off = '+ JSON.stringify(state1));
            expect(state1).assertEqual(bluetooth.BluetoothState.STATE_OFF);
        }
        done();
    })
})
Q
quanli 已提交
902
}