我目前正在开发一个android应用程序,它连接到具有蓝牙SPP功能的arduino微处理器。我的应用程序显示所有以前配对的蓝牙设备,但我想知道是否可以只显示当前打开的配对设备?我到处寻找答案,但还没有找到,希望这是可能的。
发布于 2016-08-06 01:27:04
您需要创建一个BluetoothAdapter,并注册一个BroadcastReceiver到BluetoothDevice.ACTION_FOUND.中,将那些找到的设备存储在某种列表中。
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);
}
}
};您还可以将接收器注册到BluetoothDevice.ACTION_DISCOVERY_STARTED和BluetoothDevice.ACTION_DISCOVERY_FINISHED,并相应地处理它们。
注意:不要忘记在清单中注册广播接收器,并包括必要的权限。
https://stackoverflow.com/questions/38794057
复制相似问题