在Android上使用蓝牙的服务发现失败例外

我目前正在研究一个通过蓝牙连接到仪器的Android应用程序,需要编写string命令并接收string响应。 目前,我有通过Wi-Fi连接/读/写TCP / IP的工作,现在试图实现蓝牙。 但是我遇到了一些障碍。 我一直在寻找networking,试图find类似的例子,并没有任何运气。 我一直在使用Android开发人员资源的例子:蓝牙聊天作为我的主要参考点。

我目前的代码似乎工作..然后它会在连接点引发服务发现失败的exception。 我正在使用DeviceListActivity类来发现和select要连接的设备。 它返回一个活动结果,然后我的蓝牙类等待它来处理,然后连接到它。 下面的代码几乎与蓝牙聊天应用程序相同。

 public void onActivityResult(int requestCode, int resultCode, Intent data) { if(!m_BluetoothAdapter.isEnabled()) { m_BluetoothAdapter.enable(); } switch (requestCode) { case REQUEST_CONNECT_DEVICE: // When DeviceListActivity returns with a device to connect if (resultCode == Activity.RESULT_OK) { // Get the device MAC address String address = data.getExtras() .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); // Get the BLuetoothDevice object BluetoothDevice device = m_BluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device connect(device); } break; case REQUEST_ENABLE_BT: // When the request to enable Bluetooth returns if (resultCode == Activity.RESULT_OK) { // Bluetooth is now enabled, so set up a chat session } else { // User did not enable Bluetooth or an error occured Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_SHORT).show(); finish(); } } } 

这是我的连接function:

 private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private void connect(BluetoothDevice device) { m_Device = device; BluetoothSocket tmp = null; // Get a BluetoothSocket for a connection with the // given BluetoothDevice try { tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { } m_Socket = tmp; m_BluetoothAdapter.cancelDiscovery(); try { // This is a blocking call and will only return on a // successful connection or an exception m_Socket.connect(); } catch (IOException e) { try { m_Socket.close(); } catch (IOException e2) { } return; } } 

希望不pipe我做错了什么都很简单,但是恐怕从来没这么简单。 这是我第一次做任何蓝牙开发,也许我正在做一些明显的错误…但我不知道为什么我的服务发现失败的例外。

您可以在手机上随时手机配对/查找设备…它需要密码,但我不认为这是我的问题。

三天后,我得到了一些非常有帮助的职位感谢。

我必须replace:

 tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 

有:

 Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); tmp = (BluetoothSocket) m.invoke(device, 1); 

瞧,它的工作!

从API 15开始,您可以使用以下方法:

尝试使用BluetoothDevice类的getUuids ()方法的返回值replace您的UUID。 对我来说有效的是这样的:

 UUID uuid = bluetoothDevice.getUuids()[0].getUuid(); BluetoothSocket socket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid); 

原因是不同的设备支持不同的UUID,通过使用getUuids获取设备的UUID,您可以支持所有function和设备。

另一个有趣的新方法(从API 14开始支持)是这样的: BluetoothHealth.getConnectionState 。 还没有尝试过,但看起来很有希望…

这是一个来自匿名用户的build议编辑,试图回复接受的答案。

你之前和之后的代码之间的一个很大的区别是你传递的UUID。 我在这里find了我的答案: http : //developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createRfcommSocketToServiceRecord(java.util.UUID)

我必须replace:

 tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 

有:

 private static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); tmp = device.createRfcommSocketToServiceRecord(SPP_UUID); 

和瞧,它的作品! 原来的代码是一个点对点的Android应用程序。 连接到简单的串行蓝牙设备时使用应用程序UUID是没有意义的。 这就是为什么发现失败。

就像上面提到的那样,问题是你需要使用服务器正在等待的UUID。

如果要连接蓝牙设备(如耳机或鼠标),则需要检查设备正在监听的UUID。 你可以看到这样的UUID。

 UUID[] uuids = bluetoothDevice.getUuids(); 

如果你想知道这些UUID是什么意思,看看这个 。

这是一个真正的老问题,但我发现使用createInsecureRfcommSocketToServiceRecord()而不是createRfcommSocketToServiceRecord()与前面提到的createRfcommSocketToServiceRecord()一起为我做的伎俩

 UUID uuid = bluetoothDevice.getUuids()[0].getUuid(); BluetoothSocket socket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(uuid);