【问题标题】:Bluetooth connection on Android ICS not possible无法在 Android ICS 上进行蓝牙连接
【发布时间】:2012-06-18 12:25:15
【问题描述】:

我正在编写一个将字节码从平板电脑发送到 µ 控制器的应用程序。在联想 A1 (Androi 2.3) 和三星 Galaxy Tab 7 Plus N (Android 3.2) 上一切正常。现在我遇到了新的三星 Galaxy Tab 2 (Android 4.0) 的问题。

我可以与蓝牙天线配对(它连接到 µ 控制器并通过串行协议进行通信)。当我启动应用程序时,我再次被要求输入密码并进行配对。输入配对密码后,我的主布局可见,但未建立连接。

eclipse中的LogCat告诉我:

06-19 16:00:20.656: V/BluetoothSocket.cpp(3189): availableNative
06-19 16:00:20.664: V/BluetoothSocket.cpp(3189): abortNative
06-19 16:00:20.664: V/BluetoothSocket.cpp(3189): ...asocket_abort(49) complete
06-19 16:00:20.664: I/ActivityManager(185): No longer want com.google.android.partnersetup (pid 3220): hidden #16
06-19 16:00:20.671: V/BluetoothSocket.cpp(3189): availableNative
06-19 16:00:20.671: V/BluetoothSocket.cpp(3189): destroyNative
06-19 16:00:20.671: V/BluetoothSocket.cpp(3189): ...asocket_destroy(49) complete
06-19 16:00:20.679: D/KeyguardViewMediator(185): setHidden false
06-19 16:00:20.679: W/System.err(3189): java.io.IOException: socket closed
06-19 16:00:20.679: W/System.err(3189):     at android.bluetooth.BluetoothSocket.available(BluetoothSocket.java:370)
06-19 16:00:20.679: W/System.err(3189):     at android.bluetooth.BluetoothInputStream.available(BluetoothInputStream.java:40)
06-19 16:00:20.679: W/System.err(3189):     at java.io.BufferedInputStream.available(BufferedInputStream.java:114)
06-19 16:00:20.687: W/System.err(3189):     at ebs.alphadidact.control.ReceiveThread.run(ReceiveThread.java:79)

更进一步的是LogCat接收一千次消息:

V/BluetoothSocket.cpp(3189): availableNative

所以当我在网上搜索时,我发现了一些有类似问题但没有解决方案的人。有人知道这个问题吗?

可能是天线和android 4.0的兼容性问题。我不认为错误出现在我的代码中,因为正如我所说,相同的代码完美地在较旧的 android 版本上运行。

【问题讨论】:

    标签: android bluetooth galaxy-tab


    【解决方案1】:

    好的,我发现了问题所在。我不确定这只是三星问题还是 Android ICS 问题。

    我尝试像往常一样使用(获取 Socket)连接到天线:

    clientSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    

    好吧,我的天线和平板电脑设置似乎无法解决问题,所以我尝试了:

    clientSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
    

    这确实有效。第一个选项强制系统取消天线配对,然后再次请求配对。

    【讨论】:

    • 哇,这对我有帮助!谢谢!
    【解决方案2】:

    实际上创建一个不安全的套接字与连接两个未配对的设备相同。 这显然不是最好的处理方式。

    我发现 Android 尝试修复设备,然后拒绝配对响应。在这种奇怪的行为之后,它将接受下一次连接尝试!

    我还尝试了 Android bugtracker:Bluetooth RFCOMM Server Socket no longer connects properly to embedded device on ICS 4.0.3

    仍在等待回复...

    【讨论】:

      【解决方案3】:

      感谢@fuentessifuentes 的回答,我写了这个方法,包括向后兼容性:

      private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
          if(Build.VERSION.SDK_INT >= 10){
              try {
                  final Method  m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
                  return (BluetoothSocket) m.invoke(device, SPP_UUID);
              } catch (Exception e) {
                  Log.e(TAG, "Could not create Insecure RFComm Connection",e);
              }
          }
          return  device.createRfcommSocketToServiceRecord(SPP_UUID);
      }
      

      也许它可以帮助somone,摆脱这个问题。

      【讨论】:

        【解决方案4】:

        我相信我遇到了同样的问题。我正在使用来自 Google Play 的 spp 终端应用程序,该应用程序在将设备与我的库存 droid x 配对后完美运行。但现在我的 Galaxy s3 使用相同的应用程序和相同的设备,每次都需要重新配对。

        您的解决方案是一种解决方法。看来android在ICS中改变了这种行为。因此,真正的解决方案是让 Google 修复 ICS 以允许 spp 设备在不“正在配对”的情况下配对和连接。

        但是,我确实看到了一些解决类似问题的代码:

        BluetoothSocket mSocket = null;
        mBluetoothAdapter.cancelDiscovery();
        Method method;
        try {
            method = mBluetoothDevice.getClass()
                .getMethod("createRfcommSocket", new Class[] { int.class});
            mSocket = (BluetoothSocket) method.invoke(mBluetoothDevice,1);
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        
        mSocket.connect();
        

        【讨论】:

        • 您可以编辑自己的答案以包含您想要的任何补充。只需点击上面的小“编辑”链接(答案的左下角)。
        【解决方案5】:

        使用此代码:

        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        mBluetoothAdapter.cancelDiscovery();
        
        Method m;
        try {
            m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class });
            btSocket = (BluetoothSocket) m.invoke(device, 1);
        } catch (SecurityException e1) {
            e1.printStackTrace();
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        

        并将以下内容添加到我们的应用清单中工作

        <uses-sdk android:minSdkVersion="13" android:targetSdkVersion="16"/>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-12
          • 2012-11-09
          • 1970-01-01
          • 2018-07-07
          • 2012-01-20
          • 1970-01-01
          • 2016-11-08
          • 2016-07-08
          相关资源
          最近更新 更多