• Stars
    star
    467
  • Rank 93,935 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Google Assistant API sample for Android Things

Google Assistant SDK for devices - Android Things

This sample shows how to call the Google Assistant Service from Android Things using gRPC. It records a spoken request from the connected microphones, sends it to the Google Assistant API and plays back the Assistant's spoken response on the connected speaker.

Note: The Android Things Console will be turned down for non-commercial use on January 5, 2022. For more details, see the FAQ page.

Pre-requisites

Run the sample

  1. Create or open a project in the Actions Console
  2. Follow the instructions to register a device model
  3. Download client_secret_XXXX.json
  4. Configure the OAuth consent screen for your project
  5. Install the google-oauthlib-tool in a Python 3 virtual environment:
python3 -m venv env
env/bin/python -m pip install --upgrade pip setuptools
env/bin/pip install --upgrade google-auth-oauthlib[tool]
source env/bin/activate
google-oauthlib-tool --client-secrets client_secret_XXXX.json \
                     --credentials app/src/main/res/raw/credentials.json \
                     --scope https://www.googleapis.com/auth/assistant-sdk-prototype \
                     --save
  • Make sure to set the Activity Controls for the Google Account using the application.
  • On the first install, grant the sample required permissions for audio and internet access:
./gradlew assembleDebug
adb install -g app/build/outputs/apk/debug/app-debug.apk
  • On Android Studio, click on the "Run" button or on the command line, type:
./gradlew installDebug
adb shell am start com.example.androidthings.assistant/.AssistantActivity
  • Try the assistant demo:

    • Press the button: recording starts.
    • Ask a question in the microphone. After your question is finished, recording will end.
    • The Google Assistant answer should playback on the speaker.

Audio Configuration

By default the sample routes audio to the I2S Voice Hat on Raspberry Pi 3 and default audio on other boards (on-board Line out or HDMI/USB if connected).

You can change those mappings by changing the USE_VOICEHAT_I2S_DAC constant or replacing the audio configuration in the onCreate method of AssistantActivity with one of the following:

// Force using on-board Line out:
audioInputDevice = findAudioDevice(AudioManager.GET_DEVICES_INPUTS, AudioDeviceInfo.TYPE_BUILTIN_MIC);
audioOutputDevice = findAudioDevice(AudioManager.GET_DEVICES_OUTPUTS, AudioDeviceInfo.TYPE_BUILTIN_SPEAKER);

// Force using USB:
audioInputDevice = findAudioDevice(AudioManager.GET_DEVICES_INPUTS, AudioDeviceInfo.TYPE_USB_DEVICE);
audioOutputDevice = findAudioDevice(AudioManager.GET_DEVICES_OUTPUTS, AudioDeviceInfo.TYPE_USB_DEVICE);

// Force using I2S:
audioInputDevice = findAudioDevice(AudioManager.GET_DEVICES_INPUTS, AudioDeviceInfo.TYPE_BUS);
audioOutputDevice = findAudioDevice(AudioManager.GET_DEVICES_OUTPUTS, AudioDeviceInfo.TYPE_BUS);

Device Actions

With Device Actions, you can control hardware connected to your device. In this sample, you can turn on and off the LED attached to your Android Things board.

Follow the guide here to learn how to register your device.

  • After you register your device model and id, replace the device model and instance PLACEHOLDER values in AssistantActivity:
private static final String DEVICE_MODEL_ID = "my-device-model-id";
private static final String DEVICE_INSTANCE_ID = "my-device-instance-id";
  • Handle a Device Actions response if you get one.
mEmbeddedAssistant = new EmbeddedAssistant.Builder()
    ...
    .setConversationCallback(new ConversationCallback() {
        ...
        @Override
        public void onDeviceAction(String intentName, JSONObject parameters) {
            // Check the type of command
            if (intentName.equals("action.devices.commands.OnOff")) {
                try {
                    boolean turnOn = parameters.getBoolean("on");
                    mLed.setValue(turnOn);
                } catch (JSONException e) {
                    Log.e(TAG, "Cannot get value of command", e);
                } catch (IOException e) {
                    Log.e(TAG, "Cannot set value of LED", e);
                }
            }
        }
    }
    ...
...

Try it:

  • "Turn on"
  • "Turn off"

The LED should change states based on your command.

Enable auto-launch behavior

This sample app is currently configured to launch only when deployed from your development machine. To enable the main activity to launch automatically on boot, add the following intent-filter to the app's manifest file:

<activity ...>

    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.HOME"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

</activity>

License

Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

More Repositories

1

sample-tensorflow-imageclassifier

Classify camera images locally using TensorFlow models
Java
623
star
2

contrib-drivers

Open source peripheral drivers
Java
559
star
3

doorbell

Cloud-based photo doorbell with companion app
Java
297
star
4

new-project-template

Java
263
star
5

weatherstation

Sensor-based peripheral sample using Android Things
Java
213
star
6

sample-bluetooth-le-gattserver

Build a Bluetooth GATT server with Android Things
Java
205
star
7

sample-simplepio

Basic Peripheral I/O examples with Android Things
Java
190
star
8

sample-bluetooth-audio

Bluetooth A2DP sample using Android Things
Java
161
star
9

drivers-samples

Peripheral driver samples
Java
153
star
10

sample-simpleui

Connect GPIO states with a graphical UI on Android Things
Java
120
star
11

sample-button

Basic input and output using a button and LED
Java
104
star
12

sample-usbenum

Discover connected devices using the Android USB Host APIs
Java
98
star
13

sample-videoRTC

Java
97
star
14

sample-uartloopback

Echo received characters over a UART with Android Things
Java
78
star
15

sensorhub-cloud-iot

Sensor-based data collection device publishing to Cloud IoT Core
Java
71
star
16

sample-nativepio

Basic Peripheral I/O examples in C/C++ with Android Things
C++
48
star
17

edison-candle

Production sample using Android Things
Java
43
star
18

photobooth

Cloud-connected talking photobooth using TensorFlow
Java
40
star
19

cloud-iot-core-androidthings

Java
34
star
20

robocar

Android Things robot car controlled by a companion app using local Nearby APIs
Java
32
star
21

sample-lowpan

Discover and connect to Thread mesh networks on Android Things
Java
27
star
22

native-libandroidthings

NDK library for exposing Peripheral I/O APIs in C/C++
C
26
star
23

sample-updatemanager

Configure and control OTA updates on Android Things
Java
17
star
24

experiment-expression-flower

Java
15
star
25

native-new-project-template

CMake
12
star
26

sample-pio-app

Kotlin
4
star
27

snippet-cookbook

Java
3
star
28

endtoend-base

Kotlin
1
star