如何在android上定期扫描蓝牙设备 [英] How to periodically scan for bluetooth devices on android
问题描述
这听起来可能是一个愚蠢的问题.但我找不到任何答案,因此在这里发帖.
Hi this may sound as a stupid question.But I was unable to find any answers for this, thus posting here.
我正在构建一个室内应用程序,它连续扫描位于商场或图书馆等地方不同位置的蓝牙加密狗.当我拿着安卓手机在商场里移动时,我应该能够获得最近的加密狗我可以连接到(愚蠢的想法,但我想用它做其他事情).为此,我应该能够连续扫描蓝牙设备.
I am building an indoor application which continuously scans the bluetooth dongles located at different locations in a place like a mall or library.As I move in the mall with android phone in my hand I should be able to get the nearest dongle which I can connect to(Stupid Idea but I want to do something else with this).For this I should be able to continuously scan for the bluetooth devices.
请有人告诉我如何让android定期扫描可用的蓝牙设备.
Please can someone tell me how do I make android scan the available Bluetooth devices periodically.
推荐答案
我想这很简单,但之前并没有打动我.答案在这里,
I guess this was so simple but didnt strike me before. Here is the answer,
private BluetoothAdapter mBtAdapter;
mBtAdapter.startDiscovery();
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
//do something
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
{
Log.v(TAG,"Entered the Finished ");
mBtAdapter.startDiscovery();
}
因此,我们应该在 ACTION_DISCOVERY_FINISHED
上再次开始发现,它会每 12 秒连续扫描一次设备.
Thus we should start discovery again on ACTION_DISCOVERY_FINISHED
which will continuously scan for devices every 12 seconds.
这篇关于如何在android上定期扫描蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!