Giter VIP home page Giter VIP logo

sample-bluetooth-audio's Introduction

Bluetooth Audio

This sample demonstrates the use of Android Bluetooth APIs for audio from an Android Things app.

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

Introduction

This sample demonstrates how to enable an A2DP sink on your Android Things device and control lifecycle events, such as pairing, connection and playback so that other devices, like a phone, can connect and play audio in your Android Things device.

Screenshots

Bluetooth Audio sample demo

(Watch the demo on YouTube)

Pre-requisites

  • Android Things compatible board
  • Android Studio 2.2+
  • (optional) a speaker or headsets, so that you can listen to the audio and notifications.
  • (optional) Two buttons connected to the GPIO pins, so that you can control the sample at runtime. Without the buttons, you can use a keyboard or adb. For more on this, look at the main activity, where the supported commands are described.

Build and install

On Android Studio, click on the "Run" button.

If you prefer to run on the command line, type

./gradlew installDebug
adb shell am start com.example.androidthings.bluetooth.audio/.A2dpSinkActivity

Note: If you connect an audio source to an Android Things audio sink (eg this sample) but you can't hear your media playing through the audio jack, check if you have an HDMI display connected. If so, the audio will be routed to the HDMI output.

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.

sample-bluetooth-audio's People

Contributors

atn832 avatar daverim avatar mangini avatar proppy avatar qwivan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sample-bluetooth-audio's Issues

BluetoothProfileManager.getInstance(); NoSuchMethod

hi ,thanks for the demo ,recently I'm research a feature about how can use a a2dp sink on Android. so I download the androidthings and this demo project . But it's build smoothly while running crashed with "BluetoothProfileManager.getInstance(); NoSuchMethod", the androidthings is 0.6.1-devpreview android 8.1.0, a raspberry pi version. so I modify by Java reflection to get a BluetoothProfileManager Object, and it running well. then I use another android phone connect to it and play a song, the tone quality is very poor, of course I have try to remove the hdmi. I don't know why the tone quality is so poor, so I ask for a help, good wishes!

Want to create UI for this app

I don't have any issues with this project. It is working completely fine for me. I have some query in this. I
want to show all metadata from AVRCP to my activity.xml file. I want to display artist name, title track and all the other data to my user as I am using this in a car. Additionally, I also wanted to to control my player not only from my phone but from UI as well.
Thank You in advance
Please help!!!

Microphone support

From what I understand this sample allow you to transform an Android Things device into a BT speaker, would be nice to support microphone also to allow voice command, with this android things devices can be used as phone headset like in cars or deported voice assistant

Audio lags with Bluetooth A2DP

I tested the sample on my Android Things with an Android device paired and then played some audio which resulted playback in lag.

Not updated to support 0.5.1-preview

When trying to run on 0.5.1-preview I get the following exception:

com.example.androidthings.bluetooth.audio E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                         Process: com.example.androidthings.bluetooth.audio, PID: 1948
                                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidthings.bluetooth.audio/com.example.androidthings.bluetooth.audio.A2dpSinkActivity}: java.lang.SecurityException: Caller lacks required permission com.google.android.things.permission.MANAGE_INPUT_DRIVERS
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                                             at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                             at android.os.Looper.loop(Looper.java:164)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                                             at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                                          Caused by: java.lang.SecurityException: Caller lacks required permission com.google.android.things.permission.MANAGE_INPUT_DRIVERS
                                                                                             at android.os.Parcel.readException(Parcel.java:1942)
                                                                                             at android.os.Parcel.readException(Parcel.java:1888)
                                                                                             at com.google.android.things.userdriver.IInputDriverService$Stub$Proxy.createInputDevice(IInputDriverService.java:140)
                                                                                             at com.google.android.things.userdriver.InputDriver.initialize(InputDriver.java:241)
                                                                                             at com.google.android.things.userdriver.InputDriverManager.addInputDriver(InputDriverManager.java:47)
                                                                                             at com.google.android.things.userdriver.UserDriverManager.registerInputDriver(UserDriverManager.java:83)
                                                                                             at com.google.android.things.contrib.driver.button.ButtonInputDriver.register(ButtonInputDriver.java:83)
                                                                                             at com.example.androidthings.bluetooth.audio.A2dpSinkActivity.configureButton(A2dpSinkActivity.java:311)
                                                                                             at com.example.androidthings.bluetooth.audio.A2dpSinkActivity.initA2DPSink(A2dpSinkActivity.java:247)
                                                                                             at com.example.androidthings.bluetooth.audio.A2dpSinkActivity.onCreate(A2dpSinkActivity.java:173)

So it needs me to request the MANAGE_INPUT_DRIVERS permission. And if I do that:
ActivityCompat.requestPermissions(this, new String[]{"com.google.android.things.permission.MANAGE_INPUT_DRIVERS"}, 0);

Then the UI displays an "Allow ... to perform" dialog. But if my IoT device doesn't have screen and input then you can't click the allow button.

Audio source example

Will there be an example for audio source i.e. audio playback via Bluetooth speakers?

Can't connect to the Android Things device

Hello, I installed the exmaple on my Android Things device and i can start the application and see the device in my bluetooth list. Pairing is also no problem, but when i want to connect to the device, it disconnects after a few seconds.
I tried it with 3 devices (Android, Iphone and Macbook Pro). Do you know why this is happening?

Thanks in advance!

is it working on android?

I have some old phones with android. I want use it as bluetooth sink.
Can i use sample-bluetooth-audio with old phones to listen music via bluetooth?

_MS Suffix Used for Value in Seconds

A minor nitpick: DISCOVERABLE_TIMEOUT_MS suggests that the time unit is milliseconds. The actual value and its usage is for time measured in seconds, though.

Transmitting stops after few minutes

After start of play in few minutes the connection is broken with this log message:
beginning of crash 05-16 21:19:30.972 676-701/com.android.bluetooth A/libc: system/bt/hci/src/buffer_allocator.c:25: buffer_alloc: assertion "size <= BT_DEFAULT_BUFFER_SIZE" failed 05-16 21:19:30.973 676-701/com.android.bluetooth A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 701 (bluetooth wake) [ 05-16 21:19:30.973 135: 135 W/ ] debuggerd: handling request: pid=676 uid=1002 gid=1002 tid=701 05-16 21:19:31.072 3787-3787/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 05-16 21:19:31.073 3787-3787/? A/DEBUG: Build fingerprint: 'generic/iot_rpi3/rpi3:7.0/NIG86E/3884620:userdebug/test-keys' 05-16 21:19:31.073 3787-3787/? A/DEBUG: Revision: '0' 05-16 21:19:31.073 3787-3787/? A/DEBUG: ABI: 'arm' 05-16 21:19:31.073 3787-3787/? A/DEBUG: pid: 676, tid: 701, name: bluetooth wake >>> com.android.bluetooth <<< 05-16 21:19:31.073 3787-3787/? A/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr -------- 05-16 21:19:31.086 3787-3787/? A/DEBUG: Abort message: 'system/bt/hci/src/buffer_allocator.c:25: buffer_alloc: assertion "size <= BT_DEFAULT_BUFFER_SIZE" failed' 05-16 21:19:31.086 3787-3787/? A/DEBUG: r0 00000000 r1 000002bd r2 00000006 r3 00000008 05-16 21:19:31.086 3787-3787/? A/DEBUG: r4 9de7e978 r5 00000006 r6 9de7e920 r7 0000010c 05-16 21:19:31.086 3787-3787/? A/DEBUG: r8 9eb79e18 r9 9eb79e22 sl 9eb79dd8 fp 9eb79e1c 05-16 21:19:31.086 3787-3787/? A/DEBUG: ip 00000000 sp 9de7e3e0 lr b33d832f pc b33dab7c cpsr 20000010 05-16 21:19:31.116 3787-3787/? A/DEBUG: backtrace: 05-16 21:19:31.116 3787-3787/? A/DEBUG: #00 pc 00048b7c /system/lib/libc.so (tgkill+12) 05-16 21:19:31.116 3787-3787/? A/DEBUG: #01 pc 0004632b /system/lib/libc.so (pthread_kill+34) 05-16 21:19:31.116 3787-3787/? A/DEBUG: #02 pc 0001c6d5 /system/lib/libc.so (raise+10) 05-16 21:19:31.117 3787-3787/? A/DEBUG: #03 pc 00018237 /system/lib/libc.so (__libc_android_abort+34) 05-16 21:19:31.117 3787-3787/? A/DEBUG: #04 pc 00017770 /system/lib/libc.so (abort+4) 05-16 21:19:31.117 3787-3787/? A/DEBUG: #05 pc 0001a6ef /system/lib/libc.so (__libc_fatal+22) 05-16 21:19:31.117 3787-3787/? A/DEBUG: #06 pc 0001842f /system/lib/libc.so (__assert2+18) 05-16 21:19:31.117 3787-3787/? A/DEBUG: #07 pc 0007b97d /system/lib/hw/bluetooth.default.so 05-16 21:19:31.117 3787-3787/? A/DEBUG: #08 pc 0007c8e7 /system/lib/hw/bluetooth.default.so 05-16 21:19:31.117 3787-3787/? A/DEBUG: #09 pc 000e2ef1 /system/lib/hw/bluetooth.default.so 05-16 21:19:31.117 3787-3787/? A/DEBUG: #10 pc 000e3cab /system/lib/hw/bluetooth.default.so 05-16 21:19:31.117 3787-3787/? A/DEBUG: #11 pc 00045dfb /system/lib/libc.so (_ZL15__pthread_startPv+22) 05-16 21:19:31.118 3787-3787/? A/DEBUG: #12 pc 00018c8d /system/lib/libc.so (__start_thread+6)

Caused by: java.lang.RuntimeException: Stub!

Hello,

I got this error when application launch.

Caused by: java.lang.RuntimeException: Stub!
at com.google.android.things.bluetooth.BluetoothProfileManager.(BluetoothProfileManager.java:20)
at com.example.androidthings.bluetooth.audio.A2dpSinkActivity.setupBTProfiles(A2dpSinkActivity.java:224)
at com.example.androidthings.bluetooth.audio.A2dpSinkActivity.initA2DPSink(A2dpSinkActivity.java:246)
at com.example.androidthings.bluetooth.audio.A2dpSinkActivity.onCreate(A2dpSinkActivity.java:170)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6123) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 

FATAL EXCEPTION

Hi,
It has a problem with the example and not runing:

FATAL EXCEPTION: main
    Process: com.example.androidthings.bluetooth.audio, PID: 2555
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidthings.bluetooth.audio/com.example.androidthings.bluetooth.audio.A2dpSinkActivity}: java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10037 nor current process has android.permission.BLUETOOTH.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.