Home » Android » android – How do I update my List view to list discovered bluetooth devices?-Exceptionshub

android – How do I update my List view to list discovered bluetooth devices?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

So I have made a list view to list the discovered bluetooth devices for when I click a button. The problem appears when I click the button with startDiscovery() in it. The listView doesn’t list anything.

Android Manifest:

<?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>

Layout:

<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:

Even if I put startDiscovery() in my mBotDescubrir and below add the devices to the list, it won’t list anything and it won’t update the 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();
        }
    }
};

I’m really stuck so help is much appreciated

How to&Answers: