中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
1.准备:这里我使用的是openharmony3.2.3realease版本的sdk以及固件做的测试,具体系统编译可参考官方gitee文档 2.接口都是采用ohos.bluetoothmanager接口 3.问题:这边有部分问题就是配对成功后,耳机重启后才可连接,不太稳定,不知道是不是代码的原因。 gitee连接:https://gitee.com/xu-yongzhe/codingxu.git,这个选择bluetoothDemo分支。 部分代码:
/*** * 添加监听器 */ addBlueListener() { // 订阅蓝牙连接状态改变事件。 let self = this bluetoothManager.on('stateChange', (data) => { logger.info(TAG, `enter on stateChange`) // 蓝牙打开 if (data === bluetoothManager.BluetoothState.STATE_ON) { logger.info(TAG, `enter BluetoothState.STATE_ON`) this.isOn = true this.getPairDeviceInfo() self.startBleScan() } // 蓝牙关闭 if (data === bluetoothManager.BluetoothState.STATE_OFF) { logger.info(TAG, `enter BluetoothState.STATE_OFF`) // self.disconnect() // self.stopBleScan() this.isOn = false // bluetooth.stopBluetoothDiscovery() // 关闭蓝牙扫描 self.mDeviceName = '' self.mDeviceRower = '' self.discoveryList = [] } logger.info(TAG, `BluetoothState = ${JSON.stringify(data)}`) }) }
界面全部代码
build() { Column() { TitleBar({ handlerClickButton: this.handlerClickButton }) Scroll() { Column() { Row() { Column() { Text($r('app.string.bluetooth')) .fontSize(30) .margin({ top: 20 }) .alignSelf(ItemAlign.Start) if (true == this.isOn) { Row() { Text($r('app.string.discovery')) .fontSize(20) .alignSelf(ItemAlign.Start) .margin({ top: '10' }) LoadingProgress() .color(Color.Grey) .width(30) .height(30) .align(Alignment.Center) .margin({ left: 10 }) } .height(40) Text("设置当前设备名称:" + bluetoothManager.setLocalName('905')) .fontSize(20) .alignSelf(ItemAlign.Start) Text("当前设备名称为:" + bluetoothManager.getLocalName()) .fontSize(20) .alignSelf(ItemAlign.Start) } } Blank() Column() { Toggle({ type: ToggleType.Switch, isOn: this.isOn }) .selectedColor('#ff2982ea') .key('toggleBtn') .onChange((isOn: boolean) => { if (isOn) { // this.isOn = true this.initBluetooth() } else { // this.isOn = false this.exitBlutooth() } }) } } .width('90%') if (this.isOn) { Divider() .vertical(false) .strokeWidth(10) .color('#ffece7e7') .lineCap(LineCapStyle.Butt) .margin('1%') Text("BLE设备列表") .fontSize(25) .fontColor('#ff565555') .margin({ left: '5%', bottom: '2%' }) .alignSelf(ItemAlign.Start) Column() { ForEach(this.discoveryBleList, (item: BlutoothInfo) => { Row() { Column() { Text("蓝牙名称: " + item.name) .fontSize(20) Text("蓝牙地址: " + item.deviceId) .fontSize(20) }.width('60%').alignItems(HorizontalAlign.Start) Column() { Button("配对") .alignSelf(ItemAlign.Center) .onClick(() => { //订阅BEL状态变化 console.info(TAG,'开始配对') try { bluetoothManager.pairDevice(item.deviceId) // bluetoothManager.on('pinRequired') } catch (e) { } }) }.width('40%').alignItems(HorizontalAlign.End) } .alignSelf(ItemAlign.Start) .width('100%') .height(50) .margin({ left: '5%', top: '1%' }) Divider() .vertical(false) .color('#ffece7e7') .lineCap(LineCapStyle.Butt) .margin('1%') }) } .width('90%') .padding({ top: 10, left: 30, right: 30, bottom: 10 }) .margin({ top: 20, bottom: 20 }) .backgroundColor(Color.White) .borderRadius(20) .borderWidth(1) .borderColor('#a3a4a7') Divider() .vertical(false) .strokeWidth(10) .color('#ffece7e7') .lineCap(LineCapStyle.Butt) .margin('1%') Text("BLE配对设备列表") .fontSize(25) .fontColor('#ff565555') .margin({ left: '5%', bottom: '2%' }) .alignSelf(ItemAlign.Start) Column() { ForEach(this.deviceBleList, (item: BlutoothInfo) => { Row() { Column() { Text("蓝牙名称: " + item.name) .fontSize(20) Text("蓝牙地址: " + item.deviceId) .fontSize(20) }.width('60%').alignItems(HorizontalAlign.Start) Column() { Button("取消配对") .alignSelf(ItemAlign.Center) .onClick(() => { //订阅BEL状态变化 try { bluetoothManager.cancelPairedDevice(item.deviceId) } catch (e) { } // bluetoothManager.on('pinRequired') }) }.width('40%').alignItems(HorizontalAlign.End) } .alignSelf(ItemAlign.Start) .width('100%') .height(50) .margin({ left: '5%', top: '1%' }) Divider() .vertical(false) .color('#ffece7e7') .lineCap(LineCapStyle.Butt) .margin('1%') }) } .width('90%') .padding({ top: 10, left: 30, right: 30, bottom: 10 }) .margin({ top: 20, bottom: 20 }) .backgroundColor(Color.White) .borderRadius(20) .borderWidth(1) .borderColor('#a3a4a7') } } } .constraintSize({ maxHeight: '85%' }) } }
微信扫码分享