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