HarmonyOS Native SDK
Device Communication
Connect and Disconnect

Connect and Disconnect

The unified device exposes three transports through bleCapability, lanCapability, and mqttCapability. Connect only through transports whose modules are installed and initialized.

BLE connection

import { GizCapacityStatus } from '@gizwits/smart-sdk';
 
await device.bleCapability.connect(device);
const controllable: boolean =
  device.bleCapability.getStatus() === GizCapacityStatus.ONLINE_CONTROLLABLE;

The operation performs GATT service discovery and protocol authentication. Repeated calls for one device reuse an active or completed connection.

await device.bleCapability.disconnect(device);

If the advertisement remains visible, status may return to ONLINE instead of OFFLINE.

LAN connection

await device.lanCapability.connect(device);

The target must have been discovered on the LAN, and its current network profile must still be valid.

await device.lanCapability.disconnect(device);

A network change, socket error, or device timeout also takes the LAN capability offline.

Automatic MQTT connection

GizMqttManager.initialize() observes authentication and bound-device data, then automatically establishes services for bound devices with cloud profiles. Applications normally only wait for controllable state:

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

Explicit MQTT connection

Use this only when the application must explicitly enable cloud subscriptions for one device:

import { GizMqttManager } from '@gizwits/smart-sdk-mqtt';
 
await device.mqttCapability.connect(device);
// Equivalent: await GizMqttManager.connectDevice(device);

Explicit MQTT disconnection

await device.mqttCapability.disconnect(device);
// Equivalent: await GizMqttManager.disconnectDevice(device);

Disconnecting one device does not close an MQTT service shared by other devices. Unsubscribe from UI state when the page disappears; whether to disconnect depends on background-control requirements.