Bluetooth_OnOff
Bluetooth_OnOff
myapplication;
import android.app.Activity;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
BluetoothAdapter bluetooth;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetooth = BluetoothAdapter.getDefaultAdapter();
list = findViewById(R.id.listView);
findViewById(R.id.button).setOnClickListener(v -> {
if (!bluetooth.isEnabled()) {
startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
Toast.makeText(this, "Bluetooth ON", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.button2).setOnClickListener(v -> {
startActivity(new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE));
});
findViewById(R.id.button3).setOnClickListener(v -> {
bluetooth.disable();
Toast.makeText(this, "Bluetooth OFF", Toast.LENGTH_SHORT).show();
});
findViewById(R.id.button4).setOnClickListener(v -> {
ArrayList<String> devices = new ArrayList<>();
for (BluetoothDevice d : bluetooth.getBondedDevices()) {
devices.add(d.getName());
}
list.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, devices));
});
}
}
AndroidManifest.xml