Итак, я сделал список, чтобы увидеть список обнаруженных устройств Bluetooth, когда я нажимаю кнопку. Проблема появляется, когда я нажимаю кнопку с startDiscovery () в нем. ListView ничего не перечисляет.
Android Манифест:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="serenaApp.serenaapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ProfileScreen"></activity>
<activity
android:name=".LoginScreen"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".AjustesConexionSensores">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Макет:
<Button
android:id="@+id/botDesc"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="266dp"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:background="@color/naranjaApp"
android:text="Descubrir" />
<ListView
android:id="@+id/scanLv"
android:layout_width="match_parent"
android:layout_height="277dp" />
AjustesConexionesSensores:
Даже если я добавлю startDiscovery () в мой mBotDescubrir и ниже добавлю устройства в список, он ничего не отобразит и не обновит listView.
ListView scanLv
ArrayList<String> devsAL = new ArrayList<>();
ArrayAdapter<String> devsAAd;
Button mBotDescubrir;
BluetoothAdapter mBlueAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ajustes_conexion_sensores);
mBotDescubrir = findViewById(R.id.botDesc);
scanLv = findViewById(R.id.scanLv);
mBlueAdapter = BluetoothAdapter.getDefaultAdapter();
mBotDescubrir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBlueAdapter.startDiscovery();
}
});
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(bReceiver, intentFilter);
devsAAd = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,devsAL);
scanLv.setAdapter(devsAAd);
}
BroadcastReceiver bReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
devsAL.add(device.getName());
devsAAd.notifyDataSetChanged();
}
}
};
Я действительно застрял, так что помощь очень ценится