蓝牙变更说明

cl.bluetooth.1 蓝牙模块JS接口拆分,支持按profile加载接口能力。

  1. ArkTS接口,拆分在对应的.d.ts中。
  2. 权限变更,接口权限统一变更为ohos.permission.ACCESS_BLUETOOTH,部分接口额外需要ohos.permission.MANAGE_BLUETOOTH权限。
模块 接口文档
@ohos.bluetooth.a2dp js-apis-bluetooth-a2dp.md
@ohos.bluetooth.access js-apis-bluetooth-access.md
@ohos.bluetooth.baseProfile js-apis-bluetooth-baseProfile.md
@ohos.bluetooth.ble js-apis-bluetooth-ble.md
@ohos.bluetooth.connection js-apis-bluetooth-connection.md
@ohos.bluetooth.constant js-apis-bluetooth-constant.md
@ohos.bluetooth.hfp js-apis-bluetooth-hfp.md
@ohos.bluetooth.hid js-apis-bluetooth-hid.md
@ohos.bluetooth.pan js-apis-bluetooth-pan.md
@ohos.bluetooth.socket js-apis-bluetooth-socket.md

需要双权限校验的接口

模块 接口声明
@ohos.bluetooth.baseProfile setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy, callback: AsyncCallback<void>): void
@ohos.bluetooth.baseProfile setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy): Promise<void>
@ohos.bluetooth.baseProfile getConnectionStrategy(deviceId: string, callback: AsyncCallback<ConnectionStrategy>): void
@ohos.bluetooth.baseProfile getConnectionStrategy(deviceId: string): Promise<ConnectionStrategy>
@ohos.bluetooth.connection pairCredibleDevice(deviceId: string, transport: BluetoothTransport, callback: AsyncCallback<void>): void
@ohos.bluetooth.connection pairCredibleDevice(deviceId: string, transport: BluetoothTransport): Promise<void>
@ohos.bluetooth.connection setDevicePairingConfirmation(deviceId: string, accept: boolean): void
connect(hid profile) connect(deviceId: string): void
disconnect(hid profile) disconnect(deviceId: string): void
setTethering setTethering(enable: boolean): void

变更影响

  1. TS拆分后,接口调用import文件发生改变。
  2. 权限变更后,只需关注ohos.permission.ACCESS_BLUETOOTH和ohos.permission.MANAGE_BLUETOOTH权限。

关键接口/组件变更

拆分前 拆分后
bluetoothManager.enableBluetooth access.enableBluetooth
bluetoothManager.disableBluetooth access.disableBluetooth
bluetoothManager.getLocalName connection.getLocalName
bluetoothManager.getState access.getState
bluetoothManager.getBtConnectionState connection.getProfileConnectionState
bluetoothManager.setLocalName connection.setLocalName
bluetoothManager.pairDevice connection.pairDevice
bluetoothManager.getProfileConnectionState connection.getProfileConnectionState
bluetoothManager.cancelPairedDevice connection.cancelPairedDevice
bluetoothManager.getRemoteDeviceName connection.getRemoteDeviceName
bluetoothManager.getRemoteDeviceClass connection.getRemoteDeviceClass
bluetoothManager.getPairedDevices connection.getPairedDevices
bluetoothManager.setBluetoothScanMode connection.setBluetoothScanMode
bluetoothManager.getBluetoothScanMode connection.getBluetoothScanMode
bluetoothManager.startBluetoothDiscovery connection.startBluetoothDiscovery
bluetoothManager.stopBluetoothDiscovery connection.stopBluetoothDiscovery
bluetoothManager.setDevicePairingConfirmation connection.setDevicePairingConfirmation
bluetoothManager.on('bluetoothDeviceFind') connection.on('bluetoothDeviceFind')
bluetoothManager.off('bluetoothDeviceFind') connection.off('bluetoothDeviceFind')
bluetoothManager.on('pinRequired') connection.on('pinRequired')
bluetoothManager.off('pinRequired') connection.off('pinRequired')
bluetoothManager.on('bondStateChange') connection.on('bondStateChange')
bluetoothManager.off('bondStateChange') connection.off('bondStateChange')
bluetoothManager.on('stateChange') access.on('stateChange')
bluetoothManager.off('stateChange') access.off('stateChange')
bluetoothManager.sppListen socket.sppListen
bluetoothManager.sppAccept socket.sppAccept
bluetoothManager.sppConnect socket.sppConnect
bluetoothManager.sppCloseServerSocket socket.sppCloseServerSocket
bluetoothManager.sppCloseClientSocket socket.sppCloseClientSocket
bluetoothManager.sppWrite socket.sppWrite
bluetoothManager.on('sppRead') socket.on('sppRead')
bluetoothManager.off('sppRead') socket.off('sppRead')
bluetoothManager.getProfileInstance 已废弃
createGattServer ble.createGattServer
createGattClientDevice ble.createGattClientDevice
getConnectedBLEDevices ble.getConnectedBLEDevices
startBLEScan ble.startBLEScan
stopBLEScan ble.stopBLEScan
on('BLEDeviceFind') ble.on('BLEDeviceFind')
off('BLEDeviceFind') ble.off('BLEDeviceFind')
getConnectionDevices baseProfile.getConnectedDevices
getDeviceState baseProfile.getConnectionState
A2dpSourceProfile A2dpSourceProfile
connect(a2dp profile) connect
disconnect(a2dp profile) disconnect
on('connectionStateChange') baseProfile.on('connectionStateChange')
off('connectionStateChange') baseProfile.off('connectionStateChange')
getPlayingState getPlayingState
HandsFreeAudioGatewayProfile HandsFreeAudioGatewayProfile
connect(hfp profile) connect
disconnect(hfp profile) disconnect
on('connectionStateChange')(hfp profile) baseProfile.on('connectionStateChange')
off('connectionStateChange')(hfp profile) baseProfile.off('connectionStateChange')
connect(hid profile) connect
disconnect(hid profile) disconnect
on('connectionStateChange')(hid profile) baseProfile.on('connectionStateChange')
off('connectionStateChange')(hid profile) baseProfile.off('connectionStateChange')
disconnect(pan profile) disconnect
on('connectionStateChange')(pan profile) baseProfile.on('connectionStateChange')
off('connectionStateChange')(pan profile) baseProfile.off('connectionStateChange')
setTethering setTethering
isTetheringOn isTetheringOn
startAdvertising ble.startAdvertising
stopAdvertising ble.stopAdvertising
addService addService
removeService removeService
close close
notifyCharacteristicChanged notifyCharacteristicChanged
sendResponse sendResponse
on('characteristicRead') on('characteristicRead')
off('characteristicRead') off('characteristicRead')
on('characteristicWrite') on('characteristicWrite')
off('characteristicWrite') off('characteristicWrite')
on('descriptorRead') on('descriptorRead')
off('descriptorRead') off('descriptorRead')
on('descriptorWrite') on('descriptorWrite')
off('descriptorWrite') off('descriptorWrite')
on('connectStateChange') on('connectionStateChange')
off('connectStateChange') off('connectionStateChange')
connect connect
disconnect disconnect
close close
getServices(callback) getServices
getServices(promise) getServices
readCharacteristicValue(callback) readCharacteristicValue
readCharacteristicValue(promise) readCharacteristicValue
readDescriptorValue(callback) readDescriptorValue
readDescriptorValue(promise) readDescriptorValue
writeCharacteristicValue(callback) writeCharacteristicValue
writeDescriptorValue(promise) writeCharacteristicValue
setBLEMtuSize setBLEMtuSize
setNotifyCharacteristicChanged setCharacteristicChangeNotification
on('BLECharacteristicChange') on('BLECharacteristicChange')
off('BLECharacteristicChange') off('BLECharacteristicChange')
on('BLEConnectionStateChange') on('BLEConnectionStateChange')
off('BLEConnectionStateChange') off('BLEConnectionStateChange')
getDeviceName(callback) getDeviceName
getDeviceName(promise) getDeviceName
getRssiValue(callback) getRssiValue
getRssiValue(promise) getRssiValue
ScanMode ScanMode
BondState BondState
SppOption SppOptions
SppType SppType
GattService GattService
BLECharacteristic BLECharacteristic
BLEDescriptor BLEDescriptor
NotifyCharacteristic NotifyCharacteristic
CharacteristicReadRequest CharacteristicReadRequest
CharacteristicWriteRequest CharacteristicWriteRequest
DescriptorReadRequest DescriptorReadRequest
DescriptorWriteRequest DescriptorWriteRequest
ServerResponse ServerResponse
BLEConnectChangedState BLEConnectionChangeState
ProfileConnectionState ProfileConnectionState
ScanFilter ScanFilter
ScanOptions ScanOptions
ScanDuty ScanDuty
MatchMode MatchMode
ScanResult ScanResult
BluetoothState BluetoothState
AdvertiseSetting AdvertiseSetting
AdvertiseData AdvertiseData
ManufactureData ManufactureData
ServiceData ServiceData
PinRequiredParam PinRequiredParam
BondStateParam BondStateParam
StateChangeParam StateChangeParam
DeviceClass DeviceClass
MajorClass MajorClass
MajorMinorClass MajorMinorClass
PlayingState PlayingState
ProfileId ProfileId

新增接口声明

模块名 新增声明
@ohos.bluetooth.connection pairCredibleDevice(deviceId: string, transport: BluetoothTransport, callback: AsyncCallback<void>): void
@ohos.bluetooth.connection pairCredibleDevice(deviceId: string, transport: BluetoothTransport): Promise<void>
@ohos.bluetooth.connection getRemoteProfileUuids(deviceId: string, callback: AsyncCallback<Array<ProfileUuids>>): void
@ohos.bluetooth.connection getRemoteProfileUuids(deviceId: string): Promise<Array<ProfileUuids>>
@ohos.bluetooth.connection getLocalProfileUuids(callback: AsyncCallback<Array<ProfileUuids>>): void
@ohos.bluetooth.connection getLocalProfileUuids(): Promise<Array<ProfileUuids>>
@ohos.bluetooth.connection setDevicePinCode(deviceId: string, code: string, callback: AsyncCallback<void>): void
@ohos.bluetooth.connection setDevicePinCode(deviceId: string, code: string): Promise<void>
@ohos.bluetooth.baseProfile setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy, callback: AsyncCallback<void>): void
@ohos.bluetooth.baseProfile setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy): Promise<void>
@ohos.bluetooth.baseProfile getConnectionStrategy(deviceId: string, callback: AsyncCallback<ConnectionStrategy>): void
@ohos.bluetooth.baseProfile getConnectionStrategy(deviceId: string): Promise<ConnectionStrategy>
@ohos.bluetooth.baseprofile ConnectionStrategy
@ohos.bluetooth.ble properties
@ohos.bluetooth.ble deviceName
@ohos.bluetooth.ble includeDeviceName
@ohos.bluetooth.ble GattProperties
@ohos.bluetooth.ble setCharacteristicChangeIndication(characteristic: BLECharacteristic, enable: boolean, callback: AsyncCallback<void>): void
@ohos.bluetooth.ble setCharacteristicChangeIndication(characteristic: BLECharacteristic, enable: boolean): Promise<void>
@ohos.bluetooth.ble on(type: 'BLEMtuChange', callback: Callback<number>): void
@ohos.bluetooth.ble off(type: 'BLEMtuChange', callback?: Callback<number>): void
@ohos.bluetooth.connection pinType
@ohos.bluetooth.connection BluetoothTransport
@ohos.bluetooth.constant ProfileUuids

补充promise调用方式

模块名 新增声明
@ohos.bluetooth.ble notifyCharacteristicChanged(deviceId: string, notifyCharacteristic: NotifyCharacteristic): Promise<void>
@ohos.bluetooth.ble writeCharacteristicValue(characteristic: BLECharacteristic, writeType: GattWriteType): Promise<void>
@ohos.bluetooth.ble writeDescriptorValue(descriptor: BLEDescriptor): Promise<void>
@ohos.bluetooth.ble setCharacteristicChangeNotification(characteristic: BLECharacteristic, enable: boolean): Promise<void>

适配指导

注意根据不同profile模块选择import方式,部分接口需要import多个TS文件。

//例如@ohos.bluetooth.baseProfile下getConnectionState接口。
import a2dp from '@ohos.bluetooth.a2dp';
try {
    let a2dpSrc = a2dp.createA2dpSrcProfile();
    let ret = a2dpSrc.getConnectionState('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}