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