1# @ohos.bluetooth.pbap (蓝牙pbap模块)(系统接口) 2 3<!--Kit: Connectivity Kit--> 4<!--Subsystem: Communication--> 5<!--Owner: @enjoy_sunshine--> 6<!--Designer: @chengguohong; @tangjia15--> 7<!--Tester: @wangfeng517--> 8<!--Adviser: @zhang_yixin13--> 9 10pbap模块提供了访问电话簿相关功能的方法。 11 12> **说明:** 13> 14> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.bluetooth.pbap (蓝牙pbap模块)](js-apis-bluetooth-pbap.md)。 16 17 18## 导入模块 19 20```js 21import { pbap } from '@kit.ConnectivityKit'; 22``` 23 24## PbapServerProfile 25 26使用PbapServerProfile方法之前需要创建该类的实例进行操作,通过createPbapServerProfile()方法构造此实例。 27 28### disconnect 29 30disconnect(deviceId: string): void 31 32断开连接设备的Pbap服务。 33 34**系统接口**:此接口为系统接口。 35 36**需要权限**:ohos.permission.ACCESS_BLUETOOTH 37 38**系统能力**:SystemCapability.Communication.Bluetooth.Core 39 40**参数:** 41 42| 参数名 | 类型 | 必填 | 说明 | 43| ------ | ------ | ---- | ------- | 44| deviceId | string | 是 | 远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 45 46**错误码**: 47 48以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 49 50| 错误码ID | 错误信息 | 51| -------- | ---------------------------- | 52|201 | Permission denied. | 53|202 | Non-system applications are not allowed to use system APIs. | 54|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 55|801 | Capability not supported. | 56|2900001 | Service stopped. | 57|2900003 | Bluetooth disabled. | 58|2900004 | Profile not supported. | 59|2900099 | Operation failed. | 60 61**示例:** 62 63```js 64import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 65try { 66 let pbapServerProfile = pbap.createPbapServerProfile(); 67 pbapServerProfile.disconnect('XX:XX:XX:XX:XX:XX'); 68} catch (err) { 69 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 70} 71``` 72 73### setShareType 74 75setShareType(deviceId: string, type: ShareType, callback: AsyncCallback<void>): void 76 77设置电话簿信息的共享类型。使用Callback异步回调。 78 79**系统接口**:此接口为系统接口。 80 81**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 82 83**系统能力**:SystemCapability.Communication.Bluetooth.Core 84 85**参数:** 86 87| 参数名 | 类型 | 必填 | 说明 | 88| -------- | ------ | ---- | ----------------------------------- | 89| deviceId | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 90| type | [ShareType](#sharetype) | 是 | 表示共享类型的枚举值。 | 91| callback | AsyncCallback<void> | 是 | 回调函数。当设置成功,err为undefined,否则为错误对象。 | 92 93**错误码**: 94 95以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 96 97| 错误码ID | 错误信息 | 98| -------- | ---------------------------- | 99|201 | Permission denied. | 100|202 | Non-system applications are not allowed to use system APIs. | 101|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 102|801 | Capability not supported. | 103|2900001 | Service stopped. | 104|2900003 | Bluetooth disabled. | 105|2900004 | Profile not supported. | 106|2900099 | Operation failed. | 107 108**示例:** 109 110```js 111import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 112try { 113 let pbapServerProfile = pbap.createPbapServerProfile(); 114 pbapServerProfile.setShareType('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError) => { 115 console.info('setShareType'); 116 }); 117} catch (err) { 118 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 119} 120``` 121 122 123### setShareType 124 125setShareType(deviceId: string, type: ShareType): Promise<void> 126 127设置电话簿信息的共享类型。使用Promise异步回调。 128 129**系统接口**:此接口为系统接口。 130 131**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 132 133**系统能力**:SystemCapability.Communication.Bluetooth.Core 134 135**参数:** 136 137| 参数名 | 类型 | 必填 | 说明 | 138| -------- | ------ | ---- | ----------------------------------- | 139| deviceId | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 140| type | [ShareType](#sharetype) | 是 | 表示共享类型的枚举值。 | 141 142**返回值:** 143 144| 类型 | 说明 | 145| ------------------------------------------------- | ------------------- | 146| Promise<void> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 | 147 148**错误码**: 149 150以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 151 152| 错误码ID | 错误信息 | 153| -------- | ---------------------------- | 154|201 | Permission denied. | 155|202 | Non-system applications are not allowed to use system APIs. | 156|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 157|801 | Capability not supported. | 158|2900001 | Service stopped. | 159|2900003 | Bluetooth disabled. | 160|2900004 | Profile not supported. | 161|2900099 | Operation failed. | 162 163**示例:** 164 165```js 166import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 167try { 168 let pbapServerProfile = pbap.createPbapServerProfile(); 169 pbapServerProfile.setShareType('XX:XX:XX:XX:XX:XX', 0).then(() => { 170 console.info('setShareType'); 171 }); 172} catch (err) { 173 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 174} 175``` 176 177### getShareType 178 179getShareType(deviceId: string, callback: AsyncCallback<ShareType>): void 180 181获取电话簿信息的共享类型。使用Callback异步回调。 182 183**系统接口**:此接口为系统接口。 184 185**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 186 187**系统能力**:SystemCapability.Communication.Bluetooth.Core 188 189**参数:** 190 191| 参数名 | 类型 | 必填 | 说明 | 192| -------- | ------ | ---- | ----------------------------------- | 193| deviceId | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 194| callback | AsyncCallback<[ShareType](#sharetype)> | 是 | 回调函数。当获取成功,err为undefined,否则为错误对象。 | 195 196**错误码**: 197 198以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 199 200| 错误码ID | 错误信息 | 201| -------- | ---------------------------- | 202|201 | Permission denied. | 203|202 | Non-system applications are not allowed to use system APIs. | 204|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 205|801 | Capability not supported. | 206|2900001 | Service stopped. | 207|2900003 | Bluetooth disabled. | 208|2900004 | Profile not supported. | 209|2900099 | Operation failed. | 210 211**示例:** 212 213```js 214import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 215try { 216 let pbapServerProfile = pbap.createPbapServerProfile(); 217 pbapServerProfile.getShareType('XX:XX:XX:XX:XX:XX', (err, type) => { 218 console.info('getShareType ' + type); 219 }); 220} catch (err) { 221 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 222} 223``` 224 225### getShareType 226 227getShareType(deviceId: string): Promise<ShareType> 228 229获取电话簿信息的共享类型。使用Promise异步回调。 230 231**系统接口**:此接口为系统接口。 232 233**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 234 235**系统能力**:SystemCapability.Communication.Bluetooth.Core 236 237**参数:** 238 239| 参数名 | 类型 | 必填 | 说明 | 240| -------- | ------ | ---- | ----------------------------------- | 241| deviceId | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 242 243**返回值:** 244 245| 类型 | 说明 | 246| ------------------------------------------------- | ------------------- | 247| Promise<[ShareType](#sharetype)> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 | 248 249**错误码**: 250 251以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 252 253| 错误码ID | 错误信息 | 254| -------- | ---------------------------- | 255|201 | Permission denied. | 256|202 | Non-system applications are not allowed to use system APIs. | 257|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 258|801 | Capability not supported. | 259|2900001 | Service stopped. | 260|2900003 | Bluetooth disabled. | 261|2900004 | Profile not supported. | 262|2900099 | Operation failed. | 263 264**示例:** 265 266```js 267import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 268try { 269 let pbapServerProfile = pbap.createPbapServerProfile(); 270 pbapServerProfile.getShareType('XX:XX:XX:XX:XX:XX').then((type) => { 271 console.info('getShareType ' + type); 272 }); 273} catch (err) { 274 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 275} 276``` 277 278### setPhoneBookAccessAuthorization 279 280setPhoneBookAccessAuthorization(deviceId: string, authorization: AccessAuthorization, callback: AsyncCallback<void>): void 281 282设置电话簿信息的访问权限。使用Callback异步回调。 283 284**系统接口**:此接口为系统接口。 285 286**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 287 288**系统能力**:SystemCapability.Communication.Bluetooth.Core 289 290**参数:** 291 292| 参数名 | 类型 | 必填 | 说明 | 293| -------- | ------ | ---- | ----------------------------------- | 294| deviceId | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 295| authorization | [AccessAuthorization](js-apis-bluetooth-constant-sys.md#accessauthorization11) | 是 | 表示访问权限枚举值。 | 296| callback | AsyncCallback<void> | 是 | 回调函数。当设置成功,err为undefined,否则为错误对象。 | 297 298**错误码**: 299 300以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 301 302| 错误码ID | 错误信息 | 303| -------- | ---------------------------- | 304|201 | Permission denied. | 305|202 | Non-system applications are not allowed to use system APIs. | 306|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 307|801 | Capability not supported. | 308|2900001 | Service stopped. | 309|2900003 | Bluetooth disabled. | 310|2900004 | Profile not supported. | 311|2900099 | Operation failed. | 312 313**示例:** 314 315```js 316import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 317try { 318 let pbapServerProfile = pbap.createPbapServerProfile(); 319 pbapServerProfile.setPhoneBookAccessAuthorization('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError) => { 320 console.info('setPhoneBookAccessAuthorization'); 321 }); 322} catch (err) { 323 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 324} 325``` 326 327 328### setPhoneBookAccessAuthorization 329 330setPhoneBookAccessAuthorization(deviceId: string, authorization: AccessAuthorization): Promise<void> 331 332设置电话簿信息的访问权限。使用Promise异步回调。 333 334**系统接口**:此接口为系统接口。 335 336**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 337 338**系统能力**:SystemCapability.Communication.Bluetooth.Core 339 340**参数:** 341 342| 参数名 | 类型 | 必填 | 说明 | 343| -------- | ------ | ---- | ----------------------------------- | 344| deviceId | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 345| authorization | [AccessAuthorization](js-apis-bluetooth-constant-sys.md#accessauthorization11) | 是 | 表示访问权限枚举值。 | 346 347**返回值:** 348 349| 类型 | 说明 | 350| ------------------------------------------------- | ------------------- | 351| Promise<void> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 | 352 353**错误码**: 354 355以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 356 357| 错误码ID | 错误信息 | 358| -------- | ---------------------------- | 359|201 | Permission denied. | 360|202 | Non-system applications are not allowed to use system APIs. | 361|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 362|801 | Capability not supported. | 363|2900001 | Service stopped. | 364|2900003 | Bluetooth disabled. | 365|2900004 | Profile not supported. | 366|2900099 | Operation failed. | 367 368**示例:** 369 370```js 371import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 372try { 373 let pbapServerProfile = pbap.createPbapServerProfile(); 374 pbapServerProfile.setPhoneBookAccessAuthorization('XX:XX:XX:XX:XX:XX', 0).then(() => { 375 console.info('setPhoneBookAccessAuthorization'); 376 }); 377} catch (err) { 378 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 379} 380``` 381 382### getPhoneBookAccessAuthorization 383 384getPhoneBookAccessAuthorization(deviceId: string, callback: AsyncCallback<AccessAuthorization>): void 385 386获取电话簿信息的访问权限。使用Callback异步回调。 387 388**系统接口**:此接口为系统接口。 389 390**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 391 392**系统能力**:SystemCapability.Communication.Bluetooth.Core 393 394**参数:** 395 396| 参数名 | 类型 | 必填 | 说明 | 397| -------- | ------ | ---- | ----------------------------------- | 398| deviceId | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 399| callback | AsyncCallback<[AccessAuthorization](js-apis-bluetooth-constant-sys.md#accessauthorization11)> | 是 | 回调函数。当获取成功,err为undefined,否则为错误对象。 | 400 401**错误码**: 402 403以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 404 405| 错误码ID | 错误信息 | 406| -------- | ---------------------------- | 407|201 | Permission denied. | 408|202 | Non-system applications are not allowed to use system APIs. | 409|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 410|801 | Capability not supported. | 411|2900001 | Service stopped. | 412|2900003 | Bluetooth disabled. | 413|2900004 | Profile not supported. | 414|2900099 | Operation failed. | 415 416**示例:** 417 418```js 419import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 420try { 421 let pbapServerProfile = pbap.createPbapServerProfile(); 422 pbapServerProfile.getPhoneBookAccessAuthorization('XX:XX:XX:XX:XX:XX', (err, authorization) => { 423 console.info('authorization ' + authorization); 424 }); 425} catch (err) { 426 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 427} 428``` 429 430### getPhoneBookAccessAuthorization 431 432getPhoneBookAccessAuthorization(deviceId: string): Promise<AccessAuthorization> 433 434获取电话簿信息的访问权限。使用Promise异步回调。 435 436**系统接口**:此接口为系统接口。 437 438**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH 439 440**系统能力**:SystemCapability.Communication.Bluetooth.Core 441 442**参数:** 443 444| 参数名 | 类型 | 必填 | 说明 | 445| -------- | ------ | ---- | ----------------------------------- | 446| deviceId | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | 447 448**返回值:** 449 450| 类型 | 说明 | 451| ------------------------------------------------- | ------------------- | 452| Promise<[AccessAuthorization](js-apis-bluetooth-constant-sys.md#accessauthorization11)> | 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。 | 453 454**错误码**: 455 456以下错误码的详细介绍请参见[通用错误码说明文档](../errorcode-universal.md)和[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 457 458| 错误码ID | 错误信息 | 459| -------- | ---------------------------- | 460|201 | Permission denied. | 461|202 | Non-system applications are not allowed to use system APIs. | 462|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 463|801 | Capability not supported. | 464|2900001 | Service stopped. | 465|2900003 | Bluetooth disabled. | 466|2900004 | Profile not supported. | 467|2900099 | Operation failed. | 468 469**示例:** 470 471```js 472import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 473try { 474 let pbapServerProfile = pbap.createPbapServerProfile(); 475 pbapServerProfile.getPhoneBookAccessAuthorization('XX:XX:XX:XX:XX:XX').then((authorization) => { 476 console.info('authorization ' + authorization); 477 }); 478} catch (err) { 479 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 480} 481``` 482 483## ShareType 484 485枚举,共享类型。 486 487**系统接口**:此接口为系统接口。 488 489**系统能力**:SystemCapability.Communication.Bluetooth.Core 490 491| 名称 | 值 | 说明 | 492| ------------------ | ---- | ------ | 493| SHARE_NAME_AND_PHONE_NUMBER | 0 | 共享名字和号码信息。<br/>此接口为系统接口。 | 494| SHARE_ALL | 1 | 共享所有信息。<br/>此接口为系统接口。 | 495| SHARE_NOTHING | 2 | 不共享。<br/>此接口为系统接口。 |