我是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;
}
}
任何帮助将不胜感激。谢谢你。