通过蓝牙 UUID 连接到 Android 设备时遇到问题

发布于 2024-12-03 03:24:22 字数 1899 浏览 0 评论 0原文

美好的一天,我正在尝试创建蓝牙应用程序,但无法连接到另一台 Android 设备。似乎发生的问题是在传出连接 createRfcommSocketToServiceRecord(UUID) 中 - 我认为 UUID 对于这两种设备来说都不正确。文档说:

如果您要连接到 Android 对等点,请生成您自己的唯一 UUID。

我的问题是 - 如果其他 Android 设备没有安装我的应用程序,我如何使用正确的 UUID 连接到它?我尝试简单地使用通用 UUID 00001101-0000-1000-8000-00805F9B34FB,但这似乎无法解决问题。

以下是代码示例的一部分:

private static final UUID MY_UUID_SECURE = 
    UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

private class ConnectThread extends Thread {
    BluetoothDevice mdevice;
    BluetoothSocket mclientSocket;

    //private String mSocketType;
    private Handler handler;
    private ProgressDialog dialog;

    public ConnectThread(BluetoothDevice device) {
        mdevice = device;
        BluetoothSocket temp = null;

        try {
            System.out.println("making connection to remote device");
            temp = mdevice.createRfcommSocketToServiceRecord(MY_UUID_SECURE);

        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.i(TAG, "Bluetooth Socket" + temp.toString() + "obtained");
        mclientSocket = temp;
    }

    public synchronized void run() {
        try {
            Log.i(TAG, "starting to connect");
            mclientSocket.connect();
        } catch (IOException e) {
            Log.e(TAG, "connection Failed");

            try {
                mclientSocket.close();
            } catch (IOException e2) {
                ; // Do nothing.
            }
        }
    }

    //public void cancel() {
        //try {
            // mclientSocket.close();
        //} catch (IOException e) {
            //Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
        //}
    //}

注意:我尚未实现蓝牙服务器套接字,但我正在尝试了解如何连接到另一个未安装我的应用程序的 Android 设备。一如既往地感谢帮助。谢谢。

Good day, I am trying to create an Bluetooth application and I am failing to connect to another Android device. The problem which seems to be occurring is in the outgoing connection createRfcommSocketToServiceRecord(UUID) - in which I believe the UUID is not correct for both devices. The documentation says:

if you are connecting to an Android peer then please generate your own unique UUID.

My question is - if the other android device does not have my application installed, how do I connect to it with the proper UUID? I have tried simply using the generic UUID 00001101-0000-1000-8000-00805F9B34FB, but that does not seem to resolve the issue.

Here is a part of the code sample:

private static final UUID MY_UUID_SECURE = 
    UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

private class ConnectThread extends Thread {
    BluetoothDevice mdevice;
    BluetoothSocket mclientSocket;

    //private String mSocketType;
    private Handler handler;
    private ProgressDialog dialog;

    public ConnectThread(BluetoothDevice device) {
        mdevice = device;
        BluetoothSocket temp = null;

        try {
            System.out.println("making connection to remote device");
            temp = mdevice.createRfcommSocketToServiceRecord(MY_UUID_SECURE);

        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.i(TAG, "Bluetooth Socket" + temp.toString() + "obtained");
        mclientSocket = temp;
    }

    public synchronized void run() {
        try {
            Log.i(TAG, "starting to connect");
            mclientSocket.connect();
        } catch (IOException e) {
            Log.e(TAG, "connection Failed");

            try {
                mclientSocket.close();
            } catch (IOException e2) {
                ; // Do nothing.
            }
        }
    }

    //public void cancel() {
        //try {
            // mclientSocket.close();
        //} catch (IOException e) {
            //Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
        //}
    //}

Note: I have not yet implemented the Bluetooth Server socket, but am instead trying to understand how you connect to another Android device that does not have my application installed. Help appreciated as always. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2024-12-10 03:24:22

来自: http://developer.android.com/guide/topics/wireless/bluetooth .html

为了在两个设备上的应用程序之间创建连接,您必须同时实现服务器端和客户端机制,因为一个设备必须打开服务器套接字,而另一个设备必须启动连接(使用服务器设备的 MAC 地址发起 联系)。当服务器和客户端在同一 RFCOMM 通道上都有一个已连接的 BluetoothSocket 时,它们被视为已相互连接。此时,每个设备都可以获得输入和输出流,并且可以开始数据传输,这将在“管理连接”部分中讨论。本节介绍如何在两个设备之间发起连接。

服务器设备和客户端设备各自获取所需的BluetoothSocket的方式不同。当接受传入连接时,服务器将收到它。当客户端打开到服务器的 RFCOMM 通道时,客户端将收到它。

From: http://developer.android.com/guide/topics/wireless/bluetooth.html

In order to create a connection between your application on two devices, you must implement both the server-side and client-side mechanisms, because one device must open a server socket and the other one must initiate the connection (using the server device's MAC address to initiate a connection). The server and client are considered connected to each other when they each have a connected BluetoothSocket on the same RFCOMM channel. At this point, each device can obtain input and output streams and data transfer can begin, which is discussed in the section about Managing a Connection. This section describes how to initiate the connection between two devices.

The server device and the client device each obtain the required BluetoothSocket in different ways. The server will receive it when an incoming connection is accepted. The client will receive it when it opens an RFCOMM channel to the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文