跳到主要内容

设备状态

更新时间:2025-09-24 16:30:47
package com.polaris.iot.appsdk.libsignaling

/**
* 专属版设备状态通知管理
*/
object SpecialDeviceNotificationMgr{
/**
* 获取设备状态
*
* @param iotId 设备唯一ID
* @param callback
*/
fun getDeviceStatus(iotId: String, callback: IoTCallback)

/**
* 订阅消息通知
*
* @param deviceInfo 设备信息[SampleDeviceInfo]
* @param notificationType 订阅类型[SpecialNotificationType]
* @param callback 回调[SpecialMessageNotificationCallback]
*/
fun subscribe(deviceInfo: SampleDeviceInfo, notificationType: SpecialNotificationType, callback: SpecialMessageNotificationCallback)

/**
* 取消订阅
*
* @param deviceInfo 设备信息[SampleDeviceInfo]
* @param notificationType 订阅类型[SpecialNotificationType]
* @param callback 回调[SpecialMessageNotificationCallback]
*/
fun unsubscribe(deviceInfo: SampleDeviceInfo, notificationType: SpecialNotificationType, callback: SpecialMessageNotificationCallback)
}


/**
* 消息通知回调
*/
abstract class SpecialMessageNotificationCallback {
abstract fun onMessage(message: SpecialNotificationMessage)
}


/**
* 通知消息
*/
data class SpecialNotificationMessage(
val id: String,
val type: SpecialNotificationType,
val iotId: String?,
val data: JSONObject?,
val lensId: Int?,
) {
var productKey: String? = null
var deviceName: String? = null
}
/**
* 通知类型
*/
enum class SpecialNotificationType {
DEVICE_STATUS, //设备在线状态
}

获取设备在线状态

主动获取设备当前在线状态

import com.polaris.iot.appsdk.libsignaling.SpecialDeviceNotificationMgr;

String iotId = "设备ID";
String productKey = "设备型号";
String deviceName = "设备激活码";
//设备工作模式
String deviceWorkMode = "2";
//视频编码
String videoCodec = "H265";
//音频编码
String audioCodec = "G711";
//设备镜头数 多目设备有多个镜头
int lensCount = 1

SpecialDeviceInfo deviceInfo = new SpecialDeviceInfo(iotId, productKey, deviceName, isSupportP2P, lensCount);
SpecialDeviceNotificationMgr.getDeviceStatus(iotId, new IoTCallback() {
@Override
public void onFailure(@Nullable IoTRequest request, @NonNull IoTSDKError exception) {
hideLoading();
showToast(getString(R.string.error_api_failed) + exception);
}

@Override
public void onResponse(@NonNull IoTRequest request, @NonNull IoTResponse response) {
hideLoading();
JSONObject rspData = (JSONObject) response.getData();
Integer status = null;
if (rspData != null) {
//设备状态。0(表示未激活);1(表示在线);2(休眠) 3(表示离线);8(表示禁用)。
status = rspData.getInteger("status");
}
if (status != null) deviceInfo.setStatus(status);
}
});

监听设备通知

订阅设备通知,请在需要时订阅,不再需要通知时取消订阅

import com.polaris.iot.appsdk.libuserdevicemgr.enums.DeviceStatus;
import com.polaris.iot.appsdk.libsignaling.SpecialMessageNotificationCallback;
import com.polaris.iot.appsdk.libsignaling.SpecialDeviceNotificationMgr;
import com.polaris.iot.appsdk.libsignaling.SpecialNotificationMessage;
import com.polaris.iot.appsdk.libsignaling.SpecialNotificationType;

private DeviceInfo mDeviceInfo;
private IoTDeviceInfo mPlayDeviceInfo;

private final SpecialMessageNotificationCallback mSpecialMessageNotificationCallback = new SpecialMessageNotificationCallback() {
@Override
public void onMessage(@NonNull SpecialNotificationMessage message) {
if (message.getType() == SpecialNotificationType.DEVICE_STATUS) {
Integer status = getStatus(message);
if (status == null || status == mDeviceInfo.getStatus()) return;
mDeviceInfo.setStatus(status);
mPlayDeviceInfo = IoTDeviceInfo.copyFrom(mDeviceInfo);
//去直播
}
}

@Nullable
private static Integer getStatus(@NonNull SpecialNotificationMessage message) {
JSONObject payload = message.getData();
//这里的设备状态与主动获取设备在线状态接口返回的值定义不同: 0:离线 1:在线 2:休眠
assert payload != null;
Integer status = payload.getInteger("status");
if (status != null) {
if (status.equals(1)) status = DeviceStatus.ONLINE.getStatus();
if (status.equals(0) || status.equals(3)) status = DeviceStatus.OFFLINE.getStatus();
if (status.equals(2)) status = DeviceStatus.SLEEP.getStatus();
}
return status;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SpecialDeviceNotificationMgr.subscribe(
mDeviceInfo,
SpecialNotificationType.DEVICE_STATUS,
mSpecialMessageNotificationCallback
);
}

@Override
public void onDestroy() {
super.onDestroy();
SpecialDeviceNotificationMgr.unsubscribe(
mDeviceInfo,
SpecialNotificationType.DEVICE_STATUS,
mSpecialMessageNotificationCallback
);
}