paired device list view in a spinner.

Thread Starter

Nik@

Joined Apr 1, 2021
21
Hi all Happy new year full of happiness, health and success for all.
i am trying to pass a list view in a spinner. The problem i am facing is that the list shown as should but when i click the BT device for connection nothing happens. I am posting the code below if someone has an idea i would be grateful. If further info needed please ask.

Mod: added Code type.E

C-like:
public class MainActivity extends Activity {


    final static int REQUEST_ENABLE_BT = 1;

    BluetoothAdapter btAdapter;

    BtHandler btHandler;

    Set<BluetoothDevice> pairedDevices;

    static String btCurrentDeviceName;

    private Spinner btDeviceSpinner;

    private static ArrayAdapter<String> adapter;

    ArrayList<String> listViewItems = new ArrayList<>();

    private EditText textToSend;

    Button sendButton;


    [USER=54416]@override[/USER]

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listViewItems);

        btDeviceSpinner = findViewById(R.id.btDeviceSpinner);

        btDeviceSpinner.setAdapter(adapter);


        textToSend = findViewById(R.id.textToSend);

        sendButton = findViewById(R.id.sendButton);

        btAdapter = BluetoothAdapter.getDefaultAdapter();


        if (btAdapter != null) {

            if (!btAdapter.isEnabled()) {

                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

            }

            pairedDevices = GetPairedDevices(btAdapter);

        }


        sendButton.setOnClickListener(v -> {

            if (MainActivity.btCurrentDeviceName.equals("")){

                return;

            }

            sendTextViaBT();

            writeData();

        });


        btDeviceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            [USER=54416]@override[/USER]

            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                btCurrentDeviceName = String.valueOf(btDeviceSpinner.getSelectedItem());

                Log.d("MainActivity", "DeviceInfo : " + btCurrentDeviceName);

            }


            [USER=54416]@override[/USER]

            public void onNothingSelected(AdapterView<?> parentView) {

                // your code here

                Log.d("MainActivity", "Nothing selected.");

            }

        });

    }
 
Last edited by a moderator:

Ya’akov

Joined Jan 27, 2019
9,246
You should use CODE tags to make reading your code easier. You can find a nice form in the ... menu in the menu bar to make setting the attributes easier.

1640966107335.png
 
Top