android开发 nfc,Android NFC开发实战

关于Android 4.0 SDK中提供的Beam例子,关于NFC开发来说确实是一个不错的模板 。关于了解NFC的NDEF

信息

解决过程

不妨看下面的代码 。

public class Beam extends Activity implements CreateNdefMessageCallback,

OnNdefPushCompleteCallback {

NfcAdapter mNfcAdapter;

TextView mInfoText;

private static final int MESSAGE_SENT = 1;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mInfoText = (TextView) findViewById(R.id.textView);

mNfcAdapter = NfcAdapter.getDefaultAdapter(this); //实例化NFC

设施

if (mNfcAdapter == null) {

mInfoText = (TextView) findViewById(R.id.textView);

mInfoText.setText("NFC is not available on this device.");

}

mNfcAdapter.setNdefPushMessageCallback(this, this); //注册NDEF回调

信息

mNfcAdapter.setOnNdefPushCompleteCallback(this, this);

}

@Override

public NdefMessage createNdefMessage(NfcEvent event) {

Time time = new Time();

time.setToNow();

String text = ("Beam me up!\n\n" +

"Beam Time: " + time.format("%H:%M:%S"));

NdefMessage msg = new NdefMessage(

new NdefRecord[] { createMimeRecord(

"application/com.example.android.beam", text.getBytes())

});

return msg;

}

@Override

public void onNdefPushComplete(NfcEvent arg0) {

// A handler is needed to send messages to the activity when this

// callback occurs, because it happens from a binder thread

mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();

}

private final Handler mHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case MESSAGE_SENT:

Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();

break;

}

}

};

@Override

public void onResume() {

super.onResume();

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {

processIntent(getIntent());

}

}

@Override

public void onNewIntent(Intent intent) {

// onResume gets called after this to handle the intent

setIntent(intent);

}

/**

* Parses the NDEF Message from the intent and prints to the TextView

*/

void processIntent(Intent intent) {

Parcelable[] rawMsgs = intent.getParcelableArrayExtra(

NfcAdapter.EXTRA_NDEF_MESSAGES);

// only one message sent during the beam

NdefMessage msg = (NdefMessage) rawMsgs[0];

// record 0 contains the MIME type, record 1 is the AAR, if present

mInfoText.setText(new String(msg.getRecords()[0].getPayload()));

}

/**

* Creates a custom MIME type encapsulated in an NDEF record

*

* @param mimeType

*/

public NdefRecord createMimeRecord(String mimeType, byte[] payload) {

byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));

NdefRecord mimeRecord = new NdefRecord(

NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);

return mimeRecord;

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// If NFC is not available, we won't be needing this menu

if (mNfcAdapter == null) {

return super.onCreateOptionsMenu(menu);

}

MenuInflater inflater = getMenuInflater();

inflater.inflate(R.menu.options, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.menu_settings:

Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);

startActivity(intent);

return true;

default:

return super.onOptionsItemSelected(item);

}

}

}

关于Android 4.0 SDK中提供的Beam例子,关于NFC开发来说确实是一个不错的模板 。关于了解NFC的NDEF

信息

解决过程

不妨看下面的代码 。

public class Beam extends Activity implements CreateNdefMessageCallback,

OnNdefPushCompleteCallback {

NfcAdapter mNfcAdapter;

TextView mInfoText;

private static final int MESSAGE_SENT = 1;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mInfoText = (TextView) findViewById(R.id.textView);

mNfcAdapter = NfcAdapter.getDefaultAdapter(this); //实例化NFC

设施

if (mNfcAdapter == null) {

mInfoText = (TextView) findViewById(R.id.textView);

mInfoText.setText("NFC is not available on this device.");

}

mNfcAdapter.setNdefPushMessageCallback(this, this); //注册NDEF回调

信息

mNfcAdapter.setOnNdefPushCompleteCallback(this, this);

}

@Override

public NdefMessage createNdefMessage(NfcEvent event) {

Time time = new Time();

time.setToNow();

String text = ("Beam me up!\n\n" +

"Beam Time: " + time.format("%H:%M:%S"));

NdefMessage msg = new NdefMessage(

new NdefRecord[] { createMimeRecord(

"application/com.example.android.beam", text.getBytes())

});

return msg;

}

@Override

public void onNdefPushComplete(NfcEvent arg0) {

// A handler is needed to send messages to the activity when this

// callback occurs, because it happens from a binder thread

mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();

}

private final Handler mHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case MESSAGE_SENT:

Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();

break;

}

}

};

@Override

public void onResume() {

super.onResume();

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {

processIntent(getIntent());

}

}

@Override

public void onNewIntent(Intent intent) {

// onResume gets called after this to handle the intent

setIntent(intent);

}

/**

* Parses the NDEF Message from the intent and prints to the TextView

*/

void processIntent(Intent intent) {

Parcelable[] rawMsgs = intent.getParcelableArrayExtra(

NfcAdapter.EXTRA_NDEF_MESSAGES);

// only one message sent during the beam

NdefMessage msg = (NdefMessage) rawMsgs[0];

// record 0 contains the MIME type, record 1 is the AAR, if present

mInfoText.setText(new String(msg.getRecords()[0].getPayload()));

}

/**

* Creates a custom MIME type encapsulated in an NDEF record

*

* @param mimeType

*/

public NdefRecord createMimeRecord(String mimeType, byte[] payload) {

byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));

NdefRecord mimeRecord = new NdefRecord(

NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);

return mimeRecord;

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// If NFC is not available, we won't be needing this menu

if (mNfcAdapter == null) {

return super.onCreateOptionsMenu(menu);

}

MenuInflater inflater = getMenuInflater();

inflater.inflate(R.menu.options, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.menu_settings:

Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);

startActivity(intent);

return true;

default:

return super.onOptionsItemSelected(item);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值