HarmonyOS 3.0开发初探!软硬结合第一个? 原创 精华

入门大师小波哥
发布于 2022-7-28 10:48
浏览
6收藏

新·蓝海

昨晚8点,万众瞩目的HarmonyOS 3.0正式发布了!除了性能提升和众多新功能外这些咱不再多说,最重要的是,HarmonyOS搭载设备数已经突破3亿,对开发者来说也意味着新机遇的开始。


我为何要这么说呢?因为从HarmonyOS 3.0开始,在开发框架层面,已经正式剔除了Java代码,包括Java UI + Java API,也就意味着需要完全使用华为新研发的ArkUI(ETS/JS + JS API)来进行App开发。

Cut the crap, show me the code!咱废话少说,来第一个3.0软硬结合项目玩玩?

这个作品其实已经提交第二届的鸿蒙创新大赛并且入围,不过当时是2.0,还必须结合Java去开发,代码属实有点杂乱。智能打蒜器。 视频演示:组装好的智能打蒜器

今天3.0终于发布了,那么直接上ETS版本的DSL以及JS API代码(首页):

import socket from '@ohos.net.socket'


@Entry
@Component
struct Index {
  @State connected: boolean = false
  @State on: boolean = false
  udp  = null

  build() {
    Stack() {
      Column() {
        Text('智能打蒜器')
          .fontSize(50)
          .fontWeight(FontWeight.Lighter)
          .fontColor(Color.White)
        Text(this.connected ? '已连接' : '搜索设备信号中...')
          .fontSize(20)
          .fontWeight(FontWeight.Lighter)
          .fontColor(Color.White)
        Text(this.on ? '运行中' : '')
          .fontSize(20)
          .fontWeight(FontWeight.Lighter)
          .fontColor(Color.White)
        Blank()

        Stack() {
          Image($r("app.media.panel2"))
            .height('100%')
            .width('100%')
            .objectFit(ImageFit.Contain)
          Image(this.connected ?
          $r("app.media.switch_red") :
          $r("app.media.switch_blue"))
            .width(120)
            .objectFit(ImageFit.Contain)
            .aspectRatio(1)
        }
        .height(180)
        .width('100%')

      }
      .width('100%')
      .height('100%')
      .backgroundColor(this.connected ? '#F2A6A6' : '#71A9FE')

      Image(this.on ? $r("app.media.on") : $r("app.media.off"))
        .objectFit(ImageFit.Contain)
        .onClick(() => {
          this.connected = !this.connected
          this.on = !this.on
          this.sendData()
        })
    }
    .width('100%')
    .height('100%')
    .onAppear(() => {
      this.initUdp()
    })
  }

  async initUdp() {
    this.udp = socket.constructUDPSocketInstance()
    this.udp.on('listening', () => {
      console.log("==on listening success");
    })

    this.udp.on('message', value => {
      console.log("==收到消息:" + value.message + ", remoteInfo:" + value.remoteInfo)
    })
    this.udp.on('message1', value => {
      console.log("==收到消息1:" + JSON.stringify(value))
    })

    this.udp.on('close', () => {
      console.log("==on closed");
    })

    this.udp.on('error', err => {
      console.log("==on error, err:" + JSON.stringify(err))
    })

    try {
      await this.udp.bind({ address: '127.0.0.1', port: 59791 })

      let state = await this.udp.getState()
      console.log('==UDP状态:' + JSON.stringify(state))
    } catch (e) {
      console.log("==UDP绑定错误:" + JSON.stringify(e))
    }

  }

  async sendData() {
    try {
      let state = await this.udp.getState()

      if (!state.isBound) {
        this.initUdp()
      }

      await this.udp.send({ data: 'Hello, server!',
        address: { address: '127.0.0.1', port: 59791 }
      })
      console.log('==发送数据成功!')

    } catch (e) {
      console.log("==发送数据错误:" + JSON.stringify(e))
    }
  }
}
  • 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.

运行预览如下几张图:
HarmonyOS 3.0开发初探!软硬结合第一个?-鸿蒙开发者社区
HarmonyOS 3.0开发初探!软硬结合第一个?-鸿蒙开发者社区
HarmonyOS 3.0开发初探!软硬结合第一个?-鸿蒙开发者社区

新版本v2发布直播

不过上述代码是第一版App,v2更炫酷适配3.0的设备更多,想了解本项目的来龙去脉,晚上来看直播吧!
世界首个?HarmonyOS 3.0软硬件结合初探直播

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
9
收藏 6
回复
举报
9
2
6
2条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

把握新机遇,从老师的公开课开始!

回复
2022-7-28 14:23:09
鸿蒙坚果
鸿蒙坚果

你手机有了?

回复
2022-7-28 14:58:48


回复
    相关推荐