HarmonyOS Native SDK
Event Listening

Event Listening

The SDK exposes login, device, transport, and data point events through GizState<T>. subscribe() emits the current value immediately by default and returns an unsubscribe function.

User state

const unsubscribeUser = userManager.subscribeUserState().subscribe((state): void => {
  // Copy authentication state and user data into ArkUI state.
});

Bound-device list

const unsubscribeBound = userManager.subscribeBoundDeviceList().subscribe((profiles): void => {
  // Cloud-bound devices changed.
});

Data point events

import { GizDataPointEvent } from '@gizwits/smart-sdk';
import { hilog } from '@kit.PerformanceAnalysisKit';
 
const unsubscribeData = device.subscribeDataPointEvents().subscribe(
  (event: GizDataPointEvent | undefined): void => {
    if (event !== undefined) {
      hilog.info(0x0000, 'GizwitsSample', 'Data source: %{public}s', String(event.source));
    }
  }
);

subscribeDataPoints() exposes merged current values. subscribeDataPointEvents() also includes the source, alerts, and faults for each event. Use event.source to distinguish BLE, LAN, and MQTT reports.

Transport status

const unsubscribeStatus = device.bleCapability.subscribeStatus().subscribe((status): void => {
  // Enable control after ONLINE_CONTROLLABLE.
});

MQTT binding events

const unsubscribeMqttBinding = GizMqttManager.subscribeBindingEvents().subscribe((event): void => {
  if (event !== undefined) {
    // event.deviceId / event.isBound
  }
});

subscribeBindEvent() is a compatibility name for the same state stream.

GizState.getValue and subscribe

const currentStatus = device.bleCapability.subscribeStatus().getValue();
const unsubscribe = device.subscribeBoundState().subscribe((isBound: boolean): void => {
  // The current value is emitted immediately by default.
});

Call every unsubscribe function in ArkUI aboutToDisappear() or when the Ability is destroyed. Do not use setValue() to mutate SDK-owned state.