在android

时间:2015-09-25 11:11:53

标签: android bluetooth broadcastreceiver progressdialog

我的应用程序应该通过蓝牙检查周围是否有某些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 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中的代码:

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();
                }
        }
    }

我有三个问题。

首先,我遇到问题&#34;服务器未找到&#34;信息。甚至当arduino在附近时也会显示出来。我真的不知道将该行放在我的代码中的哪个位置。我试着把它放在不同的代码中,但是我无法得到所需要的东西。

其次,发现服务器的消息显示两次。我在广播接收器里面吐司,而不是在配对设备内吐司(在这之后我把1111识别出哪个吐司显示)。我不明白第二次执行吐司的代码部分是什么。

我也遇到了进度对话问题。我无法从屏幕上删除对话框,即使找到服务器,它仍然存在。我把dialog.dismiss()放在发现完成的块和找到的设备中,但它仍然在屏幕上。

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

每次收到意图时都会设置变量。

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

这不应该放在OnReceived()中,但必须设置为BroadCastReceiver的私有变量。 这将取消进度并且“服务器未找到”吐司,因为当您收到ACTION_DISCOVERY_FINISHED时,b总是假的,并且您尝试解除未显示的progressDialog。

关于多次调用“Server found”调用两次,请检查当您不再需要时取消注册BroadcastReceiver。

希望有所帮助。

编辑: 设置你的broadcastReceiver,应该在ACTION_DISCOVERY_STARTED中创建progressDialog:

    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();
            }
        }
    }
}