如何让安卓手机成为蓝牙耳机? [英] How to make Android phone as a bluetooth headset?

查看:68
本文介绍了如何让安卓手机成为蓝牙耳机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道 Android 已经实现了蓝牙耳机配置文件,但它在音频网关角色中,而不是在耳机角色中.

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

我想做的是在 Android 手机上开发一个应用程序将用作蓝牙耳机,因此它可以通过以下方式连接到我的笔记本电脑蓝牙.当我试图给某人打电话时,我可以用我的电话给他打电话,而我的应用程序将通过蓝牙将语音转发到我的笔记本电脑,笔记本电脑上运行的另一个应用程序将获取语音数据并将它们转发到 Skype 或 GTalk 或其他一些 VoIP 程序.

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.

换句话说,如何在 Headset 中实现 Headset Profile在Android手机上的作用?提前致谢!

In other words, how can I implement the Headset Profile in Headset Role on Android phone? Thanks in advance!

推荐答案

从android方面来说,我认为最好的解决方案是在你的电脑中打开服务的连接:

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();

将其作为输出流获取:

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

然后使用 AudioRecord 发送记录的数据:

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);
}

当然,所有这些都应该在单独的线程上完成,以避免应用程序崩溃,并在录制停止或连接丢失时提供处理机制.另外,我很确定它应该可以在 wifi 上工作,尽管我不确定它是否与蓝牙相同(尽管大多数 BT 设备现在都有 wifi 并且你会获得更多带宽)

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)

我还没有测试过这段代码,所以我不能 100% 确定它会起作用.

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).

这是我的 2 美分;我很想知道它是否有效.;)

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

这篇关于如何让安卓手机成为蓝牙耳机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆