0

I am trying to make a program that checks is a certain bluetooth headset a popup would be displayed above the screen just as the airpod connection screen on the iphone. Right now i am struggling to get the phone to recognise the connected bluetooth devices.

This is the MainActivity.java code:

package com.example.haylougt1;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private static Button btnTest;
    TextView txtStatus;
    TextView txtName;
    TextView txtMAC;

    private static final int REQUEST_ENABLE_BT = 0;
    private static final int REQUEST_DISCOVER_BT = 1;

    BluetoothAdapter blueAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // textfield to show status
        txtStatus = findViewById(R.id.txtStatus);
        txtName = findViewById(R.id.lblName);
        txtMAC = findViewById(R.id.lblMAC);

        // bluetooth adaptar
        blueAdapter = BluetoothAdapter.getDefaultAdapter();

        if (blueAdapter == null) {
            txtStatus.setText("Bluetooth is not available!");

        } else {
            txtStatus.setText("Bluetooth is available");
        }

        btnTest = (Button) findViewById(R.id.btnTest);
        btnTest.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (blueAdapter.isEnabled()) {
                            Set<BluetoothDevice> devices = blueAdapter.getBondedDevices();

                            if (devices.size() > 0) {
                                for (BluetoothDevice device : devices) {
                                    String deviceName = device.getName();
                                    String deviceMAC = device.getAddress();
                                    txtName.setText(deviceName);
                                    txtMAC.setText(deviceMAC);
                                }
                            }

                        }


                    }
                }
        );

    }


}

But when ever i start the app on my phone the only thing i get is the earbuds that were connected earlier but not connected now You can see that i am not connected to any bluetooth device but still it shows that i am connected

Giorgos Neokleous
  • 1,709
  • 1
  • 14
  • 25
abod sakka
  • 47
  • 9

0 Answers0