关于蓝牙客户端连接出现run: read failed, socket might closed or timeout, read ret: -1的问题

在尝试了修改uuid,新建子线程得方法后均失败后,通过反射机制发现将端口号改为2时连接成功,分享给小伙伴 
try {
        // MY_UUID是应用程序的UUID,客户端代码使用相同的UUID
        //tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        //bluetoothSocket的端口号为2时蓝牙连接成功
        tmp = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mmDevice,2);
    } catch (Exception e) { }
        mmSocket = tmp;
    //mmSocket =(BluetoothSocket) mmDevice.getClass("createRfcommSocket", new Class[] {int.class}).invoke(mmDevice,1);
}

public void run() {
    new Thread() {
        public void run() {
            mBluetoothAdapter.cancelDiscovery();
            try {
                // 通过socket连接设备,阻塞运行直到成功或抛出异常时

                mmSocket.connect();
                Log.e("eeeeeeeeeee", "Bluetooth connection!");
            } catch (Exception connectException) {
                mHandler.sendMessage(mHandler.obtainMessage(Constant.MSG_ERROR, connectException));
                // 如果无法连接则关闭socket并退出
                try {
                    Log.e("wwwwwwwwwwwwww", "Couldn't establish Bluetooth connection!");
                    mmSocket.close();
                } catch (IOException closeException) {

                }
                return;
            }
        }
    }.start();
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值