HarmonyOS 蓝牙ble相关demo

在操作蓝牙ble,会遇到蓝牙读写问题,不确定是调用问题还是其他问题,想问下有没有官方蓝牙相关操作的demo?

HarmonyOS
2024-12-28 08:38:38
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

蓝牙ble模块可参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-bluetooth-ble-V5#blestartblescan

demo代码总共有4个页面。

主页MainPage上:

import { ble } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Client {
  @State clientInstanceSwitch: boolean = false;
  @State scanSwitch: boolean = false;
  @State deviceFindSwitch: boolean = false;
  @State bleDevices: Array<ble.ScanResult> = []

  build() {
    Column({ space: 15 }) {
      Row() {
        Text('BLE扫描')
        Blank()
        Toggle({ type: ToggleType.Switch, isOn: this.scanSwitch })
          .onChange((isOn: boolean) => {
            if (isOn) {
              this.startScan()
            } else {
              this.stopScan()
            }
            console.info('ble server instanceSwitch status:' + isOn)
          })
      }
      .itemStyle()

      Column() {
        Row() {
          Text('订阅BLE设备')
          Blank()
          Toggle({ type: ToggleType.Switch, isOn: this.deviceFindSwitch })
            .onChange((isOn: boolean) => {
              if (isOn) {
                this.onBLEDeviceFind()
              } else {
                this.offBLEDeviceFind()
              }
              console.info('ble server instanceSwitch status:' + isOn)
            })
        }
        .itemStyle()
        .borderRadius({ topLeft: 10, topRight: 10 })

        Divider()
        Scroll() {
          if (this.bleDevices.length > 0) {
            List() {
              ForEach(this.bleDevices, (item: ble.ScanResult) => {
                ListItem() {
                  Navigator({ target: 'pages/ClientDetail', type: NavigationType.Push }) {
                    Text(item.deviceId)
                      .width('100%').textAlign(TextAlign.Start)
                  }
                  .params(item)
                }
              })
            }
          } else {
            Text('暂无设备发现')
          }
        }
        .itemStyle()
        .height(380)
        .borderRadius({ bottomLeft: 10, bottomRight: 10 })
        .scrollBar(BarState.Off)
      }
    }
    .height('100%')
    .width('100%')

MainPage中:

.padding({ left: 15, right: 15, top: 20, bottom: 20 })
  .backgroundColor($r('app.color.light_gray'))
}

/**
 * 发起BLE扫描流程
 */
startScan() {
  try {
    let scanFilter: ble.ScanFilter = {
      serviceUuid: "00001810-0000-1000-8000-00805F9B34FB"
    };

    let scanOptions: ble.ScanOptions = {
      interval: 500,
      dutyMode: ble.ScanDuty.SCAN_MODE_LOW_POWER,
      matchMode: ble.MatchMode.MATCH_MODE_AGGRESSIVE,
    }
    ble.startBLEScan([scanFilter], scanOptions);
    console.info('ble Client start BLE Scan success')
    this.scanSwitch = true;
  } catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  }
}

/**
 * 关闭BLE扫描流程
 */
stopScan() {
  try {
    ble.stopBLEScan();
    console.info('ble Client stop BLE Scan success')
    this.scanSwitch = false;
  } catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  }
}

/**
 * 取消订阅BLE设备发现
 */
onBLEDeviceFind() {
  ble.on('BLEDeviceFind', (data: Array<ble.ScanResult>) => {
    console.info('bluetooth device find on = ' + JSON.stringify(data));
    this.bleDevices = data;
  });
  this.deviceFindSwitch = true;
}
MainPage下:
/**
 * 取消订阅BLE设备发现
 */
offBLEDeviceFind() {
  ble.off('BLEDeviceFind', (data: Array<ble.ScanResult>) => {
    console.info('bluetooth device find off = ' + JSON.stringify(data));
    // this.bleDevices = data;
  });
  this.deviceFindSwitch = false;
}
}

@Styles
function itemStyle() {
  .width('100%')
  .padding({ left: 15, right: 15, top: 8, bottom: 8 })
  .backgroundColor($r('app.color.white'))
  .borderRadius(10)
}

mainpage页面:

import { router } from '@kit.ArkUI'
import PermissionsUtil from '../utils/PermissionsUtil'

@Entry
@Component
struct MainPage {
  aboutToAppear(): void {
    PermissionsUtil.requestPermissions(['ohos.permission.ACCESS_BLUETOOTH'])
  }

  build() {
    Column() {
      Button('客户端').onClick((event: ClickEvent) => {
        router.pushUrl({ url: "pages/Client" })
      })
        .width('50%')

      Button('服务端').onClick((event: ClickEvent) => {
        router.pushUrl({ url: "pages/Server" })
      })
        .width('50%')
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.SpaceAround)
  }
}

ClientDetail页面代码:

import { ble, constant } from '@kit.ConnectivityKit';
import { router } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct ClientDetail {
  device: ble.ScanResult = router.getParams() as ble.ScanResult;
  @State gattClient: ble.GattClientDevice | undefined = undefined;
  @State deviceName: string = '';
  @State gattServiceInfo: ble.GattService | undefined = undefined;
  @State connectSwitch: boolean = false;
  @State stateListenSwitch: boolean = false;
  @State connectStateSwitch: boolean = false;
  @State bleCharChangeSwitch: boolean = false;
  @State connectState: ble.ProfileConnectionState = constant.ProfileConnectionState.STATE_DISCONNECTED;
  @State characteristicValue: string = '';
  private serviceUuid = "00001810-0000-1000-8000-00805F9B34FB";
  @State writeValue: string = ''

  aboutToAppear(): void {
    if (!this.gattClient) {
      this.gattClient = ble.createGattClientDevice(this.device?.deviceId);
      this.getDeviceName()
    }
  }

  build() {
    Scroll() {
      Column({ space: 10 }) {
        Text() {
          Span('设备名称: ')
          Span(this.deviceName)
        }
        .itemStyle()

        Row() {
          Text('连接状态监听')
          Blank()
          Toggle({ type: ToggleType.Switch, isOn: this.stateListenSwitch })
            .onChange((isOn: boolean) => {
              if (isOn) {
                this.onBLEConnectionStateChange()
              } else {
                this.offBLEConnectionStateChange()
              }
              console.info('ble server instanceSwitch status:' + isOn)
            })
        }
        .itemStyle()

        Row() {
          Text('连接状态')
          Blank()
          if (this.connectState == constant.ProfileConnectionState.STATE_CONNECTING) {
            LoadingProgress().height(15).width(15)
          } else {
            Toggle({ type: ToggleType.Switch, isOn: this.connectStateSwitch })
              .enabled(false)
          }
        }
        .itemStyle()

        Row() {
          Text('连接开关')
          Blank()
          Toggle({ type: ToggleType.Switch, isOn: this.connectSwitch })
            .onChange((isOn: boolean) => {
              if (isOn) {
                this.connectServer()
              } else {
                this.disconnectServer()
              }
              console.info('ble server instanceSwitch status:' + isOn)
            })
        }
        .itemStyle()

        Column({ space: 10 }) {
          Button('服务发现').onClick((event: ClickEvent) => {
            this.getServices()
          })

          Scroll() {
            if (this.gattServiceInfo) {
              Text(JSON.stringify(this.gattServiceInfo))
            } else {
              Text('暂无数据')
            }
          }
          .height(100)
          .scrollBar(BarState.Off)
        }
        .itemStyle()

        Column({ space: 10 }) {
          Button('client端读取蓝牙低功耗设备特定服务的特征值').onClick((event: ClickEvent) => {
            this.readCharacteristicValue()
          })
          Scroll() {
            if (this.characteristicValue) {
              Text(JSON.stringify(this.characteristicValue))
            } else {
              Text('暂无数据')
            }
          }
          .height(100)
          .scrollBar(BarState.Off)
        }
        .itemStyle()
分享
微博
QQ
微信
回复
2024-12-28 11:40:17
相关问题
HarmonyOS Ble蓝牙demo
273浏览 • 1回复 待解决
HarmonyOS ble蓝牙问题
253浏览 • 1回复 待解决
HarmonyOS 手机蓝牙相关demo
326浏览 • 1回复 待解决
HarmonyOS 蓝牙BLE开发 Dome
720浏览 • 1回复 待解决
HarmonyOS 有没有蓝牙,NFC相关DEMO
542浏览 • 1回复 待解决
HarmonyOS有没有蓝牙相关的操作demo
579浏览 • 1回复 待解决
HarmonyOS 蓝牙ble模块getServices失败
223浏览 • 1回复 待解决
HarmonyOS 蓝牙ble写入失败 2900099
265浏览 • 1回复 待解决
HarmonyOS 如何开发低功耗蓝牙ble
179浏览 • 1回复 待解决
HarmonyOS 蓝牙BLE使用是否需要定位
241浏览 • 1回复 待解决
BLE蓝牙开发如何实现对智能灯的控制?
6994浏览 • 1回复 待解决
HarmonyOS BLE蓝牙发送数据量大的问题
293浏览 • 1回复 待解决
HarmonyOS 录制相关demo
73浏览 • 1回复 待解决
HarmonyOS 日历门票相关demo
249浏览 • 1回复 待解决
HarmonyOS Mqtt相关demo
261浏览 • 1回复 待解决
HarmonyOS 音频录制相关demo
234浏览 • 1回复 待解决
HarmonyOS 上传文件相关Demo
761浏览 • 1回复 待解决
HarmonyOS 多媒体相关demo
274浏览 • 1回复 待解决