【Android】デバイスの現在のBluetooth名を取得して表示する方法
はじめに
この記事では、Androidアプリでデバイスの現在のBluetooth名(デバイス名)を取得し、画面に表示する方法を解説します。Bluetooth名は、他の端末とペアリングする際などに使用される識別名であり、BluetoothManagerを使えば簡単に取得できます。
ステップ1:新規プロジェクトの作成
まず、Android Studioで新しいプロジェクトを作成します。メニューから「File」→「New Project」を選択し、必要な項目を入力してプロジェクトを生成しましょう。
ステップ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: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>
上記のコードでは、Bluetooth名を表示するためのTextViewを画面中央に配置しています。文字サイズは30spに設定され、見やすくなっています。
ステップ3: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);
final BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
textView.setText(manager.getAdapter().getName());
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
}
ここでは、getSystemService(BLUETOOTH_SERVICE)によってシステムサービスからBluetoothManagerを取得し、さらにgetAdapter().getName()を呼び出すことで、デバイスに設定されているBluetooth名を取得しています。取得した名前はsetText()でTextViewに渡され、画面に表示されます。
ステップ4:マニフェストへの権限追加
最後に、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.ACCESS_NETWORK_STATE" />
<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" />
<action android:name = "android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
重要なポイント:Bluetooth機能を利用するには、「BLUETOOTH」と「BLUETOOTH_ADMIN」のパーミッション宣言が必須です。これらが未宣言の場合、アプリは正常に動作しませんので注意してください。また、Android 12(API 31)以降では「BLUETOOTH_CONNECT」権限のランタイム許可も必要になる点にも留意しましょう。
アプリの実行
それでは、アプリケーションを実行してみましょう。実機のAndroidスマートフォンがPCに接続されていることを前提とします。Android Studioからアプリを実行するには、プロジェクト内のアクティビティファイルを開き、ツールバーの実行アイコンをクリックしてください。プルダウンから接続中のモバイルデバイスを選択すると、実機の画面にBluetooth名が表示されます。

-
Androidで現在のスレッドがデーモンかどうかを確認する方法【サンプルコード付き】
はじめに:スレッドとデーモンスレッドとはスレッドとは、軽量なサブプロセスのことであり、UI(ユーザーインターフェース)を妨げることなくバックグラウンド処理を実行する仕組みです。本記事では、Androidアプリにおいて現在のスレッドがデーモンスレッドかどうかを確認する方法を、実際のサンプルコードとともにわかりやすく解説します。なお、デーモンスレッドとは、JVM(Java仮想マシン)の終了を待たずに動作するバックグラウンドスレッドのことです。UIを担当するメインスレッドは、通常デーモンには設定されていません。手順1:新しいプロジェクトを作成するまず、Android Studioで新しいプロジェクト
-
【Android】現在のスレッドIDを取得する方法を解説
本記事では、Androidアプリで現在実行中のスレッドIDを取得する方法を、サンプルコードとともに解説します。スレッドとは?例に入る前に、まず「スレッド」について簡単におさらいしておきましょう。スレッドとは、軽量なサブプロセス(処理の単位)のことであり、UIの動作を妨げることなくバックグラウンドで処理を実行するために使われます。メインスレッドとは別に動作させることで、画面の描画やユーザー操作をブロックせずに、重い処理を並行して行うことが可能になります。このサンプルでは、Thread.currentThread().getId() を呼び出すことで、現在実行中のスレッドIDを取得し、TextVi