rokubrightscript

How to check for network connection in BrightScript


I am trying to identify the internet connection in BrightScript using scenegraph. If there is no internet, I wanted show some error screen on internet goes off.

I checked in Roku's documentation, I didn't find much coding samples there.

I found that we can use, GetInfo().linkStatus to get the network status

But I want to add observer to get the status everytime whenever status changes.


Solution

  • you can check your message port for linkStatus events on BrightScript and reflect its state into your SG scene.

    Here is an example:

    Add a field in scene.xml to be your link status interface between brs and SG, such as

    <field id="offline" type="boolean" value="false" alwaysNotify="true" onChange="onOfflineChanged" />
    

    Then, in main.brs, create an roDeviceInfo instance (in case haven't one already) and assign the same message port you are already using:

    m.deviceInfo = CreateObject("roDeviceInfo")
    m.deviceInfo.setMessagePort(m.port)
    

    Enable link status events on your roDeviceInfo instance

    m.deviceInfo.EnableLinkStatusEvent(true)
    

    to be able to receive roDeviceInfoEvent events when waiting for new messages from your roMessagePort.

    Each time you get a message from your port, check its type and assign the new linkStatus value to the scene field created in the first step

    if msgType = "roDeviceInfoEvent" and msg.isStatusMessage() then scene.offline = not msg.getInfo().linkStatus
    

    The callback should be declared in the scene.brs file

    function onOfflineChanged()
        if(m.top.offline)
           ' your code here
        end if
    end function
    

    and that's it.

    For extra correctness, you should check its status as soon as the app is launched. You could do so with

    scene.offline = not m.deviceInfo.GetLinkStatus()
    

    before entering the while(true) loop.