How to make Android phone as a bluetooth headset?

前端 未结 2 1373
轻奢々
轻奢々 2020-12-24 02:36

Yes, I know Android has already implemented the Bluetooth Headset Profile, but it is in Audio Gateway Role, not in HeadSet Role.

What I want to do is develop an appl

相关标签:
2条回答
  • 2020-12-24 03:10

    From the android side, I think the best solution is to open the connection to the service in your computer:

    URL url = new URL("http://192.186.0.1/path/to/service");
    URLConnection connection = url.openConnection();
    

    Get it as an OutputStream:

    OutputStream out = new BufferedStream(connection.getOutputStream());
    

    and then use a AudioRecord to send though the recorded data:

    public static final int DEFAULT_SAMPLE_RATE = 8000; 
    private static final int DEFAULT_BUFFER_SIZE = 4096; 
    private static final int CALLBACK_PERIOD = 4000;
    
    AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 
                DEFAULT_SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_DEFAULT, 
                AudioFormat.ENCODING_DEFAULT, DEFAULT_BUFFER_SIZE);
    recorder.setPositionNotificationPeriod(CALLBACK_PERIOD);
    
    int bytesRead = 0;
    ByteBuffer buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE);
    while ((bytesRead = recorder.read(buffer, DEFAULT_BUFFER_SIZE)) > 0) {
        out.write(buffer.array(), 0, bytesRead);
    }
    

    All this should be done on a separate thread of course to avoid crashing the app and a mechanism to handle when the recording stops or the connection is lost. Also, I'm pretty sure it should work over wifi although I am not sure if it will be the same with bluetooth (although most devices with BT have wifi now a days and you get more bandwidth)

    I haven't tested this code so I'm not 100% sure it will work.

    The next thing will be on the machine to transfer the audio into the desire app, but that's above my experience. I imagine you will have to do a virtual driver or something like that. Also will have to do the inverse mechanism for the audio sent from the desktop app into the phone (I'm rather interested on that part since would make a nice wireless headset for watching movies as well).

    Here are my 2 cents; I am eager to know if it works. ;)

    0 讨论(0)
  • 2020-12-24 03:15

    I don't know if i understood the real question or your main goal... but I had posted here how receive and send the sound from your headset via bluetooth in android.

    I hope to help you...

    0 讨论(0)
提交回复
热议问题