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

查看:177
本文介绍了如何以编程与蓝牙连接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 - 这是你的蓝牙管理器

Start with the BluetoothAdapter - it is your Bluetooth manager.

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

如果 bluetoothAdapter 为空,则意味着在Android设备不支持蓝牙(它没有蓝牙无线电。虽然我认为它很少遇到这些设备... )

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

接下来,确保蓝牙是:

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

如果没有工作,我们开始询问用户来启用它的活动。

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

可以说,用户也能(我想你应该检查,如果他做到了,做在你的 onActivityResult 法) 我们可以查询配对设备:

Lets 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.getBoundedDevices();

然后遍历它们:的(BluetoothDevice类设备: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是包含您的应用程序的特殊ID的UUID对象。阅读关于它这里

现在,尝试连接(你正在试图连接到必须有一个相同的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();

连接()你的线程,直到建立连接,或者发生错误 - 一个exeception会在这种情况下抛出。所以,你应该叫连接在一个单独的线程。

connect() blocks your thread untill a connection is established, or an error occurs - an exeception will be thrown in this case. So you should call connect on a seperate 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 seperate 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天全站免登陆