1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import bluetooth from '@ohos.bluetoothManager'; 17import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' 18import { UiComponent, UiDriver, BY, Component, Driver, UiWindow, ON, MatchPattern, DisplayRotation, ResizeDirection, UiDirection, MouseButton, WindowMode, PointerMatrix, UIElementInfo, UIEventObserver } from '@ohos.UiTest' 19 20export default function btManagerGattManagerTest() { 21describe('btManagerGattManagerTest', function() { 22 let gattServer = null; 23 let gattClient = null; 24 function sleep(delay) { 25 return new Promise(resovle => setTimeout(resovle, delay)) 26 } 27 28 async function openPhone() { 29 try{ 30 let drivers = Driver.create(); 31 console.info('[bluetooth_js] bt driver create:'+ drivers); 32 await drivers.delayMs(1000); 33 await drivers.wakeUpDisplay(); 34 await drivers.delayMs(5000); 35 await drivers.swipe(1500, 1000, 1500, 100); 36 await drivers.delayMs(10000); 37 } catch (error) { 38 console.info('[bluetooth_js] driver error info:'+ error); 39 } 40 } 41 42 async function clickTheWindow() { 43 console.info('[bluetooth_js] clickRequestPermission start'); 44 let driver = Driver.create(); 45 await driver.delayMs(3000); 46 try { 47 let button = await driver.findComponent(ON.text("开启")); 48 await button.click(); 49 await driver.delayMs(3000); 50 console.info('[bluetooth_js] click 开启 end'); 51 } catch (err) { 52 console.info('[bluetooth_js] clickRequestPermission failed. ' + err); 53 } 54 try { 55 let button1 = await driver.findComponent(ON.text("允许")); 56 await button1.click(); 57 await driver.delayMs(3000); 58 console.info('[bluetooth_js] click 允许 end'); 59 } catch (err) { 60 console.info('[bluetooth_js] click 允许 failed. ' + err); 61 } 62 } 63 64 async function tryToEnableBt() { 65 let sta = bluetooth.getState(); 66 switch(sta){ 67 case 0: 68 console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta)); 69 bluetooth.enableBluetooth(); 70 await clickTheWindow(); 71 await sleep(10000); 72 break; 73 case 1: 74 console.info('[bluetooth_js] bt turning on:'+ JSON.stringify(sta)); 75 await sleep(3000); 76 break; 77 case 2: 78 console.info('[bluetooth_js] bt turn on:'+ JSON.stringify(sta)); 79 break; 80 case 3: 81 console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta)); 82 bluetooth.enableBluetooth(); 83 await clickTheWindow(); 84 await sleep(10000); 85 break; 86 default: 87 console.info('[bluetooth_js] enable success'); 88 } 89 } 90 beforeAll(async function (done) { 91 console.info('beforeAll called') 92 await openPhone(); 93 await tryToEnableBt(); 94 gattServer = bluetooth.BLE.createGattServer(); 95 console.info('[bluetooth_js]bmgm gattServer create info:' + gattServer); 96 gattClient = bluetooth.BLE.createGattClientDevice('92:3A:C0:3A:4C:28'); 97 console.info('[bluetooth_js]bmgm gattClient create info:' + gattClient); 98 done(); 99 }) 100 beforeEach(async function(done) { 101 console.info('beforeEach called') 102 await tryToEnableBt() 103 done() 104 }) 105 afterEach(function () { 106 console.info('afterEach called') 107 }) 108 afterAll(async function (done) { 109 console.info('afterAll called') 110 await gattServer.close(); 111 console.info('[bluetooth_js]bmgm gattServer close success'); 112 done(); 113 }) 114 115 /** 116 * @tc.number SUB_COMMUNICATION_BTMANAGER_GATTCONNECT_0100 117 * @tc.name testConnect 118 * @tc.desc Test api. 119 * @tc.type Function 120 * @tc.size MediumTest 121 * @tc.level Level 2 122 */ 123 it('SUB_COMMUNICATION_BTMANAGER_GATTCONNECT_0100', 0, async function (done) { 124 try { 125 gattClient.connect(); 126 console.info(`[bluetooth_js]connect success`); 127 } 128 catch(error) { 129 console.info(`[bluetooth_js]connect failed, code is ${error.code},message is ${error.message}`); 130 expect(Number(error.code)).assertEqual(2900099); 131 } 132 try { 133 gattClient.disconnect(); 134 console.info(`[bluetooth_js]disconnect success`); 135 } 136 catch(error) { 137 console.info(`[bluetooth_js]disconnect failed, code is ${error.code},message is ${error.message}`); 138 expect(Number(error.code)).assertEqual(2900099); 139 } 140 done(); 141 }) 142 143 /** 144 * @tc.number SUB_COMMUNICATION_BTMANAGER_GATTCONNECT_0200 145 * @tc.name testGetConnectedBLEDevices 146 * @tc.desc Test api 201 - Permission denied. 147 * @tc.type Function 148 * @tc.size MediumTest 149 * @tc.level Level 2 150 */ 151 it('SUB_COMMUNICATION_BTMANAGER_GATTCONNECT_0200', 0, async function (done) { 152 try { 153 let result = bluetooth.BLE.getConnectedBLEDevices(); 154 console.info("[bluetooth_js] getConnDev:" + JSON.stringify(result) 155 + "length:" +result.length); 156 expect(result.length).assertEqual(0); 157 } catch (error) { 158 console.error(`[bluetooth_js]getConnDev failed, code is ${error.code}, 159 message is ${error.message}`); 160 expect(true).assertFalse(); 161 } 162 done(); 163 }) 164 165 /** 166 * @tc.number SUB_COMMUNICATION_BTMANAGER_GATTCONNECT_0300 167 * @tc.name testBLEconnectStateChange 168 * @tc.desc Test on and off api 169 * @tc.type Function 170 * @tc.size MediumTest 171 * @tc.level Level 2 172 */ 173 it('SUB_COMMUNICATION_BTMANAGER_GATTCONNECT_0300', 0, async function (done) { 174 try { 175 function ConnectStateChanged(state) { 176 console.log('bluetooth connect state changed'); 177 let connectState = state.state; 178 console.info('[bluetooth_js] state changed' + connectState) 179 expect(true).assertEqual(connectState!=null); 180 } 181 await gattClient.on('BLEConnectionStateChange', ConnectStateChanged); 182 gattClient.connect(); 183 gattClient.disconnect(); 184 done(); 185 } catch (error) { 186 console.error(`[bluetooth_js]GattConnect_0300 failed, code is ${error.code}, 187 message is ${error.message}`); 188 expect(Number(error.code)).assertEqual(2900099); 189 done() 190 } 191 await gattClient.off("BLEConnectionStateChange"); 192 done(); 193 }) 194 195 /** 196 * @tc.number SUB_COMMUNICATION_BTMANAGER_GETRSSIVALUE_0100 197 * @tc.name testGetRssiValue 198 * @tc.desc Test getRssiValue api by promise. 199 * @tc.type Function 200 * @tc.size MediumTest 201 * @tc.level Level 2 202 */ 203 it('SUB_COMMUNICATION_BTMANAGER_GETRSSIVALUE_0100', 0, async function (done) { 204 try { 205 gattClient.connect(); 206 await gattClient.getRssiValue().then((data) => { 207 console.info('[bluetooth_js] BLE read rssi:' + data); 208 expect(data).assertNotEqual(null); 209 done(); 210 }).catch(err => { 211 console.info('bluetooth getRssiValue has error: '+ JSON.stringify(err)); 212 let b=false; 213 if(err.code==2900099||err.code==-1) 214 { 215 b=true 216 } 217 expect(b).assertEqual(true); 218 done(); 219 }); 220 } catch (error) { 221 console.error(`[bluetooth_js]GetRssiValue error, code is ${error.code},message is ${error.message}`); 222 expect(Number(error.code)).assertEqual(2900099); 223 } 224 gattClient.disconnect(); 225 done(); 226 }) 227 228 /** 229 * @tc.number SUB_COMMUNICATION_BTMANAGER_GETRSSIVALUE_0200 230 * @tc.name testGetRssiValue 231 * @tc.desc Test testGetDeviceName api by callback. 232 * @tc.type Function 233 * @tc.size MediumTest 234 * @tc.level Level 2 235 */ 236 it('SUB_COMMUNICATION_BTMANAGER_GETRSSIVALUE_0200', 0, async function (done) { 237 try { 238 gattClient.connect(); 239 function getRssi() { 240 return new Promise((resolve,reject) => { 241 gattClient.getRssiValue((err, data)=> { 242 if (err) { 243 console.error('getRssi failed' + err); 244 reject(err.code); 245 } else 246 { 247 console.info('[bluetooth_js]getRssi value:'+JSON.stringify(data)); 248 expect(true).assertEqual(data != null); 249 } 250 resolve(); 251 }); 252 }); 253 } 254 await getRssi().then((data) => { 255 console.info("[bluetooth_js]02 getRssiValue done"); 256 done(); 257 }) 258 .catch(e => { 259 console.info("[bluetooth_js]02 getRssiValue failed" + e); 260 expect(2900099).assertEqual(e); 261 done(); 262 }) 263 } catch (error) { 264 console.error(`[bluetooth_js]GetRssiValue error, code is ${error.code},message is ${error.message}`); 265 } 266 gattClient.disconnect(); 267 done(); 268 }) 269 270 /** 271 * @tc.number SUB_COMMUNICATION_BTMANAGER_GETDEVICENAME_0100 272 * @tc.name testGetDeviceName 273 * @tc.desc Test GetDeviceName api by promise. 274 * @tc.type Function 275 * @tc.size MediumTest 276 * @tc.level Level 2 277 */ 278 it('SUB_COMMUNICATION_BTMANAGER_GETDEVICENAME_0100', 0, async function (done) { 279 try { 280 gattClient.connect(); 281 await gattClient.getDeviceName().then((data) => { 282 console.info('[bluetooth_js] device name:' + JSON.stringify(data)) 283 expect(true).assertEqual(data != null); 284 done(); 285 }).catch(err => { 286 console.error('[bluetooth_js] bluetooth getDeviceName has error: '+ JSON.stringify(err)); 287 expect(Number(err.code)).assertEqual(2900099); 288 done(); 289 }); 290 } catch (error) { 291 console.error(`[bluetooth_js]GetDeviceName failed, code is ${error.code},message is ${error.message}`); 292 expect(Number(error.code)).assertEqual(2900099); 293 } 294 gattClient.disconnect(); 295 done(); 296 }) 297 298 /** 299 * @tc.number SUB_COMMUNICATION_BTMANAGER_GETDEVICENAME_0200 300 * @tc.name testGetDeviceName 301 * @tc.desc Test testGetDeviceName api by callback. 302 * @tc.type Function 303 * @tc.size MediumTest 304 * @tc.level Level 2 305 */ 306 it('SUB_COMMUNICATION_BTMANAGER_GETDEVICENAME_0200', 0, async function (done) { 307 try { 308 gattClient.connect(); 309 function getName() { 310 return new Promise((resolve,reject) => { 311 gattClient.getDeviceName((err, data)=> { 312 if (err) { 313 console.error('getName failed' + err); 314 reject(err.code); 315 } else 316 { 317 console.info('[bluetooth_js]getName value:'+JSON.stringify(data)); 318 expect(true).assertEqual(data != null); 319 } 320 resolve(); 321 }); 322 }); 323 } 324 await getName().then((data) => { 325 console.info("[bluetooth_js]02 getDeviceName done"); 326 done(); 327 }) 328 .catch(e => { 329 console.info("[bluetooth_js]02 getDeviceName failed" + e); 330 expect(Number(error.code)).assertEqual(2900099); 331 done(); 332 }) 333 } catch (error) { 334 console.error(`[bluetooth_js]getDeviceName error, code is ${error.code},message is ${error.message}`); 335 expect(Number(error.code)).assertEqual(2900099); 336 } 337 gattClient.disconnect(); 338 done(); 339 }) 340 341 /** 342 * @tc.number SUB_COMMUNICATION_BTMANAGER_GETSERVICE_0100 343 * @tc.name testGetServices 344 * @tc.desc Test GetServices api by promise. 345 * @tc.type Function 346 * @tc.size MediumTest 347 * @tc.level Level 3 348 */ 349 it('SUB_COMMUNICATION_BTMANAGER_GETSERVICE_0100', 0, async function (done) { 350 try { 351 gattClient.connect(); 352 await gattClient.getServices().then((GattService) => { 353 console.info('[bluetooth_js] getServices successfully:'+JSON.stringify(GattService)); 354 expect(true).assertEqual(GattService != null); 355 done(); 356 }).catch(err => { 357 console.error('[bluetooth_js] getServices has error:'+ JSON.stringify(err)); 358 done(); 359 }); 360 } catch (error) { 361 console.error(`[bluetooth_js]GetService_0100 failed, code is ${error.code},message is ${error.message}`); 362 expect(Number(error.code)).assertEqual(2900099); 363 } 364 gattClient.disconnect(); 365 done(); 366 }) 367 368 /** 369 * @tc.number SUB_COMMUNICATION_BTMANAGER_GETSERVICE_0200 370 * @tc.name testGetServices 371 * @tc.desc Test GetServices api by callback. 372 * @tc.type Function 373 * @tc.size MediumTest 374 * @tc.level Level 3 375 */ 376 it('SUB_COMMUNICATION_BTMANAGER_GETSERVICE_0200', 0, async function (done) { 377 try { 378 gattClient.connect(); 379 function getService() { 380 return new Promise((resolve,reject) => { 381 gattClient.getServices((err, data)=> { 382 if (err) { 383 console.error('getService failed' + err); 384 reject(err.code); 385 } else 386 { 387 console.info('[bluetooth_js]getService value:'+JSON.stringify(data)); 388 expect(true).assertEqual(data != null); 389 } 390 resolve(); 391 }); 392 }); 393 } 394 await getService().then((data) => { 395 console.info("[bluetooth_js]02 getServices done"); 396 done(); 397 }) 398 .catch(e => { 399 console.info("[bluetooth_js]02 getServices failed" + e); 400 expect(2900099).assertEqual(e); 401 done(); 402 }) 403 } catch (error) { 404 console.error(`[bluetooth_js]getServices error, code is ${error.code},message is ${error.message}`); 405 } 406 gattClient.disconnect(); 407 done(); 408 }) 409 410 /** 411 * @tc.number SUB_COMMUNICATION_BTMANAGER_MTUSIZE_0100 412 * @tc.name testSetBLEMtuSize 413 * @tc.desc Test SetBLEMtuSize api. 414 * @tc.type Function 415 * @tc.size MediumTest 416 * @tc.level Level 1 417 */ 418 it('SUB_COMMUNICATION_BTMANAGER_MTUSIZE_0100', 0, function () { 419 try { 420 gattClient.setBLEMtuSize(100); 421 console.info("[bluetooth_js]setBLEMtuSize success") 422 expect(true).assertFalse(); 423 } catch (error) { 424 console.error(`[bluetooth_js]MtuSize_0100 failed, code is ${error.code}, 425 message is ${error.message}`); 426 expect(Number(error.code)).assertEqual(2900099); 427 } 428 }) 429 430 /** 431 * @tc.number SUB_COMMUNICATION_BTMANAGER_MTUSIZE_0200 432 * @tc.name testSetBLEMtuSize 433 * @tc.desc Test SetBLEMtuSize api. 434 * @tc.type Function 435 * @tc.size MediumTest 436 * @tc.level Level 2 437 */ 438 it('SUB_COMMUNICATION_BTMANAGER_MTUSIZE_0200', 0, function () { 439 try { 440 gattClient.setBLEMtuSize(22); 441 console.info("[bluetooth_js]setBLEMtuSize success") 442 expect(true).assertFalse(); 443 } catch (error) { 444 console.error(`[bluetooth_js]MtuSize_0200 failed, code is ${error.code}, 445 message is ${error.message}`); 446 expect(Number(error.code)).assertEqual(2900099); 447 } 448 }) 449 450 /** 451 * @tc.number SUB_COMMUNICATION_BTMANAGER_MTUSIZE_0300 452 * @tc.name testSetBLEMtuSize 453 * @tc.desc Test SetBLEMtuSize api. 454 * @tc.type Function 455 * @tc.size MediumTest 456 * @tc.level Level 3 457 */ 458 it('SUB_COMMUNICATION_BTMANAGER_MTUSIZE_0300', 0, function () { 459 try { 460 gattClient.setBLEMtuSize(21); 461 console.info("[bluetooth_js]setBLEMtuSize success") 462 expect(true).assertFalse(); 463 } catch (error) { 464 console.error(`[bluetooth_js]MtuSize_0300 failed, code is ${error.code}, 465 message is ${error.message}`); 466 expect(Number(error.code)).assertEqual(2900099); 467 } 468 }) 469 470 /** 471 * @tc.number SUB_COMMUNICATION_BTMANAGER_MTUSIZE_0400 472 * @tc.name testSetBLEMtuSize 473 * @tc.desc Test SetBLEMtuSize api. 474 * @tc.type Function 475 * @tc.size MediumTest 476 * @tc.level Level 3 477 */ 478 it('SUB_COMMUNICATION_BTMANAGER_MTUSIZE_0400', 0, function () { 479 try { 480 gattClient.setBLEMtuSize(512); 481 console.info("[bluetooth_js]setBLEMtuSize success") 482 expect(true).assertFalse(); 483 } catch (error) { 484 console.error(`[bluetooth_js]MtuSize_0400 failed, code is ${error.code}, 485 message is ${error.message}`); 486 expect(Number(error.code)).assertEqual(2900099); 487 } 488 }) 489 490 /** 491 * @tc.number SUB_COMMUNICATION_BTMANAGER_MTUSIZE_0500 492 * @tc.name testSetBLEMtuSize 493 * @tc.desc Test SetBLEMtuSize api. 494 * @tc.type Function 495 * @tc.size MediumTest 496 * @tc.level Level 3 497 */ 498 it('SUB_COMMUNICATION_BTMANAGER_MTUSIZE_0500', 0, function () { 499 try { 500 gattClient.setBLEMtuSize(513); 501 console.info("[bluetooth_js]setBLEMtuSize success") 502 expect(true).assertFalse(); 503 } catch (error) { 504 console.error(`[bluetooth_js]MtuSize_0500 failed, code is ${error.code}, 505 message is ${error.message}`); 506 expect(Number(error.code)).assertEqual(2900099); 507 } 508 }) 509 510 /** 511 * @tc.number SUB_COMMUNICATION_BTMANAGER_READCHARACTERISTIC_1900 512 * @tc.name testReadDescriptorValue 513 * @tc.desc Test ReadDescriptorValue api by promise. 514 * @tc.type Function 515 * @tc.size MediumTest 516 * @tc.level Level 2 517 */ 518 it('SUB_COMMUNICATION_BTMANAGER_READCHARACTERISTIC_1900', 0, async function (done) { 519 try { 520 let descriptors = []; 521 let arrayBuffer = new ArrayBuffer(8); 522 let desValue = new Uint8Array(arrayBuffer); 523 desValue[0] = 11; 524 let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 525 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 526 descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', 527 descriptorValue: arrayBuffer}; 528 descriptors[0] = descriptor; 529 let arrayBufferCCC = new ArrayBuffer(8); 530 let cccValue = new Uint8Array(arrayBufferCCC); 531 cccValue[0] = 32; 532 let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 533 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 534 characteristicValue: arrayBufferCCC, descriptors:descriptors}; 535 await gattClient.readCharacteristicValue(characteristic).then((data) => { 536 console.info('[bluetooth_js] readCharacValue promise data:' 537 + JSON.stringify(data)); 538 expect(true).assertEqual(data.length>=0); 539 done(); 540 }).catch(err => { 541 console.error(`bluetooth readCharacteValue19 promise has error: ${err}`); 542 expect(true).assertEqual(true); 543 done(); 544 }) 545 expect(true).assertFalse(); 546 } catch (error) { 547 console.error(`[bluetooth_js]readDescrValue19 failed, code is ${error.code}, 548 message is ${error.message}`); 549 expect(Number(error.code)).assertEqual(401); 550 done() 551 } 552 553 }) 554 555 /** 556 * @tc.number SUB_COMMUNICATION_BTMANAGER_READCHARACTERISTIC_2000 557 * @tc.name testReadDescriptorValue 558 * @tc.desc Test ReadDescriptorValue api by callback. 559 * @tc.type Function 560 * @tc.size MediumTest 561 * @tc.level Level 2 562 */ 563 it('SUB_COMMUNICATION_BTMANAGER_READCHARACTERISTIC_2000', 0, async function(done) { 564 try { 565 function readCcc(code, BLECharacteristic) { 566 console.log('bluetooth characteristic code:'+ code.code); 567 if (code.code != 0) { 568 return; 569 } 570 console.log('bluetooth characteristic serviceUuid:'+ BLECharacteristic.serviceUuid); 571 console.log('bluetooth characteristic characteristicUuid:'+ BLECharacteristic.characteristicUuid); 572 console.log('bluetooth characteristic characteristicValue:'+ BLECharacteristic.characteristicValue); 573 console.log('bluetooth characteristic descriptors:'+ BLECharacteristic.descriptors); 574 console.log('bluetooth characteristic properties:'+ BLECharacteristic.properties); 575 576 console.log('bluetooth BLECharacteristic write:'+ BLECharacteristic.properties.write); 577 console.log('bluetooth BLECharacteristic writeNoResponse:'+ BLECharacteristic.properties.writeNoResponse); 578 console.log('bluetooth BLECharacteristic read:'+ BLECharacteristic.properties.read); 579 console.log('bluetooth BLECharacteristic notify:'+ BLECharacteristic.properties.notify); 580 console.log('bluetooth BLECharacteristic indicate:'+ BLECharacteristic.properties.indicate); 581 582 let value = new Uint8Array(BLECharacteristic.characteristicValue); 583 console.log('bluetooth characteristic value: ' 584 + value[0] +','+ value[1]+','+ value[2]+','+ value[3]); 585 } 586 let descriptors = []; 587 let arrayBuffer = new ArrayBuffer(8); 588 let desValue = new Uint8Array(arrayBuffer); 589 desValue[0] = 11; 590 let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 591 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 592 descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', 593 descriptorValue: arrayBuffer}; 594 descriptors[0] = descriptor; 595 let arrayBufferCCC = new ArrayBuffer(8); 596 let cccValue = new Uint8Array(arrayBufferCCC); 597 cccValue[0] = 32; 598 let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 599 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 600 characteristicValue: arrayBufferCCC, descriptors:descriptors}; 601 gattClient.readCharacteristicValue(characteristic,readCcc); 602 done(); 603 } catch (error) { 604 console.error(`[bluetooth_js]readDescrValue20 failed, code is ${error.code},message is ${error.message}`); 605 expect(Number(error.code)).assertEqual(401); 606 done(); 607 } 608 done(); 609 }) 610 611 /** 612 * @tc.number SUB_COMMUNICATION_BTMANAGER_READCHARACTERISTIC_2100 613 * @tc.name testCharacteristicReadOn 614 * @tc.desc Test On and off api. 615 * @tc.type Function 616 * @tc.size MediumTest 617 * @tc.level Level 3 618 */ 619 it('SUB_COMMUNICATION_BTMANAGER_READCHARACTERISTIC_2100', 0, async function (done) { 620 try { 621 let arrayBufferCCC = new ArrayBuffer(8); 622 let cccValue = new Uint8Array(arrayBufferCCC); 623 cccValue[0] = 1123; 624 function ReadCharacteristicReq(CharacteristicReadReq) { 625 let deviceId = CharacteristicReadReq.deviceId; 626 let transId = CharacteristicReadReq.transId; 627 let offset = CharacteristicReadReq.offset; 628 let characteristicUuid = CharacteristicReadReq.characteristicUuid; 629 630 let serverResponse = {deviceId: deviceId, transId: transId, 631 status: 0, offset: offset, value:arrayBufferCCC}; 632 gattServer.sendResponse(serverResponse); 633 } 634 await gattServer.on("characteristicRead", ReadCharacteristicReq); 635 await gattServer.off("characteristicRead"); 636 } catch (error) { 637 console.error(`[bluetooth_js]readDescrValue21 failed, code is ${error.code}, 638 message is ${error.message}`); 639 expect(true).assertFalse(); 640 } 641 done(); 642 643 }) 644 645 /** 646 * @tc.number SUB_COMMUNICATION_BTMANAGER_READDESCRIPTOR_1300 647 * @tc.name testReadDescriptorValue 648 * @tc.desc Test ReadDescriptorValue api by promise. 649 * @tc.type Function 650 * @tc.size MediumTest 651 * @tc.level Level 2 652 */ 653 it('SUB_COMMUNICATION_BTMANAGER_READDESCRIPTOR_1300', 0, async function (done) { 654 try { 655 let arrayBuffer = new ArrayBuffer(8); 656 let desValue = new Uint8Array(arrayBuffer); 657 desValue[0] = 11; 658 let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 659 characteristicUuid: '00001830-0000-1000-8000-00805F9B34FB', 660 descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', 661 descriptorValue: arrayBuffer}; 662 await gattClient.readDescriptorValue(descriptor).then((object) => { 663 console.info('[bluetooth_js]readDescripValue null:' + JSON.stringify(object)); 664 expect(true).assertEqual(object!=null); 665 done(); 666 }).catch(err => { 667 console.error('[bluetooth_js]readDescrValue promise err:'+JSON.stringify(err)) 668 expect(true).assertEqual(true); 669 done(); 670 }) 671 expect(true).assertFalse(); 672 } catch (error) { 673 console.error('[bluetooth_js]readDescrValue13 error code:'+JSON.stringify(error.code)); 674 expect(Number(error.code)).assertEqual(401); 675 done() 676 } 677 }) 678 679 /** 680 * @tc.number SUB_COMMUNICATION_BTMANAGER_READDESCRIPTOR_1400 681 * @tc.name testReadDescriptorValue 682 * @tc.desc Test ReadDescriptorValue api by callback. 683 * @tc.type Function 684 * @tc.size MediumTest 685 * @tc.level Level 2 686 */ 687 it('SUB_COMMUNICATION_BTMANAGER_READDESCRIPTOR_1400', 0, async function (done) { 688 try { 689 function readDesc(code, BLEDescriptor) { 690 if (code.code != 0) { 691 console.info('[bluetooth_js] descriptor code: ' + BLEDescriptor.descriptorUuid); 692 expect(true).assertEqual(BLEDescriptor.descriptorUuid==null); 693 } 694 console.info('[bluetooth_js] descriptor uuid: ' + BLEDescriptor.descriptorUuid); 695 let value = new Uint8Array(BLEDescriptor.descriptorValue); 696 console.info('[bluetooth_js] descriptor value: ' 697 + value[0] +','+ value[1]+','+ value[2]+','+ value[3]); 698 } 699 let arrayBuffer = new ArrayBuffer(8); 700 let desValue = new Uint8Array(arrayBuffer); 701 desValue[0] = 11; 702 let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 703 characteristicUuid: '00001830-0000-1000-8000-00805F9B34FB', 704 descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', descriptorValue: arrayBuffer}; 705 gattClient.readDescriptorValue(descriptor,readDesc); 706 expect(true).assertFalse(); 707 done() 708 } catch (error) { 709 console.error('[bluetooth_js]readDescrValue14 error code:'+JSON.stringify(error)); 710 expect(Number(error.code)).assertEqual(401); 711 done() 712 } 713 }) 714 715 /** 716 * @tc.number SUB_COMMUNICATION_BTMANAGER_READDESCRIPTOR_1500 717 * @tc.name testReadDescriptorOn 718 * @tc.desc Test On and Off api. 719 * @tc.type Function 720 * @tc.size MediumTest 721 * @tc.level Level 3 722 */ 723 it('SUB_COMMUNICATION_BTMANAGER_READDESCRIPTOR_1500', 0, async function (done) { 724 try { 725 let arrayBufferCCC = new ArrayBuffer(8); 726 let cccValue = new Uint8Array(arrayBufferCCC); 727 cccValue[0] = 1011; 728 function ReadDescriptorReq(DescriptorReadReq) { 729 let deviceId = DescriptorReadReq.deviceId; 730 let transId = DescriptorReadReq.transId; 731 let offset = DescriptorReadReq.offset; 732 let characteristicUuid = DescriptorReadReq.characteristicUuid; 733 734 let serverResponse = {deviceId: deviceId, transId: transId, 735 status: 0, offset: offset, value:arrayBufferCCC}; 736 gattServer.sendResponse(serverResponse); 737 console.info("[bluetooth_js] DesRedon jsondata:" + 738 'deviceId:' + deviceId + 'transId:' +transId + 'offset:' + 739 offset +'descriptorUuid:' + DescriptorReadReq.descriptorUuid + 740 'characteristicUuid:' +characteristicUuid + 741 'serviceUuid:' + DescriptorReadReq.serviceUuid); 742 expect(true).assertEqual(DescriptorReadReq !=null); 743 } 744 await gattServer.on("descriptorRead", ReadDescriptorReq); 745 await gattServer.off("descriptorRead"); 746 } catch (error) { 747 console.error(`[bluetooth_js]readDescrValue15 failed, code is ${error.code}, 748 message is ${error.message}`); 749 expect(true).assertFalse(); 750 } 751 done(); 752 }) 753 754 /** 755 * @tc.number SUB_COMMUNICATION_BTMANAGER_WRITECHARACTERISTIC_1900 756 * @tc.name testWriteCharacteristicValue 757 * @tc.desc Test Client WriteCharacteristicValue api. 758 * @tc.type Function 759 * @tc.size MediumTest 760 * @tc.level Level 2 761 */ 762 it('SUB_COMMUNICATION_BTMANAGER_WRITECHARACTERISTIC_1900', 0, function () { 763 try { 764 let descriptors = []; 765 let arrayBuffer = new ArrayBuffer(8); 766 let desValue = new Uint8Array(arrayBuffer); 767 desValue[0] = 11; 768 let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 769 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 770 descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', 771 descriptorValue: arrayBuffer}; 772 descriptors[0] = descriptor; 773 let arrayBufferCCC = new ArrayBuffer(8); 774 let cccValue = new Uint8Array(arrayBufferCCC); 775 cccValue[0] = 32; 776 let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 777 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 778 characteristicValue: arrayBufferCCC, descriptors:descriptors}; 779 gattClient.writeCharacteristicValue(characteristic); 780 expect(true).assertFalse(); 781 } catch (error) { 782 console.error(`[bluetooth_js]writeCharacteristicValue19 failed, code is ${error.code}, 783 message is ${error.message}`); 784 expect(Number(error.code)).assertEqual(401); 785 } 786 787 }) 788 789 /** 790 * @tc.number SUB_COMMUNICATION_BTMANAGER_WRITECHARACTERISTIC_2000 791 * @tc.name testCharacteristicWriteOn 792 * @tc.desc Test on and off api. 793 * @tc.type Function 794 * @tc.size MediumTest 795 * @tc.level Level 3 796 */ 797 it('SUB_COMMUNICATION_BTMANAGER_WRITECHARACTERISTIC_2000', 0, async function (done) { 798 try { 799 let arrayBufferCCC = new ArrayBuffer(8); 800 let cccValue = new Uint8Array(arrayBufferCCC); 801 function WriteCharacteristicReq(CharacteristicWriteReq) { 802 let deviceId = CharacteristicWriteReq.deviceId; 803 let transId = CharacteristicWriteReq.transId; 804 let offset = CharacteristicWriteReq.offset; 805 let isPrep = CharacteristicWriteReq.isPrep; 806 let needRsp = CharacteristicWriteReq.needRsp; 807 let value = new Uint8Array(CharacteristicWriteReq.value); 808 let characteristicUuid = CharacteristicWriteReq.characteristicUuid; 809 810 cccValue[0] = value[0]; 811 let serverResponse = {deviceId: deviceId, transId: transId, 812 status: 0, offset: offset, value:arrayBufferCCC}; 813 gattServer.sendResponse(serverResponse); 814 } 815 gattServer.on("characteristicWrite", WriteCharacteristicReq); 816 gattServer.off("characteristicWrite"); 817 } catch (error) { 818 console.error(`[bluetooth_js]writeCharacteristicValue20 failed, code is ${error.code}, 819 message is ${error.message}`); 820 expect(true).assertFalse(); 821 } 822 done(); 823 }) 824 825 /** 826 * @tc.number SUB_COMMUNICATION_BTMANAGER_WRITEDESCRIPTOR_1100 827 * @tc.name testWriteDescriptorValue 828 * @tc.desc Test Client WriteDescriptorValue api. 829 * @tc.type Function 830 * @tc.size MediumTest 831 * @tc.level Level 2 832 */ 833 it('SUB_COMMUNICATION_BTMANAGER_WRITEDESCRIPTOR_1100', 0, function () { 834 try { 835 let arrayBuffer = new ArrayBuffer(8); 836 let desValue = new Uint8Array(arrayBuffer); 837 desValue[0] = 11; 838 let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 839 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 840 descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', 841 descriptorValue: arrayBuffer}; 842 gattClient.writeDescriptorValue(descriptor); 843 expect(true).assertFalse(); 844 } catch (error) { 845 console.error(`[bluetooth_js]writeDescriptorValue11 failed, code is ${error.code}, 846 message is ${error.message}`); 847 expect(Number(error.code)).assertEqual(401); 848 } 849 850 }) 851 852 /** 853 * @tc.number SUB_COMMUNICATION_BTMANAGER_WRITEDESCRIPTOR_1200 854 * @tc.name testWriteDescriptorOn 855 * @tc.desc Test on and off api. 856 * @tc.type Function 857 * @tc.size MediumTest 858 * @tc.level Level 3 859 */ 860 it('SUB_COMMUNICATION_BTMANAGER_WRITEDESCRIPTOR_1200', 0, async function (done) { 861 try { 862 let arrayBufferDesc = new ArrayBuffer(8); 863 let descValue = new Uint8Array(arrayBufferDesc); 864 function WriteDescriptorReq(DescriptorWriteReq) { 865 let deviceId = DescriptorWriteReq.deviceId; 866 let transId = DescriptorWriteReq.transId; 867 let offset = DescriptorWriteReq.offset; 868 let isPrep = DescriptorWriteReq.isPrep; 869 let needRsp = DescriptorWriteReq.needRsp; 870 let value = new Uint8Array(DescriptorWriteReq.value); 871 let descriptorUuid = DescriptorWriteReq.descriptorUuid; 872 descValue[0] = value[0]; 873 let serverResponse = {deviceId: deviceId, transId: transId, 874 status: 0, offset: offset, value:arrayBufferDesc}; 875 gattServer.sendResponse(serverResponse); 876 console.info("[bluetooth_js] desWriOn jsondata: " +'deviceId: ' 877 + deviceId + 'transId:' + transId + 'offset:' + offset +'descriptorUuid:' 878 + descriptorUuid + 'charUuid:' + DescriptorWriteReq.characteristicUuid + 879 'serviceUuid:' + DescriptorWriteReq.serviceUuid + 880 'value:' + DescriptorWriteReq.value + 'needRsp' + 881 needRsp + 'isPrep:' +isPrep ); 882 expect(true).assertEqual(DescriptorWriteReq !=null); 883 } 884 gattServer.on("descriptorWrite", WriteDescriptorReq); 885 gattServer.off("descriptorWrite"); 886 done() 887 } catch (error) { 888 console.error(`[bluetooth_js]writeDescriptorValue12 failed, code is ${error.code}, 889 message is ${error.message}`); 890 expect(true).assertFalse(); 891 done() 892 } 893 }) 894 895 /** 896 * @tc.number SUB_COMMUNICATION_BTMANAGER_SETNOTIFYCHARACTERISTIC_1200 897 * @tc.name testSetNotifyCharacteristicChanged 898 * @tc.desc Test SetNotifyCharacteristicChanged api. 899 * @tc.type Function 900 * @tc.size MediumTest 901 * @tc.level Level 2 902 */ 903 it('SUB_COMMUNICATION_BTMANAGER_SETNOTIFYCHARACTERISTIC_1200', 0, async function (done) { 904 try { 905 let descriptors = []; 906 let arrayBuffer = new ArrayBuffer(8); 907 let desValue = new Uint8Array(arrayBuffer); 908 desValue[0] = 11; 909 let arrayBufferNotify = new ArrayBuffer(8); 910 let descNotifyValue = new Uint8Array(arrayBufferNotify); 911 descNotifyValue[0] = 1 912 let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 913 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 914 descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', 915 descriptorValue: arrayBuffer}; 916 let descriptorNotify = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 917 descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', 918 descriptorValue: arrayBufferNotify}; 919 descriptors[0] = descriptor; 920 descriptors[1] = descriptorNotify; 921 let arrayBufferCCC = new ArrayBuffer(8); 922 let cccValue = new Uint8Array(arrayBufferCCC); 923 cccValue[0] = 1; 924 let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 925 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 926 characteristicValue: arrayBufferCCC, descriptors:descriptors}; 927 gattClient.setNotifyCharacteristicChanged(characteristic, true); 928 expect(true).assertFalse(); 929 done(); 930 } catch (error) { 931 console.error(`[bluetooth_js]setNotifyCharacteristicChanged12 failed, code is ${error.code}, 932 message is ${error.message}`); 933 expect(Number(error.code)).assertEqual(401); 934 done() 935 } 936 937 }) 938 939 /** 940 * @tc.number SUB_COMMUNICATION_BTMANAGER_SETNOTIFYCHARACTERISTIC_1300 941 * @tc.name testSetNotifyCharacteristicChanged 942 * @tc.desc Test SetNotifyCharacteristicChanged api. 943 * @tc.type Function 944 * @tc.size MediumTest 945 * @tc.level Level 2 946 */ 947 it('SUB_COMMUNICATION_BTMANAGER_SETNOTIFYCHARACTERISTIC_1300', 0, async function (done) { 948 try { 949 let descriptors = []; 950 let arrayBuffer = new ArrayBuffer(8); 951 let desValue = new Uint8Array(arrayBuffer); 952 desValue[0] = 11; 953 let arrayBufferNotify = new ArrayBuffer(8); 954 let descNotifyValue = new Uint8Array(arrayBufferNotify); 955 descNotifyValue[0] = 1 956 let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 957 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 958 descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', 959 descriptorValue: arrayBuffer}; 960 let descriptorNotify = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 961 descriptorUuid: '00002902-0000-1000-8000-00805F9B34FB', 962 descriptorValue: arrayBufferNotify}; 963 descriptors[0] = descriptor; 964 descriptors[1] = descriptorNotify; 965 let arrayBufferCCC = new ArrayBuffer(8); 966 let cccValue = new Uint8Array(arrayBufferCCC); 967 cccValue[0] = 1; 968 let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 969 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 970 characteristicValue: arrayBufferCCC, descriptors:descriptors}; 971 gattClient.setNotifyCharacteristicChanged(characteristic, false); 972 expect(true).assertFalse(); 973 done(); 974 } catch (error) { 975 console.error(`[bluetooth_js]setNotifyCharacteristicChanged13 failed, code is ${error.code}, 976 message is ${error.message}`); 977 expect(Number(error.code)).assertEqual(401); 978 done() 979 } 980 981 }) 982 983 /** 984 * @tc.number SUB_COMMUNICATION_BTMANAGER_SETNOTIFYCHARACTERISTIC_1400 985 * @tc.name testBLECharacteristicChangeON 986 * @tc.desc Test On and off api. 987 * @tc.type Function 988 * @tc.size MediumTest 989 * @tc.level Level 3 990 */ 991 it('SUB_COMMUNICATION_BTMANAGER_SETNOTIFYCHARACTERISTIC_1400', 0, async function (done) { 992 try { 993 function CharacteristicChange(CharacteristicChangeReq) { 994 let serviceUuid = CharacteristicChangeReq.serviceUuid; 995 let characteristicUuid = CharacteristicChangeReq.characteristicUuid; 996 let value = new Uint8Array(CharacteristicChangeReq.characteristicValue); 997 expect(true).assertEqual(CharacteristicChangeReq !=null); 998 } 999 gattClient.on('BLECharacteristicChange', CharacteristicChange); 1000 let descriptors = []; 1001 let arrayBuffer = new ArrayBuffer(8); 1002 let desValue = new Uint8Array(arrayBuffer); 1003 desValue[0] = 11; 1004 let descriptor = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1005 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 1006 descriptorUuid: '00001830-0000-1000-8000-00805F9B34FB', 1007 descriptorValue: arrayBuffer}; 1008 descriptors[0] = descriptor;; 1009 let arrayBufferCCC = new ArrayBuffer(8); 1010 let cccValue = new Uint8Array(arrayBufferCCC); 1011 cccValue[0] = 1; 1012 let characteristic = {serviceUuid: '00001810-0000-1000-8000-00805F9B34FB', 1013 characteristicUuid: '00001820-0000-1000-8000-00805F9B34FB', 1014 characteristicValue: arrayBufferCCC, descriptors:descriptors}; 1015 gattClient.setNotifyCharacteristicChanged(characteristic, false); 1016 } catch (error) { 1017 console.error(`[bluetooth_js]setNotifyCharacteristicChanged14 failed, code is ${error.code},message is ${error.message}`); 1018 expect(Number(error.code)).assertEqual(401); 1019 } 1020 gattClient.off('BLECharacteristicChange'); 1021 done(); 1022 }) 1023 1024 /** 1025 * @tc.number SUB_COMMUNICATION_BTMANAGER_GATTCLOSE_0100 1026 * @tc.name testGattClientClose 1027 * @tc.desc Test close api. 1028 * @tc.type Function 1029 * @tc.size MediumTest 1030 * @tc.level Level 3 1031 */ 1032 it('SUB_COMMUNICATION_BTMANAGER_GATTCLOSE_0100', 0, async function (done) { 1033 try { 1034 await gattClient.close(); 1035 console.info('[bluetooth_js]bmgm gattClient close success'); 1036 } catch (error) { 1037 console.error(`[bluetooth_js]GattClose_0100 failed, code is ${error.code}, 1038 message is ${error.message}`); 1039 expect(true).assertFalse(); 1040 } 1041 done(); 1042 }) 1043 1044}) 1045}