2016-04-27 8 views
0

私はBluetoothデバイスオブジェクトを作成する新しいアクティビティを作成します。私はonActivityResult()メソッドを使ってこれを返す必要があるとき、私は問題があります。onActivityResultでオブジェクトを取得

子アクティビティ

Intent intent = new Intent(); 
      intent.putExtra("BluetoothDevice", DeviceArrayList.get(arg2)); 
      setResult(Activity.RESULT_OK, intent); 
      finish(); 

DeviceArrayList.get(arg2)はデバイス・オブジェクトです。私はbtDeviceでオブジェクトを取得することができます

親アクティビティ

BluetoothDevice btDevice; 
... 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

      if (requestCode == 1) { 
       if(resultCode == Activity.RESULT_OK){ 

        btDevice = data.getExtras("BluetoothDevice"); 

       } 
       if (resultCode == Activity.RESULT_CANCELED) { 
        Toast.makeText(this, "Resultado cancelado", Toast.LENGTH_SHORT) 
          .show(); 
       } 
      } 
     }//onActivityResult 

+0

ホセMのようにそれをキャスト: 詳細はアンドロイドのドキュメントを見てくださいBluetoothDeviceがSerilizableまたはParceableの仲間であるです? SerializableまたはParceableオブジェクトのみをインテントで渡すことができます:) –

+0

mm ..わかりません:(この場合、私はそれをシリアル化できますか?) –

+1

android.bluetooth.BluetoothDeviceを使用している場合、パーセルブルが拡張されていますintent.putParcelable( "key"、object) – darwin

答えて

2

あなたのBluetoothデバイスクラスをserializabaleまたはparceableにします。私は、シリアライズ上parceable好む

intent.getParcelableExtra("bluetooth") 

を:

intent.putExtra("bluetooth", myBluetoothDeviceObject); 

をそして、あなたは使用して第二の活動で同じことを得ることができます。そして、あなたは意図を用いて第2のアクティビティに渡すことができます。 BluetoothDeviceクラスはシリアライズ可能 http://developer.android.com/reference/android/os/Parcelable.html

0

とonActivityResultにその

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (requestCode == 1) { 
      if(resultCode == Activity.RESULT_OK){ 

       btDevice =(BluetoothDevice)data.getSerializableExtra("BluetoothDevice"); 

      } 
      if (resultCode == Activity.RESULT_CANCELED) { 
       Toast.makeText(this, "Resultado cancelado", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } 
    }//onActivityResult 
関連する問題