AndroidでBluetoothがLE 2M PHYに対応しているか確認する方法
このチュートリアルでは、Android端末のBluetoothがLE 2M PHYに対応しているかどうかをプログラムから確認する方法を、ステップごとにわかりやすく解説します。
LE 2M PHYとは?
LE 2M PHYはBluetooth 5で導入された物理層(PHY)の一種で、BLE(Bluetooth Low Energy)の通信速度を従来の1M PHYの約2倍(2Mbps)に引き上げられる機能です。IoTデバイスやウェアラブル機器との大容量データ転送において重要な要素となります。
対応確認には isLe2MPhySupported() メソッドを使用します。このメソッドはAPIレベル26(Android 8.0 Oreo)以降で利用可能なため、実行時のOSバージョンチェックが必要です。
手順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>
上記のレイアウトでは、画面中央にTextViewを配置しています。このTextViewにLE 2M PHYのサポート結果を表示します。
手順3:MainActivity.java を実装する
src/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);
final BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
textView.setText(""+manager.getAdapter().isLe2MPhySupported());
}
}
}
コードのポイント
getSystemService(BLUETOOTH_SERVICE)でシステムからBluetoothManagerを取得します。Build.VERSION.SDK_INT >= Build.VERSION_CODES.Oにより、Android 8.0(API 26)以降でのみ処理を実行するようにガードしています。これはisLe2MPhySupported()がAPI 26で追加されたためです。manager.getAdapter().isLe2MPhySupported()の戻り値(true / false)をTextViewに表示します。
手順4:AndroidManifest.xml を編集する
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" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
マニフェストでは、Bluetooth操作に必要な BLUETOOTH と BLUETOOTH_ADMIN のパーミッションを宣言しています。
アプリを実行してみよう
それでは、実際にアプリを動かしてみましょう。実機のAndroidスマートフォンをPCに接続した状態で、Android StudioのツールバーにあるRunアイコンをクリックします。実行デバイスとして接続したスマートフォンを選択すると、アプリが起動し、画面にLE 2M PHYのサポート状況(true または false)が表示されます。
- true と表示された場合:お使いの端末のBluetoothはLE 2M PHYに対応しており、高速なBLE通信が可能です。
- false と表示された場合:端末またはBluetoothアダプタがLE 2M PHY非対応のため、従来の1M PHYで動作します。
対応チェックを行うことで、Bluetooth 5の高速通信機能を活用するか、互換性のある通信方式へフォールバックするかなど、アプリ側の挙動を柔軟に制御できます。
-
AndroidでArrayBlockingQueueのサイズを取得する方法を解説
本記事では、AndroidアプリでArrayBlockingQueueのサイズを取得する方法を解説します。まず、ArrayBlockingQueueについて簡単に確認しておきましょう。ArrayBlockingQueueはFIFO(先入れ先出し)方式で動作するキューであり、最初に追加された要素が最も長く保持され、最後に追加された要素が最も短い期間保持されるという特徴があります。ArrayBlockingQueueとはArrayBlockingQueueは、java.util.concurrentパッケージに含まれるスレッドセーフなキューの実装です。配列をベースとしており、容量が固定されている点
-
Android開発:ConcurrentLinkedQueueで要素を検索する方法(contains()の使い方を解説)
ConcurrentLinkedQueueとはConcurrentLinkedQueueは、リンクノードを基盤とした無制限(アンバウンド)のキューです。複数のスレッドから安全に要素へアクセスできる、スレッドセーフなコレクションクラスとして知られています。要素はFIFO(先入れ先出し)方式で処理され、新しい要素は常に末尾(テール)から挿入されます。なお、null値は許可されていない点に注意してください。この記事では、AndroidアプリでConcurrentLinkedQueue内の特定の要素を検索する方法を、サンプルコードとともにわかりやすく解説します。実装手順ステップ1:新規プロジェクトの作