作为数据传输的蓝牙类的类型处理程序传入什么?

问题描述 投票:0回答:2

我正在开发一个 Android 应用程序,通过 HC-05 通过蓝牙与 Arduino 进行无线通信。我一直在关注 Android 的传输蓝牙数据文档。以下代码是我用来发送和接收数据的代码。

    class MyBluetoothService(
        // handler that gets info from Bluetooth service
        private val handler: Handler
    ) {

        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

            override fun run() {
                var numBytes: Int // bytes returned from read()

                // Keep listening to the InputStream until an exception occurs.
                while (true) {
                    // Read from the InputStream.
                    numBytes = try {
                        mmInStream.read(mmBuffer)
                    } catch (e: IOException) {
                        Log.d(TAG, "Input stream was disconnected", e)
                        break
                    }

                    // Send the obtained bytes to the UI activity.
                    val readMsg = handler.obtainMessage(
                        MESSAGE_READ, numBytes, -1,
                        mmBuffer
                    )
                    readMsg.sendToTarget()
                }
            }

            // 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.
                    val writeErrorMsg = handler.obtainMessage(MESSAGE_TOAST)
                    val bundle = Bundle().apply {
                        putString("toast", "Couldn't send data to the other device")
                    }
                    writeErrorMsg.data = bundle
                    handler.sendMessage(writeErrorMsg)
                    return
                }

                // Share the sent message with the UI activity.
                val writtenMsg = handler.obtainMessage(
                    MESSAGE_WRITE, -1, -1, mmBuffer
                )
                writtenMsg.sendToTarget()
            }

            // 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)
                }
            }
        }
    }`

当我创建 MyBluetoothService(Handler) 的实例时,该处理程序使用什么?我希望它能够不断监听来自 Arduino 的消息。

kotlin android-bluetooth android-handler kotlin-android
2个回答
0
投票

我能够找到问题的答案。我最终在代码顶部定义了该值。代码如下所示。

private val handler = Handler(Looper.getMainLooper())

我能够使用它并将其作为参数传递。


0
投票

我的朋友扎克..现在,当我尝试从 kotlin 应用程序获取数据到 esp32 时,我遇到了同样的问题,您的答案可以更宽敞吗?您在哪里定义了处理程序来解决问题

© www.soinside.com 2019 - 2024. All rights reserved.