-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1003 from kaloudis/android-nfc-linking
Android: set up NFC app linking
- Loading branch information
Showing
9 changed files
with
142 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// based on Blixt LNDMobileTools | ||
// https://github.com/hsjoberg/blixt-wallet/blob/master/android/app/src/main/java/com/blixtwallet/MainApplication.java | ||
package app.zeusln.zeus; | ||
|
||
import android.nfc.Tag; | ||
import android.nfc.NfcAdapter; | ||
import android.nfc.NdefMessage; | ||
import android.nfc.tech.Ndef; | ||
import android.nfc.NdefRecord; | ||
|
||
import java.util.Arrays; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.Promise; | ||
|
||
import com.hypertrack.hyperlog.HyperLog; | ||
|
||
class MobileTools extends ReactContextBaseJavaModule { | ||
final String TAG = "MobileTools"; | ||
|
||
public MobileTools(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "MobileTools"; | ||
} | ||
|
||
@ReactMethod | ||
public void getIntentNfcData(Promise promise) { | ||
// https://code.tutsplus.com/tutorials/reading-nfc-tags-with-android--mobile-17278 | ||
Tag tag = getReactApplicationContext() | ||
.getCurrentActivity().getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG); | ||
if (tag == null) { | ||
promise.resolve(null); | ||
return; | ||
} | ||
|
||
Ndef ndef = Ndef.get(tag); | ||
if (ndef == null) { | ||
HyperLog.d(TAG, "NFC tag is not NDEF"); | ||
promise.resolve(null); | ||
} | ||
|
||
NdefMessage ndefMessage = ndef.getCachedNdefMessage(); | ||
|
||
NdefRecord[] records = ndefMessage.getRecords(); | ||
if (records.length > 0) { | ||
// Get first record and ignore the rest | ||
NdefRecord record = records[0]; | ||
if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) { | ||
/* | ||
* See NFC forum specification for "Text Record Type Definition" at 3.2.1 | ||
* | ||
* http://www.nfc-forum.org/specs/ | ||
* | ||
* bit_7 defines encoding | ||
* bit_6 reserved for future use, must be 0 | ||
* bit_5..0 length of IANA language code | ||
*/ | ||
byte[] payload = record.getPayload(); | ||
|
||
// Get the Text Encoding | ||
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; | ||
|
||
// Get the Language Code | ||
int languageCodeLength = payload[0] & 0063; | ||
|
||
// String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII"); | ||
// e.g. "en" | ||
|
||
try { | ||
String s = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); | ||
promise.resolve(s); | ||
return; | ||
} catch (UnsupportedEncodingException e) { | ||
HyperLog.e(TAG, "Error returning ndef data", e); | ||
} | ||
} | ||
else { | ||
HyperLog.d(TAG, "Cannot read NFC Tag Record"); | ||
} | ||
} | ||
promise.resolve(null); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
android/app/src/main/java/com/zeus/MobileToolsPackage.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package app.zeusln.zeus; | ||
|
||
import com.facebook.react.ReactPackage; | ||
import com.facebook.react.bridge.NativeModule; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.uimanager.ViewManager; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class MobileToolsPackage implements ReactPackage { | ||
@Override | ||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { | ||
return Arrays.asList(new MobileTools(reactContext)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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