Q. Develope A Program To TRUN ON and OFF The Bluetooth, Write The Java File and Permission Tags
Q. Develope A Program To TRUN ON and OFF The Bluetooth, Write The Java File and Permission Tags
Develope a program to TRUN ON and OFF the bluetooth, Write the Java file and
permission tags.
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize BluetoothAdapter
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
finish();
if (!bluetoothAdapter.isEnabled()) {
startActivity(enableBtIntent);
} }
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.disable();
} } }
AndroidManifest.xml (Permissions)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetooth">
<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:roundIcon="@mipmap/ic_launcher_round"
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>