连接到蓝牙设备发生故障时的深度睡眠 [英] Connecting to Bluetooth device fails in deep sleep

查看:830
本文介绍了连接到蓝牙设备发生故障时的深度睡眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到已配对的蓝牙装置每25秒,通过AlarmManager计划,触发一个WakefulBroadcastReceiver开始做连接的服务。一旦设备进入睡眠状态,一切的伟大工程的前几个小时,但它开始后约4-5小时要失败,我认为该设备将进入的深度睡眠的。

I'm attempting to connect to a paired bluetooth device every 25 seconds, scheduled through AlarmManager which triggers a WakefulBroadcastReceiver to start a service to do the connection. Once the device goes to sleep, everything works great for the first few hours, but it starts to fail after about 4-5 hours, when I assume the device goes into a deep sleep.

我从ParcelFileDescriptor一个NullPointerException异常,陈述的FileDescriptor不能为空。我试图寻找这个错误,甚至通过code在ParcelFileDescriptor.java走了,但我在一个死胡同。我在一台Nexus 10上运行这一点,与Android 4.4.2。在code它试图连接是如下:

I get a NullPointerException from ParcelFileDescriptor, stating the "FileDescriptor must not be null". I've tried searching this error, and have even gone through the code in ParcelFileDescriptor.java, but am at a dead end. I'm running this on a Nexus 10, with Android 4.4.2. The code which tries to connect is below:

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()线,以下堆栈跟踪:

GatewaySocket is a thin subclass of BluetoothSocket. The error occurs at the btSocket.connect() line, with the following stack trace:

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

任何建议将是AP preciated!

Any suggestions would be appreciated!

推荐答案

我已经找到了解决办法经过蓝牙库类后,发现它不关闭的FileDescriptor同时呼吁 bluetoothsocket.close (); 尽管它调用调度()法在的FileDescriptor,不关闭它

I have found the workaround after going through the Bluetooth library classes, and found it doesn't close the FileDescriptor while calling bluetoothsocket.close(); despite it calls the dispatch() method over the FileDescriptor, which doesn't close it.

就在打电话之前这个方法 bluetoothsocket.close();

Just call this method before the 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方法来处理。

Hope this will resolve others problem as well. But it's should be handled by the BluetoothSocket class over the close method.

这篇关于连接到蓝牙设备发生故障时的深度睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆