Bluetooth Adapter Enable করার উদাহরণ
এই উদাহরণটি দেখাবে কিভাবে অ্যাপ্লিকেশন থেকে সরাসরি ব্লুটুথ অন করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি
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" /> </LinearLayout>
ধাপ ৩: MainActivity.java (src/MainActivity.java)
package com.example.myapplication; import android.bluetooth.BluetoothManager; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.os.Build; import android.os.Bundle; import android.os.health.SystemHealthManager; import android.provider.Telephony; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.telephony.SmsManager; import android.view.View; import android.view.WindowManager; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView; @RequiresApi(api = Build.VERSION_CODES.N) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text); textView.setText("enable"); final BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { manager.getAdapter().enable(); } }); } @Override protected void onStop() { super.onStop(); } @Override protected void onResume() { super.onResume(); } }
দ্রষ্টব্য: `enable()` মেথড অ্যাসিঙ্ক্রোনাসলি কাজ করে এবং ব্যবহারকারীর অনুমতির ডায়ালগ দেখাতে পারে। প্রোডাকশন কোডে `startActivityForResult` সহ `ACTION_REQUEST_ENABLE` ব্যবহার করা ভালো।
অ্যাপ্লিকেশন চালান
ডিভাইস সংযুক্ত থাকলে Run আইকনে ক্লিক করুন। TextView-এ ক্লিক করলে ব্লুটুথ অন হওয়ার চেষ্টা করবে।
