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();
}
});
}
}