Android Bluetooth Tutorial
MSBTE DIPLOMA SEMESTER 6- Android Development
Hello !! So today I have come up with a new topic of Android Studio code/program of java android bluetooth example app where we will code for java android bluetooth connection with on/off button facility. We will build an android application to turn on and off the bluetooth programatically for our android device using own customized android application in java.
Here is a step by step tutorial for Android Bluetooth permission using on/off toggle switch in Java.
I have attached a XML, Java and Manifest file as well. There are many other Android Studio, C++, etc related programs on our blog do read them as well !! (links below)
XML code for Bluetooth on/off:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
/>
</LinearLayout>
JAVA code for Bluetooth on/off:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
ToggleButton toggleButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting toggle button and textView from activity_main
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
textView = (TextView) findViewById(R.id.textView);
// Put listener on toggle button
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked) {
textView.setText("Bluetooth is ON");
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.enable();
} else {
textView.setText("Bluetooth is OFF");
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.disable();
}
}
});
// For initial setting
if (toggleButton.isChecked()) {
textView.setText("Bluetooth is ON");
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.enable();
} else {
textView.setText("Bluetooth is OFF");
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.disable();
}
}
}
Manifest file of Bluetooth on/off:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
For many other Language Programs of microprocessor 8086, C++ , Data Structures , etc and MSBTE diploma,BCA,MCA and engineering related concepts visit our BLOG
No comments:
Post a Comment