Bt套接字关闭异常

我建立一个应用程序,应该通过蓝牙套接字发送文件,但由于某种原因,我不断得到相同的例外。 错误消息说,套接字已经关闭,但我不明白如何或为什么它被关闭之前,我试图读取它。 用户将看到一个AlertDialog选项可供选择。 他们应该:1.配对设备。 2.发送文件到设备(目前在对话框中标记为“连接”)。 在选择连接选项后,设备配对后会发生异常。 当接收设备试图从套接字读取我得到这个异常:

Process: com.example.zemcd.toofxchange, PID: 1074 java.io.IOException: bt socket closed, read return: -1 at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:588) at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96) at java.io.BufferedInputStream.read1(BufferedInputStream.java:273) at java.io.BufferedInputStream.read(BufferedInputStream.java:334) at java.io.FilterInputStream.read(FilterInputStream.java:107) at kotlin.io.ByteStreamsKt.copyTo(IOStreams.kt:101) at kotlin.io.ByteStreamsKt.copyTo$default(IOStreams.kt:98) at kotlin.io.ByteStreamsKt.readBytes(IOStreams.kt:117) at com.example.zemcd.toofxchange.BluetoothUtils$Companion.receiveFile(BluetoothUtils.kt:82) at com.example.zemcd.toofxchange.ListenThread$run$acceptThread$1.run(BluetoothUtils.kt:104) at java.lang.Thread.run(Thread.java:761) 

下面是用于pair,unpair和connect(用于发送数据)以及使用的线程子类的函数:

 class BluetoothUtils { companion object { var listener: ListenThread? = null val _UUID = UUID.fromString("a0e7e4c7-0e4e-43b7-9d18-659192512164") val TAG = "BluetoothUtils" val receiver = MainBTStatusReceiver() fun initPairingServer(adapter: BluetoothAdapter){ var mmServerSocket: BluetoothServerSocket? try { var tmp = adapter.listenUsingRfcommWithServiceRecord(TAG, _UUID) mmServerSocket = tmp listener = ListenThread(mmServerSocket) listener!!.start() }catch (ioe: IOException){ Log.e(TAG, "Error initializing Bluetooth", ioe) } } fun cancelListener() = listener!!.cancel() fun connect(adapter: BluetoothAdapter, device: BluetoothDevice):Unit{ var btSocket: BluetoothSocket? Log.d(TAG, "connect function called") if (device.bondState==BluetoothDevice.BOND_NONE)return //to prompt user to pair try { adapter.cancelDiscovery() btSocket = device.createRfcommSocketToServiceRecord(_UUID) ConnectThread(btSocket).start() }catch (ioe: IOException){ Log.e(TAG, "error connecting", ioe) } } fun startPair(adapter: BluetoothAdapter, device: BluetoothDevice): Unit{ adapter.cancelDiscovery() Log.d(TAG, device.bondState.toString()) device.createBond() } fun unPair(device: BluetoothDevice): Any = device::class.java.getMethod("removeBond").invoke(device) fun sendFile(btSocket: BluetoothSocket){ val out = btSocket.outputStream.buffered() out.use { val msg = "hello".toByteArray() Log.d(TAG, "sending data") it.write(msg, 0, msg.size) it.flush() } btSocket.close() } fun receiveFile(btSocket: BluetoothSocket){ Log.d(TAG, "receiveFile called") val inStream = btSocket.inputStream.buffered() //val bytes: ByteArray = ByteArray(1024) val bytes: ByteArray = inStream.use { it.readBytes(1024)} Log.d(TAG, bytes.toString()) btSocket.close() } } } class ListenThread(val btServSock: BluetoothServerSocket) : Thread(){ companion object { val TAG = "ListenThread" } var btSocket: BluetoothSocket? = null override fun run() { super.run() while (true){ try { Log.d(TAG, "listening . . . ") btSocket = btServSock.accept() Log.d(TAG, btSocket.toString() + " was accepted") val acceptThread = Thread(Runnable { BluetoothUtils.receiveFile(btSocket!!) }) acceptThread.start() }catch (ioe: IOException){ Log.e(TAG, "Error", ioe) break } } } fun cancel() = btServSock.close() } class ConnectThread(val btSocket: BluetoothSocket) : Thread(){ companion object { val TAG = "Pairing Thread" } override fun run() { super.run() try { Log.d(TAG, "attempting to connect") btSocket.connect() BluetoothUtils.sendFile(btSocket) }catch (ioe: IOException){ Log.e(TAG, "error connecting", ioe) btSocket.close() } } } 

他们在OnClickListener里被这样调用:

 ops.forEach { it.setOnClickListener { Toast.makeText(it.context, it.id.toString(), Toast.LENGTH_SHORT).show() when(it.id){ R.id.statOp -> {} R.id.connectOp -> { Log.d(TAG, "connectOp reached") BluetoothUtils.connect(BluetoothAdapter.getDefaultAdapter(), btDevice) dialog!!.dismiss() } R.id.pairOp -> { Log.d(TAG, "pairOp reached") mReceiver.setFocus(this@DeviceHolder) BluetoothUtils.startPair(BluetoothAdapter.getDefaultAdapter(), btDevice) Log.d(TAG, "start pair complete") dialog!!.dismiss() } R.id.unPairOp -> { Log.d(TAG, "unPairOp reached") mReceiver.setFocus(this@DeviceHolder) BluetoothUtils.unPair(btDevice) Log.d(TAG, "unpair complete") dialog!!.dismiss() } R.id.sendOp -> {} } } } 

这里是我的资源功能:

 inline fun <T: AutoCloseable, U> withResources(resource: T, fn: (T) -> U) : U{ try { return fn(resource) }finally { resource.close() } } 

请帮我找到我的错误。 如果其他部分我的代码需要让我知道,我会张贴他们。 感谢您的帮助。