3

から値を解析して取得する必要があります。

Parcelable[] uuidExtra = intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");

私の目標は、Parcelable[] の上から UUID を取得することです。それを達成する方法は?

4

4 に答える 4

6

このようなことを試してください。それは私のために働いた:

   if(BluetoothDevice.ACTION_UUID.equals(action)) {
     BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
     for (int i=0; i<uuidExtra.length; i++) {
       out.append("\n  Device: " + device.getName() + ", " + device + ", Service: " + uuidExtra[i].toString());
     }

お役に立てれば!

于 2012-05-02T02:23:08.247 に答える
4

Parcelable[] を反復処理し、各 Parcelable を ParcelUuid にキャストし、ParcelUuid.getUuid() を使用して UUID を取得する必要があります。別の回答のように Parcelables で toString() を使用できますが、これにより、UUID オブジェクトではなく、UUID を表す文字列のみが得られます。

Parcelable[] uuids = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
if (uuids != null) {
    for (Parcelable parcelable : uuids) {
        ParcelUuid parcelUuid = (ParcelUuid) parcelable;
        UUID uuid = parcelUuid.getUuid();
        Log.d("ParcelUuidTest", "uuid: " + uuid);
    }       
}       
于 2016-01-14T16:52:36.463 に答える
1

ドキュメントを引用し、返されたオブジェクトが ParcelUuid 型であると言う点で、受け入れられた答えは正しいです。ただし、彼はそれへのリンクを提供していません。ここにあります: BluetoothDevice.EXTRA_UUID

さらに、提供されたコードは 2 つの点で間違っています。1つは、質問と同じ方法を参照していないこと、および2つ目は、コンパイルできないことです(ここではいくつかの哲学的自由を取ります)。両方の問題を修正するには、コードを次のようにする必要があります。

Parcelable[] uuidExtra = intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");
if (uuidExtra != null) {
   for (int j=0; j<uuidExtra.length; j++) {
      ParcelUuid extraUuidParcel = (ParcelUuid)uuidExtra[j];
      // put code here
   }
}

第 3 に、追加の保護が必要な場合 (ただし、通常、オブジェクトは常に である必要がありますParcelUuid)、 で以下を使用できますfor

   ParcelUuid extraUuidParcel = uuidExtra[j] instanceof ParcelUuid ? ((ParcelUuid) uuidExtra[j]) : null;
   if (extraUuidParcel != null) {
      // put code here
   }

このソリューションはArneによって提供されました。まだコメントを追加することはできません。さらに、ドキュメント ページを提供します :)

于 2016-09-21T13:46:45.907 に答える
-2

ドキュメントから、エクストラはParcelUuidであると述べられています

だからあなたは使うべきです

ParcelUuid uuidExtra intent.getParcelableExtra("android.bluetooth.device.extra.UUID");
UUID uuid = uuidExtra.getUuid();

それが役立つことを願っています。

于 2012-05-01T08:13:57.350 に答える