Created
August 14, 2013 22:04
-
-
Save thinkAmi/6236128 to your computer and use it in GitHub Desktop.
RubotoでNFCを読み込むサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='UTF-8'?> | |
<manifest package='com.example.nfc_read' android:versionCode='1' android:versionName='1.0' xmlns:android='http://schemas.android.com/apk/res/android'> | |
<application android:hardwareAccelerated='true' android:icon='@drawable/ic_launcher' android:label='@string/app_name' android:largeHeap='true'> | |
<activity android:label='@string/app_name' android:name='NfcReadActivity'> | |
<intent-filter> | |
<action android:name='android.intent.action.MAIN'/> | |
<category android:name='android.intent.category.LAUNCHER'/> | |
</intent-filter> | |
<!-- intent-filterの追加 ここから --> | |
<intent-filter> | |
<action android:name="android.nfc.action.TECH_DISCOVERED"/> | |
<category android:name="android.intent.category.DEFAULT"/> | |
</intent-filter> | |
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter"/> | |
<!-- intent-filterの追加 ここまで --> | |
</activity> | |
<activity android:exported='false' android:name='org.ruboto.RubotoActivity'/> | |
<activity android:exported='false' android:name='org.ruboto.RubotoDialog' android:theme='@android:style/Theme.Dialog'/> | |
<service android:exported='false' android:name='org.ruboto.RubotoService'/> | |
</application> | |
<uses-sdk android:minSdkVersion='17' android:targetSdkVersion='17'/> | |
<!-- permissionの追加 ここから --> | |
<uses-permission android:name="android.permission.NFC" /> | |
<!-- permissionの追加 ここまで --> | |
<!-- GooglePlayでの表示の追加 ここから --> | |
<uses-feature android:name="android.hardware.nfc" android:required="true" /> | |
<!-- GooglePlayでの表示の追加 ここまで --> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'ruboto/widget' | |
ruboto_import_widgets :LinearLayout, :TextView | |
# NFCまわり | |
java_import 'android.nfc.NfcAdapter' | |
java_import 'android.nfc.tech.NfcA' | |
java_import 'android.nfc.tech.NfcB' | |
java_import 'android.nfc.tech.NfcF' | |
java_import 'android.nfc.tech.IsoDep' | |
java_import 'android.nfc.tech.MifareUltralight' | |
class NfcReadActivity | |
def onCreate(bundle) | |
super | |
set_title 'NFC Reader Sample' | |
intent = getIntent | |
case intent.getAction | |
when NfcAdapter::ACTION_TECH_DISCOVERED || NfcAdapter::ACTION_TAG_DISCOVERED | |
tag = intent.getParcelableExtra(NfcAdapter::EXTRA_TAG) | |
view = | |
linear_layout orientation: :vertical do | |
@idm_view = text_view text: tag.nil? ? 'No IDm' : to_hex_string(tag.getId), | |
width: :match_parent, | |
gravity: :center, | |
text_size: 48.0 | |
@tag_info_view = text_view text: tag.nil? ? '' : tag.toString | |
# getTechList.joinだとエラーになるため、to_aryを経由する | |
@tech_list_view = text_view text: tag.nil? ? '' : tag.getTechList.to_ary.join(',') | |
end | |
self.content_view = view | |
tech_view = create_tech_view(@tech_list_view.text, tag) | |
view.addView tech_view unless tech_view.nil? | |
end | |
end | |
def create_tech_view(tech_list, tag) | |
view = linear_layout orientation: :vertical do | |
@option_view = text_view text: add_delimiter + '以降はNFCタイプごとの項目' + add_delimiter | |
end | |
tech_list.split(',').each do |tech| | |
# いろいろ簡略化したことによりActivityへ集約するサンプル | |
# その分、条件ごとにメソッドを作るという、あまり良くない書き方になってしまった | |
case tech | |
when 'android.nfc.tech.NfcA' | |
tech_view = create_nfca_view(tag) | |
when 'android.nfc.tech.NfcB' | |
tech_view = create_nfcb_view(tag) | |
when 'android.nfc.tech.NfcF' | |
tech_view = create_nfcf_view(tag) | |
when 'android.nfc.tech.IsoDep' | |
tech_view = create_isodep_view(tag) | |
when 'android.nfc.tech.MifareUltralight' | |
tech_view = create_mifare_ultralight_view(tag) | |
else | |
next | |
end | |
view.addView(tech_view) | |
end | |
view | |
end | |
def create_nfca_view(tag) | |
nfc = NfcA.get(tag) | |
view = linear_layout orientation: :vertical do | |
@atqa = text_view text: '[NFC-A] atqa: ' + to_hex_string(nfc.getAtqa) | |
@sak = text_view text: '[NFC-A] sak: ' + nfc.getSak.to_s | |
end | |
end | |
def create_nfcb_view(tag) | |
nfc = NfcB.get(tag) | |
view = linear_layout orientation: :vertical do | |
@application_data = text_view text: '[NFC-B] application_data: ' + to_hex_string(nfc.getApplicationData) | |
@protocol_info = text_view text: '[NFC-B] protocol_info: ' + to_hex_string(nfc.getProtocolInfo) | |
end | |
end | |
def create_nfcf_view(tag) | |
nfc = NfcF.get(tag) | |
view = linear_layout orientation: :vertical do | |
@manufacturer = text_view text: '[NFC-F] manufacturer: ' + to_hex_string(nfc.getManufacturer) | |
@system_code = text_view text: '[NFC-F] system_code: ' + to_hex_string(nfc.getSystemCode) | |
end | |
end | |
def create_isodep_view(tag) | |
nfc = IsoDep.get(tag) | |
view = linear_layout orientation: :vertical do | |
@hi_layer_response = text_view text: '[ISO Dep] hi_layer_response: ' + to_hex_string(nfc.getHiLayerResponse) | |
@historical_bytes = text_view text: '[ISO Dep] historical_bytes: ' + to_hex_string(nfc.getHistoricalBytes) | |
end | |
end | |
def create_mifare_ultralight_view(tag) | |
nfc = MifareUltralight.get(tag) | |
view = linear_layout orientation: :vertical do | |
@mifare_ultralight = text_view text: '[Mifare Ultralight] mifare_ultralight_type: ' + nfc.getType.to_s | |
end | |
end | |
def to_hex_string(bytes) | |
bytes.nil? ? '' : bytes.map{ |byte| "%02X" % (byte & 0xff) }.join | |
end | |
def add_delimiter | |
'-' * 6 | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> | |
<tech-list> | |
<tech>android.nfc.tech.IsoDep</tech> | |
</tech-list> | |
<tech-list> | |
<tech>android.nfc.tech.NfcA</tech> | |
</tech-list> | |
<tech-list> | |
<tech>android.nfc.tech.NfcB</tech> | |
</tech-list> | |
<tech-list> | |
<tech>android.nfc.tech.NfcF</tech> | |
</tech-list> | |
<tech-list> | |
<tech>android.nfc.tech.NfcV</tech> | |
</tech-list> | |
<tech-list> | |
<tech>android.nfc.tech.Ndef</tech> | |
</tech-list> | |
<tech-list> | |
<tech>android.nfc.tech.NdefFormatable</tech> | |
</tech-list> | |
<tech-list> | |
<tech>android.nfc.tech.MifareClassic</tech> | |
</tech-list> | |
<tech-list> | |
<tech>android.nfc.tech.MifareUltralight</tech> | |
</tech-list> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment