[英]Unable to Connect to Bluetooth device
This is the piece of code from Standard from Google sample code BluetoothChat. 这是来自Google示例代码BluetoothChat的Standard的代码片段。 BluetoothChatService.java
BluetoothChatService.java
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (mState == STATE_CONNECTED) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
As soon as I try to connect the device using 我尝试使用以下方式连接设备后
mChatService.connect(device, false);
I get following exception 我收到以下异常
java.io.IOException: bt socket closed, read return: -1
at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:872)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
at java.io.InputStream.read(InputStream.java:163)
Any help would be appreciated. 任何帮助,将不胜感激。
Did you include in android manifest Bluetooth permission for your app? 您是否在Android清单中包括了您应用的蓝牙许可?
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
I guess the problem is the mmSocket.connect()
我想问题是
mmSocket.connect()
Try to get the method createRfcommSocket
by reflect. 尝试通过反射获取方法
createRfcommSocket
。
Here is an example base on Google sample code BluetoothChat. 这是一个基于Google示例代码BluetoothChat的示例。
// Make a connection to the BluetoothSocket
try {
Thread.sleep(500);
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
OutputStream os = mmSocket.getOutputStream();
// Here: connect success
} catch (Exception e) {
Log.i(TAG, "Lost 1st try; SPP connect[2]");
try {
mmSocket.close();// close the current socket
mmSocket = null;
Thread.sleep(500L); // wait for a while
mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, 1); // The key method !
mmSocket.connect();
OutputStream os = mmSocket.getOutputStream();
// Here: connect success
synchronized (ESDevice.this) {
mSPPConnectThread = null;
}
connected(mmSocket, mmDevice); // begin connected thread
} catch (Exception e2) {
Log.e(TAG, "Lost 2nd try. Connect fail", e2);
connectionFailed();
return;
}
return;
}
It looks like socket was closed before you passed it in your mConnectedThread
's constructor. 在您将套接字传递给
mConnectedThread
的构造函数之前,套接字似乎已关闭。
There is one tricky thing in Google sample, in mConnectThread
when socket was actually connected we make mConnectThread null, and next code won't be executed: Google示例中有一件棘手的事情,在
mConnectThread
当实际连接套接字时,我们将mConnectThread设为null,并且下一个代码将不会执行:
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
that means that cancel()
method which closes the socket will be skipped. 这意味着关闭套接字的
cancel()
方法将被跳过。
Basically, make sure you don't call close()
method in ConnectThread
after your socket was connected. 基本上,请确保在连接套接字后,不要在
ConnectThread
调用close()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.