与搜索可用的设备,通过蓝牙,在android系统问题 [英] Problems with searching for available device, via bluetooth, in android

查看:214
本文介绍了与搜索可用的设备,通过蓝牙,在android系统问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用应该只是检查,通过蓝牙,如果有一定的左右Arduino的服务器,和烤面包正确的消息。

My app should just check, via bluetooth, if there is certain Arduino server around, and toast proper message.

这是code,当用户presses按钮搜索服务器:

This is the code when user presses button to search for server:

 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 1111", Toast.LENGTH_SHORT);
                        break;
                    }
                }
            }
            IntentFilter filter = new IntentFilter();
            filter.addAction(BluetoothDevice.ACTION_FOUND);
            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
            filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            registerReceiver(discoveryResult, filter);
            mBluetoothAdapter.startDiscovery();

            }

和里面的BroadcastReceiver code:

And code inside BroadcastReceiver:

public void onReceive(Context context, Intent intent) {
        Boolean b = false;
        String action = intent.getAction();
        ProgressDialog dialog = new ProgressDialog(ConnectActivity.this);
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
            dialog.setMessage("Searching for Arduino server...");
            dialog.show();
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
            dialog.dismiss();
            if (!b)
                Toast.makeText(ConnectActivity.this, "Server not found", Toast.LENGTH_SHORT).show();
        }
        else if (BluetoothDevice.ACTION_FOUND.equals(action)){
                String deviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
                if (deviceName.equals("ARD_SPP")) {
                   Toast.makeText(ConnectActivity.this, "Arduino server found, please sign up 2222", Toast.LENGTH_SHORT).show();
                   sendButton.setVisibility(View.VISIBLE);
                   openButton.setVisibility(View.GONE);
                   b = true;
                   dialog.dismiss();
                }
        }
    }

我有三个问题与此有关。

I've got three problems with this.

首先,我有找不到服务器消息的问题。当Arduino是围绕它甚至显示。我真的没有知道从哪里把那行,我code。我试图把它在code不同lilnes,但我无法得到需要什么。

First, I have the problem with "Server not found" message. It is shown even when arduino is around. I really don't have idea where to put that line in my code. I tried to put it in different lilnes of code, but I couldn't get what is required.

其次,该服务器是发现信息中示出两次。我MEAM敬酒广播接收器里面,敬酒不吃吃里面pairedDevices(这个以后我把1111识别显示其土司)。我不明白什么是那里的吐司第二次执行code的一部分。

Second, message that server is found is shown two times. I meam toast inside broadcast receiver, not toast inside pairedDevices(after this one I put 1111 to recognize which toast is shown). I don't understand what is the part of the code where that toast is executed for the second time.

和我还与进度对话框的问题。我无法从屏幕上删除对话框,有人甚至当服务器发现仍然存在。我把dialog.dismiss()无论是在发现成品块设备发现,但它仍然是在屏幕上。

And I also had problems with progress dialog. I couldn't remove dialog from the screen, it was still there even when the server is found. I put dialog.dismiss() both in discovery finished block and device found, but it is still on the screen.

有谁请帮助我呢?

推荐答案

您在每次你收到意图时候你的变量。

You set your variable every time you received an intent.

Boolean b = false;
ProgressDialog dialog = new ProgressDialog(ConnectActivity.this);

这不应该被放在OnReceived(),但必须设置好​​的为您的广播接收器的私有变量。
这将这个进度不排除与找不到服务器敬酒,因为当您收到ACTION_DISCOVERY_FINISHED b为总是假的,并尝试驳回progressDialog不显示。

This should not be put in the OnReceived() but must be setted as private variable of your BroadCastReceiver. This will this the progress not dismiss and the "Server not found" toast, since b is always false when you received ACTION_DISCOVERY_FINISHED, and you try to dismiss a progressDialog not showed.

有关的服务器发现了多个呼叫呼叫两次,检查是否注销你的广播接收器,当你不再需要。

Concerning the multiple call of "Server found" call twice, check if you unregister your BroadcastReceiver when you don't need anymore.

希望帮助。

编辑:
设置你的广播接收器一样的是,progressDialog应该在ACTION_DISCOVERY_STARTED创建:

EDIT : Set your broadcastReceiver like that, the progressDialog should be create in the ACTION_DISCOVERY_STARTED :

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    private boolean b = false;
    private ProgressDialog dialog ;
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
            dialog = new ProgressDialog(ConnectActivity.this);
            dialog.setMessage("Searching for Arduino server...");
            dialog.show();
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
            dialog.dismiss();
            if (!b)
                Toast.makeText(ConnectActivity.this, "Server not found", Toast.LENGTH_SHORT).show();
        }
        else if (BluetoothDevice.ACTION_FOUND.equals(action)){
            String deviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
            if (deviceName.equals("ARD_SPP")) {
                Toast.makeText(ConnectActivity.this, "Arduino server found, please sign up 2222", Toast.LENGTH_SHORT).show();
                sendButton.setVisibility(View.VISIBLE);
                openButton.setVisibility(View.GONE);
                b = true;
                dialog.dismiss();
            }
        }
    }
}

这篇关于与搜索可用的设备,通过蓝牙,在android系统问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆