我试图将手机连接到嵌入在arduino中的蓝牙模块HC-05。但我明白了,它抛出了一个奇怪的例外。有人可以告诉我为什么我面临这个问题,如果可能的话,我该如何解决这个问题? 这是我的代码......
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn= (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice("98:D3:33:80:67:F2");
ConnectThread connectThread =new ConnectThread(bluetoothDevice);
connectThread.start();
}
});
}
class ConnectThread extends Thread{
BluetoothSocket mmBluetoothSocket;
BluetoothDevice mmBLuetoothDevice;
private final UUID myuuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
public ConnectThread(BluetoothDevice bluetoothDevice){
mmBLuetoothDevice = bluetoothDevice;
BluetoothSocket tmp = null;
try {
tmp = mmBLuetoothDevice.createInsecureRfcommSocketToServiceRecord(myuuid);
} catch (IOException e) {
e.printStackTrace();
}
mmBluetoothSocket = tmp;
}
@Override
public void run() {
try {
mmBluetoothSocket.connect(); //java.io.IOException: read failed, socket might closed or timeout,
} catch (IOException e) {
e.printStackTrace();
}
try {
mmBluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
return;
}
public void cancel() {
try {
mmBluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我正面临这个异常java.io.IOException: read failed, socket might closed or timeout.
我以前从未遇到过此异常。