Android NFC开发读写NDEF数据实例
2015-07-14 17:16
459 查看
一、Android 4.0提供的Demo
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);
}
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()));
}
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);
}
}
}
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);
}
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()));
}
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 内存溢出的几点解决方法
- Android 内存优化的几点知识
- 编写高效的android代码(译文)
- Android 获取系统相册里的照片及缩略图
- Android 使用SmsManager发送短信
- Android 申请签名证书详解
- Android 复制文件的一种方法
- Android 解析XML
- Android 读取系统日志LogCat
- Android 蓝牙编程
- Android 设置全屏及自定义标题栏
- Android List与数组的相互转化
- Android 监听EditText的状态改变
- Android 调用照相机拍照把照片转化为字节数组
- Android 解决Error generating final archive: Debug Certificate 问题
- Android ScrollView与ListView一起(嵌套)使用解决ListView自动滚动的几点方案
- Android SQLite中表自动生成字段的方法
- Android环境配置
- Android 如何使用draw9patch,介绍的很详细,图文并茂,收藏下,备忘!
- Android 设置Dialog不允许返回或搜索键消除