0

the app keeps crashing due to something with the setadapter function My end goal is to have a program that connects to bluetooth and displays a list view that is clickable of bluetooth devices that are paired. I worked through many tutorials and forums and could not get it to work. Any help in the right direction would be greatly appreciated. Thank you //My Main code is:

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ListFragment;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.recyclerview.extensions.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class MainActivity extends AppCompatActivity {
public static int Request_Bluetooth = 1;
int bt = 1;
public ArrayList<BluetoothDevice> btDevices = new ArrayList<>();
ListView newDevices;
TextView tvDeviceName;
BluetoothAdapter Bluetooth;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);
    Bluetooth = BluetoothAdapter.getDefaultAdapter();
    newDevices = (ListView) findViewById(R.id.newDevices);
    tvDeviceName = (TextView) findViewById(R.id.tvDeviceName);
    btDevices = new ArrayList<>();

    if(Bluetooth == null){
        new AlertDialog.Builder(this)
                .setTitle("Bluetooth")
                .setMessage("This device does not support Bluetooth")
                .setPositiveButton("Main Menu",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                System.exit(0);
                            }
                        })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }
    if (!Bluetooth.isEnabled()){
        Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enable, Request_Bluetooth);

    }
    Intent discoverable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,1000);
    startActivity(discoverable);

    Set<BluetoothDevice> pairedDevices = Bluetooth.getBondedDevices();
    ArrayAdapter<String> s = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, R.id.newDevices);
    for(BluetoothDevice bt : pairedDevices) {
        s.add(bt.getName());
        //Used to check that the devices are being captured 
       // tvDeviceName.append("\n Device:" + bt.getName() + ", " +bt);


    }
    newDevices.setAdapter(s);


}


}

Please help

Umer Farooq
  • 762
  • 1
  • 8
  • 17
  • Welcome to Stack Overflow! It would be very nice if you could tell us more about the error you're receiving, as well as the LogCat. As this question currently stands, it is subject to being put on hold for lacking a clear problem statement. – gparyani May 04 '18 at 02:39
  • For this you'll need to create custom adapter for listview . See https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – Umer Farooq May 04 '18 at 03:18
  • add stacktrace with crash – Vygintas B May 04 '18 at 05:16
  • when you say custom adapter for listview are you referring to having a separate .java script that the listview calls to? – Java_noober May 08 '18 at 00:36

0 Answers0