Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ssaguiar
Streaming Star

How can I detect internet connection change?

Hi to all

I am developing  a channel on which I depend of internet connection to play a video.

The problem is that the solution I have found at 

https://stackoverflow.com/questions/56127302/how-to-check-for-network-connection-in-brightscript

doesn't work.

This is the way I have implemented it:

 

in the main.brs:


' The roSGScreen object is a SceneGraph canvas that displays the contents of a Scene node instance
m.screen = CreateObject("roSGScreen")
m.deviceInfo = CreateObject("roDeviceInfo")

' message port is the place where events are sent
m.port = CreateObject("roMessagePort")

' sets the message port which will be used for events from the screen
m.screen.SetMessagePort(m.port)
m.deviceInfo.setMessagePort(m.port)

' Network connection status
if(m.deviceInfo.EnableLinkStatusEvent(true)) then
print "deviceInfo Link Status IS enabled!"
else
print "deviceInfo Link Status NOT enabled!"
end if
 

' every screen object must have a Scene node, or a node that derives from the Scene node
m.scene = m.screen.CreateScene("MainScene")
m.screen.Show() ' Init method in MainScene.brs is invoked

m.scene.offline = not m.deviceInfo.GetLinkStatus()
 
 
' event loop
while(true)
' waiting for events from screen
msg = wait(0, m.port)
msgType = type(msg)
print "main - msg - ";msg
print "main - msg type - ";msgType
 
if (msgType = "roDeviceInfoEvent") then
print "MainScene roDeviceInfoEvent | msg = "; msg.getMessage() " | index = "; msg.GetIndex()
 
if (msg.isScreenClosed()) then
print "MainScene Screen closed"
end if

if(msg.isStatusMessage()) then
print "MainScene isStatusMessage"
' m.scene.offline = not msg.getInfo().linkStatus
m.scene.offline = not m.deviceInfo.GetLinkStatus()
end if
end if

if (msgType = "roSGScreenEvent") then
if (msg.IsScreenClosed()) then
print "MainScene Screen closed"
return
end if
end if

end while
 
 
In the MainScene.xml:
 
<component name="MainScene" extends="Scene">
<interface>
<field id="offline"
    type="boolean"
    value="false"
    alwaysNotify="true"
    onChange="onOfflineChanged"
/>
</interface>
 
And, in the MainScene.brs:
 
function onOfflineChanged()
    if(m.top.offline)
        print "We are OFFline."
    else
        print "We are ONline."
    end if
end function
 
 
 The problem is that the code works only when I start the channel (app).
If, after the channel is running, I turn off my wifi router (and the Roku device gets disconnected from internet), I don't get notified of disconnection, BUT, when I reconnect the power to the wifi router, after some seconds, I get the disconnection message AND the connection message.
 
 
 
main - msg - <Component: roDeviceInfoEvent>
main - msg type - roDeviceInfoEvent
MainScene roDeviceInfoEvent | msg = | index = 0
MainScene isStatusMessage
We are OFFline.
main - msg - <Component: roDeviceInfoEvent>
main - msg type - roDeviceInfoEvent
MainScene roDeviceInfoEvent | msg = | index = 0
MainScene isStatusMessage
We are ONline.
 
 
 
Can somebody shows some solution to have some notification of network connection/disconnection?
 
There is NOTHING about this in the Roku documentation examples, as always.
 
 
Thanks to all
 
0 Kudos
7 REPLIES 7
fragualej
Newbie

Re: How can I detect internet connection change?

I hope this works for you, I used to handle error connections

https://stackoverflow.com/a/76057268/16117496

ssaguiar
Streaming Star

Re: How can I detect internet connection change?

Thank you for your answer.

But, what is called at:

scene.callFunc("handleLinkStatusEvents",{})

?

How handleLinkStatusEvents works? What are the parameters the function must deal with?

Thanks

 

0 Kudos
fragualej
Newbie

Re: How can I detect internet connection change?

It's a functional field that you could add on the mainScene XML, you can send the flag with the linkStatus:

https://developer.roku.com/es-co/docs/developer-program/core-concepts/handling-application-events.md...

 

ssaguiar
Streaming Star

Re: How can I detect internet connection change?

Thank you for your answers.

Anyway, with the code you provided the link, it keeps doing the same thing:

If I disconnect the router (no internet), it will not show any message.

When I reconnect the router, after some seconds, it shows the disconnected message and the connected one, at the same time. It doesn't detect the disconnection when the router is turned off.

 

0 Kudos
ssaguiar
Streaming Star

Re: How can I detect internet connection change?

After some time I realized that the solution shown above doesn't work.
Eventually it will fire when the Internet connection is lost but will no fire when the connection is restaured.

 

The ONLY way I have found to monitoring the Internet connection state is:

In xml:

 

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

 

 

Have a timer (mine is fired every 10 seconds, but I think that it will be from 30 seconds to 1 minute).
Then, when the timer fire, this is the code to run:

 

 

function VerifyInternetConnection() ' Verify internet connection every 10 seconds (every minute?)
    print chr(10) + "Verify internet connection:"
    m.MainSceneDeviceInfo.ForceInternetStatusCheck().  ' This is de deviceInfo instance
    m.top.offline = NOT m.MainSceneDeviceInfo.GetInternetStatus()
    print "m.scene.offline = "; m.top.offline
end function

 


Then, in onOfflineChanged() :

 

function onOfflineChanged() ' m.top.offline has changed.
    if(m.top.offline)
        print "PlayerScene -> We are offline."
    else
        print "PlayerScene -> We are online."
    end if
end function

 

 
0 Kudos
danielFav
Channel Surfer

Re: How can I detect internet connection change?

You should be able to do it with 3 different ways:

1. Create a timer that will fire every x seconds and inside the callback function instantiate a new device info object that will read the internet connection status:

sub myTimerCallback()
    deviceInfo = CreateObject("rodeviceinfo")
    connectionStatus = deviceInfo.GetInternetStatus()

    ? connectionStatus ' this will show false or true
end sub

 2. Enable the internet status change event and inside the application main loop read the event and do something with it:

sub main
  deviceInfo = CreateObject("roDeviceInfo")
  deviceInfo.EnableInternetStatusEvent(true)
  port = CreateObject("roMessagePort")
  deviceInfo.setMessagePort(port)

  while (true)
      msg = wait(0, port)
      msgType = type(msg)

      if msgType = "roDeviceInfoEvent"
          connectionStatus = msg.getData()
          ' do something with connectionStatus
      end if
end sub

3. Dont use device info and create a timer that will fetch an image with x MB of size stored in some repo. Based on the time the image needs to load, compute the current internet speed or if the user is connected to internet

0 Kudos
ssaguiar
Streaming Star

Re: How can I detect internet connection change?

Dear friend @danielFav:

Unfortunately the internet status change event don't work (at least for me).

I even tried it again using your code, but it still didn't work. It fires the first time but never fires again.

The only solution was using the timer.

Now it works 100%.

Thanks for your help. I appreciate.

 

0 Kudos