嗨我想创建一个Android应用程序,它将连接到我想要发送数据的蓝色SMiRF蓝牙适配器。我已经阅读了开发者页面并查看了多个不同的示例,但是我目前在创建与套接字的连接时遇到了问题。代码的蓝牙部分来自我能找到的一个例子。当试图连接到蓝牙适配器时,应用程序会关闭某个部队,因为有一些错误我没有正确处理。不过,我也尝试使用该应用程序只是为了连接到另一台电脑,并且由于某种原因连接无法正确建立,即使我已经通过蓝牙设置与设备配对,甚至在我运行该应用程序之前。我已经发布了一些更重要的代码,以供我认为我的问题可能在哪里。任何帮助将非常感激,请让我知道,如果我应该发布任何额外的代码。连接到Android蓝牙插座
protected void connect(BluetoothDevice device) {
//BluetoothSocket socket = null;
try {
//Create a Socket connection: need the server's UUID number of registered
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));
socket.connect();
Log.d("EF-BTBee", ">>Client connectted");
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
outputStream.write(new byte[] { (byte) 0xa0, 0, 7, 16, 0, 4, 0 });
new Thread() {
public void run() {
while(true)
{
try {
Log.d("EF-BTBee", ">>Send data thread!");
OutputStream outputStream = socket.getOutputStream();
outputStream.write(new byte[] { (byte) 0xa2, 0, 7, 16, 0, 4, 0 });
} catch (IOException e) {
Log.e("EF-BTBee", "", e);
}
}
};
}.start();
} catch (IOException e) {
Log.e("EF-BTBee", "", e);
} finally {
if (socket != null) {
try {
Log.d("EF-BTBee", ">>Client Close");
socket.close();
finish();
return ;
} catch (IOException e) {
Log.e("EF-BTBee", "", e);
}
}
}
}`
我一直在使用
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
,而不是仅仅“套接字=”从上面还行没有成功也试过。
感谢您的评论。不幸的是,它仍然没有正确创建蓝牙套接字连接。 – user2744785
重读它(最初必须读错),但是能够正确创建蓝牙套接字,但它实际上还存在连接问题。 – user2744785