简体   繁体   English

除了使用BroadcastReceiver,还有其他发现蓝牙的方法吗?

[英]Is there any other way to discover Bluetooth apart from using BroadcastReceiver?

I have created a BroadcastReceiver for BluetoothDevice.ACTION_FOUND in a Service to scan and Log the Bluetooth Device available. 我已经在服务中为BluetoothDevice.ACTION_FOUND创建了BroadcastReceiver,以扫描和记录可用的Bluetooth设备。 Part of this Service is to keep on checking every 30 seconds if the any of the previously found bluetooth devices is still available or not. 该服务的一部分是每30秒检查一次以前找到的蓝牙设备是否仍然可用。 Currently it throws me an error for Leaked IntendReceiver, I can fix that error, however I am just not convinced this is the right way to do this. 目前,它为我的Leaked IntendReceiver引发了一个错误,我可以修复该错误,但是我只是不确信这是正确的方法。 I am creating a new thread to handle bluetooth scanning, creating a while loop which runs every 30 seconds and inside that loop registering the BroadcastReceiver, putting thread on sleep, by the time sleep time is over the onReceive gives me results of the current scan, then I unregister BroadcastReceiver and repeat the loop. 我正在创建一个新线程来处理蓝牙扫描,创建了一个每30秒运行一次的while循环,并在该循环内注册了BroadcastReceiver,使线程进入睡眠状态,直到睡眠时间超过onReceive时,我才能得到当前扫描的结果,然后我取消注册BroadcastReceiver并重复循环。

I am unregistering the BroadcastReceiver after every single completion of while loop because the next scan gives me list of currently available devices and then I compare that with previous scan's data. 我在while循环的每个完成之后都注销了BroadcastReceiver,因为下一次扫描会向我提供当前可用设备的列表,然后将其与上一次扫描的数据进行比较。

It is fulfilling my requirement however I have a strong feeling that its not the correct design. 它可以满足我的要求,但是我强烈感觉到它不是正确的设计。 Could you please advice me on an alternate approach? 您能给我建议另一种方法吗? Thanks.. 谢谢..

Below is the relevant code from the Service- 以下是来自服务的相关代码-

 class ScanBT extends Thread 
{
     static final long DELAYBT = 30000;

        @Override       
        public void run()
          {

            isBTRunning = true;
            Looper.prepare(); 
            BluetoothAdapter mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();

            try { 
                Log.d(TAG, "BT Scanning started");

                while(isBTRunning)
                { 

                if (!mBluetoothAdapter.isEnabled())
                {
                        mBluetoothAdapter.enable();                     
                        Thread.sleep(15000);                    
                }

                IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                registerReceiver(mReceiver, filter);

                mBluetoothAdapter.startDiscovery();
                Log.d(TAG,"Inside while loop for BT");
                Thread.sleep(DELAYBT);
                unregisterReceiver(mReceiver);
             }
                Looper.loop();
            }
            catch (InterruptedException e) {
                Log.d(TAG, "BT Scanning stopped");
                    Looper.myLooper().quit();
            }

          }     
}

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                        BTDevice =  device.getName(); 
                BTAddress = device.getAddress();
                Log.d(TAG,"BT Found name: " + BTDevice);
                Log.d(TAG,"BT Found address: " + BTAddress);
                        //Code to compare with previous scan results
            }
        }
    };

Figured it out. 弄清楚了。 There is no other way to achieve this. 没有其他方法可以实现这一目标。 To keep the performance under control, I am now registering the receiver only once and then starting the discovery inside a loop with a sleep interval of 60 seconds. 为了控制性能,我现在只注册一次接收器,然后在睡眠间隔为60秒的循环内启动发现。

暂无
暂无

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

相关问题 除了意图之外,还有其他方法可以跨Android Studio中的活动发送数据 - Apart from intent is there any other way to send data across activities in android studio 使用Xamarin使用蓝牙发现设备 - Discover devices using bluetooth with xamarin 从BroadcastReceiver返回值(蓝牙) - Return values from BroadcastReceiver (Bluetooth) Android 6.0蓝牙程序无法发现任何可用的蓝牙设备 - Android 6.0 Bluetooth Program Cannot Discover Any Available Bluetooth Devices 使用蓝牙/ Wi-Fi Direct / BLE API或任何其他方式将ios设备与android设备进行通信 - Communicate ios device with android device using bluetooth/wi-fi direct/BLE api or any other way 如何将扫描响应与 Android.Bluetooth.LE (C#) 中的所有其他广告类型区分开来 - How to tell apart Scan-Response from all other Advertisement-Types in Android.Bluetooth.LE (C#) 三星 S7 没有; 发现任何蓝牙设备 - Samsung S7 doesn't; discover any Bluetooth devices 当在gridview中单击除前三个图像以外的任何其他图像时,应用程序突然关闭。 - When clicked on any other images apart from first three in the gridview, app abruptly shuts down. 除了Listview外,还有其他替代方法可以在android中显示MySQL服务器数据吗? - Any other alternatives to displaying MySQL server data in android apart from Listview? 除开发者网站之外的任何其他官方一站式Android开发指南来源? - Any other official one-stop source for android development guidelines apart from developer site?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM