কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েডে ব্লুটুথ ডিজেবল করার পদ্ধতি

অ্যান্ড্রয়েডে প্রোগ্রাম্যাটিক্যালি ব্লুটুথ ডিজেবল করা

এই টিউটোরিয়ালে শিখব কিভাবে `BluetoothManager` এবং `BluetoothAdapter` ব্যবহার করে অ্যাপ্লিকেশন থেকে ব্লুটুথ অফ করা যায়। নোট: Android 10+ এ ব্লুটুথ এনাবল/ডিজেবল করার জন্য সিস্টেম ডায়ালগ দেখাতে হয়।

প্রয়োজনীয় ধাপসমূহ

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project নির্বাচন করে প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট ফাইল

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:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/text"
        android:textSize="30sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="ব্লুটুথ বন্ধ করতে ক্লিক করুন"
        android:gravity="center" />
</LinearLayout>

ধাপ ৩: জাভা কোড

src/MainActivity.java ফাইলে:

package com.example.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    private BluetoothAdapter bluetoothAdapter;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView = findViewById(R.id.text);
        
        // ব্লুটুথ অ্যাডাপ্টার পাওয়া
        BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();
        
        if (bluetoothAdapter == null) {
            textView.setText("এই ডিভাইসে ব্লুটুথ হার্ডওয়্যার নেই");
            return;
        }
        
        updateUI();
        
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                disableBluetooth();
            }
        });
    }
    
    private void disableBluetooth() {
        if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                // Android 10+ : ইউজার কনফার্মেশন ডায়ালগ দেখাতে হবে
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISABLE);
                startActivityForResult(enableBtIntent, 1);
            } else {
                // Android 9 এবং নিচে: সরাসরি ডিজেবল (পারমিশন দরকার)
                bluetoothAdapter.disable();
                updateUI();
                Toast.makeText(this, "ব্লুটুথ বন্ধ করা হয়েছে", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "ব্লুটুথ ইতিমধ্যে বন্ধ", Toast.LENGTH_SHORT).show();
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            updateUI();
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, "ব্লুটুথ বন্ধ করা হয়েছে", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "ব্লুটুথ বন্ধ করা বাতিল হয়েছে", Toast.LENGTH_SHORT).show();
            }
        }
    }
    
    private void updateUI() {
        if (bluetoothAdapter != null) {
            textView.setText(bluetoothAdapter.isEnabled() ? 
                "ব্লুটুথ: চালু (বন্ধ করতে ক্লিক করুন)" : 
                "ব্লুটুথ: বন্ধ (চালু করতে ক্লিক করুন)");
        }
    }
}

ধাপ ৪: ম্যানিফেস্টে পারমিশন

AndroidManifest.xml ফাইলে:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.myapplication">
    
    <!-- Android 11 و নিচে জন্য -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    
    <!-- Android 12+ (API 31+) জন্য -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    
    <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>

Android ভার্সন অনুযায়ী পারমিশন

Android ভার্সনপারমিশনব্যবহার
Android 11 (API 30) এবং নিচেBLUETOOTH, BLUETOOTH_ADMINসরাসরি enable()/disable() কাজ করে
Android 12 (API 31) এবং উপরেBLUETOOTH_CONNECTরানটাইম পারমিশন + ইউজার ডায়ালগ

রানটাইম পারমিশন হ্যান্ডলিং (Android 12+)

private void checkBluetoothPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        if (checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) 
            != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.BLUETOOTH_CONNECT}, 100);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 100 && grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // পারমিশন মেলেছে, এখন disableBluetooth() কল করুন
    }
}

সাম্প্রতিক পরিবর্তন (Android 13+ / API 33+)

  • `BLUETOOTH_ADMIN` ডিপ্রিকেটেড
  • `disable()` মেথড ডিপ্রিকেটেড - `ACTION_REQUEST_DISABLE` ইন্টেন্ট ব্যবহার করুন
  • অ্যাপ্লিকেশন থেকে সরাসরি ব্লুটুথ কন্ট্রোল সীমিত করা হয়েছে

অ্যাপ্লিকেশন চালানো

রান করলে টেক্সট ভিউতে ব্লুটুথ স্ট্যাটাস দেখাবে। ক্লিক করলে Android 10+ এ সিস্টেম ডায়ালগ আসবে, নিচের ভার্সনে সরাসরি বন্ধ হবে।

সুরক্ষা ও গোপনীয়তা

  • ব্যবহারকারীর সহমতি ছাড়া ব্লুটুথ কন্ট্রোল করা অনুদানীয় নয় (আধুনিক Android-এ)
  • সেটিংস অ্যাপ থেকে ইউজার নিজে কন্ট্রোল করতে পারেন
  • অ্যাপ্লিকেশন শুধু অনুরোধ করতে পারে, বাধ্য করতে পারে না
  1. অ্যান্ড্রয়েডে Chrome মিউট করার ধাপে ধাপে গাইড

  2. অ্যান্ড্রয়েডে অ্যাপস অটো-লঞ্চ বন্ধ করুন: ধাপে ধাপে গাইড

  3. অ্যান্ড্রয়েডে Gear VR Service ডিজেবল করুন: বেটার ব্যাটারি পারফরম্যান্সের জন্য কুইক গাইড

  4. অ্যান্ড্রয়েডে অটোকরেক্ট অফ করার ধাপে ধাপে গাইড