android 蓝牙 socket.cnnect() 异常

蓝牙设备系统可以连上,但是写的demo程序里两种连接方式都连接失败
1、btSocket = btDev
.createRfcommSocketToServiceRecord(uuid);
btSocket.connect();
2、 btSocket =(BluetoothSocket) btDev.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(btDev,1);
btSocket.connect();
两种连接方式都试过了都报
java.IOException:read failed,socket might closed or timeout.read ret:-1
有人说UUID更换可以解决 试着换了两个:
"00001124-0000-1000-8000-00805F9B34FB"
"00001125-0000-1000-8000-00805F9B34FB"
"00001101-0000-1000-8000-00805F9B34FB"未果
求大神帮忙

设备已经连接了蓝牙,无需程序处理,直接获取键值就可以了

不知道为什么, 但是如果多开一个线程的话就可以. 大家可以参考一下
http://developer.android.com/guide/topics/connectivity/bluetooth.html

楼上那个哥们发的连接里面说的很好,我也遇到了这个问题,估计大家犯的错误差不多(connect连接操作都是放在Activity中执行的!不要那样做。另外开一个线程去调用connect方法,成功率会高很多。),API文档里面有说,当手机作为客户端去发起连接时候,connect();是一个很耗时间的操作,所以放Activity里面有很大几率会连接超时或者阻塞。刚才看了api里面规范的示例代码。写的很规范,改了之后一下就成功了。没事还是得多看看开发文档,基础不大行啊。希望能碰到有相同问题的同学。

try {
socket.connect();
Log.e("","Connected");
} catch (IOException e) {
Log.e("",e.getMessage());
try {
Log.e("","trying fallback...");

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

                Log.e("","Connected");
            }
         catch (Exception e2) {
             Log.e("", "Couldn't establish Bluetooth connection!");
          }
        }
                    这个能够解决

if (btdevaddr != "?")
{
BluetoothDevice device = btAdapter.getRemoteDevice(btdevaddr);

        UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // bluetooth serial port service
        //UUID SERIAL_UUID = device.getUuids()[0].getUuid(); //if you don't know the UUID of the bluetooth device service, you can get it like this from android cache

        BluetoothSocket socket = null;

        try {
            socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
        } catch (Exception e) {Log.e("","Error creating socket");}

        try {
            socket.connect();
            Log.e("","Connected");
        } catch (IOException e) {
            Log.e("",e.getMessage());
            try {
                Log.e("","trying fallback...");

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

                Log.e("","Connected");
            }
         catch (Exception e2) {
             Log.e("", "Couldn't establish Bluetooth connection!");
          }
        }
    }
    else
    {
        Log.e("","BT device not selected");
    }
}
    根据自己的代码将socket的创建还有connect部分的代码替换掉
    我是这样解决的,如果不行可以换一下uuid为你要连接的设备的唯一的uuid(自己百度怎么获取设备的uuid)
    如果还不行,参考下面的网址,多试试(国外网站,硬着头皮看,四级过了差不多都能看懂)
    https://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3