java - 深度 sleep 连接蓝牙设备失败

标签 java android bluetooth sleep android-bluetooth

我正在尝试每 25 秒连接到配对的蓝牙设备,通过 AlarmManager 安排,它会触发 WakefulBroadcastReceiver 以启动服务以进行连接。设备进入休眠状态后,前几个小时一切正常,但大约 4-5 小时后开始出现故障,此时我假设设备进入深度 sleep

我从 ParcelFileDescriptor 得到一个 NullPointerException,指出“FileDescriptor 不能为空”。我已经尝试搜索此错误,甚至还查看了 ParcelFileDescriptor.java 中的代码,但我已无路可走。我在装有 Android 4.4.2 的 Nexus 10 上运行它。尝试连接的代码如下:

public GatewaySocket getSocket() throws IOException
{
    if (!BluetoothAdapter.checkBluetoothAddress(macAddress))
        return new GatewaySocket("Address " + macAddress + " is not a valid Bluetooth MAC Address");

    BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
    if (bluetooth == null)
        return new GatewaySocket("Sorry, no Bluetooth adapter available");

    BluetoothDevice device = bluetooth.getRemoteDevice(macAddress);

    BluetoothSocket btSocket = null;

    try
    {
        btSocket = device.createRfcommSocketToServiceRecord(uuid);
    }
    catch (Exception e)
    {
        log(3, "" + this, "Error closing socket on connection: " + e);
    }

    if (btSocket == null)
        return new GatewaySocket("Unable to launch insecure connection to " + device);

    try
    {
        btSocket.connect();
    }
    catch (IOException ex)
    {
        try
        {
                btSocket.close();
        }
        catch (IOException ex2)
        {
                // do nothing
        }

    throw (ex);
    }

    GatewaySocket socket = new GatewaySocket(btSocket, btSocket.getInputStream(), btSocket.getOutputStream());

    return socket;
}

GatewaySocket 是 BluetoothSocket 的瘦子类。错误发生在 btSocket.connect() 行,堆栈跟踪如下:

01-10 09:13:57.796: W/BluetoothAdapter(3591): getBluetoothService() called with no BluetoothManagerCallback
01-10 09:13:57.801: D/BTIF_SOCK(979): service_uuid: 00001101-0000-1000-8000-00805f9b34fb
01-10 09:13:57.801: E/bt-btif(979): SOCK_THREAD_FD_RD signaled when rfc is not connected, slot id:4374, channel:-1
01-10 09:13:57.801: W/System.err(3591): java.lang.NullPointerException: FileDescriptor must not be null
01-10 09:13:57.806: W/System.err(3591):     at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:174)
01-10 09:13:57.806: W/System.err(3591):     at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:905)
01-10 09:13:57.806: W/System.err(3591):     at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:897)
01-10 09:13:57.806: W/System.err(3591):     at android.bluetooth.IBluetooth$Stub$Proxy.connectSocket(IBluetooth.java:1322)
01-10 09:13:57.806: W/System.err(3591):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:308)
01-10 09:13:57.806: W/System.err(3591):     at com.gateway.service.AndroidGMConversation.getSocket(AndroidGMConversation.java:162)
01-10 09:13:57.806: W/System.err(3591):     at com.gateway.GatewayManagerConversation.converse(GatewayManagerConversation.java:81)
01-10 09:13:57.806: W/System.err(3591):     at com.gateway.GatewayManagerConversation.run(GatewayManagerConversation.java:72)
01-10 09:13:57.806: W/System.err(3591):     at java.lang.Thread.run(Thread.java:841)
01-10 09:13:57.806: V/PS(3591): Finished with device 06:92:25:0A:A5:50

如有任何建议,我们将不胜感激!

最佳答案

我在浏览蓝牙库类后找到了解决方法,发现它在调用 bluetoothsocket.close(); 时不会关闭 FileDescriptor,尽管它调用了 dispatch() FileDescriptor 上的方法,它不会关闭它。

只需在 bluetoothsocket.close();

之前调用此方法
private synchronized void clearFileDescriptor(){
        try{
            Field field = BluetoothSocket.class.getDeclaredField("mPfd");
            field.setAccessible(true);
            ParcelFileDescriptor mPfd = (ParcelFileDescriptor)field.get(socket);
            if(null == mPfd){
                return;
            }

            mPfd.close();
        }catch(Exception e){
            Log.w(SensorTicker.TAG, "LocalSocket could not be cleanly closed.");
        }
    }

希望这也能解决其他问题。但它应该由 BluetoothSocket 类通过 close 方法处理。

关于java - 深度 sleep 连接蓝牙设备失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21166222/

相关文章:

java - 检测 Scala 程序中函数变化的最佳实践?

java - 如何通过移动手机进行更改

linux - Bluez-5.36 StartDiscovery() 方法

java - 如果重新打开应用程序,SharedPreferences 不会保存

android - BluetoothGatt 连接后返回空服务列表

java - Android 蓝牙 IOException 错误的文件号

java - 在 Java 中解析 JSON 字符串

java - Android API 是基于回调的吗?

Java:如何获取上传和下载速度

android - 未使用 OpenGL ES 2.0 和 Android NDK 绘制纹理