Device Information

Last Updated on : 2024-08-22 06:09:08download

Overview

A device information query can return the following information.

  • The device ID.
  • The IP address of the device.
  • The local IP address of the device.
  • The time zone of the device.
  • The MAC addresses.
  • The signal strength of the device.
  • The integrated circuit card identifier (ICCID) of the SIM card.
  • The connectivity of the device.
  • The device channel (for Zigbee devices only).
  • The manufacturer of the device (for third-party Matter devices only).
  • The reference signal received power (RSRP) of the device (for LTE Cat.1 devices only).

The ThingDeviceInfoManager component offers more features than the ThingSmartDevice. If you are still using ThingSmartDevice, please refer to this document.

API list

Device information model

ThingDeviceDetailInfo

Property Type Description
devId NSString The device ID.
iccid NSString The ICCID, the unique ID of the SIM card. This parameter applies to LTE Cat.1 devices only.
netStrength NSString The signal strength of the device.
lanIp NSString The local IP address of the device.
ip NSString The IP address of the device.
mac NSString The MAC address of the device.
timezone NSString The time zone of the device.
channel NSString The channel ID. This parameter applies to Zigbee gateways only.
rsrp NSNumber The RSRP value, indicating the network signal strength of the device. This parameter applies to LTE Cat.1 devices only.
wifiSignal NSNumber The Wi-Fi signal strength of the device.
homekitCode NSString The HomeKit code.
connectAbility ThingDeviceInfoConnectAbility Device connectivity:
  • ThingDeviceInfoConnectAbilityUnknown: Unknown
  • ThingDeviceInfoConnectAbilityPhone: Mobile phone only
  • ThingDeviceInfoConnectAbilityGateway: Gateway only
  • ThingDeviceInfoConnectAbilityPhoneAndGateway: Mobile phone and gateway
vendorName NSString The manufacturer of the device. This parameter applies to third-party Matter devices only.

Get device information

API description

This method returns the existing device information immediately. The information is displayed on the launch screen to prevent the app from showing a blank screen due to network latency.

- (nullable ThingDeviceDetailInfo *)deviceInfo;

Description of return values

For more information about the data model of return values, see Device information model.

Get device information asynchronously

API description

This method retrieves device information from the server asynchronously.

- (void)fetchDataSuccess:(void(^)(ThingDeviceDetailInfo *info, NSDictionary *hardwareInfo))success failure:(void(^)(NSError *error))failure;

Description of return values

Data Type Description
success None. The success callback.
  • info: Device information. For more information, see Device information model.
  • hardwareInfo: Reserved for firmware information.
failure None. The failure callback.

Method and protocol for listener

API description

Listen for changes in Wi-Fi signals.

@protocol ThingDeviceInfoManagerListener <NSObject>
@optional
- (void)deviceInfoManager:(ThingDeviceInfoManager *)manager wifiSignalDidUpdate:(int)wifiSignal;
@end

Before listening for events, add a listener through addListener:.

- (void)addListener:(id<ThingDeviceInfoManagerListener>)listener;

Remove a listener through removeListener:.

- (void)removeListener:(id<ThingDeviceInfoManagerListener>)listener;

Example

import ThingDeviceDetailKit

class DeviceDetailKitInfoVC: UITableViewController {

    var deviceId: String
    var manager: ThingDeviceInfoManager
    var items: [CustomMenuModel] = []

    init(deviceId: String) {
        self.deviceId = deviceId
        self.manager = ThingDeviceInfoManager(deviceId: deviceId)
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
        self.manager.add(self)
        self.loadData()

    }

    func loadData() {
        SVProgressHUD.show()
        self.manager.fetchDataSuccess { [weak self] info, hardware in
            var list = [CustomMenuModel]()
            list.append(CustomMenuModel(title: "devId", detail: info.devId))
            list.append(CustomMenuModel(title: "iccid", detail: info.iccid ?? ""))
            list.append(CustomMenuModel(title: "netStrength", detail: info.netStrength ?? ""))
            list.append(CustomMenuModel(title: "lanIp", detail: info.lanIp ?? ""))
            list.append(CustomMenuModel(title: "ip", detail: info.ip ?? ""))
            list.append(CustomMenuModel(title: "mac", detail: info.mac ?? ""))
            list.append(CustomMenuModel(title: "timezone", detail: info.timezone ?? ""))
            list.append(CustomMenuModel(title: "channel", detail: info.channel ?? ""))
            list.append(CustomMenuModel(title: "rsrp", detail: info.rsrp != nil ? "\(info.rsrp!)" : ""))
            list.append(CustomMenuModel(title: "wifiSignal", detail: info.wifiSignal != nil ? "\(info.wifiSignal!)" : ""))
            list.append(CustomMenuModel(title: "homekitCode", detail: info.homekitCode ?? ""))
            list.append(CustomMenuModel(title: "connectAbility", detail: "\(info.connectAbility)"))
            list.append(CustomMenuModel(title: "vendorName", detail: info.vendorName ?? ""))
            self?.items = list
            self?.tableView.reloadData()
            SVProgressHUD.dismiss()
        } failure: { e in
            SVProgressHUD.dismiss()
        }

    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
        cell.textLabel?.text = self.items[indexPath.row].title
        cell.detailTextLabel?.text = self.items[indexPath.row].detail
        return cell
    }

}

extension DeviceDetailKitInfoVC: ThingDeviceInfoManagerListener {
    func deviceInfoManager(_ manager: ThingDeviceInfoManager, wifiSignalDidUpdate wifiSignal: Int32) {
        let item = self.items.first { item in
            return item.title == "wifiSignal"
        }
        item?.detail = "\(wifiSignal)"
        self.tableView.reloadData()
    }
}

Define data model

class CellModel: NSObject {
    var title: String
    var detail: String

    init(title: String, detail: String) {
        self.title = title
        self.detail = detail
    }
}

This model is used for displaying lists. The UI looks like this:

Device Information

Create a controller

class DeviceDetailKitInfoVC: UITableViewController {

    var items: [CellModel] = []
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
        self.tableView.reloadData()
    }
}

extension DeviceDetailKitInfoVC {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
        cell.textLabel?.text = self.items[indexPath.row].title
        cell.detailTextLabel?.text = self.items[indexPath.row].detail
        return cell
    }
}

Create a controller to display device information. items stores the data, which will be present on the UI.

Get data

  1. The device ID is required to request device information. Therefore, pass in the device ID. Alter the code:

    class DeviceDetailKitInfoVC: UITableViewController {
        var deviceId: String
    
        init(deviceId: String) {
            self.deviceId = deviceId
            super.init(nibName: nil, bundle: nil)
        }
    }
    
    
  2. To get device information through ThingDeviceInfoManager, create a ThingDeviceInfoManager object. Alter the code:

    class DeviceDetailKitInfoVC: UITableViewController {
        var manager: ThingDeviceInfoManager
    
        init(deviceId: String) {
            self.deviceId = deviceId
            self.manager = ThingDeviceInfoManager(deviceId: deviceId)
            super.init(nibName: nil, bundle: nil)
        }
    }
    
  3. With the ThingDeviceInfoManager object, you can request the data.

    class DeviceDetailKitInfoVC: UITableViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
            self.loadData()
        }
    
        func loadData() {
            SVProgressHUD.show()
            self.manager.fetchDataSuccess { [weak self] info, hardware in
                var list = [CellModel]()
                list.append(CellModel(title: "devId", detail: info.devId))
                list.append(CellModel(title: "iccid", detail: info.iccid ?? ""))
                list.append(CellModel(title: "netStrength", detail: info.netStrength ?? ""))
                list.append(CellModel(title: "lanIp", detail: info.lanIp ?? ""))
                list.append(CellModel(title: "ip", detail: info.ip ?? ""))
                list.append(CellModel(title: "mac", detail: info.mac ?? ""))
                list.append(CellModel(title: "timezone", detail: info.timezone ?? ""))
                list.append(CellModel(title: "channel", detail: info.channel ?? ""))
                list.append(CellModel(title: "rsrp", detail: info.rsrp != nil ? "\(info.rsrp!)" : ""))
                list.append(CellModel(title: "wifiSignal", detail: info.wifiSignal != nil ? "\(info.wifiSignal!)" : ""))
                list.append(CellModel(title: "homekitCode", detail: info.homekitCode ?? ""))
                list.append(CellModel(title: "connectAbility", detail: "\(info.connectAbility)"))
                list.append(CellModel(title: "vendorName", detail: info.vendorName ?? ""))
                self?.items = list
                self?.tableView.reloadData()
                SVProgressHUD.dismiss()
            } failure: { e in
                SVProgressHUD.dismiss()
            }
        }
    }
    
    1. Define the method loadData.
    2. In this method, data is obtained through ThingDeviceInfoManager.fetchDataSuccess:failure:, items is constructed accordingly, and the UI is then refreshed.
    3. Finally, call loadData in the viewDidLoad method.

    The UI looks like this:

    Device Information

Listen for signal changes

The device’s signal value is reported via MQTT. Therefore, register a listener for signals to respond to changes in the device signals.

class DeviceDetailKitInfoVC: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
        self.manager.add(self)
        self.loadData()
    }

}

extension DeviceDetailKitInfoVC: ThingDeviceInfoManagerListener {
    func deviceInfoManager(_ manager: ThingDeviceInfoManager, wifiSignalDidUpdate wifiSignal: Int32) {
        let item = self.items.first { item in
            return item.title == "wifiSignal"
        }
        item?.detail = "\(wifiSignal)"
        self.tableView.reloadData()
    }
}

Register the listener in viewDidLoad and then implement ThingDeviceInfoManagerListener. This enables you to receive notifications of signal changes and update the UI accordingly.

Complete code

class DeviceDetailKitInfoVC: UITableViewController {

    var items: [CellModel] = []
    var deviceId: String

    var manager: ThingDeviceInfoManager

    init(deviceId: String) {
        self.deviceId = deviceId
        self.manager = ThingDeviceInfoManager(deviceId: deviceId)
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
        self.manager.add(self)
        self.loadData()
    }

    func loadData() {
        SVProgressHUD.show()
        self.manager.fetchDataSuccess { [weak self] info, hardware in
            var list = [CellModel]()
            list.append(CellModel(title: "devId", detail: info.devId))
            list.append(CellModel(title: "iccid", detail: info.iccid ?? ""))
            list.append(CellModel(title: "netStrength", detail: info.netStrength ?? ""))
            list.append(CellModel(title: "lanIp", detail: info.lanIp ?? ""))
            list.append(CellModel(title: "ip", detail: info.ip ?? ""))
            list.append(CellModel(title: "mac", detail: info.mac ?? ""))
            list.append(CellModel(title: "timezone", detail: info.timezone ?? ""))
            list.append(CellModel(title: "channel", detail: info.channel ?? ""))
            list.append(CellModel(title: "rsrp", detail: info.rsrp != nil ? "\(info.rsrp!)" : ""))
            list.append(CellModel(title: "wifiSignal", detail: info.wifiSignal != nil ? "\(info.wifiSignal!)" : ""))
            list.append(CellModel(title: "homekitCode", detail: info.homekitCode ?? ""))
            list.append(CellModel(title: "connectAbility", detail: "\(info.connectAbility)"))
            list.append(CellModel(title: "vendorName", detail: info.vendorName ?? ""))
            self?.items = list
            self?.tableView.reloadData()
            SVProgressHUD.dismiss()
        } failure: { e in
            SVProgressHUD.dismiss()
        }

    }
}

extension DeviceDetailKitInfoVC {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
        cell.textLabel?.text = self.items[indexPath.row].title
        cell.detailTextLabel?.text = self.items[indexPath.row].detail
        return cell
    }
}

extension DeviceDetailKitInfoVC: ThingDeviceInfoManagerListener {
    func deviceInfoManager(_ manager: ThingDeviceInfoManager, wifiSignalDidUpdate wifiSignal: Int32) {
        let item = self.items.first { item in
            return item.title == "wifiSignal"
        }
        item?.detail = "\(wifiSignal)"
        self.tableView.reloadData()
    }
}