Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anyone had success hooking up the demo with BlueTooth mic? #197

Open
dhamya opened this issue Feb 16, 2023 · 7 comments
Open

Anyone had success hooking up the demo with BlueTooth mic? #197

dhamya opened this issue Feb 16, 2023 · 7 comments

Comments

@dhamya
Copy link

dhamya commented Feb 16, 2023

Based on this link, it seems default mic on the android device will be used. Has anyone tried using BT mic connected to android device? Any ideas/pointers/forks?

@Repkap11
Copy link

I did this with reflection just before calling SpeechService.startListening()

@RequiresApi(api = Build.VERSION_CODES.M)
private static void setPreferredDeviceWithReflection(SpeechService service, AudioDeviceInfo audioDevice) {
    if (audioDevice == null) {
        Log.i(TAG, "No external mic requested");
        return;
    }
    try {
        Field recorderField = SpeechService.class.getDeclaredField("recorder");
        recorderField.setAccessible(true);
        AudioRecord recorder = (AudioRecord) recorderField.get(service);
        if (recorder == null) {
            Log.w(TAG, "Getting recorder with reflection failed");
            return;
        }
        boolean worked = recorder.setPreferredDevice(audioDevice);
        if (!worked) {
            Log.w(TAG, "Unable to request that the mic be used");
        }
    } catch (IllegalAccessException | NoSuchFieldException e) {
        e.printStackTrace();
    }
}

An AudioDeviceInfo comes from

AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS);

@dhamya
Copy link
Author

dhamya commented Feb 17, 2023

Thanks @Repkap11 - I'll try this out. Do you have a fuller "gist" available for above?

@Repkap11
Copy link

In my case I was using a USB mic. The code is from another project I work on and specifically targets a unique piece of hardware.
https://github.com/Six15-Technologies/ST1-Examples/blob/master/vosk-speech-recognition/src/main/java/com/six15/vosk_speech_recognition/HudSpeechRecognitionHelper.java

@dhamya
Copy link
Author

dhamya commented Feb 17, 2023

This is great, thanks a LOT @Repkap11

@albert0m
Copy link

albert0m commented Sep 8, 2023

thank you for the snippet! I tried connecting a generic bluetooth headset, it's recognized by the AudioManager and is set correctly as preferred device, but when I speak no words are received by the onPartialResult an onResult methods. As soon as I disable the bluetooth it works again. Any ideas?

@actor10
Copy link

actor10 commented Jan 16, 2024

From what I've read, Android restricts bluetooth mic usage to a few of their own apps.

@dhamya
Copy link
Author

dhamya commented Jan 16, 2024

fwiw: I ended up using a modified solution inspired by code @Repkap11 shared, seems to work,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants