如何在Android编程中使用蓝牙发送和接收数据而无需配对?

发布于 2024-11-17 03:33:40 字数 91 浏览 2 评论 0原文

我是 Android 编程新手,并且有 java 概念,我想知道如何使用蓝牙发送和接收数据而无需配对或任何密码(仅当两个设备都安装了我的应用程序时), 有什么建议吗?

I am new to Android programming, and have java concept, I want to know that how can I send and receive data using bluetooth without pairing or any password (Only if both device has my app installed),
any suggestion?

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

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

发布评论

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

评论(2

烧了回忆取暖 2024-11-24 03:33:40

据我所知,在没有配对的情况下不可能通过 RFCOMM 套接字发送或接收数据。我已经在我正在开发的应用程序中进行了广泛的尝试。我的结果是:

  • 如果两个设备已配对且可发现,则可以进行双向通信
  • 如果两个设备已配对,但“服务器”设备(Android 设备尝试连接的设备)设置为 不可发现,则仍然可以进行双向通信
  • 如果两个设备未配对,但“服务器”设备可发现,则在双向通信之前仍需要配对请求定向通信是可能的。这意味着 RFCOMM 客户端 套接字(即来自 Android 的套接字)需要设备配对。这是在运行 Android 2.2 的 Samsung Captivate 上进行测试的。我觉得这很奇怪,因为我可以理解在允许 RFCOMM 服务器套接字之前需要配对,但要求客户端套接字配对有点严格。

正如 @ethrbunny 提到的,您也可以只使用 WiFi,在每个设备上设置并行服务器/客户端线程,然后发送您想要的任何内容。要发现本地网络上的服务,您可以选择使用 Zeroconf。

As far as I know it's impossible to send or receive data over RFCOMM sockets without pairing. I've tried this extensively with an application that I'm developing. My results are:

  • If the two devices are paired and discoverable, bi-directional communication is possible
  • If the two devices are paired, but the "server" device (the one the android device is trying to connect to) is set to be not discoverable, then bi-directional communication is still possible
  • If the two devices are not paired, but the "server" device is discoverable, then a pairing request is still required before bi-directional communication is possible. This means that RFCOMM client sockets (i.e., those from Android) require the devices to be paired. This was tested on a Samsung Captivate running Android 2.2. I find this very strange, as I can understand requiring pairing before allowing RFCOMM server sockets, but requiring pairing for client sockets is a bit stringent.

As @ethrbunny mentioned you can also just use WiFi, setup parallel server/client threads on each device, and send whatever you want. To discover services on a local network you can optionally use zeroconf.

缱倦旧时光 2024-11-24 03:33:40

我从 Kristopher Micinski 发布的 Google 群组帖子。
希望有帮助。

我相信让它发挥作用的关键是在 mUuid 列表中。

仔细看看它在做什么:

for (int i = 0; i < Connection.MAX_SUPPORTED && myBSock == null; i++) {
    for (int j = 0; j < 3 && myBSock == null; j++) {
        myBSock = getConnectedSocket(myBtServer, mUuid.get(i));
        if (myBSock == null) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                Log.e(TAG, "InterruptedException in connect", e);
            }
        }
    }
}

这段代码的作用是看起来连接到设备,但它是如何连接的
这样做吗?它使用多个 UUID 多次尝试套接字
会议。

本质上,这意味着我们只能使用 UUID一次。所以改为这个
应用程序使用七个UUID来实现,然后服务器监听并
接受服务器端的每个 UUID,这就是使用
以下代码:

for (int i = 0; i < Connection.MAX_SUPPORTED && maxConnections > 0; i++) {
    BluetoothServerSocket myServerSocket = mBtAdapter.listenUsingRfcommWithServiceRecord(srcApp, mUuid.get(i));
    BluetoothSocket myBSock = myServerSocket.accept();
    myServerSocket.close(); // Close the socket now that the
    // connection has been made.

    String address = myBSock.getRemoteDevice().getAddress();

    mBtSockets.put(address, myBSock);
    mBtDeviceAddresses.add(address);
    Thread mBtStreamWatcherThread = new Thread(new BtStreamWatcher(address));
    mBtStreamWatcherThread.start();
    mBtStreamWatcherThreads.put(address, mBtStreamWatcherThread);
    maxConnections = maxConnections - 1;
    if (mCallback != null) {
        mCallback.incomingConnection(address);
    }
}

现在,在客户端做了什么?客户没有
了解服务器当前有多少个活动连接。

如果我们有一些客户必须使用的商定订单,我们可以
只需使用它,但是,在我们的例子中,我们只需尝试每个 UUID
按顺序进行,直到我们“找到正确的”。

简短版本: -- 使用多个 UUID,一次只能使用一个。因此,定义七个(微微网使用的最大值)并尝试每一个,直到
您找到了合适的人。

I got the the following from this Google groups post by Kristopher Micinski.
Hope it helps.

I believe the key to getting this to work is in the mUuid list.

Take a close look at what this is doing:

for (int i = 0; i < Connection.MAX_SUPPORTED && myBSock == null; i++) {
    for (int j = 0; j < 3 && myBSock == null; j++) {
        myBSock = getConnectedSocket(myBtServer, mUuid.get(i));
        if (myBSock == null) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                Log.e(TAG, "InterruptedException in connect", e);
            }
        }
    }
}

What this code does is looks to connect to the device, but how does it
do so? It tries the socket multiple times, using multiple UUIDs for
the session.

In essence it means that we can use UUID only once. So instead this
application implements using seven UUIDs, then the server listens and
accepts each UUID on the server side, this is what is done with the
following code:

for (int i = 0; i < Connection.MAX_SUPPORTED && maxConnections > 0; i++) {
    BluetoothServerSocket myServerSocket = mBtAdapter.listenUsingRfcommWithServiceRecord(srcApp, mUuid.get(i));
    BluetoothSocket myBSock = myServerSocket.accept();
    myServerSocket.close(); // Close the socket now that the
    // connection has been made.

    String address = myBSock.getRemoteDevice().getAddress();

    mBtSockets.put(address, myBSock);
    mBtDeviceAddresses.add(address);
    Thread mBtStreamWatcherThread = new Thread(new BtStreamWatcher(address));
    mBtStreamWatcherThread.start();
    mBtStreamWatcherThreads.put(address, mBtStreamWatcherThread);
    maxConnections = maxConnections - 1;
    if (mCallback != null) {
        mCallback.incomingConnection(address);
    }
}

Now, on the client side of things what is done? The client does not
know how many active connections the server currently has.

If we have some agreed upon order that the clients must use we can
simply use this, however, in our case, we simply just try each UUID
in sequence until we "find the right one."

Short version: -- Use multiple UUIDs, you can only use one at once. So define seven (max for piconet usage) and try each one until
you find the right one.

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