Android 蓝牙开发 - 连接到未配对的设备

发布于 2024-10-13 14:53:13 字数 358 浏览 4 评论 0原文

首先,如果我事先手动配对过,我就可以通过蓝牙以编程方式连接到设备。但是,我还没有找到一种(用户友好的)方法来连接未配对的设备(显然是可发现的),即使使用未更改的示例 Android 蓝牙代码也是如此。

我想弹出配对对话框,要求用户输入设备的密码。我的代码和示例 Android 蓝牙代码发生的情况是,使用可发现的蓝牙设备创建套接字

device.createRfcommSocketToServiceRecord(MY_UUID); 

会将通知添加到屏幕顶部的通知栏;然而,用户不太可能注意到新的通知(我知道我没有)。

我如何弹出配对对话框,提示用户输入密钥 - 或者更好的是,自动为特定设备提供密钥?

First off, I AM able to programmatically connect to a device via bluetooth if I have manually paired it beforehand. However, I haven't found a (user-friendly) way to connect with unpaired devices (that are discoverable, obviously), even using the unchanged sample Android Bluetooth code.

I would like to pop up the pairing dialog box that requests the user to input a passkey for the device. What happens with my code, and the sample Android Bluetooth code, is that making a socket with a discoverable bluetooth device

device.createRfcommSocketToServiceRecord(MY_UUID); 

will add a notification to the notification bar at the top of the screen; however, it is pretty unlikely that the user will even notice a new notification (I know I didn't).

How would I pop up that pairing dialog box, prompting the user to enter the passkey - or better yet, supply a passkey for a particularly device automatically?

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

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

发布评论

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

评论(2

梦一生花开无言 2024-10-20 14:53:13

您可以使用 BlueCove 库来做到这一点。然而,它可能比您想要的要重一点,因为它需要本机组件,但作为回报,您可以真正访问手机上的蓝牙。

Tim:我可以看到流氓应用程序如何滥用此机制,但有些设备根本不支持传统的基于 PIN 的身份验证方法(例如 WiiMote)。

You can use BlueCove library to do that. However, it may be a bit heavier than you'd want as it requires a native component but in return gives you real access to Bluetooth on your phone.

Tim: I can see how a rogue app could abuse this mechanism but there are devices out there which don't support traditional PIN-based authentication method at all (for example the WiiMote).

爱她像谁 2024-10-20 14:53:13

我看过一些关于此的主题,但还没有真正找到好的
回答。

我正在将 Nexus 1 连接到蓝牙 spp 服务器设备以进行读取
并写入数据。

经过一些实验,我发现以下

  1. 如果我从 UI 线程卸载连接的建立
    (我认为这是一个好主意)配对对话框(输入引脚)是
    不显示 - 而是显示用户必须的通知项目
    单击以配对设备。

  2. 如果我从 UI 线程内建立连接,那么
    配对对话框将出现在前台,但显然有一个
    等待此操作时,丑陋的小 UI 会暂停。

无论哪种情况,建立连接的请求都会超时
在用户(我)可以使用 IOException 服务输入 pin 之前
发现失败。

我的理想是配对对话框出现在前面(没有
必须阻止我的 UI 线程)并且连接尝试不会
等待配对输入时超时。

还有其他人想出一种方法来实现这项工作吗?

I've seen a few threads on this but haven't really found a good
answer.

I am connecting my nexus 1 to a bluetooth spp server device to read
and write data.

After some experimentation I have found the following

  1. If I offload the establishment of the connection from the UI thread
    (which I would consider a good idea) the pairing dialog (enter pin) is
    not displayed - rather a notification item appears which the user must
    click to pair the devices.

  2. If I establish the connection from within the UI thread then the
    paring dialog will come to the foreground but obviously there is a
    small ugly UI pause while waiting for this.

In either case the request to establish the connection is timing out
before the user (me) can enter the pin with an IOException Service
discovery failed.

My ideal would be that the pairing dialog comes to the front (without
have to block my UI thread) and that the connection attempt would not
time out while waiting on pairing entry.

Has anyone else come up with a way to make this work?

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