消息没有收到蓝牙聊天。 我的处理程序是否坏了

我正在开发Android开发项目。 蓝牙的网站,它似乎是正确的发送消息,但没有收到他们。 过去,我试图创建另一个蓝牙应用程序,当连接到设备时,系统提示我确认连接,并显示PIN码。 在当前的应用程序工作时,我从来没有得到提示。 但是,它并没有出现任何异常情况,并且消息似乎正在发送。 这是我的Thread类用于管理连接:

inner class ConnectedThread(val socket:BluetoothSocket, val socketType:String) : Thread(){ val inStream = socket.inputStream val outStream = socket.outputStream init { mState = STATE_CONNECTED } override fun run() { Log.i(TAG, "BEGIN mConnectedThread") val buffer = ByteArray(1024) var bytes:Int while (mState == STATE_CONNECTED){ try { bytes = inStream.read(buffer) mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer) .sendToTarget() }catch (ioe:IOException){ Log.e(TAG, "disconnected", ioe) connectionLost() break } } } fun write(buffer:ByteArray){ try { outStream.writeMessage(buffer, mHandler) }catch (ioe:IOException){ Log.e(TAG, "Exception during write", ioe) } } fun cancel(){ try { socket.close() }catch (ioe:IOException){ Log.e(TAG, "close of connect socket failed", ioe) } } } } 

并在下面我用于写入OutputStream的扩展函数:

 fun OutputStream.writeMessage(buffer:ByteArray, handler:Handler? = null){ write(buffer) handler?.let { it.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer) .sendToTarget() } } 

我只是不确定我的错误在哪里。 我在想这是我发送信息的方式有问题吗? 但是当我用调试程序遍历我的代码时,我确实没有看到任何不合适的地方。 现在我正在想,问题是在接收设备的末端,似乎根本没有收到任何东西。 我将会发布我的Handler和broadcastReceiver:

 val mHandler = object:Handler(){ override fun handleMessage(msg: Message?) { when(msg!!.what) { Constants.MESSAGE_STATE_CHANGE -> { when(msg.arg1) { BluetoothChatService.STATE_CONNECTED -> { setStatus(getString(R.string.title_connected_to, mConnectedDeviceName)) mConversationArrayAdapter!!.clear() } BluetoothChatService.STATE_CONNECTING -> { setStatus(getString(R.string.title_connecting)) } BluetoothChatService.STATE_LISTEN -> { } BluetoothChatService.STATE_NONE -> { setStatus(getString(R.string.title_not_connected)) } } } Constants.MESSAGE_WRITE -> { val writeBuf = msg.obj as ByteArray val writeMessage = String(writeBuf) mConversationArrayAdapter!!.add("Me: $writeMessage") } Constants.MESSAGE_READ -> { val readBuf = msg.obj as ByteArray val readMessage = String(readBuf, 0, msg.arg1) mConversationArrayAdapter!!.add("$mConnectedDeviceName: $readMessage") } Constants.MESSAGE_DEVICE_NAME -> { mConnectedDeviceName = msg.data.getString(Constants.DEVICE_NAME) if (activity!=null)Toast.makeText(activity, "Connected to $mConnectedDeviceName", Toast.LENGTH_SHORT).show() } Constants.MESSAGE_TOAST -> { if (activity!=null)Toast.makeText(activity, msg.data.getString(Constants.TOAST), Toast.LENGTH_SHORT).show() } } } } val mReceiver:BroadcastReceiver = object:BroadcastReceiver(){ override fun onReceive(context: Context, intent: Intent) { val action = intent.action if (BluetoothDevice.ACTION_FOUND.equals(action)){ val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE) if (device.bondState!=BluetoothDevice.BOND_BONDED){ mNewDevicesArrayAdapter.add("${device.name} \n ${device.address}") } }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ setProgressBarIndeterminateVisibility(false) setTitle(R.string.select_device) if (mNewDevicesArrayAdapter.count==0){ val noDevices = resources.getText(R.string.none_found).toString() mNewDevicesArrayAdapter.add(noDevices) } } } } 

以下是我的AcceptThread:

  inner class AcceptThread(val secure:Boolean) : Thread(){ var mmServerSocket:BluetoothServerSocket? = null val mSocketType:String = if (secure) "Secure" else "Insecure" init { try { mmServerSocket = if (secure){ mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, UUID_SECURE) } else { mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME_INSECURE, UUID_INSECURE) } }catch (ioe:IOException){ Log.e(TAG, "Socket Type: $mSocketType listen() failed", ioe) } mState = STATE_LISTEN } override fun run() { Log.d(TAG, "Socket Type: $mSocketType BEGIN $this") name = "AcceptThread $mSocketType" var socket:BluetoothSocket? = null while (mState!=STATE_CONNECTED){ try { socket = mmServerSocket!!.accept() }catch (ioe:IOException){ Log.e(TAG, "Socket Type: $mSocketType accept failed", ioe) break } } socket?.let { synchronized(this@BluetoothChatService){ when(mState){ STATE_LISTEN -> {} STATE_CONNECTING -> { connected(socket!!, socket!!.remoteDevice, mSocketType) } STATE_NONE -> {} STATE_CONNECTED -> { try { it.close() }catch (ioe:IOException){ Log.e(TAG, "Could not close unwanted socket", ioe) } } else->{throw BluetoothChatServiceException("Invalid Service State")} } } } Log.i(TAG, "End mAcceptThread, Socket Type: $mSocketType") } fun cancel(){ Log.d(TAG, "Socket Type: $mSocketType cancel $this") try { mmServerSocket!!.close() }catch (ioe:IOException){ Log.e(TAG, "Socket Type: $mSocketType close() of server failed", ioe) } } } 

请让我知道,如果你能够发现任何东西。 我真的很感谢帮助。 另外,我应该提到,原始代码是在Java中。 我正在Kotlin写我的。

我注意到, when Kotlin块没有从Java代码复制函数的时候。

科特林:

 when (mState) { STATE_LISTEN -> {} STATE_CONNECTING -> { connected(socket!!, socket!!.remoteDevice, mSocketType) } ... 

不是Java代码:

 switch (mState) { case STATE_LISTEN: case STATE_CONNECTING: // Situation normal. Start the connected thread. connected(socket, socket.getRemoteDevice(), mSocketType); break; ... 

但是是这样的:

 switch (mState) { case STATE_LISTEN: break; case STATE_CONNECTING: // Situation normal. Start the connected thread. connected(socket, socket.getRemoteDevice(), mSocketType); break; ... 

你的Kotlin代码应该是:

 when (mState) { STATE_LISTEN, STATE_CONNECTING -> { connected(socket!!, socket!!.remoteDevice, mSocketType) } ... 

在Java中,没有breakswitch分支直到下一行代码,直到程序在Kotlin中敲入一个break为止,带有箭头/大括号的条目定义了一个分支。 只需仔细检查所有的语句,以确保它们遵循预期的Java switch行为。