我是java新手,目前正在开发一个项目。我想要熟悉蓝牙。到目前为止,我已经能够在应用程序内打开和关闭蓝牙。我遇到的问题是,在执行操作后屏幕不会刷新。例如,当我打开蓝牙时,它会亮起,但是在我翻转手机之前,textview中的文字才会更新。当我断开连接时也一样。这是我的代码
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;
}
}
任何帮助都将不胜感激。谢谢。
答案 0 :(得分:1)
我看错了两件事:
setIp()
)的方法一次,因为onCreate()
仅在每次创建活动时调用一次。转为adapter on/off, is done outside the UI Thread后,您必须制作BroadcastReceiver
并收听BluetoothAdapter.ACTION_STATE_CHANGED
,然后相应地更新。 ==
进行比较。您应该使用字符串,然后使用.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);
}
}