HarmonyOS 数组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)}`);
    }
  }
}
@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'))
}
  • 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.
HarmonyOS
2024-12-24 14:57:14
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以尝试一下这样的foreach写法,示例如下:

List({ space: 0 }) {
  List({ space: 0 }) {
    ForEach(this.styleList, (item: getCategoryDetailCourseListItemData,index:number) => {
      ListItem() {
        rightItemView({
          item:item
        })
      }.onClick(() =>{
        this.listItemClick(item)
      })
    }, (item: getCategoryDetailCourseListItemData) => JSON.stringify(item))
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
分享
微博
QQ
微信
回复
2024-12-24 17:05:16


相关问题
如何更新页面列表数据
7545浏览 • 1回复 待解决
如何判断APP是否是第一次请求权限?
833浏览 • 1回复 待解决
怎么在进度条更新时候刷新页面
5223浏览 • 1回复 待解决
HarmonyOS 数据改变未刷新页面
1067浏览 • 0回复 待解决
HarmonyOS 如何刷新页面内容
345浏览 • 1回复 待解决
提问
该提问已有2人参与 ,帮助了3人