android - 检查蓝牙设备当前是否正在配对
问题描述
我有两个蓝牙设备:手机和 pos 打印机(远程设备),所以我想检查它们之间的配对状态(如果它们正在配对(手机的蓝牙打开,pos 打印机的蓝牙打开,并且它们正在相互连接) ),我将向打印机发送一些命令以打印出一些东西)。
但我不知道如何检查他们是否正在连接。我尝试了一些方法:
Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
即使 pos 打印机的蓝牙关闭,我仍然可以得到 pos 打印机的蓝牙信息(如device.getBondState()
, bluetoothClass.getDeviceClass
, device.getName()
)
我试图检查状态,BroadcastReceiver
但它什么也没显示:
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
final BluetoothDevice device = intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE );
if (BluetoothDevice.ACTION_ACL_CONNECTED.equalsIgnoreCase( action ) ) {
Log.d(CTAG, "We are now connected to " + device.getName() );
if (!connectedDevices.contains(device))
connectedDevices.add(device);
}
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equalsIgnoreCase( action ) ) {
Log.d(CTAG, "We have just disconnected from " + device.getName() );
connectedDevices.remove(device);
}
}
我怎样才能做到这一点?任何想法将不胜感激。
解决方案
推荐阅读
- bash - 如何用一个脚本杀死多个 PID 并使其返回提示
- c++ - 打开 CSV 文件进行阅读,但我不能
- c++ - 理解新关键字异常语法的问题
- c - 如何在 c 中使用 Dijkstra 的最短路径打印路径?
- statistics - 用至少一个其他值替换缺失值并删除所有值都缺失的情况
- shiny - 使用 shinywidgets 从 pickerInput 中删除“全选”操作按钮
- sql-server - 如何在 Linux/CENTOS 上为 SQL Server Developer Edition 启用 TCP/IP 协议
- angular - 我们应该如何更新另一个列表变量中的列表而不影响角度的父列表
- c++ - 无法调用基类成员函数,因为基类是模板化的
- vbscript - 什么会导致 Mailgun SMTP 的 ASP 脚本错误?