我已经创建了一个 Android 应用程序,如果我的 Android 有一个蓝牙连接到我的电脑并且我关闭了我的电脑,isConnected 仍然返回 true。有人知道这是否可以修复吗?
不知道有没有蓝牙心跳
最佳答案
试试这个。
IntentFilter connectivityFilter = new IntentFilter();
connectivityFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
connectivityFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
registerReceiver(bluetoothModeCheckingReceiver, connectivityFilter);
private final BroadcastReceiver bluetoothModeCheckingReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
// to do
} else if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
// to do
}
}
};
关于android - BluetoothSocket.isConnected 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43803884/