1# @ohos.bluetooth.pbap (Bluetooth PBAP Module) 2 3The **pbap** module provides APIs for accessing the phone book of a device using the Bluetooth Phone Book Access Profile (PBAP). 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9 10 11## Modules to Import 12 13```js 14import pbap from '@ohos.bluetooth.pbap'; 15``` 16 17 18## pbap.createPbapServerProfile 19 20createPbapServerProfile(): PbapServerProfile 21 22Create a **PbapServerProfile** instance. 23 24**System capability**: SystemCapability.Communication.Bluetooth.Core 25 26**Return value** 27 28| Type | Description | 29| ----------------------------- | ---------- | 30| PbapServerProfile | **PbapServerProfile** instance created.| 31 32**Example** 33 34```js 35import { BusinessError } from '@ohos.base'; 36try { 37 let pbapServerProfile = pbap.createPbapServerProfile(); 38 console.info('pbapServer success'); 39} catch (err) { 40 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 41} 42``` 43 44 45## PbapServerProfile 46 47Provides APIs for accessing the phone book of a device. Before using any API of **PbapServerProfile**, you need to create an instance of this class by using **createPbapServerProfile()**. 48 49 50### disconnect 51 52disconnect(deviceId: string): void 53 54Disconnects the PBAP service for a device. 55 56**System API**: This is a system API. 57 58**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 59 60**System capability**: SystemCapability.Communication.Bluetooth.Core 61 62**Parameters** 63 64| Name | Type | Mandatory | Description | 65| ------ | ------ | ---- | ------- | 66| deviceId | string | Yes | Address of the remote device.| 67 68**Error codes** 69 70For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 71 72| ID| Error Message| 73| -------- | ---------------------------- | 74|2900001 | Service stopped. | 75|2900003 | Bluetooth switch is off. | 76|2900004 | Profile is not supported. | 77|2900099 | Operation failed. | 78 79**Example** 80 81```js 82import { BusinessError } from '@ohos.base'; 83try { 84 let pbapServerProfile = pbap.createPbapServerProfile(); 85 pbapServerProfile.disconnect('XX:XX:XX:XX:XX:XX'); 86} catch (err) { 87 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 88} 89``` 90 91### setShareType 92 93setShareType(deviceId: string, type: ShareType, callback: AsyncCallback<void>): void 94 95Sets the share type of the phone book information for a device. This API uses an asynchronous callback to return the result. 96 97**System API**: This is a system API. 98 99**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 100 101**System capability**: SystemCapability.Communication.Bluetooth.Core 102 103**Parameters** 104 105| Name | Type | Mandatory | Description | 106| -------- | ------ | ---- | ----------------------------------- | 107| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 108| type | [ShareType](#sharetype) | Yes | Share type to set.| 109| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.<br> If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 110 111**Error codes** 112 113For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 114 115| ID| Error Message| 116| -------- | ---------------------------- | 117|2900001 | Service stopped. | 118|2900003 | Bluetooth switch is off. | 119|2900004 | Profile is not supported. | 120|2900099 | Operation failed. | 121 122**Example** 123 124```js 125import { BusinessError } from '@ohos.base'; 126try { 127 let pbapServerProfile = pbap.createPbapServerProfile(); 128 pbapServerProfile.setShareType('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError) => { 129 console.info('setShareType'); 130 }); 131} catch (err) { 132 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 133} 134``` 135 136 137### setShareType 138 139setShareType(deviceId: string, type: ShareType): Promise<void> 140 141Sets the share type of the phone book information for a device. This API uses a promise to return the result. 142 143**System API**: This is a system API. 144 145**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 146 147**System capability**: SystemCapability.Communication.Bluetooth.Core 148 149**Parameters** 150 151| Name | Type | Mandatory | Description | 152| -------- | ------ | ---- | ----------------------------------- | 153| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 154| type | [ShareType](#sharetype) | Yes | Share type to set.| 155 156**Return value** 157 158| Type | Description | 159| ------------------------------------------------- | ------------------- | 160| Promise<void> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 161 162**Error codes** 163 164For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 165 166| ID| Error Message| 167| -------- | ---------------------------- | 168|2900001 | Service stopped. | 169|2900003 | Bluetooth switch is off. | 170|2900004 | Profile is not supported. | 171|2900099 | Operation failed. | 172 173**Example** 174 175```js 176import { BusinessError } from '@ohos.base'; 177try { 178 let pbapServerProfile = pbap.createPbapServerProfile(); 179 pbapServerProfile.setShareType('XX:XX:XX:XX:XX:XX', 0).then(() => { 180 console.info('setShareType'); 181 }); 182} catch (err) { 183 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 184} 185``` 186 187### getShareType 188 189getShareType(deviceId: string, callback: AsyncCallback<ShareType>): void 190 191Obtains the phone book share type of a device. This API uses an asynchronous callback to return the result. 192 193**System API**: This is a system API. 194 195**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 196 197**System capability**: SystemCapability.Communication.Bluetooth.Core 198 199**Parameters** 200 201| Name | Type | Mandatory | Description | 202| -------- | ------ | ---- | ----------------------------------- | 203| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 204| callback | AsyncCallback<[ShareType](#sharetype)> | Yes | Callback invoked to return the result.<br> If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 205 206**Error codes** 207 208For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 209 210| ID| Error Message| 211| -------- | ---------------------------- | 212|2900001 | Service stopped. | 213|2900003 | Bluetooth switch is off. | 214|2900004 | Profile is not supported. | 215|2900099 | Operation failed. | 216 217**Example** 218 219```js 220import { BusinessError } from '@ohos.base'; 221try { 222 let pbapServerProfile = pbap.createPbapServerProfile(); 223 pbapServerProfile.getShareType('XX:XX:XX:XX:XX:XX', (err, type) => { 224 console.info('getShareType ' + type); 225 }); 226} catch (err) { 227 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 228} 229``` 230 231### getShareType 232 233getShareType(deviceId: string): Promise<ShareType> 234 235Obtains the phone book share type of a device. This API uses a promise to return the result. 236 237**System API**: This is a system API. 238 239**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 240 241**System capability**: SystemCapability.Communication.Bluetooth.Core 242 243**Parameters** 244 245| Name | Type | Mandatory | Description | 246| -------- | ------ | ---- | ----------------------------------- | 247| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 248 249**Return value** 250 251| Type | Description | 252| ------------------------------------------------- | ------------------- | 253| Promise<[ShareType](#sharetype)> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 254 255**Error codes** 256 257For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 258 259| ID| Error Message| 260| -------- | ---------------------------- | 261|2900001 | Service stopped. | 262|2900003 | Bluetooth switch is off. | 263|2900004 | Profile is not supported. | 264|2900099 | Operation failed. | 265 266**Example** 267 268```js 269import { BusinessError } from '@ohos.base'; 270try { 271 let pbapServerProfile = pbap.createPbapServerProfile(); 272 pbapServerProfile.getShareType('XX:XX:XX:XX:XX:XX').then((type) => { 273 console.info('getShareType ' + type); 274 }); 275} catch (err) { 276 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 277} 278``` 279 280### setPhoneBookAccessAuthorization 281 282setPhoneBookAccessAuthorization(deviceId: string, authorization: AccessAuthorization, callback: AsyncCallback<void>): void 283 284Sets the phone book access authorization for a device. This API uses an asynchronous callback to return the result. 285 286**System API**: This is a system API. 287 288**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 289 290**System capability**: SystemCapability.Communication.Bluetooth.Core 291 292**Parameters** 293 294| Name | Type | Mandatory | Description | 295| -------- | ------ | ---- | ----------------------------------- | 296| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 297| authorization | [AccessAuthorization](js-apis-bluetooth-constant.md#AccessAuthorization) | Yes | Phone book access authorization to set.| 298| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.<br> If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 299 300**Error codes** 301 302For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 303 304| ID| Error Message| 305| -------- | ---------------------------- | 306|2900001 | Service stopped. | 307|2900003 | Bluetooth switch is off. | 308|2900004 | Profile is not supported. | 309|2900099 | Operation failed. | 310 311**Example** 312 313```js 314import { BusinessError } from '@ohos.base'; 315try { 316 let pbapServerProfile = pbap.createPbapServerProfile(); 317 pbapServerProfile.setPhoneBookAccessAuthorization('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError) => { 318 console.info('setPhoneBookAccessAuthorization'); 319 }); 320} catch (err) { 321 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 322} 323``` 324 325 326### setPhoneBookAccessAuthorization 327 328setPhoneBookAccessAuthorization(deviceId: string, authorization: AccessAuthorization): Promise<void> 329 330Sets the phone book access authorization for a device. This API uses a promise to return the result. 331 332**System API**: This is a system API. 333 334**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 335 336**System capability**: SystemCapability.Communication.Bluetooth.Core 337 338**Parameters** 339 340| Name | Type | Mandatory | Description | 341| -------- | ------ | ---- | ----------------------------------- | 342| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 343| authorization | [AccessAuthorization](js-apis-bluetooth-constant.md#AccessAuthorization) | Yes | Phone book access authorization to set.| 344 345**Return value** 346 347| Type | Description | 348| ------------------------------------------------- | ------------------- | 349| Promise<void> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 350 351**Error codes** 352 353For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 354 355| ID| Error Message| 356| -------- | ---------------------------- | 357|2900001 | Service stopped. | 358|2900003 | Bluetooth switch is off. | 359|2900004 | Profile is not supported. | 360|2900099 | Operation failed. | 361 362**Example** 363 364```js 365import { BusinessError } from '@ohos.base'; 366try { 367 let pbapServerProfile = pbap.createPbapServerProfile(); 368 pbapServerProfile.setPhoneBookAccessAuthorization('XX:XX:XX:XX:XX:XX', 0).then(() => { 369 console.info('setPhoneBookAccessAuthorization'); 370 }); 371} catch (err) { 372 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 373} 374``` 375 376### getPhoneBookAccessAuthorization 377 378getPhoneBookAccessAuthorization(deviceId: string, callback: AsyncCallback<AccessAuthorization>): void 379 380Obtains the phone book access authorization of a device. This API uses an asynchronous callback to return the result. 381 382**System API**: This is a system API. 383 384**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 385 386**System capability**: SystemCapability.Communication.Bluetooth.Core 387 388**Parameters** 389 390| Name | Type | Mandatory | Description | 391| -------- | ------ | ---- | ----------------------------------- | 392| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 393| callback | AsyncCallback<[AccessAuthorization](js-apis-bluetooth-constant.md#AccessAuthorization)> | Yes | Callback invoked to return the result.<br> If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object. | 394 395**Error codes** 396 397For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 398 399| ID| Error Message| 400| -------- | ---------------------------- | 401|2900001 | Service stopped. | 402|2900003 | Bluetooth switch is off. | 403|2900004 | Profile is not supported. | 404|2900099 | Operation failed. | 405 406**Example** 407 408```js 409import { BusinessError } from '@ohos.base'; 410try { 411 let pbapServerProfile = pbap.createPbapServerProfile(); 412 pbapServerProfile.getPhoneBookAccessAuthorization('XX:XX:XX:XX:XX:XX', (err, authorization) => { 413 console.info('authorization ' + authorization); 414 }); 415} catch (err) { 416 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 417} 418``` 419 420### getPhoneBookAccessAuthorization 421 422getPhoneBookAccessAuthorization(deviceId: string): Promise<AccessAuthorization> 423 424Obtains the phone book access authorization of a device. This API uses a promise to return the result. 425 426**System API**: This is a system API. 427 428**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 429 430**System capability**: SystemCapability.Communication.Bluetooth.Core 431 432**Parameters** 433 434| Name | Type | Mandatory | Description | 435| -------- | ------ | ---- | ----------------------------------- | 436| deviceId | string | Yes | Address of the remote device, for example, XX:XX:XX:XX:XX:XX.| 437 438**Return value** 439 440| Type | Description | 441| ------------------------------------------------- | ------------------- | 442| Promise<[AccessAuthorization](js-apis-bluetooth-constant.md#AccessAuthorization)> | Promise used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 443 444**Error codes** 445 446For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 447 448| ID| Error Message| 449| -------- | ---------------------------- | 450|2900001 | Service stopped. | 451|2900003 | Bluetooth switch is off. | 452|2900004 | Profile is not supported. | 453|2900099 | Operation failed. | 454 455**Example** 456 457```js 458import { BusinessError } from '@ohos.base'; 459try { 460 let pbapServerProfile = pbap.createPbapServerProfile(); 461 pbapServerProfile.getPhoneBookAccessAuthorization('XX:XX:XX:XX:XX:XX').then((authorization) => { 462 console.info('authorization ' + authorization); 463 }); 464} catch (err) { 465 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 466} 467``` 468 469## ShareType 470 471Enumerates the phone book share types. 472 473**System API**: This is a system API. 474 475**System capability**: SystemCapability.Communication.Bluetooth.Core 476 477| Name | Value | Description | 478| ------------------ | ---- | ------ | 479| SHARE_NAME_AND_PHONE_NUMBER | 0 | Share the name and number.<br>This is a system API.| 480| SHARE_ALL | 1 | Share all information.<br>This is a system API. | 481| SHARE_NOTHING | 2 | Share nothing.<br>This is a system API. | 482