Alexa
Functional Overview
It can only be used by devices that support Alexa. Before use, make sure that the corresponding ClientID and ProductID are configured in the Alexa official backend.
Attributes
isLogin
Are you logged in?
method
onLoginStateChanged(handle:)
Monitor login status changes
setupAlexa(delegate:clientId:)
configure alexa
- Parameters:
- delegate : proxy
- clientId: ID generated by Alexa background
authorizeRequest(productId:handle:completion:)
Alexa CBL Authorization
- Parameters:
- productId: product ID registered in Alexa backend
- handle: callback verificationUri and pairCode that need to be opened for Alexa authentication
- completion: authorization result
- Returns: Cancellable instance
logout()
sign out
Example
Configure Alexa module
Swift:
// Configure Alexa
sdk.alexa.setupAlexa(delegate: <IDOAlexaDelegate>, clientId: clientId)
// implement proxy
extension SomeClass: IDOAlexaDelegate {
/// Get health data
func getHealthValue(valueType: IDOGetValueType) -> Int {
return 0
}
/// Get heart rate
func getHrValue(dataType: Int, timeType: Int) -> Int {
return 0
}
/// Function control
///
/// funType 0 turns off the phone search function
func functionControl(funType: Int) {}
}
Kotlin:
// Configure Alexa
alexa.setupAlexa(this, "clientId from alexa developer account"/*only for test*/)
//implement proxy
private val delegate = object:IDOAlexaDelegate{
/// Get health data
override fun getHealthValue(valueType: IDOGetValueType): Int {
return 0
}
/// Get heart rate
override fun getHrValue(dataType: Int, timeType: Int): Int {
return 0
}
// Function control
//
// funType 0 turns off the phone search function
override fun functionControl(funType: Int) {
TODO("Not yet implemented")
}
}
Monitor login status
Swift:
// Listen for login
sdk.alexa.onLoginStateChanged { [weak self] state in
switch state {
case .logging:
// logging in...
case .logined:
// Has logged
case .logout:
// Not logged in
}
}
Kotlin:
// Listen for login
alexa.onLoginStateChanged {
when(it){
IDOAlexaLoginState.LOGGING -> {
// 登录中...
}
IDOAlexaLoginState.LOGINED -> {
// 已登录
}
IDOAlexaLoginState.LOGOUT -> {
// 未登录
}
}
}
Login (based on CBL)
Swift:
// Log in
let canceler = sdk.alexa.authorizeRequest(productId: "ProductId") { [weak self] verificationUri, pairCode in
// pairCode Your verification code, which needs to be displayed to the user during the user login process and used for verification
// verificationUri needs to jump to the login page (html)
} completion: { [weak self] rs in
if case .successful = rs {
// login successful
} else {
// Login failed
}
}
//Cancel login midway
canceler.cancel()
Kotlin:
// Log in
val canceler = alexa.authorizeRequest("ProductId", { verificationUri, pairCode ->
// pairCode Your verification code, which needs to be displayed to the user during the user login process and used for verification
// verificationUri needs to jump to the login page (html)
//for example
val dialog = AlertDialog.Builder(this@AlexaActivity).setTitle(getString(R.string.alexa_dialog_title))
.setMessage(getString(R.string.alexa_dialog_msg).format(pairCode))
.setPositiveButton(R.string.alexa_dialog_copy) { dialog, which ->
val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val mClipData = ClipData.newPlainText("Label", pairCode)
cm.setPrimaryClip(mClipData)
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(verificationUri)
startActivity(intent)
dialog.dismiss()
}.setNegativeButton(R.string.alexa_dialog_cancel, { dialog, which -> dialog.dismiss() }).show()
}, { rs ->
if (rs == IDOAlexaAuthorizeResult.SUCCESSFUL) {
// login successful
} else {
// Login failed
}
})
//Cancel login midway
canceler.cancel()
quit
Swift:
// sign out
sdk.alexa.logout()
Kotlin:
// sign out
sdk.alexa.logout()