蓝牙聊天API 10无法工作

3

我正在尝试使用蓝牙聊天示例来进行API-10的开发,在我的Micromax手机上。

当我扫描设备时,它会显示安全和非安全列表。 但是当我尝试连接时,它显示无法连接。

UUID是:

private static final UUID MY_UUID_SECURE = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID_INSECURE =UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");

我尝试过这篇文章:Service discovery failed exception using Bluetooth on Android,但是没有改变。

有什么想法吗?

这是我的连接到配对的蓝牙设备的代码。我从Android API-10示例中获取了此代码:

        try {
            if (secure) {
                tmp = device.createRfcommSocketToServiceRecord(
                        MY_UUID_SECURE);
            } else {
                tmp = device.createInsecureRfcommSocketToServiceRecord(
                        MY_UUID_INSECURE);
            }
        } catch (IOException e) {
            Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
        }
        mmSocket = tmp;
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
        setName("ConnectThread" + mSocketType);

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            mmSocket.connect();
        } catch (IOException e) {
            // Close the socket
            try {
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(TAG, "unable to close() " + mSocketType +
                        " socket during connection failure", e2);
            }
            connectionFailed();
            return;
        }

嗨@Haris 你有什么解决方案吗?我也卡在同一个地方。它可以在三星Galaxy Y上运行,但无法在Micromax A52上运行。 - Aswin Anand
我也在使用Micromax A52。我没有在其他设备上测试过。我认为你需要在另一台设备上运行相同的聊天应用程序。我从https://groups.google.com/forum/?fromgroups=#!searchin/android-developers/bluetooth$20chat$20not$20working/android-developers/fNm0vvzPlg8/fCtqyANwb9gJ得到了这个信息。 - Haris
你可以在Android Google群组中搜索“Bleutooth chat Api 10 not working”,我在那里发布了相同的问题。 - Haris
嗨Aswin,你有解决方案吗?当其他设备运行相同的聊天应用程序时,它是否有效?我没有测试过,因为现在我只有一个安卓设备... - Haris
1个回答

0
一个显而易见的问题,但还是要问一下:
你在 Android 清单文件中注册了 2 Bluetooth 权限 吗?

是的,我已经注册了......实际上通过参考一些帖子,我意识到另一个设备同时应运行蓝牙聊天应用程序.....然后才能配对设备.....但是对于我的应用程序,我正在重用蓝牙通信代码用于其他目的....在其中,我需要在可发现模式开启时将我的设备与另一个设备配对.....有什么想法吗?谢谢您的回复。 - Haris
你想让另一个设备可被发现吗?这在Android中有一个超时时间,因为它会消耗电池。请阅读此链接:http://developer.android.com/guide/topics/connectivity/bluetooth.html#EnablingDiscoverability。最大可发现时间为1小时。别忘了点赞! - Radu
是的,我正在通过蓝牙设置手动将另一个设备设置为可发现状态...然后才启动我的蓝牙聊天应用程序...它正在搜索并列出设备,但当我尝试连接到列出的设备时,它显示无法连接...但是在搜索后,我需要将我的设备与其他设备配对,而不是连接...谢谢... - Haris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接