- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个Android应用程序,其最新版本的类型为Android API(29)。用户可以搜索附近的蓝牙设备并连接到它们以发送数据。蓝牙连接是在应用程序作为客户端的情况下进行的。
我从Anroid的文档中复制了许多代码,并对其进行了更改以满足自己的需求。该应用程序确实找到了设备,可以正确配对并连接。
问题是,在连接后,当它尝试读取传入流时,在Connected线程中的第一个循环上,将引发异常,并且套接字断开连接。
我已直接连接到设备的正确uuid,以测试通过可能的服务器蓝牙uuid的循环是否会干扰rfcomsocket。
我尝试在循环中的连接线程中尝试读取mInputStream.available() > 0
,然后再尝试读取,但看来它也引发了异常。
为了测试连接,我激活了笔记本电脑的蓝牙适配器并将其设置为接收文件(作为服务器)。我曾经在Xamarin.Android中拥有此应用程序,而代码也曾以这种方式工作。目前,它在Kotlin中运行。
下面的代码介绍了从相应函数调用的Connect和Connected线程。
fun connect(device: BluetoothDevice, uuids: ArrayList<ParcelUuid>) {
if (connectionState == STATE_CONNECTING) {
if (connectThread != null) {
connectThread?.cancel()
connectThread = null
}
}
// Cancel any thread currently running a connection
if (connectedThread != null) {
connectedThread?.cancel()
connectedThread = null
}
connectThread = ConnectThread(device, this, uuids)
connectThread?.start()
}
private inner class ConnectThread(
val device: BluetoothDevice,
service: BTService,
val uuids: ArrayList<ParcelUuid>
) : Thread() {
var socket: BluetoothSocket? = null
init {
service.connectionState = STATE_CONNECTING
}
override fun run() {
// Cancel discovery because it otherwise slows down the connection.
btAdapter?.cancelDiscovery()
for (uuid in uuids) {
try {
val mmSocket: BluetoothSocket? by lazy(LazyThreadSafetyMode.NONE) {
device.createRfcommSocketToServiceRecord(uuid.uuid)
}
mmSocket?.use { thisSocket ->
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
thisSocket.connect()
// The connection attempt succeeded. Perform work associated with
// the connection in a separate thread.
socket = thisSocket
connected(thisSocket)
}
break
} catch (e: IOException) {
// Close the socket
cancel()
// Start the service over to restart listening mode
Log.e(TAG, "unable to connect() to socket.", e)
}
}
}
// Closes the client socket and causes the thread to finish.
fun cancel() {
try {
socket?.close()
} catch (e: IOException) {
Log.e(TAG, "Could not close the client socket", e)
}
}
}
//##################################### #####################################
//#####################################----->Connected<-----#####################################
//##################################### #####################################
// Start the ConnectedThread to begin managing a Bluetooth connection
fun connected(socket: BluetoothSocket) {
// Cancel the thread that completed the connection
if (connectThread != null) {
connectThread?.cancel()
connectThread = null
}
// Cancel any thread currently running a connection
if (connectedThread != null) {
connectedThread?.cancel()
connectedThread = null
}
// Start the thread to manage the connection and perform transmissions
connectedThread = ConnectedThread(socket)
connectedThread?.start()
// Send the name of the connected device back to the UI Activity
MainActivity.settingsFragment?.activity?.runOnUiThread {
(MainActivity.settingsFragment as SettingsFragment).btConnected()
}
}
private inner class ConnectedThread(private val mmSocket: BluetoothSocket) : Thread() {
private val mmInStream: InputStream = mmSocket.inputStream
private val mmOutStream: OutputStream = mmSocket.outputStream
private val mmBuffer: ByteArray = ByteArray(1024) // mmBuffer store for the stream
init {
connectionState = STATE_CONNECTED
}
override fun run() {
// Keep listening to the InputStream until an exception occurs.
while (true) {
// Read from the InputStream.
try {
mmInStream.read(mmBuffer)
} catch (e: IOException) {
connectionLost()
Log.d(TAG, "Input stream was disconnected", e)
break
}
//Do something with stream.
}
}
// Call this from the main activity to send data to the remote device.
fun write(bytes: ByteArray) {
try {
mmOutStream.write(bytes)
} catch (e: IOException) {
Log.e(TAG, "Error occurred when sending data", e)
// Send a failure message back to the activity.
return
}
}
// Call this method from the main activity to shut down the connection.
fun cancel() {
try {
mmSocket.close()
} catch (e: IOException) {
Log.e(TAG, "Could not close the connect socket", e)
}
}
}
// Stop all threads.
fun stop() {
if (connectThread != null) {
connectThread?.cancel()
connectThread = null
}
if (connectedThread != null) {
connectedThread?.cancel()
connectedThread = null
}
connectionState = STATE_NONE
}
fun write(bytes: ByteArray) {
if (connectionState != STATE_CONNECTED) {
return
}
val temp: ConnectedThread? = connectedThread
temp?.write(bytes)
}
fun connectionLost() {
MainActivity.settingsFragment?.activity?.runOnUiThread {
(MainActivity.settingsFragment as SettingsFragment).btLost()
}
connectionState = STATE_NONE
}
2019-08-03 18:30:40.583 24961-25088/com.konkarapas.rcs.full.debug W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
2019-08-03 18:30:40.776 24961-25088/com.konkarapas.rcs.full.debug D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@39f9369, channel: -1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@611f1ee, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@96eb68fmSocket: android.net.LocalSocket@15d7d1c impl:android.net.LocalSocketImpl@94fd25 fd:java.io.FileDescriptor@42f3efa, mSocketState: INIT
2019-08-03 18:30:40.780 24961-25088/com.konkarapas.rcs.full.debug E/BluetoothService: unable to connect() to socket.
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:762)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:776)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:399)
at com.konkarapas.rcs.models.BTService$ConnectThread.run(BluetoothService.kt:172)
2019-08-03 18:30:40.782 24961-25088/com.konkarapas.rcs.full.debug W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
2019-08-03 18:30:40.934 24961-25088/com.konkarapas.rcs.full.debug D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@5534f08, channel: 4, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@ebd46a1, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@3a250c6mSocket: android.net.LocalSocket@fdce887 impl:android.net.LocalSocketImpl@f5253b4 fd:java.io.FileDescriptor@cc6abdd, mSocketState: CONNECTED
2019-08-03 18:30:40.938 24961-25088/com.konkarapas.rcs.full.debug D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@5534f08, channel: 4, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@ebd46a1, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@3a250c6mSocket: null, mSocketState: CLOSED
2019-08-03 18:30:40.943 24961-25089/com.konkarapas.rcs.full.debug D/BluetoothService: Input stream was disconnected
java.io.IOException: socket closed
at android.net.LocalSocketImpl$SocketInputStream.read(LocalSocketImpl.java:104)
at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:555)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:88)
at java.io.InputStream.read(InputStream.java:101)
at com.konkarapas.rcs.models.BTService$ConnectedThread.run(BluetoothService.kt:245)
最佳答案
您正在使用mmSocket?.use
,它会在连接完成后自动清理(即关闭连接)。如果将其更改为mmsocket?.let
,则连接完成后不应自动关闭连接。 (对于网络套接字也有完全相同的问题,请像这样修复它)
关于android - 蓝牙连接的线程在连接后立即关闭 socket ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57338803/
是否可以识别我周围启用了蓝牙的设备?我不需要与他们交流,只知道他们就在那里。 我正在寻找类似于 android 的 BluetouthDevice.startDiscovery() 的东西 这样的事情
蓝牙的 HTTP 代理服务是否允许我将 BLE 设备视为 HTTP 服务器,例如以便与设备对话的应用可以向其发送 GET/POST/PUT 请求? 或者这个操作是相反的方向,BLE 设备通过应用程序向
我正在与BlueZ库一起在Linux下管理蓝牙堆栈。我正在尝试打开一个套接字,该套接字应与已知UUID的特定服务连接。我已成功尝试按照以下示例在服务器和客户端之间打开套接字: http://peopl
有谁知道蓝牙设备如何获取范围内可发现设备的设备 ID? 理想情况下,我正在寻找涉及蓝牙协议(protocol)最小实现的最简单解决方案。 一个起点会很好,我只是想创建一个设备,它可以以最小的功耗存储附
蓝牙双模设备是否可以在与 BT LE 设备配对的同时被经典蓝牙发现?如果设备不能同时运行这两种模式也没关系,但我真的应该在这些模式之间切换芯片吗?我只是在 BT 4 Core 规范中找不到答案 最佳答
我目前正在开展一个涉及乐高 Mindstorms 套件的项目。砖 block 是 NXT,我对蓝牙 ping 速率很好奇。 我对其进行了 100 次 ping 测试,得到了一些有趣的结果。延迟似乎分为
我正在启动一个通过蓝牙进行无线 MIDI 连接的项目。据我所知,BT规范中没有定义MIDI配置文件。 我想知道你们中的一些人是否有兴趣分享有关通过 BT 使用 MIDI 的最佳方式的经验,特别是关于延
Closed. This question is off-topic。它当前不接受答案。
我想通过蓝牙将我的摩托罗拉机器人连接到 OBDKey。我以 BluetoothChat 为例连接蓝牙,使用 KWP 作为协议(protocol) 然后我写byte[]命令 command[0]=ra
几个月前,我用 C# 编写了一个 Messenger 程序,可以让许多客户端连接到服务器并进行聊天。 现在,我想为 android 编写相同的程序。在阅读了 Android Developers 中的
我目前正在制作一个与蓝牙相关的 Android 实用程序,我需要更改我的设备的设备发现范围.. 我有办法这样做吗?我目前正在考虑使用 TPL 来执行此操作,但我不太确定.. Android 应用程序或
我正在为两个玩家构建 tic tac,需要蓝牙连接来交换一些数据,我可以启用蓝牙,启用发现能力,但我不知道“BluetoothServerSocket”和客户端“BluetoothSocket”中的问
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visi
我正在 Microsoft visual studio express 2012,C++ 中制作一个程序,以便与具有此 mac 地址的设备建立简单的蓝牙连接:“00:12:08:24:15:50”,
我正在为 python import bluetooth 使用蓝牙模块,我相信它是 PyBluez 包。我能够从 bluetooth.BluetoothSocket 类进行连接、发送和接收,但我的应用
我正在为 python import bluetooth 使用蓝牙模块,我相信它是 PyBluez 包。我能够从 bluetooth.BluetoothSocket 类进行连接、发送和接收,但我的应用
我尝试通过以下命令来做到这一点: ./configure -developer-build -opensource -nomake examples -nomake tests make module
我有一个服务,理论上可以在没有关联 Activity 的情况下工作(因为“服务”适用于 Android 平台)。 此服务使用蓝牙,特别是注册一个具有给定名称的蓝牙服务来监听通信。当然,它必须启用蓝牙才
谁知道是否可以制作一个应用程序通过蓝牙模拟触摸屏鼠标或触控板? 如何让 PC(或 MAC)知道我是鼠标设备? 问候, 胡安 最佳答案 您应该看看蓝牙 HID 规范。这可能是可能的,具体取决于您用来模拟
我的问题很简单。我想知道什么是我的应用程序的最佳实践,以便它可以“防打瞌睡”。随着 Android N 将在更多情况下应用 Doze,这变得更加相关。 阅读时Doze Documentation有一部
我是一名优秀的程序员,十分优秀!