简体   繁体   English

是否可以仅显示配对,打开和范围内的蓝牙设备的活动列表,而不是显示所有先前配对的设备?

[英]Is it possible to only show an active list of Bluetooth Devices that are paired, on, and in range as opposed to showing all previous paired devices?

I am currently working on an android app that connects to an arduino microprocessor with Bluetooth SPP capabilities. 我目前正在开发一个Android应用程序,它连接到具有蓝牙SPP功能的arduino微处理器。 My app shows all previously paired bluetooth devices, but I wanted to know is it possible to only show paired devices that are currently turned ON? 我的应用程序显示所有以前配对的蓝牙设备,但我想知道是否可以只显示当前打开的配对设备? I have searched for an answer everywhere and have yet to find one, hopefully this is possible. 我到处寻找答案,还没有找到答案,希望这是可能的。

You'll need to create a BluetoothAdapter, and register a BroadcastReceiver to BluetoothDevice.ACTION_FOUND. 您需要创建BluetoothAdapter,并将BroadcastReceiver注册到BluetoothDevice.ACTION_FOUND. Store those found devices in some sort of list. 将这些找到的设备存储在某种列表中。

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, intentFilter);
bluetoothAdapter.startDiscovery();

...
private ArrayList<BluetoothDevice> devices = new ArrayList<>();
private final BroadcastReceiver receiver = new BroadcastReceiver(){
    public void onReceive(Context context, Intent intent){
        String action = intent.getAction();
        if(action.equals(BluetoothAdapter.ACTION_FOUND)){
            BluetoothDevice bluetoothDevice = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            devices.add(bluetoothDevice);
        }
    }
};

You can also register the receiver to BluetoothDevice.ACTION_DISCOVERY_STARTED and BluetoothDevice.ACTION_DISCOVERY_FINISHED and handle them accordingly. 您还可以将接收器注册到BluetoothDevice.ACTION_DISCOVERY_STARTEDBluetoothDevice.ACTION_DISCOVERY_FINISHED并相应地处理它们。

Note: Don't forget to register the broadcast receiver in the manifest and include necessary permissions. 注意:不要忘记在清单中注册广播接收器并包含必要的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM