2016-10-04 37 views
1

私はここ数回、サンプルコードを見てきましたが、何が間違っているのか分かりません。Android Beam/NFCが送信したレコードを受信しないのはなぜですか?

私は私の2つのデバイスを一緒に私はビーム画面が表示され、それをタップが表示されますが、何も送信されないと思われると私のブレークポイントのいずれもヒットする。 ここに私のコードは、何も他のデバイスに送信される理由を理解するのを助けてください。エディタではBeamFileActivityのすべてが実行されていることがわかりますが、受信デバイスではMainActivityのブレークポイントは表示されません。私は間違って何をしていますか?

のAndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.myapp"> 
    <uses-permission android:name="android.permission.NFC" /> 
    <uses-feature android:name="android.hardware.nfc" android:required="true" /> 
    <application 
    android:name=".MainActivity" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme"> 
     <meta-data 
      android:name="QUERY_LOG" 
      android:value="false" /> 
     <meta-data 
      android:name="DOMAIN_PACKAGE_NAME" 
      android:value="com.myapp.domain" /> 

     <activity 
      android:name=".MainActivity" 
      android:exported="true" 
      android:label="@string/app_name" 
      android:windowSoftInputMode="adjustResize"> 

      <intent-filter> 
       <action android:name="com.google.android.apps.drive.DRIVE_OPEN" /> 
       <action android:name="com.google.android.apps.drive.DRIVE_SAVE" /> 
       <data android:mimeType="application/vnd.google-apps.drive-sdk.111111111" /> 
       <data android:mimeType="application/vnd.google-apps.spreadsheet" /> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
       <category android:name="android.intent.category.DEFAULT"/> 
       <data android:scheme="vnd.android.nfc" 
        android:host="ext" 
        android:pathPrefix="/com.myapp:SpreadsheetDom"/> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".dialog.BeamFileActivity" 
      android:label="@string/title_activity_beam_file" 
      android:parentActivityName=".MainActivity" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.myapp.MainActivity" /> 
     </activity> 
    </application> 

MainActivity:これは私が唯一の私が関連していると思われる方法を含むよ、ビームの意図を受信するものです。

@Override 
protected void onResume() { 

    super.onResume(); 

    //I removed some code that reloads my application, didn't seem relevant 

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { 
     processIntent(getIntent()); 
    } 
} 

/** 
* Parses the NDEF Message from the intent and stores the collection 
*/ 
void processIntent(Intent intent) { 

    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 
    // only one message expected during the beam 
    NdefMessage msg = (NdefMessage) rawMsgs[0]; 

    //This is the data I'm beaming, It has a method that converts its data to a byte array, and a method, fromBytes() that converts bytes into the object 
    SpreadsheetDom dom = new SpreadsheetDom(); 
    try { 

     for(int i = 0; i < msg.getRecords().length; i++) { 

      if(SpreadsheetDom.class.getName().equalsIgnoreCase(new String(msg.getRecords()[i].getType()))) { 

       dom.fromBytes(msg.getRecords()[i].getPayload()); 
       //removed some code that saves and displays changes 
      } 
     } 
    } catch(IOException | ClassNotFoundException e) { 

    } 
} 

@Override 
public void onNewIntent(Intent intent) { 

    setIntent(intent); 
} 

BeamFileActivity:これは単にデータを晴れやかための命令を表示し、実際のビームを行う別活動です。本当に

private void initFileToBeam(Long spreadsheetId) { 

    try { 

     List<SpreadsheetDom> spreadsheets = db.getSpreadsheetDao().queryForEq(SpreadsheetDom.SPREADSHEET_ID_NAME, spreadsheetId); 
     if(spreadsheets != null && spreadsheets.size() > 0) { 

      fileToBeam = spreadsheets.get(0); 
     } 
    } catch(SQLException e) { 

     ErrorDialog ed = new ErrorDialog(this, "Unable to beam current collection."); 
     ed.show(); 
    } 
} 

@Override 
public NdefMessage createNdefMessage(NfcEvent event) { 

    if(fileToBeam == null) { 

     Long spreadsheetId = Settings.getInstance().get(SettingKey.CURRENT_SPREADSHEET).getValue(); 
     initFileToBeam(spreadsheetId); 
    } 

    NdefMessage msg = null; 
    try { 

     msg = new NdefMessage(
       new NdefRecord[]{ 
         new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, "application/com.myapp:SpreadsheetDom".getBytes(), new byte[0], fileToBeam.toBytes()), 
         //NdefRecord.createExternal("com.myapp", "spreadsheetdom", fileToBeam.toBytes()), 
         NdefRecord.createApplicationRecord("com.myapp") 
       }); 
    } catch(IOException e) { 

     String textMessage = "Unable to transfer collection: " + e.getMessage(); 
     msg = new NdefMessage(
       new NdefRecord[]{ 
         NdefRecord.createMime("text/plain", textMessage.getBytes()), 
         NdefRecord.createApplicationRecord("com.myapp") 
       }); 
    } 

    return msg; 
} 

@Override 
public void onResume() { 

    super.onResume(); 

    Intent exportFileIntent = getIntent(); 
    Long spreadsheetId = exportFileIntent.getLongExtra(Constants.EXTRA_SPREADSHEET_ID, Database.INVALID_ID); 

    initFileToBeam(spreadsheetId); 

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    if (mNfcAdapter == null) { 

     Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show(); 
     finish(); 
     return; 
    } 
    // Register callback 
    mNfcAdapter.setNdefPushMessageCallback(this, this); 
} 

このすべては、あなたが何をあなたのMainActivityを期待とは異なるNDEFレコードを送信している例やドキュメントhereherehere

答えて

2

からです。 「:SpreadsheetDom com.myapp」:あなたのMainActivityは、NFCフォーラムの外部タイプに対して登録されたAndroidのインテントフィルタ以来

<intent-filter> 
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
    <category android:name="android.intent.category.DEFAULT"/> 
    <data android:scheme="vnd.android.nfc" 
      android:host="ext" 
      android:pathPrefix="/com.myapp:SpreadsheetDom"/> 
</intent-filter> 

小文字は区別されている敏感が、NFCフォーラムの外部型の名前は小文字を区別しない、アンドロイドを小文字は区別されています自動的にNFCフォーラムの外部タイプの名前を(MIMEタイプの場合と同様に) -caseに自動的に変換します。インテントフィルタには大文字「S」と「D」が含まれているため、NFCフォーラムの外部タイプのタイプ名と決して一致しません。そのため、あなたは試合を達成するために、すべて小文字ようなタイプ名を指定する必要があります(またhereを参照してください):

<intent-filter> 
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
    <category android:name="android.intent.category.DEFAULT"/> 
    <data android:scheme="vnd.android.nfc" 
      android:host="ext" 
      android:pathPrefix="/com.myapp:spreadsheetdom"/> 
</intent-filter> 

次に、BeamFileActivity無効なタイプ名で、外部レコードを作成し、あなたの「アプリケーション/ COM .myapp:spreadsheetDom」:上記のインテントフィルタは、あろう一致

msg = new NdefMessage(new NdefRecord[] { 
     new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, "application/com.myapp:SpreadsheetDom".getBytes(), new byte[0], fileToBeam.toBytes()), 
     NdefRecord.createApplicationRecord("com.myapp") 
}); 

アンNFCフォーラム外部型名 『:spreadsheetdom』(再度、全て小文字)com.myapp。あなたは(タイプ名にUS-ASCIIエンコーディングを使用してください)で、このような記録を作成することができます

msg = new NdefMessage(new NdefRecord[] { 
     new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, "com.myapp:spreadsheetdom".getBytes("US-ASCII"), new byte[0], fileToBeam.toBytes()), 
     NdefRecord.createApplicationRecord("com.myapp") 
}); 

最後に、「com.myappは」外部タイプのためのよく形成されたドメイン名ではないことに注意してくださいNFC Forumのレコードタイプ定義によると、代わりに、整形式ドメイン名は "myapp.com"(Javaパッケージ名形式の代わりにインターネットドメイン名形式)です。

+0

あなたが提案した変更を加えましたが、まだ動作していません。私は単純なテキストメッセージに切り替え、タイプをTNF_MIME_MEDIAに変更して、データを転送することができました。しかし私がスプレッドシートドームに戻ったとき、それは動作を停止しました、そして、それは問題がサイズかもしれないようです。スプレッドシートをBytearrayoutputstreamに変換し、そのオブジェクト内の9つのバッファを埋めます。それがたくさんあるかどうかわからない、たくさんのように思える。TNF_MIME_MEDIAメッセージで送信できるデータ量には制限がありますか? – Hardy

+0

@Hardyデータ・ブロブの大きさ(バイト単位)外部の型は単純なテキストメッセージで動作しますか? –

+0

私は外部のタイプを試してみると、常にプレイストアに行き、決して私のアプリを開きません。 byteAlrayOutputStream.size()は322000 – Hardy

関連する問題