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

查看:63
本文介绍了如何以编程方式将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:

从BluetoothAdapter开始-它是您的Bluetooth管理器.

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天全站免登陆