Bluetooth on Android: Debuging startDiscovery()

泪湿孤枕 提交于 2019-12-13 01:25:44

问题


I'm working on an app that searches for discoverable devices and displays them as buttons. When calling startDiscovery() I would say it works 30% of the time, based on the way I'm currently debugging it, with the BroadcastReceiver and ACTION_DISCOVERY_FINISHED.

I'm also using isDiscovering() to test if the startDiscovery() function is called but it returns false.

Is there a way to know if startDiscovery() is called successfully? And can you identify something in my code that would make it not fail?

Obs.: I have both BLUETOOTH AND BLUETOOTH_ADMIN permissions.

Here is my code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);

        mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                String Address;
                // 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);
                    Address = device.getAddress();
                    System.out.println("Found Address: " + Address );  //DEBUG
                            //Do something with Address
                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    System.out.println("Discovery finished");
                }
            }
        };

        // Register the BroadcastReceiver
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mReceiver, filter);    

        MainActivity.mBluetoothAdapter.startDiscovery();

        if (MainActivity.mBluetoothAdapter.isDiscovering()) {
            System.out.println("Discovering...");  //DEBUG
        }

}

Although I have a few discoverable devices available, none of them trigger onReceive() with ACTION_FOUND

UPDATE: I went to "Scan" under Bluetooth Settings while the app was running and I could not scan for new devices. I disabled/enabled Bluetooth and returned to the app and the problem was resolved. I don't know if that indicates that the adapter is busy or halted somehow.


回答1:


I confirm this issue.

On some telephones you just need to disable/active BT. You can doit programatically with

mBluetoothAdapter.disable(); 
mBluetoothAdapter.enable();

On some telephones its not enough ( Samsung S5 ). To detect it, I use timer, and if on end of timeout the change of BT broadcast state (BluetoothAdapter.ACTION_DISCOVERY_STARTED or BluetoothAdapter.ACTION_DISCOVERY_FINISHED ) wasnt received => its sign that BT is not working. Actually I show dialog which propose to user reboot the telephone.



来源:https://stackoverflow.com/questions/22921632/bluetooth-on-android-debuging-startdiscovery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!