Android 如何让安卓手机成为蓝牙耳机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3163453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Android phone as a bluetooth headset?
提问by ET Worker
Yes, I know Android has already implemented the Bluetooth Headset Profile, but it is in Audio Gateway Role, not in HeadSet Role.
是的,我知道 Android 已经实现了蓝牙耳机配置文件,但它是在音频网关角色中,而不是在耳机角色中。
What I want to do is develop an application on Android phone which will act as a bluetooth headset, so it can connect to my laptop by bluetooth. When I try to call somebody, I can use my phone to dial him, and my application will forward the voice through bluetooth to my laptop, and one other application running on laptop will get the voice data and forward them to Skype or GTalk or some VoIP program else.
我想要做的是在 Android 手机上开发一个应用程序,它将充当蓝牙耳机,因此它可以通过蓝牙连接到我的笔记本电脑。当我尝试打电话给某人时,我可以用我的手机给他打电话,我的应用程序将通过蓝牙将语音转发到我的笔记本电脑,笔记本电脑上运行的另一个应用程序将获取语音数据并将它们转发到 Skype 或 GTalk 或其他VoIP 程序其他。
In other words, how can I implement the Headset Profile in Headset Role on Android phone? Thanks in advance!
换句话说,如何在 Android 手机上的耳机角色中实现耳机配置文件?提前致谢!
回答by pablisco
From the android side, I think the best solution is to open the connection to the service in your computer:
从android方面,我认为最好的解决方案是在您的计算机中打开与服务的连接:
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:
然后使用 aAudioRecord
发送记录的数据:
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)
当然,所有这些都应该在一个单独的线程上完成,以避免应用程序崩溃,以及在录制停止或连接丢失时进行处理的机制。另外,我很确定它应该在 wifi 上工作,尽管我不确定它是否与蓝牙相同(尽管大多数 BT 设备现在都有 wifi 并且你会获得更多带宽)
I haven't tested this code so I'm not 100% sure it will work.
我还没有测试过这段代码,所以我不能 100% 确定它会起作用。
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. ;)
这是我的 2 美分;我很想知道它是否有效。;)