एंड्रॉइड में प्रोग्रामेटिक रूप से ब्लूटूथ अक्षम करें
यह ट्यूटोरियल दिखाता है कि BluetoothManager और BluetoothAdapter.disable() का उपयोग करके ब्लूटूथ कैसे बंद करें।
⚠️ महत्वपूर्ण चेतावनी
BluetoothAdapter.disable() और enable() डिप्रिकेटेड हैं और सामान्य ऐप्स के लिए काम नहीं करते। केवल सिस्टम ऐप्स या विशेष परमिशन वाले ऐप्स ब्लूटूथ स्टेट बदल सकते हैं। उपयोगकर्ता को सेटिंग्स में भेजना अनुशंसित है।
चरण 1: प्रोजेक्ट बनाएं
एंड्रॉइड स्टूडियो में नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="24dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/statusText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:padding="24dp"
android:background="#E3F2FD"
android:text="ब्लूटूथ स्थिति: जांच हो रही है..."
android:clickable="true"
android:focusable="true"/>
</LinearLayout>
चरण 3: मैनिफेस्ट परमिशन (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.bluetoothdisable">
<!-- लिगेसी परमिशन (Android 11 और नीचे के लिए) -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- Android 12+ के लिए -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<application ...>
<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>
चरण 4: एक्टिविटी कोड (src/MainActivity.java)
package com.example.bluetoothdisable;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
public class MainActivity extends AppCompatActivity {
private TextView statusText;
private BluetoothAdapter bluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
statusText = findViewById(R.id.statusText);
// ब्लूटूथ एडाप्टर प्राप्त करें
BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = manager.getAdapter();
updateStatusText();
// क्लिक पर ब्लूटूथ टॉगल करने का प्रयास
statusText.setOnClickListener(v -> attemptDisableBluetooth());
}
private void updateStatusText() {
if (bluetoothAdapter == null) {
statusText.setText("ब्लूटूथ हार्डवेयर नहीं मिला");
return;
}
String state = bluetoothAdapter.isEnabled() ? "सक्षम (ON)" : "अक्षम (OFF)";
statusText.setText("ब्लूटूथ: " + state + "\nटैप करके अक्षम करने का प्रयास करें");
}
private void attemptDisableBluetooth() {
if (bluetoothAdapter == null) {
Toast.makeText(this, "ब्लूटूथ हार्डवेयर नहीं मिला", Toast.LENGTH_SHORT).show();
return;
}
if (!bluetoothAdapter.isEnabled()) {
Toast.makeText(this, "ब्लूटूथ पहले से अक्षम है", Toast.LENGTH_SHORT).show();
return;
}
// Android 10+ के लिए: सेटिंग्स में भेजें (अनुशंसित)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
showSettingsRedirectDialog();
return;
}
// Android 9 और नीचे के लिए (डिप्रिकेटेड लेकिन काम कर सकता है)
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_ADMIN)
== PackageManager.PERMISSION_GRANTED) {
boolean success = bluetoothAdapter.disable();
Toast.makeText(this, success ? "ब्लूटूथ अक्षम करने का अनुरोध भेजा" : "अक्षम करने में विफल", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.BLUETOOTH_ADMIN}, 101);
}
// स्थिति अपडेट के लिए थोड़ा इंतजार करें
statusText.postDelayed(this::updateStatusText, 1000);
}
private void showSettingsRedirectDialog() {
new android.app.AlertDialog.Builder(this)
.setTitle("ब्लूटूथ अक्षम करें")
.setMessage("Android 10+ में ऐप्स सीधे ब्लूटूथ बंद नहीं कर सकते।\nकृपया सेटिंग्स में जाकर मैन्युअली बंद करें।")
.setPositiveButton("सेटिंग्स खोलें", (dialog, which) -> {
Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(intent);
})
.setNegativeButton("रद्द", null)
.show();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 101 && grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
attemptDisableBluetooth();
}
}
}
आधुनिक दृष्टिकोण (Android 10+)
// उपयोगकर्ता को ब्लूटूथ सेटिंग्स पर भेजें Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); startActivity(intent); // या क्विक सेटिंग्स टाइल (Android 7+) Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS); // नहीं, ब्लूटूथ के लिए अलग इंटेंट
कंपेनियन डिवाइस मैनेजर (Android 8.0+)
विशिष्ट डिवाइस से कनेक्ट/डिस्कनेक्ट करने के लिए:
CompanionDeviceManager cdm = (CompanionDeviceManager) getSystemService(Context.COMPANION_DEVICE_SERVICE); // डिवाइस जोड़ने/हटाने के लिए एसोसिएशन बनाएं
चरण 5: टेस्ट करें
- ऐप चलाएं
- ब्लूटूथ स्थिति देखें
- टेक्स्ट पर टैप करें
- Android 10+: सेटिंग्स डायलॉग खुलेगा
- Android 9-: सीधे अक्षम करने का प्रयास करेगा
