0

I want to learn how to work with bluetooth. I can't start the simplest application. All the time, bluetooth output is not supported. How to fix that you could turn on bluetooth?

public class MainActivity extends AppCompatActivity {


    private static final int REQUEST_ENABLE_BT = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button buttonOn = (Button) findViewById(R.id.btnOn);
        Button buttonOff = (Button) findViewById(R.id.btnOff);
        BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();

        //int  REQUEST_ENABLE_BT = 1;
        buttonOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(bAdapter == null) {
                    Toast.makeText(getApplicationContext(),"Bluetooth not supported", Toast.LENGTH_LONG).show();
                }
                else{
                    if(!bAdapter.isEnabled()){
                        Intent enableBt =  new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableBt, REQUEST_ENABLE_BT);
                        Toast.makeText(getApplicationContext(),"Bluetooth adapter On", Toast.LENGTH_LONG).show();
                    }
                }
            }
        });

        buttonOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(),"Bluetooth adapter Off", Toast.LENGTH_LONG).show();
                bAdapter.disable();
            }
        });
    }
}
  • Is Bluetooth disabled in the settings? If your device doesn't support Bluetooth, you can't use Bluetooth on that device. Sorry. – cocomac Dec 14 '21 at 17:05
  • Hi Welcome to stack overflow please read [how-to-ask?](http://stackoverflow.com/help/how-to-ask). Include just enough code to allow others to reproduce the problem. For help with this, read [How to create a Minimal, Complete, and Verifiable example.](https://stackoverflow.com/help/mcve) – AgentP Dec 14 '21 at 17:10

1 Answers1

0

If the device does not support bluetooth BluetoothAdapter will be null. enter image description here

And it seems you need to request permissions and you might need new permissions for Android 12.

        requestPermissions(
            arrayOf(
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.BLUETOOTH_SCAN,
                Manifest.permission.BLUETOOTH_CONNECT
            ),
            BL_REQUEST_CODE
        )

Here is the answer that might help: Android 12 New Bluetooth Permissions

Inliner
  • 1,061
  • 7
  • 10
  • I added the permission in manifest.xml. BLUETOOTH_ADMIN, BLUETOOTH. – Евгений Смирнов Dec 14 '21 at 18:38
  • I might need more information to help you, what device are you using? is it an emulator or a phone? and what version of Android is there? – Inliner Dec 15 '21 at 08:22
  • Hello, Inliner. I am testing all programs in the emulator, version of android 8.0 Oreo, no errors. You need to try to test it on a real device. And another question about the variable REQUEST_ENABLE_BT, did I define it correctly? – Евгений Смирнов Dec 15 '21 at 09:13
  • Евгений, sorry for the delay, I just checked on my Moto X Play with Android 7 and Cube T10 with Android 6, and your code is working. You only need BLUETOOTH permission in manifest to turn bluetooth ON and BLUETOOTH_ADMIN permission to turn it OFF. For Android 12 you also need to request BLUETOOTH_CONNECT permission and not just add it to manifest. Here it is explained more thorougly. https://developer.android.com/guide/topics/connectivity/bluetooth/permissions – Inliner Dec 19 '21 at 14:22
  • As for your error it might be device specific, so at least let us know what device you are using – Inliner Dec 19 '21 at 14:23