不幸的是,我的安卓蓝牙有一些问题。对于我的测试环境,我使用带有 Android 4.4.2 的 Nexus 4。
我的 PC 上有一个 Java 应用程序,它使用 bluecove 作为客户端建立 SPP 连接。该程序正在寻找特殊的服务名称并与我的 Android 手机连接。然后它会向我的 Android 手机发送 72 个字节并等待答复。当得到答案时,程序会休眠 3 秒,然后重新启动。
在我的 Android 手机上,我有一个带有背景蓝牙监听器的应用程序,该应用程序在启动时启动。该应用程序基于BluetoothChat 示例演示。接收蓝牙数据时,我检查传入的数据并发回答案。
一切都运行良好。但是在 489 个蓝牙连接之后,Android 应用程序失败,并在 PC-java-app 运行时出现以下错误片段:
getBluetoothService() called with no BluetoothManagerCallback
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x41b34ba8)
FATAL EXCEPTION: main
Process: de.tum.lme.diamantum:remote_blue, PID: 21567
java.lang.NullPointerException: FileDescriptor must not be null
at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:174)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:905)
at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:897)
at android.bluetooth.IBluetooth$Stub$Proxy.createSocketChannel(IBluetooth.java:1355)
at android.bluetooth.BluetoothSocket.bindListen(BluetoothSocket.java:349)
at android.bluetooth.BluetoothAdapter.createNewRfcommSocketAndRecord(BluetoothAdapter.java:1055)
at android.bluetooth.BluetoothAdapter.listenUsingRfcommWithServiceRecord(BluetoothAdapter.java:976)
at com.test.btconn.BluetoothHandling$AcceptThread.<init>(BluetoothHandling.java:449)
at com.test.btconn.BluetoothHandling.start(BluetoothHandling.java:216)
at com.test.btconn.BluetoothListenerService.setupBtSockets(BluetoothListenerService.java:330)
at com.test.btconn.BluetoothListenerService.manageBtState(BluetoothListenerService.java:249)
at com.test.btconn.BluetoothListenerService.setBtStateDisconnected(BluetoothListenerService.java:383)
at com.test.btconn.BluetoothListenerService.access$5(BluetoothListenerService.java:378)
at com.test.btconn.BluetoothListenerService$2.handleMessage(BluetoothListenerService.java:421)
因此应用程序的 ParcelFileDescriptor 存在问题,它突然为空。但为什么?
当更改 PC-java-app 上的暂停时间、使用不同的数据大小进行传输和使用不同的智能手机时,上述所有情况也会发生。当使用反射“listenUsingRfcommWithServiceRecord”时,505 传输后也会发生同样的情况。使用唤醒锁也不会改变任何事情。
顺便说一下,我在使用 BluetoothChat 示例时得到了相同的行为。
那么,有没有人提示一下,会发生什么?
Update:
每次连接后都会关闭BluetoothServerSocket,如果蓝牙状态为3,则关闭BluetoothSocket。