gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:22 35 4
gpt4 key购买 nike

我正在尝试每 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com