Android的蓝牙接收()/连接()与已配对设备 [英] Android Bluetooth accept() / connect() with already paired devices

查看:340
本文介绍了Android的蓝牙接收()/连接()与已配对设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过蓝牙,只有它发生在他们之前已经配对连接两个Android设备的麻烦。我正在运行一个作为服务器而另一个为客户端。

I am having trouble connecting two Android devices via Bluetooth, which happens only when they have been paired before. I am running one as the server and the other as the client.

下面是一个东西在服务器端的顺序:

Here is the sequence of things on the server side:

  1. 检查各种蓝牙状态(适配器可用,启用,等等)。
  2. 在调用listenUsingRfcommWithServiceRecord()与我选择了pre定义的UUID。
  3. 的要求,使设备可发现
  4. 自从被发现异步发生,我调用accept(),并等待进入连接。

在客户端:

  1. 检查各种蓝牙状态(适配器可用,启用,等等)。
  2. 在getBondedDevices每个设备(),我比较的getName()与服务器的名称。如果有比赛,跳到第6步。
  3. 在开始BT发现
  4. 对于每个发现的设备(注意,配对设备从2A不会出现在这里),比较与服务器名称的设备名称。如果有匹配,则转到步骤6。
  5. 取消发现
  6. 在设备被发现从第2步,调用createRfcommSocketToServiceRecord()与用于在服务器端相同的pre定义的UUID。
  7. 在调用connect(),并等待它返回一个连接的插座。

以上处理工作完全正常,我在客户端和服务器以前从未配对。然而,在Android的注册他们的设备列表中,也免不了在超时的connect()/接受()阶段。

The above process works perfectly fine for me when the client and the server have never been paired before. However, after Android registered them in the device list, they will inevitably timeout at the connect()/accept() stage.

我一直在寻找了几天的解决方案现在已经尝试了不少事情,包括这一个: <一href="http://stackoverflow.com/questions/6760102/connecting-to-a-already-paired-bluetooth-device">Connecting到已配对的蓝牙设备

I have been searching for a solution for a couple days now and have tried quite a few things including this one: Connecting to a already paired Bluetooth device

反射方法不适用于我的任何工作。看来,连接()将立即返回,但是当我尝试的getOutputStream(),我得到一个异常。在接受()方面,它甚至没有注册,有人试图连接。我认真地需要一些帮助或指针上获得设备建立,一旦他们之前已经配对连接。

The reflection method does not work for me either. It seems that connect() would return immediately but when I tried to getOutputStream() I get an exception. On the accept() side it does not even register that someone tried to connect. I seriously need some help or pointer on getting the devices to establish a connection once they have been paired before.

下面是有关设备的一些信息:

Here is some info about the devices:

  • 在我测试的两个LG G2X电话服务器和客户端。
  • 他们都在Android 2.3.3,这相当于API级别10上运行。
  • 同样,在我上述工程手动设置解除配对的设备。

感谢您的时间提前。我大约2周龄的Andr​​oid和蓝牙功能,所以如果你看到任何丢失的步骤或最佳实践,请提出来为好。

Thank you ahead of time. I am about 2-week-old in Android and Bluetooth, so if you see any missing steps or best practices, please point them out as well.

推荐答案

在我试图对绑定的设备连接的客户端,我只是把它称为对BluetoothDevice类,我发现在 BluetoothAdapter。 getBondedDevices()。这是行不通的。

In the client when I attempt to make a connection to a bonded device, I simply called it on the BluetoothDevice I found in BluetoothAdapter.getBondedDevices(). This does NOT work.

为了正确地建立了蓝牙连接,我不得不做类似下面的伪code的内容:

In order to properly establish the Bluetooth connection, I had to do something similar to the pseudocode below:

BluetoothDevice bonded = a device from BluetoothAdapter.getBondedDevices();
BluetoothDevice actual = BluetoothAdapter.getRemoteDevice(bonded.getAddress());

BluetoothSocket socket = actual.createRfcommSocketToServiceRecord(SOME_UUID);
socket.connect();

我来到这个答案通过蓝牙聊天的例子如下正是:<一href="http://developer.android.com/resources/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.html"相对=nofollow>蓝牙聊天服务。为什么它没有在设备上,从 getBondedDevices工作()我是无法理解。也许有人与Android的更深入的了解可以回答这个问题。

I arrived this answer by following exactly the Bluetooth chat example: Bluetooth Chat Service. Why it doesn't work on the device from getBondedDevices() is beyond me. Maybe someone with more intimate knowledge of Android can answer that.

这篇关于Android的蓝牙接收()/连接()与已配对设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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