এন্ড্রয়েডে ব্লুটুথ অ্যাডাপ্টারের নাম জানা
এই উদাহরণে দেখানো হয়েছে কিভাবে এন্ড্রয়েড ডিভাইসের ব্লুটুথ অ্যাডাপ্টারের বর্তমান নাম প্রোগ্রাম্যাটিক্যালি পাওয়া যায়।
পদক্ষেপ ১: নতুন প্রজেক্ট তৈরি করুন
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-তে লজিক যোগ করুন
package com.example.myapplication;
import android.bluetooth.BluetoothManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
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);
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
textView.setText(manager.getAdapter().getName());
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
}
পদক্ষেপ ৪: AndroidManifest.xml-এ ব্লুটুথ পারমিশন যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<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>
অ্যাপ্লিকেশন চালান
ডিভাইস কম্পিউটারের সাথে সংযুক্ত থাকলে, Android Studio-তে Run আইকনে ক্লিক করে অ্যাপটি চালান। ডিভাইসে ব্লুটুথের নাম দেখাবে।