如何以编程方式将 2 个 android 设备与蓝牙连接? [英] How to programmatically connect 2 android devices with bluetooth?

查看:19
本文介绍了如何以编程方式将 2 个 android 设备与蓝牙连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它应该自动通过蓝牙连接 2 个 Android 设备.假设它们已经配对.有可能实现吗?

I am developing an application which should connect 2 Android devices through Bluetooth automatically. Let's say they are already paired. Is it possible to achieve that?

推荐答案

当然有可能.我将根据文档制作一个简短的教程:

Of course it is possible. I'll make a short tutorial out of the documentation:

从蓝牙适配器开始 - 它是您的蓝牙管理器.

Start with the BluetoothAdapter - it is your Bluetooth manager.

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

如果bluetoothAdapter为null,说明这个Android设备不支持蓝牙(它没有蓝牙无线电.虽然我认为很少遇到这些设备......)

If bluetoothAdapter is null, it means that this Android device does not support Bluetooth (It has no Bluetooth radio. Though I think it's rare to encounter these devices...)

接下来,确保蓝牙已开启:

Next, make sure Bluetooth is on:

if (!bluetoothAdapter.isEnabled()) {
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, request_code_for_enabling_bt);
}

如果它没有打开,我们会启动要求用户启用它的活动.

If it's not on, we start the activity which asks the user to enable it.

假设用户确实启用了(我想您应该检查他是否启用了,在您的 onActivityResult 方法中进行).我们可以查询配对的设备:

Let's say the user did enable (I guess you should check if he did, do it in your onActivityResult method). We can query for the paired devices:

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

然后遍历它们:for(BluetoothDevice device : pairedDevices) 并找到您要连接的那个.

Then loop over them: for(BluetoothDevice device : pairedDevices) and find the one you want to connect to.

找到设备后,创建一个套接字来连接它:

Once you have found a device, create a socket to connect it:

BluetoothSocket socket = device.createRfcommSocketToServiceRecord(YOUR_UUID);

YOUR_UUID 是一个 UUID 对象,其中包含您应用的特殊 ID.在此处阅读相关信息.

YOUR_UUID is a UUID object containing a special ID of your app. Read about it here.

现在,尝试连接(您尝试连接的设备必须在侦听模式下创建具有相同 UUID 的套接字):

Now, attempt to connect (The device you are trying to connect to must have a socket created with the same UUID on listening mode):

socket.connect();

connect() 阻塞您的线程,直到建立连接或发生错误 - 在这种情况下将抛出异常.所以你应该在一个单独的线程上调用 connect.

connect() blocks your thread until a connection is established, or an error occurs - an exception will be thrown in this case. So you should call connect on a separate thread.

还有!您已连接到另一台设备.现在获取输入和输出流:

And there! You are connected to another device. Now get the input and output streams:

InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();

然后您就可以开始发送/接收数据了.请记住,这两个操作(发送和接收)都是阻塞,因此您应该从不同的线程调用它们.

and you can begin sending/receiving data. Keep in mind that both actions (sending and receiving) are blocking so you should call these from separate threads.

阅读有关此内容的更多信息,并了解如何在 蓝牙文档.

Read more about this, and find out how to create the server (Here we've created a client) in the Bluetooth documentation.

这篇关于如何以编程方式将 2 个 android 设备与蓝牙连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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