【问题标题】:Android bluetooth socket.connect() failsAndroid 蓝牙 socket.connect() 失败
【发布时间】:2014-08-25 18:15:58
【问题描述】:

我正在尝试在 android 设备和 RFID 阅读器蓝牙之间建立连接。为此,我使用蓝牙聊天代码 (bluetooth chat example)。但是当我在蓝牙聊天示例的第 329 行执行mmSocket.connect(); 时,每次都会生成一个java.io.IOException 连接。我也尝试了这种方法来获取套接字:

tmp = mDevice.createRfcommSocketToServiceRecord(MY_UUID);
Method m = mDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(mDevice, 1);

但什么都没有。我尝试了 3 种不同的设备。首先,运行 android 4.4.2 的三星 S2 给我这个错误:

failed:read failed, socket might closed, read ret: -1

使用运行 android 4.0.3 的平板电脑给我这个错误:

IOException: Connection refused

好奇的是,如果我尝试将手机与平板电脑连接起来,我会失败。但是,如果我在 2 个设备上运行此应用程序,并且我尝试连接第二个设备,而第二个设备正在搜索要连接的设备,则连接成功。但前提是第二台设备正在运行此应用程序并搜索要连接的一些设备。我也尝试取消配对但没有。最后我想说,如果我尝试通过我的 rfid 蓝牙阅读器设置连接 2 台设备或一台设备,则连接成功。最后我想说的是,当我尝试将 2 个设备或设备与读卡器 rfid 连接时,如果设备未配对,请比较一个对话框,要求我配对 2 个设备,但此后连接失败.

【问题讨论】:

    标签: android sockets bluetooth connection


    【解决方案1】:

    我也遇到了这个问题,我尝试了这篇文章中所说的所有内容,但没有任何效果。我终于修好了。 我的错误代码使用了类变量 BluetoothAdapter 来检查蓝牙是否打开,如下所示:

    public class PrintClass extends Activity  {
    private XXXX myClassVar;
    private BluetoothAdapter bluetooth; // created and checked in other method
    
    ... other methods in my class....
    

    }

    我一直 bt 套接字连接失败。但是我用私有方法激活蓝牙,socket问题就消失了。

        public class PrintClass extends Activity  {
    
    private XXXX myClassVar;
    
    private void turnBlueToothOn(){
    
         try {
            BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
            if(!bluetooth.isEnabled()){
                bluetooth.enable();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    ....other methods in my class....
    }
    

    为什么?我不知道,只是为我工作!!!!

    【讨论】:

    • 谢谢,我不知道为什么,但重新启用蓝牙,解决了这个问题
    【解决方案2】:

    有时客户端蓝牙到服务器蓝牙的命令连接不起作用。可能是驱动程序错误或蓝牙模块错误,或者库中的错误在 Java 中实现蓝牙。对我来说只是帮助关闭然后打开蓝牙,然后连接开始正常工作。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。这很容易解决。您必须使用他的配置程序将您的 RFID 阅读器配置为 SPP 协议,然后他才能工作。

      【讨论】:

      • 你能澄清一下解决方案吗?
      • 我们如何配置,请问我有同样的问题
      【解决方案4】:

      您的代码假定安全的 BT 连接,并以两种不同的方式进行尝试(这很好)。您需要尝试的第一个测试是使用以下方法不安全的 BT 连接

      BluetoothSocket sock;
      
      // open API
      sock = device.createInsecureRfcommSocketToServiceRecord(serviceUuid);
      
      // if failed: hidden API
      createMethod = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });
      sock = (BluetoothSocket)createMethod.invoke(device, 1);
      


      如果这不能解决问题,你应该探测。转移到检查正确性 您传递给创建方法的 uuid。您可能(?)使用 默认 SPP uuid:

      UUID DEFAULT_SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
      

      这适用于许多设备,但并非所有设备。


      尝试询问你的同伴以获取它支持的 uuid 列表

      // for apiVer >= 15
      supportedUuids = device.getUuids();
      
      
      // for apiVer < 15
      try {
         Class cl = Class.forName("android.bluetooth.BluetoothDevice");
         Class[] params = {};
         Method method = cl.getMethod("getUuids", params);
         Object[] args = {};
         supportedUuids = (ParcelUuid[])method.invoke(device, args);
      }
      catch (Exception e) {
          // no op
          Log.e("uuids", "Activation of getUuids() via reflection failed: " + e);
      }
      

      如果不为空,则使用数组中的第一个作为创建方法的参数。

      您也可以使用真正由您编写的BTWiz,它以透明的直通模型为您完成所有这些工作,并且还支持异步蓝牙 IO。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-16
        • 1970-01-01
        • 1970-01-01
        • 2016-12-13
        • 2014-07-12
        • 2012-08-18
        相关资源
        最近更新 更多