question

pure-developer avatar image
pure-developer asked

Any android based AVS sample code

Hi, We are building a new voice based android mobile application with amazon alexa voice services. Our application is like a talent search application based on the candidate knowledge and location. We want to use the NPM with Alexa Service Kit(ASK) and Amazon Lambda, that is the reason we are using AVS. This service is very new to the market and I am not able to find any sample application over internet. Do you have any sample application to work with or if you can pass me the sample code of any of your own application if possible. That will be very helpful in building this application. I am only looking for android based solution for that for time-being, but if you have any ios solution also, please mail me at jain.peeyush@gmail.com Thanks, pure_developer
alexa voice service
10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Eric@Amazon avatar image
Eric@Amazon answered
Unfortunately, we do not currently have sample code showing how to use AVS purely within an application. However, these resources might be helpful. https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/docs/authorizing-your-alexa-enabled-product-from-an-android-or-ios-mobile-app Make sure that when you register with the Alexa Voice service in the Alexa developer portal choose Application. https://forums.developer.amazon.com/forums/thread.jspa?messageID=21699 If you have any issues, feel free to ask questions in the forum! -Eric
10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Eric@Amazon avatar image
Eric@Amazon answered
Hey pure_developer, Are you having any difficulties making an AVS app? I wanted to expand on my previous answer to hopefully make it easier for you to implement. I'd start with the reference app - https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/docs/reference-implementation-guide That has the majority of the code you'll need to actually talk to AVS. In particular, look at AVSClient.java and AVSController.java. When you build an Android app, you'll have to authenticate with the Login With Amazon SDK, as described here https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/docs/authorizing-your-alexa-enabled-product-from-an-android-or-ios-mobile-app However, in your case the 'device' is the Android app itself. So, you'll want to call AmazonAuthorizationManager#authorize, followed by a call to AmazonAuthorizationManager#getToken. In both cases, the scope you'll want to authorize/getToken with is "alexa:all". Once you've received the access token from getToken, you can use it to make API calls to AVS (again, see AVSClient.java and AVSController.java above) To record audio from the phone's microphone, investigate AudioRecord. You'll want AudioSource.MIC, 16000 Hz, mono channel, and PCM 16 bit encoding. -Eric
10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Eric@Amazon avatar image
Eric@Amazon answered
Sorry, one minor thing to mention about using the LWA instructions. In order to get the access token, do NOT include the following lines when you call #authorize options.putBoolean(AuthzConstants.BUNDLE_KEY.GET_AUTH_CODE.val, true); options.putString(AuthzConstants.BUNDLE_KEY.CODE_CHALLENGE.val, CODE_CHALLENGE); options.putString(AuthzConstants.BUNDLE_KEY.CODE_CHALLENGE_METHOD.val, "S256"); That will tell LWA that you want an auth code, not an access token. To make an AVS app, you want the access token.
10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

luis sergio lugo victorino avatar image
luis sergio lugo victorino answered
Hi Eric, I'm trying to implement the Login with amazon library on android with the changes that allows to use Alexa Voice Service, but When I changed the scope to "alexa:all" I'm getting and invalid scope error, and as i can saw on the documentation the only valid scopes are "profile","postal_code", can you help me with this? Regards
10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

elstryan@Amazon avatar image
elstryan@Amazon answered
Hi, One of the reasons for the “invalid_scope_data” error is a mismatch in security profile and the device. Can you please try the following steps and see if it resolves the issue? 1. Please visit https://developer.amazon.com/edw/home.html and login using your developer account user ID and password. 2. Please click on “Get Started” under Alexa Voice Service. You will see your device listed in the table. 3. Please click on “Edit” button for your device entry. 5. On the left, please click on “Security Profile” and then click on “Android/Kindle Settings” (in the center of the page). Your will see your Android “Key” here. 6. Using this “Key” in your Android app should help you fix the “invalid_scope_data” error that you are seeing. Also please make sure that you are crafting your scope_data correctly. String scope_data = "{\"alexa:all\":{\"productID\":\"" + PRODUCT_ID +"\", \"productInstanceAttributes\":{\"deviceSerialNumber\":\"" + PRODUCT_DSN + "\"}}}";
10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

luis sergio lugo victorino avatar image
luis sergio lugo victorino answered
Hi Elstryan now I'm getting an access token, but when I used it to perform a request to https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize it is returning the following error: { "error": { "code": "com.amazon.alexahttpproxy.exceptions.InvalidAccessTokenException", "message": "AVS was not able to validate the provided access token." } } 403 Forbidden The request was a legal request, but the server is refusing to respond to it. Unlike a 401 Unauthorized response, authenticating will make no difference. I really appreciate your help, I'm gonna create a post on my blog once this works. Regards
10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Eric@Amazon avatar image
Eric@Amazon answered
Hi Luis, There's a chance you might not be setting the header correctly. Using the documentation at the link here, can you go through and ensure that the request matches exactly? https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/rest/speechrecognizer-recognize-request#HTTP%20Request For example, if your access token is ABCXYZ, the beginning of your request should look like this: POST /v1/avs/speechrecognizer/recognize HTTP/1.1 Host: access-alexa-na.amazon.com Authorization: Bearer ABCXYZ Content-Type: multipart/form-data; boundary=boundary_term Transfer-Encoding: chunked ... ... In particular, make sure that the Authorization line matches exactly. "Bearer" should not have a semicolon after it. If that doesn't help you resolve the problem, could you reply with the request you are making? -Eric
10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

luis sergio lugo victorino avatar image
luis sergio lugo victorino answered
Thanks Eric, I could use the Alexa service on an android example app, now I'm gonna start to create my own Alexa's skills Regards
1 comment
10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hello, my friend, can you show me the android example app? i am doing now, and confusing this, thanks a lot, if you see my message, you can send to my email xiaomai2257@gmail.com...

0 Likes 0 ·
Matteo Fregiari avatar image
Matteo Fregiari answered
Hi Luis,can you give me your sample android project? I'm trying to do the same thing and it can help me! Thanks very much Matteo
10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

newuser-c1761fe4-008f-42c2-8cb7-b98c317f0e68 avatar image
newuser-c1761fe4-008f-42c2-8cb7-b98c317f0e68 answered

Hi. Eric.

I have further questions about AVS sample code, you said

"you can use it to make API calls to AVS (again, see AVSClient.java and AVSController.java above) To record audio from the phone's microphone, investigate AudioRecord. You'll want AudioSource.MIC, 16000 Hz, mono channel, and PCM 16 bit encoding. -Eric"

Does it mean i have to implement PCM part, right? because AVS sample code have PCM recording/playing part. but it seems does not work at android device. please clarify

10 |3000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.