ListView 仅在我旋转 phone 时显示数据
ListView only show data when I rotate the phone
我开始关注一些关于将蓝牙模块与 Android 一起使用的阅读材料和示例,基本上我想制作一个测试应用程序,在我启动应用程序时显示绑定设备列表,我基本上了解所有我遵循的代码来制作我的测试应用程序,但我被卡住了,因为当我 运行 这个应用程序要求启用蓝牙并且什么都不显示时,应用程序只在我旋转 phone 时显示列表.我究竟做错了什么?感谢您的帮助
这是我在 java
中的代码
public class MainActivity extends AppCompatActivity {
ListView listViewDispositivos;
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
ArrayList<String> arrayListaConectados = new ArrayList<>();
ArrayAdapter<String> adaptador;
String infoMac, direccionMac;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listViewDispositivos = findViewById(R.id.listViewDispositivos);
if(bluetoothAdapter == null){
Toast.makeText(MainActivity.this, "Dispositivo no disponible", Toast.LENGTH_LONG).show();
finish();
}
else if(!bluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
else {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
adaptador = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, arrayListaConectados);
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
arrayListaConectados.add(device.getName() + "\n" + device.getAddress());
adaptador.notifyDataSetChanged();
}
} else {
Toast.makeText(MainActivity.this, "No se encontraron dispositivos", Toast.LENGTH_LONG).show();
}
listViewDispositivos.setAdapter(adaptador);
}
}
最初未启用蓝牙,您正在使用 Intent 请求启用显示对话框的蓝牙。这不会调用 else 条件,因为蓝牙已关闭。
因此您需要在 onActivityResult 中获取配对设备,它将在您启用蓝牙后立即调用。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
if (resultCode == RESULT_OK) {
//Bluetooth is enable get paired device list here
}else {
//Show Error
}
}
}
我开始关注一些关于将蓝牙模块与 Android 一起使用的阅读材料和示例,基本上我想制作一个测试应用程序,在我启动应用程序时显示绑定设备列表,我基本上了解所有我遵循的代码来制作我的测试应用程序,但我被卡住了,因为当我 运行 这个应用程序要求启用蓝牙并且什么都不显示时,应用程序只在我旋转 phone 时显示列表.我究竟做错了什么?感谢您的帮助
这是我在 java
中的代码public class MainActivity extends AppCompatActivity {
ListView listViewDispositivos;
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
ArrayList<String> arrayListaConectados = new ArrayList<>();
ArrayAdapter<String> adaptador;
String infoMac, direccionMac;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listViewDispositivos = findViewById(R.id.listViewDispositivos);
if(bluetoothAdapter == null){
Toast.makeText(MainActivity.this, "Dispositivo no disponible", Toast.LENGTH_LONG).show();
finish();
}
else if(!bluetoothAdapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
else {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
adaptador = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, arrayListaConectados);
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
arrayListaConectados.add(device.getName() + "\n" + device.getAddress());
adaptador.notifyDataSetChanged();
}
} else {
Toast.makeText(MainActivity.this, "No se encontraron dispositivos", Toast.LENGTH_LONG).show();
}
listViewDispositivos.setAdapter(adaptador);
}
}
最初未启用蓝牙,您正在使用 Intent 请求启用显示对话框的蓝牙。这不会调用 else 条件,因为蓝牙已关闭。
因此您需要在 onActivityResult 中获取配对设备,它将在您启用蓝牙后立即调用。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
if (resultCode == RESULT_OK) {
//Bluetooth is enable get paired device list here
}else {
//Show Error
}
}
}