0% found this document useful (0 votes)
9 views3 pages

Manage Bluetooth in Android App

The document shows code for managing Bluetooth functionality in an Android app. It includes importing Bluetooth libraries, declaring buttons and views, checking the Bluetooth adapter status, turning Bluetooth on/off and enabling device discoverability through buttons' onClick listeners.

Uploaded by

CHALCHISA TAMIRU
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Manage Bluetooth in Android App

The document shows code for managing Bluetooth functionality in an Android app. It includes importing Bluetooth libraries, declaring buttons and views, checking the Bluetooth adapter status, turning Bluetooth on/off and enabling device discoverability through buttons' onClick listeners.

Uploaded by

CHALCHISA TAMIRU
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

package [Link].

managebluetooth;
import [Link];

import [Link].BluetoothA2dp;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {


Button bluetooth_on_btn, bluetooth_off_btn, disc_on_btn;
TextView adapter_status_text;
ImageView bluetooth_status_image;
final BluetoothAdapter bluetoothAdapter =
[Link]();

@Override
public void startActivityForResult(Intent intent, int requestCode) {
[Link](intent, requestCode);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

bluetooth_on_btn = findViewById([Link].on_blt);
bluetooth_off_btn = findViewById([Link].off_blt);
disc_on_btn = findViewById([Link].disc_on);
bluetooth_status_image = findViewById([Link].bluetooth_status);
adapter_status_text = findViewById([Link].bluetooth_adapter_status);
if(bluetoothAdapter==null){
adapter_status_text.setText("Bluetooth Not Available!");
}
else{
adapter_status_text.setText("Bluetooth Available");
}
//cheeking whether the bluetooth is Enabled or disabled then showing
appropriate image.
if([Link]()){

bluetooth_status_image.setImageResource([Link].ic_baseline_bluetooth_24);
}
else {
bluetooth_status_image.setImageResource([Link].ic_baseline_bluetooth_disa
bled_24);
bluetooth_off_btn.setEnabled(false);
disc_on_btn.setEnabled(false);
[Link]([Link], "Please turn on the bluetooth,to
proceed..", Toast.LENGTH_LONG).show();
}
//turning on the bluetooth
bluetooth_on_btn.setOnClickListener(new [Link]()
{
@Override
public void onClick (View v){
if (bluetoothAdapter == null) {
[Link]([Link], "Sorry,the device does not
support bluetooth!", Toast.LENGTH_SHORT).show();
bluetooth_on_btn.setEnabled(false);
bluetooth_off_btn.setEnabled(false);
disc_on_btn.setEnabled(false);
} else {
if (![Link]()) {
startActivityForResult(new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1);
bluetooth_off_btn.setEnabled(true);
disc_on_btn.setEnabled(true);

bluetooth_status_image.setImageResource([Link].ic_baseline_bluetooth_24);
} else {
[Link]([Link], "Bluetooth is already turned
ON!", Toast.LENGTH_SHORT).show();
}
}
}
});

// turning off bluetooth disabling the buttons


bluetooth_off_btn.setOnClickListener(new [Link]() {
@Override
public void onClick(View v) {
if(bluetooth_off_btn.isEnabled()){
[Link]();
bluetooth_off_btn.setEnabled(false);
disc_on_btn.setEnabled(false);

[Link]([Link], "Turning OFF


bluetooth..", Toast.LENGTH_SHORT).show();

bluetooth_status_image.setImageResource([Link].ic_baseline_bluetooth_disa
bled_24);
}
}
});
//Enabling device bluetooth discoverability
disc_on_btn.setOnClickListener(new [Link]() {
@Override
public void onClick(View v) {
if(![Link]()){
[Link]([Link], "Cannot Enable the
Discoverability,please turn ON the bluetooth.", Toast.LENGTH_SHORT).show();
}
else{
Intent dis_intent=new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//use the following code,if you want your bluetooth be
visible for more than 120 seconds
//
dis_intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
startActivity(dis_intent);
}
}
});
}
}

You might also like