ব্লুটুথ ডিভাইস কানেক্ট/ডিসকানেক্ট ডিটেকশন: BroadcastReceiver
এই উদাহরণটি দেখাবে কীভাবে BluetoothDevice.ACTION_ACL_CONNECTED এবং ACTION_ACL_DISCONNECTED ব্রডকাস্ট ব্যবহার করে ব্লুটুথ ডিভাইসের কানেকশন স্ট্যাটাস মনিটর করা যায়।
পদক্ষেপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট শুরু করুন।
পদক্ষেপ ২: লেআউট XML (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Bluetooth settings to test connection"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
পদক্ষেপ ৩: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device == null) return;
String deviceName = device.getName() != null ? device.getName() : "Unknown Device";
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
Toast.makeText(context, "Connected: " + deviceName, Toast.LENGTH_SHORT).show();
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
Toast.makeText(context, "Disconnected: " + deviceName, Toast.LENGTH_SHORT).show();
} else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
Toast.makeText(context, "Disconnecting: " + deviceName, Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
registerReceiver(bluetoothReceiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(bluetoothReceiver);
}
}
পদক্ষেপ ৪: AndroidManifest.xml - পারমিশন
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
গুরুত্বপূর্ণ নোটসমূহ
- রানটাইম পারমিশন (API 23+): BLUETOOTH_CONNECT এবং BLUETOOTH_SCAN ডেঞ্জারাস পারমিশন, রানটাইমে রিকোয়েস্ট করতে হবে।
- পেয়ারড ডিভাইস লিস্ট: বর্তমান কানেক্টেড ডিভাইস পাওয়ার জন্য: `bluetoothAdapter.getBondedDevices()`
- প্রোফাইল-স্পেসিফিক: A2DP (অডিও), HFP (কল), GATT (BLE) এর আলাদা ব্রডকাস্ট থাকে।
- BLE ডিভাইস: BluetoothDevice.ACTION_ACL_CONNECTED BLE-ও কাজ করে, কিন্তু BluetoothGattCallback ব্যবহার করা ভালো।
ব্লুটুথ অ্যাডাপ্টার থেকে বর্তমান স্ট্যাটাস চেক
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
Set bondedDevices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : bondedDevices) {
// বন্ডেড ডিভাইসের সাথে কানেক্টেড কিনা চেক করার জন্য প্রোফাইল প্রয়োজন
}
}
আধুনিক পদ্ধতি: CompanionDeviceManager (API 26+)
ব্লুটুথ পেয়ারিং এবং ম্যানেজমেন্টের জন্য আরও সিকিউর এবং ইউজার-ফ্রেন্ডলি API।