简体   繁体   English

在Android中使用BroadcastReceiver时无法获得理想的Toast消息

[英]Not able to get desirable toast message when using BroadcastReceiver in Android

My android app should recognize, via bluetooth, if there is appropriate arduino server around. 我的Android应用程序应该通过蓝牙识别周围是否有合适的arduino服务器。 If so, app will show toast messages depending of the presence of the desirable arduino server. 如果是这样,则应用程序将根据所需的arduino服务器的存在显示吐司消息。 If it is found it should taost message about that, and if it is not, another message. 如果找到它,则应该试听有关该消息的消息,如果找不到,则应试听另一条消息。

Here is my code when user presses button to search for arduino server: 这是我在用户按下按钮搜索arduino服务器时的代码:

public void onClick(View v) {

            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            if (pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    if (device.getName().equals("ARD_SPP")) {
                        sendButton.setVisibility(View.VISIBLE);
                        Toast.makeText(ConnectActivity.this, "Arduino server found, please sign up", Toast.LENGTH_SHORT);
                        b = true;
                        break;
                    }
                }
            }
            mBluetoothAdapter.startDiscovery();
            if (mBluetoothAdapter.startDiscovery()) {
                registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));
            }
            mBluetoothAdapter.cancelDiscovery();
            if (b == false)
                Toast.makeText(ConnectActivity.this, "Server not found", Toast.LENGTH_SHORT).show();
            }

And this is code inside BroadcastReceiver: 这是BroadcastReceiver中的代码:

BroadcastReceiver discoveryResult = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String deviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
        if (deviceName.equals("ARD_SPP")) {
            Toast.makeText(ConnectActivity.this, "Arduino server found, please sign up", Toast.LENGTH_SHORT).show();                
            b = true;
        }
    }

(I didn't put part of the code about bluetooth, to make it easier to read, consider that bluetooth is already on) (我没有放入有关蓝牙的部分代码,为了使其更易于阅读,请考虑蓝牙已启用)

The problem is that message that server is not found, is shown even if the server is around. 问题是即使服务器在附近,也会显示未找到服务器的消息。 So, at first, "server not found" is shown, and after that there is message that server is found. 因此,首先显示“找不到服务器”,然后显示消息,指出已找到服务器。 I don't know how to fix that. 我不知道该如何解决。

I tried with using boolean variable b. 我尝试使用布尔变量b。 I used condition with it in different places inside my code, inside onClick and BroadcastReceiver, but nothing gave appropriate result. 我在代码中的不同位置,onClick和BroadcastReceiver中的不同位置使用了condition,但没有任何结果能带来合适的结果。 Maybe I didn't use BroadcastReceiver in a good way... 也许我没有很好地使用BroadcastReceiver ...

Does anyone have idea how I can fix this ? 有谁知道我该如何解决? It lools like solution is quite simple but I don't get it. 它就像解决方案一样简单,但是我不明白。

The discovery process is asynchronous, so you have to wait until you get the results in the BroadcastReceiver you defined. 发现过程是异步的,因此您必须等到在定义的BroadcastReceiver中获得结果。 This will happen between the next 12 seconds (it should be pretty fast, but that's the window the BluetoothAdapter specifies for discover). 这将在接下来的12秒之间发生(应该很快,但这是BluetoothAdapter指定用于发现的窗口)。

So basically your concept error is to suppose that this is something synchronous, and code it that way. 因此,基本上,您的概念错误是假设这是同步的,并以此方式进行编码。

You should: 你应该:

1) Get rid of the mBluetoothAdapter.cancelDiscovery() as this is cancelling the ongoing discovery process. 1)删除mBluetoothAdapter.cancelDiscovery(),因为这将取消正在进行的发现过程。

2) Don't show the Server not found message when b is false (btw change the condition to if (!b)), as the b variable at that point could be just modified by pairedDevices loop, and as far as I understand, you don't want to show if the Arduino is paired but if it's around. 2)当b为假时(不要将条件更改为if(!b)),不要显示“服务器未找到”消息,因为此时的b变量可以由pairedDevices循环修改,据我所知,您不想显示Arduino是否配对,但周围是否存在。

3) To properly manage the messages that you get from the discovery process, you should have this in mind (from the official documentation http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html ): 3)为了正确管理从发现过程中获得的消息,您应该牢记这一点(从官方文档http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html ):

Register for ACTION_DISCOVERY_STARTED and ACTION_DISCOVERY_FINISHED intents to determine exactly when the discovery starts and completes. 注册ACTION_DISCOVERY_STARTED和ACTION_DISCOVERY_FINISHED意向,以准确确定发现何时开始和完成。 Register for ACTION_FOUND to be notified as remote Bluetooth devices are found. 注册ACTION_FOUND以在找到远程蓝牙设备时得到通知。

So basically if ACTION_FOUND is received, you should check if it's the Arduino and display the proper message. 因此,基本上,如果收到ACTION_FOUND,则应检查它是否是Arduino并显示正确的消息。 If during the whole process you end up receiving ACTION_DISCOVERY_FINISHED without finding it, then you can show the "Not Found" message. 如果在整个过程中您最终都收到ACTION_DISCOVERY_FINISHED而没有找到它,那么您可以显示“未找到”消息。

Always read the official documentation first to have a better understanding of what you're doing. 请务必先阅读官方文档,以更好地了解您的工作。

Hope it helps. 希望能帮助到你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM