如何定期扫描在Android的蓝牙设备

如何定期扫描在Android的蓝牙设备

本文介绍了如何定期扫描在Android的蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

你好,这听起来是一个愚蠢的question.But我无法找到任何答案,这一点,因此在这里发帖。

Hi this may sound as a stupid question.But I was unable to find any answers for this, thus posting here.

我建一个室内应用程序,它会不断扫描位于像一个商场或library.As我将在与Android手机在我手里商场一处不同位置的蓝牙适配器,我应该能够得到最近的加密狗其中我可以连接到(愚蠢的想法,但我想做些别的事情与此)。对于这一点,我应该能够为蓝牙设备的连续扫描。

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的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:23