0

我是java新手,目前正在做一个项目。我正在尝试熟悉蓝牙。到目前为止,我已经能够从应用程序中打开和关闭蓝牙。我遇到的问题是执行操作后屏幕不会刷新。例如,当我打开蓝牙时,它会亮起,但文本视图中的文本不会更新,直到我将手机翻过来。当我断开连接时也是如此。这是我的代码

package com.example.bluetooth;

 import android.os.Bundle;
 import android.app.Activity;
 import android.bluetooth.BluetoothAdapter;
 import android.view.Menu;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;


 public class MainActivity extends Activity {


BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
Button button;
TextView update;
CharSequence enabled = "Bluetooth Enabled";
CharSequence disabled = "Bluetooth disabled";
CharSequence connect = "Connect";
CharSequence disconnect = "Disconnect";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setUp();
    action();
    }

private void action() {
    if (button.getText() == connect) {
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                btAdapter.enable();

            }
        });
    }

    if (button.getText() == disconnect) {
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                btAdapter.disable();

            }
        });
    }

}

private void setUp() {
    button = (Button) findViewById(R.id.button1);
    update = (TextView) findViewById(R.id.textView1);



    button = (Button) findViewById(R.id.button1);
    if (btAdapter.isEnabled()) {

        update.setText("Bluetooth is enabled");
        button.setText(disconnect);

    }

    else {

        update.setText("Bluetooth is disabled");
        button.setText(connect);


    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

任何帮助将不胜感激。谢谢你。

4

1 回答 1

1

我看错了两件事:

  1. 您只调用一次负责更新 TextView ( setIp()) 的方法,因为onCreate()每次创建 Activity 时只调用一次。由于打开/关闭适配器是在 UI Thread 之外完成的,因此您必须制作BroadcastReceiver并监听BluetoothAdapter.ACTION_STATE_CHANGED然后相应地更新。
  2. 您正在将 CharSequences 与==. 您应该改为使用字符串,然后使用.equals(),因为这将确保进行深度比较。唯一需要更改的是类型声明是.toString()getText(). 例如button.getText().toString()

编辑:一个简单(但混乱)的解决方案可以动态地制作接收器并注册它。现在,请记住,您只需要调用setUp()一次,就像以前一样。类似下面的代码可能会起作用。

public class MainActivity extends Activity {


  BluetoothAdapter btAdapter;
  Button button;
  TextView update;
  String enabled = "Bluetooth Enabled";
  String disabled = "Bluetooth disabled";
  String connect = "Connect";
  String disconnect = "Disconnect";
  BroadcastReceiver receiver;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    setUp();
    action();
  }

  @Override
  public void onPause()
  {
    try{
      unregisterReceiver (receiver);
    }
    catch (NullPointerException e)
    {
      e.printStackTrace();
    }
    catch (IllegalStateException ie)
    {
      ie.printStackTrace();
    }
    super.onPause();
  }

  private void action() {
  button.setOnClickListener(new View.OnClickListener (){

    @Override
    public void onClick(View v) {
        if (btAdapter.isEnabled())
            btAdapter.disable();
        else
            btAdapter.enable();

    }

  });

 }

  private void setButtonText ()
  {
    if (btAdapter.isEnabled()) {       
      update.setText("Bluetooth is enabled");
      button.setText(disconnect);
    }

    else {

      update.setText("Bluetooth is disabled");
      button.setText(connect);
    }
  }
  private void setUp() {
    button = (Button) findViewById(R.id.button1);
    update = (TextView) findViewById(R.id.textView1);
    setButtonText();

    receiver = new BroadcastReceiver(){

      @Override
      public void onReceive(Context context, Intent intent) {
        setButtonText();

      }

    };

    IntentFilter filter = new IntentFilter (BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver (receiver, filter);

  }
}
于 2013-02-18T00:06:22.963 回答