私はJavaを初めて使用し、現在プロジェクトに取り組んでいます。Bluetoothに慣れようとしています。これまでのところ、アプリ内からBluetoothのオンとオフを切り替えることができました。私が抱えている問題は、アクションが実行された後、画面が更新されないことです。たとえば、Bluetoothをオンにするとオンになりますが、電話を裏返すまでテキストビューのテキストは更新されません。切断したときも同じです。これが私のコードです
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;
}
}
どんな助けでもいただければ幸いです。ありがとうございました。