Device Linking Code - Closing the App

HI Everyone,

I am trying to develop a ROKU App based “VideoPlayer” example that displays list of media items to user on “Sucessfull device linking”.
As soon the users clicks on the app, I show them the device linking screen with a 6 digit code.

if 6 digit code is validated on polling, the users are supposed to redirect to HomeScreen calling showHomeScreen(screen) function.
Instead, the app is closing on successful device linking.

After hours of debugging, I cannot seem to figure this out. Everything looks like working, Showing the Registration Screen, Successful validation and calling “InitCategoryFeed()”. Looks like something is forcing the app to close.

I am wondering if there are any asynchronous operations happening in the showLinkingCode() function below.

Below is code snippets from my code. I really appreciate if someone can help me find where I am going wrong.

PS: The device linking I am using is from ROKU developer blog post.

Please help me.

Thank You,


Sub Main()

    'initialize theme attributes like titles, logos and overhang color
    initTheme()

    'prepare the screen for display and get ready to begin
    screen=preShowHomeScreen("", "")
    if screen=invalid then
        print "unexpected error in preShowHomeScreen"
        return
    end if

	print "show linking code screen"
	rokulinkingcode = ShowLinkScreen()

	 if rokulinkingcode=1 then
        print "set to go, time to get started"
		showHomeScreen(screen)
    end if
    

End Sub


'********************************************************
'** Show Linking Request Screen Return 0 if Code validation fails Else returns 1
'********************************************************
Function ShowLinkScreen() As Integer
  result =0
  dt = CreateObject("roDateTime")

  ' create a roCodeRegistrationScreen and assign it a roMessagePort
  port = CreateObject("roMessagePort")
  screen = CreateObject("roCodeRegistrationScreen")
  screen.SetMessagePort(port)

  ' add some header text
  screen.AddHeaderText("Account Registration")
  ' add some buttons
  screen.AddButton(1, "Get new code")
  screen.AddButton(2, "Back")
	screen.AddParagraph(" ")
  ' Focal text should give specific instructions to the user
  screen.AddFocalText("Go to http://www.mywebsite.net/roku, log into your account, and enter the following code.", "spacing-normal")

  ' display a retrieving message until we get a linking code
  screen.SetRegistrationCode("Retrieving...")
  screen.Show()

  ' get a new code
  linkingCode = GetLinkingCode()
  if linkingCode <> invalid
    screen.SetRegistrationCode(linkingCode.code)
  else
    screen.SetRegistrationCode("Failed to get code...")
  end if

  screen.Show()

  while true
    ' we want to poll the API every 15 seconds for validation,
    ' so set a 5000 millisecond timeout on the Wait()
    msg = Wait(5000, screen.GetMessagePort())

    if msg = invalid
      ' poll the API for validation
      if ValidateLinkingCode(linkingCode.code)
        ' if validation succeeded, close the screen
		     result =1
        exit while
		screen.ClearButtons()
      end if

      dt.Mark()
      if dt.AsSeconds() > linkingCode.expires
        ' the code expired. display a message, then get a new one
        d = CreateObject("roMessageDialog")
        dPort = CreateObject("roMessagePort")
        d.SetMessagePort(dPort)
        d.SetTitle("Code Expired")
        d.SetText("This code has expired. Press OK to get a new one")
        d.AddButton(1, "OK")
        d.Show()

        Wait(0, dPort)
        d.Close()
        screen.SetRegistrationCode("Retrieving...")
        screen.Show()

        linkingCode = GetLinkingCode()
        if linkingCode <> invalid
          screen.SetRegistrationCode(linkingCode.code)
        else
          screen.SetRegistrationCode("Failed to get code...")
        end if
        screen.Show()
      end if
    else if type(msg) = "roCodeRegistrationScreenEvent"
      if msg.isScreenClosed()
	    print "screen closed";
        exit while
      else if msg.isButtonPressed()
        if msg.GetIndex() = 1
          ' the user wants a new code
          code = GetLinkingCode()
          linkingCode = GetLinkingCode()
          if linkingCode <> invalid
            screen.SetRegistrationCode(linkingCode.code)
          else
            screen.SetRegistrationCode("Failed to get code...")
          end if
          screen.Show()
        else if msg.GetIndex() = 2
		
          print " the user wants to close the screen"
		  screen.Close()
		  result =0
          exit while
        end if
      end if
    end if
  end while
  print result
  return result
  
end function

'********************************************************
'** Show GetLinkingCode
'********************************************************
function GetLinkingCode()
  print "getting linking code..."
  result = invalid
  DeviceESN = GetDeviceESN()
  xfer = CreateObject("roURLTransfer")
  xfer.SetURL("myURL?action=GetLinkingCode&DeviceESN="+DeviceESN)
  response = xfer.GetToString()
  xml = CreateObject("roXMLElement")
  if xml.Parse(response)
    result = {
      code: xml.linkingCode.GetText()
      expires: StrToI(xml.linkingCode@expires)
    }
  end if

  return result
end function


'********************************************************
'** Show ValidateLinkingCode
'********************************************************

function ValidateLinkingCode(linkingCode)
  result = false
  print "Validating Result---"
  xfer = CreateObject("roURLTransfer")
  xfer.SetURL("myURL?action=ValidateLinkingCode&code=" + linkingCode)
  response = xfer.GetToString()
  xml = CreateObject("roXMLElement")
  if xml.Parse(response)
  print "Validating Result: " + xml.status@msg
    if UCase(xml.status@msg) = "SUCCESS"
      sec = CreateObject("roRegistrySection", "mySection")
      sec.Write("deviceToken", xml.deviceToken.GetText())
      sec.Flush()

      result = true
    end if
  end if

  return result
end function

viewtopic.php?f=34&t=50590

–Mark

“RokuMarkn” wrote:
http://forums.roku.com/viewtopic.php?f=34&t=50590

–Mark
Mark,

I can’t thank you enough. The ‘facade’ screen thing worked for me.

Thank You so much.

Can you clarify one more thing? Is asyncpostfromstring() is the only way to send parameters to roURLTransfer object using HTTP Post?

Roku Team Rocks!!!

“1024Tech” wrote:

“RokuMarkn” wrote:
http://forums.roku.com/viewtopic.php?f=34&t=50590

–Mark
Mark,

I can’t thank you enough. The ‘facade’ screen thing worked for me.

Thank You so much.

Can you clarify one more thing? Is asyncpostfromstring() is the only way to send parameters to roURLTransfer object using HTTP Post?

Roku Team Rocks!!!
There’s also:

Integer PostFromString(String request)

  • Use the HTTP POST method to post the supplied string to the current URL and return the response code. Any response body is discarded.

Thank you #belltown!!