openharmony3.2.3release版本连接蓝牙耳机 原创

Xu_Yongzhe.
发布于 2023-11-20 10:57
947浏览
0收藏

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)}`)
    })
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

界面全部代码

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%' })
    }
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
收藏
回复
举报


回复
    相关推荐