Skip to content

适配设备入门

Sky233ml edited this page Feb 9, 2024 · 3 revisions

标准设备类

一个标准的设备的类如下

package com.github.miwu.miot.device

import ...

@SpecAttClass("device-name")
class DeviceName(
    device: MiotDevices.Result.Device,
    layout: ViewGroup?,
    manager: MiotDeviceManager?
) : DeviceType(device, layout, manager), SpecAttHelper {

    override fun onLayout(att: SpecAtt) = forEachAtt(att)

    override fun onPropertyFound(
        siid: Int,
        service: String,
        piid: Int,
        property: String,
        serviceDesc: String,
        obj: SpecAtt.Service.Property,
    ) {
        when (service to property) {}
    }

    override fun onActionFound(
        siid: Int,
        service: String,
        aiid: Int,
        action: String,
        serviceDesc: String,
        obj: SpecAtt.Service.Action,
    ) {
        when (service to action) {}
    }
}

SpecAttClass注解内需要修改为设备的类型名称

onPropertyFoundonActionFound方法是需要自己去适配的


适配案例

我们以小米米家蓝牙温湿度计2为例

我们通过页面获取到设备的urn

urn:miot-spec-v2:device:temperature-humidity-sensor:0000A00A:miaomiaoce-t2:1 

因此可以得知这款设备的类型为temperature-humidity-sensor

接着我们可以在页面中找到一个名为temperature-humidity-sensorservice

在该 service 下, 我们可以获取两个名字分别为temperaturerelative-humidityproperty

根据这些 property 的类型, 为传感器数据类型, 因此我们可以通过米屋的基础控件库去适配该设备

下面是适配完成的代码

@SpecAttClass("temperature-humidity-sensor")
class HTSensor(device: MiotDevices.Result.Device, layout: ViewGroup?, manager: MiotDeviceManager?) :
    DeviceType(device, layout, manager), SpecAttHelper {

    override fun onLayout(att: SpecAtt) = forEachAtt(att)

    override fun onPropertyFound(
        siid: Int,
        service: String,
        piid: Int,
        property: String,
        serviceDesc: String,
        obj: SpecAtt.Service.Property,
    ) {
        when (service to property) { // 通过 to 可以快速的找到指定的类型
            "temperature-humidity-sensor" to "temperature" -> {
                createView<SensorText>(siid, piid, obj) // 动态创建View
            }

            "temperature-humidity-sensor" to "relative-humidity" -> {
                createView<SensorText>(siid, piid, obj)
            }

            "battery" to "battery-level" -> { // 电池信息
                createView<SensorText>(siid, piid, obj)
            }
        }
    }

    override fun onActionFound(
        siid: Int,
        service: String,
        aiid: Int,
        action: String,
        serviceDesc: String,
        obj: SpecAtt.Service.Action,
    ) {

    }

}

做完这一系列适配后, 该设备就适配完成了

(我不知道还能编什么

Clone this wiki locally