Congratulations - C# Corner Q4, 2022 MVPs Announced
Why Join
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
TECHNOLOGIES
ANSWERS
LEARN
NEWS
BLOGS
VIDEOS
INTERVIEW PREP
BOOKS
EVENTS
Training
Live
JOBS
MORE
CAREER
MEMBERS
Turning on and Turning Off Bluetooth By Programming in Android
Abhijeet Singh
Apr 01, 2020
23.9k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
Print
Other Artcile
Here you will learn how to turn on and turn off Bluetooth by programming in Android.
bluetooth.rar
Procedure
Start Eclipse IDE.
Create a new project.
Create a MainActivity.java file.
Create an activity_main.xml for layout design.
Add three buttons and one TextView fields in the XML layout.
Then look up buttons by their ids in MainActivity.java file.
Use BluetoothAdapter like this:
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.
getDefaultAdapter();
Set the functionality of Bluetooth on buttons.
On the first button enable Bluetooth like this:
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
On the second button enable Bluetooth Duration like this:
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
On third button disable the Bluetooth like this:
mBluetoothAdapter.disable();
Code is given below
MainActivity.java
package
com.example.bluetooth;
import
android.os.Bundle;
import
android.app.Activity;
import
android.view.Menu;
import
android.app.Activity;
import
android.bluetooth.BluetoothAdapter;
import
android.content.Context;
import
android.content.Intent;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity {
private
static
final
int
REQUEST_ENABLE_BT =
0
;
private
static
final
int
REQUEST_DISCOVERABLE_BT =
0
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final
TextView out = (TextView) findViewById(R.id.out);
final
Button button1 = (Button) findViewById(R.id.button1);
final
Button button2 = (Button) findViewById(R.id.button2);
final
Button button3 = (Button) findViewById(R.id.button3);
final
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if
(mBluetoothAdapter ==
null
) {
out.append(
"device not supported"
);
}
button1.setOnClickListener(
new
View.OnClickListener() {
public
void
onClick(View v) {
if
(!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent =
new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
});
button2.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View arg0) {
if
(!mBluetoothAdapter.isDiscovering()) {
Context context = getApplicationContext();
CharSequence text =
"MAKING YOUR DEVICE DISCOVERABLE"
;
int
duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent enableBtIntent =
new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);
}
}
});
button3.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View arg0) {
mBluetoothAdapter.disable();
Context context = getApplicationContext();
CharSequence text =
"TURNING_OFF BLUETOOTH"
;
int
duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text,
15
);
toast.show();
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
}
activity_main.xml
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<
TextView
android:text
=
""
android:id
=
"@+id/out"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
>
</
TextView
>
<
Button
android:id
=
"@+id/button1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentLeft
=
"true"
android:layout_alignParentTop
=
"true"
android:layout_marginLeft
=
"30dp"
android:layout_marginTop
=
"49dp"
android:text
=
"TURN_ON"
/>
<
Button
android:id
=
"@+id/button2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/button1"
android:layout_below
=
"@+id/button1"
android:layout_marginTop
=
"27dp"
android:text
=
"DISCOVERABLE"
/>
<
Button
android:id
=
"@+id/button3"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/button2"
android:layout_below
=
"@+id/button2"
android:layout_marginTop
=
"28dp"
android:text
=
"TURN_OFF"
/>
</
RelativeLayout
>
Output
The following shows the initial output:
By clicking on Button1:
By clicking on Button2:
By clicking on Button3:
Android
Android Bluetooth programming
Android BluetoothAdapter
BluetoothAdapter in Android
Recommended Ebook
Printing in C# Made Easy
Download Now!
Similar Articles