1

我使用 JavaScript 在 JavaScript 中为三星电视构建应用程序webapis.js,现在我无法确定何时连接/断开LAN电缆。我的电视通过交换机连接到互联网。当我从交换机上断开互联网电缆时,我可以确定是通过,webapis.network.isConnectedToGateway();但是当我从交换机上断开LAN电缆时,我在电视上的应用程序卡住了,开发人员工具会抛出错误:“调试连接已关闭原因:Websocket 已断开连接”,所以我可以甚至不调试这种情况。我已经尝试在cookies/中保存日志,LocalStorage但是一旦断开开发人员工具,它们就会被删除。

那么,如何确定 LAN 电缆未连接到电视。注意:我不需要知道什么时候 GATEWAY_DISCONNECTED 而是什么时候LAN_CABLE_DETACHED

这是我迄今为止尝试过的:

function getIsConnected () {
        try {
            /// This will throw an exception when LAN is disconnected
            var isConnectedToGateway = webapis.network.isConnectedToGateway();

            console.log("isCableConnected value :: " + isConnectedToGateway);

            return isConnectedToGateway;

             /// won't help me here as when LAN is detached that doesn't work
            //return Device.getNetworkConnectionType() === "LAN";
        } catch (e) {
            log.info("isCableConnected error :: " + e.message);
            return 0;
        }
    }


/// I've also tried this, without any succes
webapis.network.getActiveConnectionType();

/// It only determines 2 states: GATEWAY_CONNECTED = 4; & GATEWAY_DISCONNECTED = 5;
/// which is not what I need 
webapis.network.addNetworkStateChangeListener(function (data) {
     if (data === 2) { // LAN_CABLE_DETACHED
       // never gets here
     } else if (data === 1) { // LAN_CABLE_ATTACHED
      // never gets here either
     }
}

4

1 回答 1

1

我可以建议您使用Systeminfo Public Web API来达到您的目的。

您可以使用:

tizen.systeminfo.getPropertyValue("ETHERNET_NETWORK", (s) => {console.log(s)}, (e) => {console.log(e)})

以确定是否连接了网络电缆。

您可以通过以下方式为有关此状态更改的通知添加侦听器:

tizen.systeminfo.addPropertyValueChangeListener("ETHERNET_NETWORK", (s) => {console.log(s)})

然后您将在电缆连接/断开时收到事件。

编辑:您可以使用公共 Web API 来确定网络是否处于以下两种状态之一:

  • 网线已连接到电视,但网络交换机无法上网(网线值为“ATTACHED”):
> tizen.systeminfo.getPropertyValue("ETHERNET_NETWORK", (s) => {console.log(s)}, (e) => {console.log(e)})
SystemInfoEthernetNetwork {cable: "ATTACHED", status: "DISCONNECTED", ipAddress: "", ipv6Address: "", macAddress: "ab:cd:ef:ab:cd:ef", …}
  • 网络电缆未连接到电视(电缆值为“DETACHED”):
> tizen.systeminfo.getPropertyValue("ETHERNET_NETWORK", (s) => {console.log(s)}, (e) => {console.log(e)})

SystemInfoEthernetNetwork {cable: "DETACHED", status: "DEACTIVATED", ipAddress: "", ipv6Address: "", macAddress: "ab:cd:ef:ab:cd:ef", …}

Web API 不支持在物理层上注册侦听器,如文档中所述在 ipAddress 和 ipv6Address 属性更改(网络层)上触发在 ETHERNET_NETWORK 属性上注册的更改侦听器。”

于 2020-01-23T11:08:32.650 回答