SwitchOff003.test.js 36.6 KB
Newer Older
Q
quanli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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.
 */

16 17
import bluetoothManager from '@ohos.bluetoothManager';
import bluetooth from '@ohos.bluetooth';
Q
quanli 已提交
18 19
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'

Q
quanli 已提交
20 21 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
import osaccount from '@ohos.account.osAccount'
import bundle from '@ohos.bundle'
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'

async function applyPermission() {
    let osAccountManager = osaccount.getAccountManager();
    console.info("=== getAccountManager finish");
    let localId = await osAccountManager.getOsAccountLocalIdFromProcess();
    console.info("LocalId is :" + localId);0
    let appInfo = await bundle.getApplicationInfo('ohos.acts.communication.bluetooth.bluetoothhost', 0, localId);
    let atManager = abilityAccessCtrl.createAtManager();
    if (atManager != null) {
        let tokenID = appInfo.accessTokenId;
        console.info('[permission] case accessTokenID is ' + tokenID);
        let permissionName = 'ohos.permission.LOCATION';
        let permissionName1 = 'ohos.permission.APPROXIMATELY_LOCATION';
        await atManager.grantUserGrantedPermission(tokenID, permissionName, 1).then((result) => {
            console.info('[permission] case grantUserGrantedPermission success :' + JSON.stringify(result));
        }).catch((err) => {
            console.info('[permis' +
            'sion] case grantUserGrantedPermission failed :' + JSON.stringify(err));
        });
        await atManager.grantUserGrantedPermission(tokenID, permissionName1, 1).then((result) => {
            console.info('[permission] case grantUserGrantedPermission success1 :' + JSON.stringify(result));
        }).catch((err) => {
            console.info('[permis' +
            'sion] case grantUserGrantedPermission failed :' + JSON.stringify(err));
        });
    } else {
        console.info('[permission] case apply permission failed, createAtManager failed');
    }
}

Q
quanli 已提交
53 54 55 56 57 58 59 60 61
export default function bluetoothBLETest3() {
describe('bluetoothBLETest3', function() {
    let gattServer = null;
    let gattClient = null;
    function sleep(delay) {
        return new Promise(resovle => setTimeout(resovle, delay))
    }

    async function tryToDisableBt() {
62
        let sta = bluetoothManager.getState();
Q
quanli 已提交
63 64 65 66 67 68 69 70 71 72
        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));
73
                bluetoothManager. disableBluetooth();
Q
quanli 已提交
74 75 76 77 78 79 80 81 82
                await sleep(3000);
                break;
            case 3:
                console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta));
                break;
            default:
                console.info('[bluetooth_js] enable success');
        }
    }
Q
quanli 已提交
83
    beforeAll(async function (done) {
Q
quanli 已提交
84
        console.info('beforeAll called')
85 86
        gattServer = bluetoothManager.BLE.createGattServer();
        gattClient = bluetoothManager.BLE.createGattClientDevice("11:22:33:44:55:66");
Q
quanli 已提交
87 88
        await applyPermission();
        done();
Q
quanli 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    })
    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()
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_0100
     * @tc.name test bluetooth Profile ConnectionState
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 2
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_0100', 0, async function (done) {
        await sleep(3000);
116
        let sta = bluetoothManager.getState();
Q
quanli 已提交
117 118
        console.info('[bluetooth_js] bt getState:'+ JSON.stringify(sta));
        try {
119
            let connState = bluetoothManager.getBtConnectionState();
Q
quanli 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            console.info('[bluetooth_js] get bt connection state result' 
                     + JSON.stringify(connState));
            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()
        }  
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_0200
     * @tc.name Test pairDevice api
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 2
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_0200', 0, async function (done) {
        try {
143
            bluetoothManager.pairDevice("11:22:55:66:33:44");
Q
quanli 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
            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()
        }  
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_0300
     * @tc.name test getRemoteDeviceName
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_0300', 0, async function (done) {
        try {
165
            let ret = bluetoothManager.getRemoteDeviceName("00:00:00:00:00:00");
Q
quanli 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
            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()
        }  
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_0400
     * @tc.name test getRemoteDeviceClass
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_0400', 0, async function (done) {
        try {
189
            let DeviceClass = bluetoothManager.getRemoteDeviceClass("00:00:00:00:00:00");
Q
quanli 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_0500
     * @tc.name test get PairedDevices
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_0500', 0, async function (done) {
        try {
214
            let ret = bluetoothManager.getPairedDevices();
Q
quanli 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_0600
     * @tc.name test Get A2DP ConnectionState
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_0600', 0, async function (done) {
        try {
237
            let a2dpSrcConn = bluetoothManager.getProfileConnectionState(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
238 239 240 241
            console.info('[bluetooth_js]get a2dp result:' + JSON.stringify(a2dpSrcConn));
            expect(true).assertFalse();
            done()
        } catch (error) {
242
            console.error('[bluetooth_js]getProfileConnState error.code:'+JSON.stringify(error.code)+
Q
quanli 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
                   'error.message:'+JSON.stringify(error.message));
            expect(error.code).assertEqual('2900003');
            done()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_0700
     * @tc.name Test setDevicePairing
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_0700', 0, async function (done) {
        try {
260
            bluetoothManager.setDevicePairingConfirmation("11:22:55:66:33:44",false);
Q
quanli 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_0800
     * @tc.name setLocalName
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 2
     */
     it('COMMUNICATION_BLUETOOTH_SwitchOff_0800', 0, async function (done) {
        try {
            let newName = 'my bluetooth';
283
            bluetoothManager.setLocalName(newName);
Q
quanli 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_0900
     * @tc.name TEST setBluetoothScanMode
     * @tc.desc TEST 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 2
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_0900', 0, async function (done) {
        try {
305
            bluetoothManager.setBluetoothScanMode(bluetoothManager.ScanMode.SCAN_MODE_LIMITED_DISCOVERABLE,0);
Q
quanli 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
            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()
        }
    })

     /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_1000
     * @tc.name TEST getBluetoothScanMode
     * @tc.desc TEST 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 2
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_1000', 0, async function (done) {
        try {
327
            let oldScanMode = bluetoothManager.getBluetoothScanMode();
Q
quanli 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_1100
     * @tc.name TEST startBluetoothDiscovery
     * @tc.desc TEST 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 2
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_1100', 0, async function (done) {
        try {
350
            bluetoothManager.startBluetoothDiscovery();
Q
quanli 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_1200
     * @tc.name TEST stopBluetoothDiscovery
     * @tc.desc TEST 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 2
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_1200', 0, async function (done) {
        try {
372
            bluetoothManager.stopBluetoothDiscovery();
Q
quanli 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_1600
     * @tc.name test getDevice HFP State.
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 1
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_1600', 0, async function (done) {
        try {
394 395
            let hfpSrc = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
            let retArray = hfpSrc.getConnectionDevices();
Q
quanli 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_1700
     * @tc.name test getDeviceState.
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 1
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_1700', 0, async function (done) {
        try {
418
            let hfpSrc = bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
Q
quanli 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_1800
     * @tc.name test A2DP Connect
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 1
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_1800', 0, async function (done) {
        try {
442
            let a2dpSrc = bluetoothManager.getProfile(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
            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()
        }
    })

   
    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_1900
     * @tc.name test A2DP disconnect
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_1900', 0, async function (done) {
        try {
466
            let a2dpSrc = bluetoothManager.getProfile(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_2000
     * @tc.name test  get A2DP Playing State
     * @tc.desc Test  2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_2000', 0, async function (done) {
        try {
489
            let a2dpSrc = bluetoothManager.getProfile(bluetoothManager.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_2100
     * @tc.name test HFP Connect
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 1
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_2100', 0, async function (done) {
        try {
            let hfpSrc = 
515
                bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
Q
quanli 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
            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()
        }
    })
   
    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_2200
     * @tc.name test HFP disconnect
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_2200', 0, async function (done) {
        try {
            let hfpSrc = 
539
               bluetoothManager.getProfileInst(bluetoothManager.ProfileId.PROFILE_HANDS_FREE_AUDIO_GATEWAY);
Q
quanli 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_2800
     * @tc.name testClassicStartBLEScan
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 2
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_2800', 0, async function (done) {
        try {
562
            bluetoothManager.BLE.startBLEScan(
Q
quanli 已提交
563 564 565 566 567 568 569
                [{
                    deviceId:"11:22:33:44:55:66",
                    name:"test",
                    serviceUuid:"00001888-0000-1000-8000-00805f9b34fb"
                }],
                {
                    interval: 500,
570 571
                    dutyMode: bluetoothManager.ScanDuty.SCAN_MODE_LOW_POWER,
                    matchMode: bluetoothManager.MatchMode.MATCH_MODE_AGGRESSIVE,
Q
quanli 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
                }
            );
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_2900
     * @tc.name testClassicStartBLEScan
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 2
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_2900', 0, async function (done) {
        try {
595
            bluetoothManager.BLE.stopBLEScan();
Q
quanli 已提交
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 638 639 640 641 642 643 644 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 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 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 882 883 884 885 886 887 888 889
            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()
        }
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_3000
     * @tc.name testStartAdvertising
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 0
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_3000', 0, async function (done) {
          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();
     })

       /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_3100
     * @tc.name teststopAdvertising
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 0
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_3100', 0, async function (done) {
          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();
     })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_3200
     * @tc.name testAddService
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @tc.type Function
     * @tc.level Level 2
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_3200', 0, async function (done) {
        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()
        }       
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_3300
     * @tc.name test removeService
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @tc.type Function
     * @tc.level Level 2
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_3300', 0, async function (done) {
        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()
        }       
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_3400
     * @tc.name test removeService
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_3400', 0, async function (done) {
        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()
        }       
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_3600
     * @tc.name testSendResponse success
     * @tc.desc Test 2900003 - Bluetooth switch is off.
     * @tc.size MEDIUM
     * @tc.type Function
     * @tc.level Level 1
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_3600', 0, async function (done) {
        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()
        }
        
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_3700
     * @tc.name test gatt connect 
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_3700', 0, async function (done) { 
        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()
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_3800
     * @tc.name test gatt disconnect
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_3800', 0, async function (done) {
        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()
    })

    /**
     * @tc.number COMMUNICATION_BLUETOOTH_SwitchOff_3900
     * @tc.name test gatt close
     * @tc.desc Test 2900003 - Bluetooth switch is off
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 3
     */
    it('COMMUNICATION_BLUETOOTH_SwitchOff_3900', 0, async function (done) {
        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()
    })

890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
    /**
    * @tc.number COMMUNICATION_BLUETOOTH_BLE_Scan_0800
    * @tc.name testClassicStartBLEScan
    * @tc.desc Test ClassicStartBLEScan api.
    * @tc.size MEDIUM
    * @ since 7
    * @tc.type Function
    * @tc.level Level 3
    */
    it('COMMUNICATION_BLUETOOTH_BLE_Scan_0800', 0, async function (done) {
        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);
            }
            function onReceiveEvent(data)
            {
                console.info('[bluetooth_js] BLEscan device result8'+JSON.stringify(data));
                expect(true).assertTrue(data.length=0);
            }
            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);
            expect(true).assertFalse();
            console.info('[bluetooth_js] BLE scan off8');
            bluetoothManager.BLE.off('BLEDeviceFind', onReceiveEvent);
            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');
            done()
        }
    })

    /**
         * @tc.number SUB_COMMUNICATION_BLUETOOTH_BLE_Scan_0900
         * @tc.name testClassicStartBLEScan
         * @tc.desc Test ClassicStartBLEScan api.
         * @tc.size MEDIUM
         * @ since 7
         * @tc.type Function
         * @tc.level Level 3
         */
    it('SUB_COMMUNICATION_BLUETOOTH_BLE_Scan_0900', 0, async function (done) {
        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 已提交
972

973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
    /**
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_Switch_0400
     * @tc.name testEnableBluetooth
     * @tc.desc Test EnableBluetooth api by promise.
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 3
     */
    it('SUB_COMMUNICATION_BLUETOOTH_BR_Switch_0400', 0, async function (done) {
        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 已提交
995 996 997 998
})
}