HarmonyOS鸿蒙Next中数组bleList,第一次更新页面正常显示,后面再次更新页面不刷新,只显示第一次的数据

HarmonyOS鸿蒙Next中数组bleList,第一次更新页面正常显示,后面再次更新页面不刷新,只显示第一次的数据

页面创建时扫描蓝牙,只有第一个能显示,后面的都不显示了。数据===bleList

附近设备

  • 图标
  • 名称
  • 状态
import { ble, socket } from '@kit.ConnectivityKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { BluetoothSocket } from '../../common/bluetooth/BluetoothSocket';
import { BluetoothUtil } from '../../common/bluetooth/BluetoothUtil';
import { BleScanManager } from '../../common/bluetooth/BleScanManager';
import { abilityAccessCtrl, PermissionRequestResult } from '@kit.AbilityKit';
import { Datas } from '../../common/utils/Datas';
import { Logger } from '../../common/utils/Logger';
import { JSON } from '@kit.ArkTS';

/**
 * 附近设备
 */
@Component
export struct VicinityLock{
  atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
  @State bleList:Array<ble.ScanResult> = []
  bleNameArray:Array<string> = [];
  build() {
    //附近设备
    Column(){
      Row(){
        Text()
          .width(5)
          .height(15)
          .backgroundColor(Color.Green)
          .borderRadius(2)
          .margin({right:5})
        Text('附近设备')
      }.width('100%')
      Column(){
        this.LockList()
      }
    }.width('90%')
  }

  @Builder LockList(){

    ForEach(this.bleList,(lock:ble.ScanResult) => {
      Row(){
        Image($r('app.media.hlzn_launcher'))
          .width(70)
          .height(70)
          .margin({left:5})

        Column({space:7}){
          Row({space:95}){
            Text(lock.deviceName)
            Text($r('app.string.lock_on_line'))
              .fontColor(Color.Green)

          }
          .width('100%')
          .justifyContent(FlexAlign.Start)

        }
        .height(70)
        .margin({left:20})

      }
      .borderColor(Color.White)
      .backgroundColor(Color.White)
      .width('100%')
      .height(80)
      .margin({bottom:5})
      .borderRadius(5)
    }, (lock:ble.ScanResult,index:number) => index.toString())

  }

  aboutToAppear(): void {

    try {
      let context = getContext(this);
      this.atManager.requestPermissionsFromUser(context, ['ohos.permission.ACCESS_BLUETOOTH'], (err: BusinessError, data: PermissionRequestResult)=>{
        console.info(`data: `+ JSON.stringify(data));
        console.info(`data permissions:` + data.permissions);
        console.info(`data authResults:` + data.authResults);
        //开启扫描
        BleScanManager.startScan();
        //监听扫描结果
        BleScanManager.onScanResult((bleResult:ble.ScanResult) => {

          if(this.bleNameArray.indexOf(bleResult.deviceName) == -1){
            this.bleList.push(bleResult);
          }
          this.bleNameArray.push(bleResult.deviceName)
          Logger.p('bleResult===='+bleResult.deviceName)
          Logger.p('bleResult===='+this.bleList.length)
        });
      });
    } catch(err) {
      console.log(`${JSON.stringify(err)}`);
    }

    // BluetoothUtil.enableBluetooth();
    // BluetoothSocket.sppListen();

  }
}
@Extend(Text) function normal(){
  .fontColor($r('app.color.text_color'))
  .fontSize($r('app.float.normal_text_size'))
}

@Extend(Text) function small(){
  .fontSize($r('app.float.small_text_size'))
  .fontColor($r('app.color.text_color'))
}

更多关于HarmonyOS鸿蒙Next中数组bleList,第一次更新页面正常显示,后面再次更新页面不刷新,只显示第一次的数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

List({ space: 0 }) { ForEach(this.styleList, (item: getCategoryDetailCourseListItemData,index:number) => { ListItem() { rightItemView({ item: item }) }.onClick(() =>{ this.listItemClick(item) }) }, (item: getCategoryDetailCourseListItemData) => JSON.stringify(item)) }

更多关于HarmonyOS鸿蒙Next中数组bleList,第一次更新页面正常显示,后面再次更新页面不刷新,只显示第一次的数据的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,如果bleList数组在第一次更新页面时正常显示,但后续更新页面不刷新,可能涉及数据绑定或状态管理问题。检查以下几点:

  1. 数据绑定:确保bleList数组与UI组件的数据绑定机制正确。鸿蒙使用@State@Link等装饰器管理状态,确保数组更新后触发UI刷新。

  2. 状态更新:确认在更新bleList数组时,使用了正确的状态更新方法。例如,使用this.bleList = newValue而不是直接修改数组元素,以确保状态变更被检测到。

  3. 生命周期:检查页面生命周期方法(如onPageShow),确保在页面再次显示时,bleList数组被正确更新。

  4. UI组件:确认使用的UI组件(如List)支持动态数据更新。某些组件可能需要显式调用notifyDataChanged方法以触发刷新。

  5. 异步操作:如果bleList的更新涉及异步操作,确保在数据返回后,状态被正确更新并触发UI刷新。

通过以上步骤,可以排查并解决bleList数组更新后页面不刷新的问题。

在HarmonyOS鸿蒙Next中,如果数组bleList更新后页面未刷新,可能是数据绑定或页面更新机制未正确触发。建议检查以下几点:

  1. 数据绑定:确保bleList与UI组件正确绑定,使用@State@Observed装饰器。
  2. 更新机制:调用this.setData()this.update()方法,确保数据更新后页面重新渲染。
  3. 生命周期:在onPageShowonRefresh生命周期中处理数据更新逻辑。
  4. 调试:使用console.log检查数据是否更新,确认问题所在。

通过这些步骤,确保数据更新后页面能够正确刷新。

回到顶部