不幸的是,我在使用 android 的蓝牙时遇到了一些问题。对于我的测试环境,我使用带有 Android 4.4.2 的 Nexus 4。
我的 PC 上有一个 Java 应用程序,它使用 bluecove 作为客户端建立 SPP 连接。该程序正在寻找一个特殊的服务名称并与我的安卓手机连接。之后它会向我的安卓手机发送 72 个字节并等待回复。当得到那个答案时,程序会休眠 3 秒钟,然后再次启动。
在我的 android 手机上,我有一个带有后台蓝牙监听器的应用程序,它在启动时启动。此应用程序基于 BluetoothChat 示例演示。接收蓝牙数据时,我会检查传入的数据并发回答案。
一切正常。但是在 489 个蓝牙连接之后,当 PC-java-app 正在进行时,android 应用程序失败并出现以下错误片段:
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 示例时得到了相同的行为。
那么,有没有人暗示,会发生什么?
更新:
BluetoothServerSocket 在每次连接后关闭,如果蓝牙状态为 3,则 BluetoothSocket 关闭。