gpt4 book ai didi

android - 不使用数据包检测蓝牙 SPP 断开连接

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:52 25 4
gpt4 key购买 nike

我有一些连接到 Android 手机的蓝牙设备,但我无法检测到断开连接。除非需要,否则蓝牙设备不会发送数据包,因此不能选择在数据包接收时使用看门狗来检测断开连接。我读过您可以使用 ACLDisconnected 广播,但是这个事件对我来说永远不会触发(我等了几分钟)。在 Android 6 中检测断开连接的可靠方法是什么?

这是我的 AclDisconnect 注册码:

_filter = new IntentFilter();
_filter.AddAction(BluetoothDevice.ActionFound);
_filter.AddAction(BluetoothDevice.ActionBondStateChanged);
_filter.AddAction(BluetoothAdapter.ActionDiscoveryStarted);
_filter.AddAction(BluetoothDevice.ActionAclDisconnected);
_filter.AddAction(BluetoothDevice.ActionAclDisconnectRequested);
context.RegisterReceiver(_bluetoothDeviceReceiver, _filter);

回调(断开连接时不会触发)

public override void OnReceive(Context context, Intent intent)
{
string action = intent.Action;

if (action == BluetoothDevice.ActionAclDisconnected || action == BluetoothDevice.ActionAclDisconnectRequested)
{
Interlocked.CompareExchange(ref Disconnected, null, null)?.Invoke();
}
}

最佳答案

我不知道 Xamarin,但在普通的 Android/Java 中,这通常是通过捕获在流关闭时由 InputStream 抛出的 IOException 来完成的。例如:

// In your listening thread
InputStream inputStream = socket.getInputStream();
while (true) {
try {
int nextByte = inputStream.read();
// Do something with it
} catch (IOException e) {
// Here you know that you are disconnected
}
}

关于android - 不使用数据包检测蓝牙 SPP 断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45344383/

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