私はここ数回、サンプルコードを見てきましたが、何が間違っているのか分かりません。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レコードを送信している例やドキュメントhere、hereとhere
あなたが提案した変更を加えましたが、まだ動作していません。私は単純なテキストメッセージに切り替え、タイプをTNF_MIME_MEDIAに変更して、データを転送することができました。しかし私がスプレッドシートドームに戻ったとき、それは動作を停止しました、そして、それは問題がサイズかもしれないようです。スプレッドシートをBytearrayoutputstreamに変換し、そのオブジェクト内の9つのバッファを埋めます。それがたくさんあるかどうかわからない、たくさんのように思える。TNF_MIME_MEDIAメッセージで送信できるデータ量には制限がありますか? – Hardy
@Hardyデータ・ブロブの大きさ(バイト単位)外部の型は単純なテキストメッセージで動作しますか? –
私は外部のタイプを試してみると、常にプレイストアに行き、決して私のアプリを開きません。 byteAlrayOutputStream.size()は322000 – Hardy