-->

在Android ICS不可能的蓝牙连接(Bluetooth connection on Andro

2019-06-25 13:47发布

即时书面方式,它发送字节代码从平板到μ-CONTROLER的应用程序。 一切对联想A1(Androi 2.3)和三星Galaxy Tab 7加上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版本上运行完美。

Answer 1:

好吧,我发现了什么问题。 我不知道,如果它仅仅是一个三星的问题或Android ICS的问题。

我试图通过(获得的Socket)连接到天线像往常一样:

clientSocket = device.createRfcommSocketToServiceRecord(MY_UUID);

那么它似乎不符合我的天线和平板设置工作了,所以我尝试:

clientSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

这并不工作。 疗法第一选项强制系统取消配对天线,后来又要求配对。



Answer 2:

实际创建一个不安全的插座是一样连接两个未配对设备。 这显然不是来处理它的最佳方式。

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

我也尝试过Android的错误追踪: 蓝牙RFCOMM服务器插槽不能正常连接到嵌入式设备上ICS 4.0.3 。

仍在等待回应...



Answer 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);
}

也许这有助于有人为,这项ISSU的。



Answer 4:

我相信我看到了同样的问题。 我使用谷歌从戏与我的股票Droid X的配对设备后,工作完美的属终端应用。 但现在我的银河S3使用相同的应用程序,同样的设备要求我每次都重新配对。

您的解决方案是一种解决方法的。 这似乎在Android的ICS改变了这种行为。 所以,真正的解决方法是为谷歌修复ICS允许属设备进行配对并连接不“重新配对。

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

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();


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



文章来源: Bluetooth connection on Android ICS not possible