6

我正在编写一个将字节码从平板电脑发送到 µ 控制器的应用程序。在联想 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 版本上运行。

4

5 回答 5

4

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

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

clientSocket = device.createRfcommSocketToServiceRecord(MY_UUID);

好吧,它似乎不适用于我的天线和平板电脑设置,所以我尝试了:

clientSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

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

于 2012-06-21T09:35:55.203 回答
2

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

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

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

还在等回复。。。

于 2012-07-03T14:48:37.600 回答
2

感谢@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,摆脱这个问题。

于 2012-12-28T13:25:10.187 回答
1

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

您的解决方案是一种解决方法。看来android在ICS中改变了这种行为。因此,真正的解决方案是谷歌修复 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();
于 2012-07-21T13:18:24.720 回答
0

使用此代码:

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"/>

于 2012-08-07T14:55:31.033 回答