- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一个使用 RFCOMM 通过蓝牙连接到设备的 Android 应用程序。我使用 BluetoothChat 示例作为建立连接的基础,大部分时间一切正常。
但是,有时由于出现套接字已打开的消息,我无法重新连接:
RFCOMM_CreateConnection - already opened state:2, RFC state:4, MCB state:5
如果我连接到设备,关闭应用程序(调用 onDestroy()),重新打开它并尝试再次连接,这往往会发生,这会导致上述情况。
我在 ConnectThread 中使用此方法进行连接(ref.BluetoothChat 示例):
Method m = device.getClass().getMethod("createRfcommSocket",new Class[] {int.class });
tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
mmSocket = tmp;
解决此问题的唯一方法是关闭/打开 Android 手机的蓝牙。
这让我相信套接字没有在 onDestroy() 中关闭,但我仍然呼吁关闭所有线程,如前面提到的示例所示。
任何想法将不胜感激。
最佳答案
我也偶然发现了这个,这是我找到的答案:
如果多次打开和关闭蓝牙套接字连接,可能会发生此错误。
解决方案
从 API 级别 14 开始,BluetoothSocket 中有一个名为 isConected() 的方法。 ,如果此套接字已连接,则返回 true,否则返回 false,这里是 API 的原始摘录:
Get the connection status of this socket, ie, whether there is an active connection with remote device.
对于 < 14 的 API 级别,您可以通过在关闭连接后让蓝牙处理线程进入休眠状态来解决此问题 - 1000 毫秒应该足够了,这是一个示例(btDevice 是 BluetoothDevice 类型并且已经在下面的代码 fragment ):
try {
//Open the socket to an SPP device (UUID taken from Android API for createRfcommSocketToServiceRecord)
BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord("00001101-0000-1000-8000-00805F9B34FB");
//Connect to the socket
btSocket.connect();
//Close the socket
btSocket.close();
//Sleep time of 1000ms after closing the socket
SystemClock.sleep(POST_RESET_DELAY);
} catch (Throwable e) {
// Log error message
}
附言除了 SystemClock.sleep,您还可以使用 Thread.sleep - 但是 SystemCock 的 sleep 不能被打断,而 Thread.sleep 可以被打断,所以这取决于您的用例,哪个选项更适合您的目的。
关于android - RFCOMM_CreateConnection - 已打开状态 :2, RFC 状态 :4, MCB 状态:5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7888294/
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 10年前关闭。 Improve this
我发现 IMAPv4 有一个缺点,因为它强制用户下载整个正文(文本/HTML + 附件)。 如何编写并提出 RFC 来解决该问题? 如果您之前有撰写技术文档、提案,尤其是 RFC 的经验,请分享。 最
URLComponents.init(url:resolvingAgainstBaseURL:) 的文档说: Returns the initialized URL components object
似乎 Internet-Draft 提供了一个下载 XML 文件的链接(例如 https://tools.ietf.org/id/draft-ietf-oauth-v2-31.xml ),但我找不到下
我注意到 RFC 中的一个错误,我想报告该错误,并希望看到发布的勘误表。 有谁知道如何向 IETF 工作组正式报告错误。 IETF 是否有某种错误/错别字跟踪? 我试图给 RFC 的作者发送一封电子邮
我看到http://www.ietf.org/rfc/rfc4122.txt RFC 4122 第 4 版的最大长度是多少?换句话说,它是否始终与从文档中获取的示例字符串值的最大长度相同? f81d4
rfc-editor说 "Obsoletes xxxx" refers to other RFCs that this one replaces. "Updates xxxx" refers to o
在原生 Android 日历应用中,使用 RFC 2445 协议(protocol)创建 .ics文件。我在一些博客中发现 RFC 2445 被 RFC 5545 取代。谁能告诉我 RFC 5545
我正在将一些 Python 代码翻译为 C++。部分代码使用base 64编码。 Python 代码使用RFC 3548编码,但我使用的C++库只有RFC 4648 . 我知道 RFC 4648 已过
我的程序在线抛出这个异常,我知道它出错的原因。 我的问题是如何找到错误的地方,Java 无法捕获此异常的位置。 如何获取有关此异常的其他信息,例如此错误的 API 请求地址。 错误信息如下: 2019
我正在尝试为 multipart/related 实现一个基本的 MIME 解析器。在 C++/Qt 中。 到目前为止,我一直在为 header 编写一些基本的解析器代码,并且我正在阅读 RFC 以了
我正在尝试在 HTTP 服务器上实现 RFC 2388 以支持多部分 POST。 我正在查看专门针对内容配置的“名称”参数的规范。 根据 RFC 2388 的第 3 节,它指出: Field name
首先,一些简单的背景知识...作为与第三方供应商集成的一部分,我有一个 C# .Net Web 应用程序,它接收一个 URL,其中包含查询字符串中的大量信息。该 URL 使用 MD5 哈希值和共享 k
早上好。 我们有一个使用 SAP RFC SDK 的“经典”API 的 SAP 工作 Idoc 接口(interface)。由于不再支持它,我们需要迁移到带有新 API 的新 Netweaver RF
我正在使用一个非常 bing 和旧的软件,它与 servlet 一起工作,并且在 URL 中包含重音符号和其他奇怪的字符。 几周前软件从JDK7升级到JDK11,服务器从Tomcat6升级到Tomca
1.情景展示 tomcat 日志时不时会报出如下异常信息,到底是怎么回事? ?
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid charact
谁能告诉我白天服务器的用途是什么?我提到了一些网站,如 wikipedia ,却找不到答案。 最佳答案 Daytime Protocol 是供计算机通信的一种广泛接受的已知格式 RFC 867允许 2
是否有关于在 RFC 上撰写评论的一般指南? 最佳答案 根据IETF ,一旦发布,RFC 不会改变。勘误可以提交到editor at rfc-editor.org .对未发表的 RFC 的评论可提交至
我写了一个ABAP功能模块,如果我用我的开发者账号执行它就可以了。 如果另一个用户执行它,他会得到一个空结果。另一个用户是无法使用 SAP GUI 登录的 RFC 帐户。 我不知道如何调试它。如何执行
我是一名优秀的程序员,十分优秀!