且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何让安卓手机成为蓝牙耳机?

更新时间:2023-02-26 22:02:00

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