首页 > 解决方案 > 检查蓝牙设备当前是否正在配对

问题描述

我有两个蓝牙设备:手机和 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);
        }
    }

我怎样才能做到这一点?任何想法将不胜感激。

标签: androidbluetoothandroid-bluetooth

解决方案


推荐阅读