Connection and Session
Android: Connection and Session
Scenario
Create a CXRLink, configure the session type (CUSTOMVIEW or CUSTOMAPP), register link callbacks, and call connect(token) to establish a coordinated session between the phone and the glasses-side service. The Sample initializes this in CustomViewTypeViewModel / CustomAppTypeViewModel, and stores the instance in CXRLSampleApplication.sharedCxrLink for reuse by the audio/photo/custom-command pages.
Prerequisites
- You have completed “02-Authentication” and obtained a non-empty
token.
Key types
com.rokid.cxr.link.CXRLinkcom.rokid.cxr.link.utils.CxrDefs.CXRSession/CxrDefs.CXRSessionTypecom.rokid.cxr.link.callbacks.ICXRLinkCbk
Session configuration
CustomView
cxrLink = CXRLink(context).apply {
configCXRSession(CxrDefs.CXRSession(CxrDefs.CXRSessionType.CUSTOMVIEW))
setCXRLinkCbk(connectCallback)
setCXRCustomViewCbk(customViewCallback)
}
(application as? CXRLSampleApplication)?.sharedCxrLink = cxrLink
cxrLink.connect(token)
CustomApp (CUSTOMAPP)
cxrLink = CXRLink(context).apply {
configCXRSession(
CxrDefs.CXRSession(
CxrDefs.CXRSessionType.CUSTOMAPP,
CONSTANT.APP_PACKAGE_NAME // glasses-side target package, e.g. com.rokid.cxrswithcxrl
)
)
setCXRLinkCbk(connectCallback)
}
(application as? CXRLSampleApplication)?.sharedCxrLink = cxrLink
cxrLink.connect(token)
For CUSTOMAPP, you also need to prepare an APK path for installation in your business flow (see “Glasses-side Custom App”).
Link callbacks (ICXRLinkCbk)
| Callback | Meaning (Sample usage) |
|---|---|
onCXRLConnected(Boolean) | CXR service connection state. In CustomView, when true and icon JSON is ready, the Sample calls customViewSetIcons. |
onGlassBtConnected(Boolean) | Glasses Bluetooth connection. When false, mark audio/photo as not ready. |
onGlassAiAssistStart/Stop() | Wake word activation. |
CustomAppTypeViewModel defines “connected successfully” as both CXR and Bluetooth being true.
connect return value
connect returns a Boolean indicating whether the connection request is successfully initiated. Final connectivity must be determined by callbacks.
Constraints
configCXRSessionmust be called beforeconnect.- Within the same process, reuse a single
CXRLinkinstance to avoid duplicatedconnectand inconsistent state (the Sample uses an Application field). - Switching session type typically requires creating a new
CXRLinkand re-configuring it (the Sample routes via different Activities). - Transport ready ≠ scene ready:
onCXRLConnectedandonGlassBtConnectedonly indicate transport readiness. Photo capture, audio, and custom commands still require the glasses-side Custom View opened or Custom App opened.
Sample reference paths
activities/customViewType/CustomViewTypeViewModel.ktactivities/customAppType/CustomAppTypeViewModel.ktCXRLSampleApplication.kt