Androidアプリ開発

Android13対応
(非推奨のwriteDescriptor)

この記事は約9分で読めます。
記事内に広告が含まれています。
スポンサーリンク

この記事はAndroidスマホ用のアプリ開発の中で、
今後の開発で再使用性が高いと思われるコーディングをまとめたものです。
Javaの開発経験、XML構文規則、Androidのアプリ開発経験がある方を対象としています。
Androidのアプリ開発でお役にたててれば、嬉しいです。
(これからAndroidのアプリ開発やJavaでの開発を始めたい方への案内は、
記事の最後で紹介します)

この記事のテーマ


Android13で非推奨になったBluetoothGatt.writeDescriptorに対応する

ポイント

BLEのGATT通信でキャラクタリスティックの更新通知設定のwriteDescriptorですが、Andorid13で非推奨となりました。
今回は、非推奨になったwriteDescriptorをAndroid13以降に対応する実装方法を紹介いたします。

BluetoothGatt.writeDescriptor

BluetoothGatt.writeDescriptor

GATT通信で使用するBluetoothGattgetServiceBluetoothGattCharacteristicを取得します。
BluetoothGattCharacteristicに対して、setCharacteristicNotificationで通知を有効化(無効化)とwriteDescriptorで更新通知を設定します。
Android13以降では、writeDescriptorで値を設定をできるよう、引数が追加されました。
更新通知は、BluetoothGattCallbackonCharacteristicChangeで受け取ります。

Android13対応(非推奨のonCharacteristicChanged)

Android12までの実装

BluetoothGattDescriptorに値をセットして、writeDescriptorで更新通知を設定します。

    :
    public void setCharacteristicNotification(UUID uuid_service, UUID uuid_characteristic, boolean enable) {
        if(bluetoothGatt != null && bluetoothGatt.getService(uuid_service) != null) {
            BluetoothGattCharacteristic bluetoothGattCharacteristic = bluetoothGatt.getService(uuid_service).getCharacteristic(uuid_characteristic);
            bluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, enable);
            BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(UUID_CONFIG);
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            bluetoothGatt.writeDescriptor(descriptor);
        }
    }
    :

Android13以降の実装

BluetoothGattDescriptorに値をセットしないで、writeDescriptorで値を指定して書き込みます。
Android112以下に対応する場合、Build.VERSION.SDK_INTで分岐させて、それぞれ処理します。

    :
    public void setCharacteristicNotification(UUID uuid_service, UUID uuid_characteristic, boolean enable) {
        if(bluetoothGatt != null && bluetoothGatt.getService(uuid_service) != null) {
            BluetoothGattCharacteristic bluetoothGattCharacteristic = bluetoothGatt.getService(uuid_service).getCharacteristic(uuid_characteristic);
            bluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, enable);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                bluetoothGatt.writeDescriptor(bluetoothGattCharacteristic.getDescriptor(UUID_CONFIG), BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            } else {
                BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(UUID_CONFIG);
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                bluetoothGatt.writeDescriptor(descriptor);
            }
        }
    }
    :

今回は、ここまでです。

非推奨のwriteDescriptorに対応しているAndroidアプリです。

誤字脱字、意味不明でわかりづらい、
もっと詳しく知りたいなどのご意見は、
このページの最後にある
コメントか、
こちらから、お願いいたします♪

ポチッとして頂けると、
次のコンテンツを作成する励みになります♪

ブログランキング・にほんブログ村へ

これからAndroidのアプリ開発やJavaでの開発を始めたい方へ

アプリケーション開発経験がない方や、アプリケーション開発経験がある方でも、JavaやC#などのオブジェクト指向言語が初めての方は、Androidのアプリ開発ができるようになるには、かなりの時間がかかります。
オンラインスクールでの習得を、強くおススメします。

未経験者からプログラマーを目指すのに最適です。まずは無料カウンセリングから♪

ゲーム系に強いスクール、UnityやUnrealEngineを習得するのに最適です。まずは無料オンライン相談から♪

参考になったら、💛をポッチとしてね♪

スポンサーリンク
シェアする
msakiをフォローする
スポンサーリンク

コメント欄

タイトルとURLをコピーしました