Method NdefRecord.createTextRecord(“en” , “string”) not working below API level 21

前端 未结 2 1259
小蘑菇
小蘑菇 2020-12-04 02:59

This code works fine when I use it on a device with Android Lollipop (5.x) or Marshmallow (6.0):

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NdefMessage          


        
相关标签:
2条回答
  • 2020-12-04 03:23

    The method NdefRecord.createTextRecord() was introduced in API level 21. Consequently, it is not available on platforms below that API level. However, you could easily assemble the Text record on your own. The payload of a Text record consists of a status byte, a language code field, and a text field:

    +-------------+---------------+--------------------------+
    | Status byte | Language code | Text                     |
    | (1 byte)    | (n byte)      | (m byte)                 |
    +-------------+---------------+--------------------------+
    
    • The status byte indicates the character encoding of the text field (0 = UTF-8, 1 = UTF-16) in bit 7 and the length n of the language code in bits 5..0. Bit 6 must always be zero.
    • The language code filed contains an IANA language code encoded in US-ASCII (e.g. "en").

    You could create the Text record using this method:

    public static NdefRecord createTextRecord(String language, String text) {
        byte[] languageBytes;
        byte[] textBytes;
        try {
            languageBytes = language.getBytes("US-ASCII");
            textBytes = text.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new AssertionError(e);
        }
    
        byte[] recordPayload = new byte[1 + (languageBytes.length & 0x03F) + textBytes.length];
    
        recordPayload[0] = (byte)(languageBytes.length & 0x03F);
        System.arraycopy(languageBytes, 0, recordPayload, 1, languageBytes.length & 0x03F);
        System.arraycopy(textBytes, 0, recordPayload, 1 + (languageBytes.length & 0x03F), textBytes.length);
    
        return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, null, recordPayload);
    }
    
    NdefRecord r = createTextRecord("en", content);
    
    0 讨论(0)
  • 2020-12-04 03:27

    Yes, createTextRecord is introduced in API 21, so you can't call it in the previous versions. https://developer.android.com/reference/android/nfc/NdefRecord.html

    Check if your API level is 21 before to call createTextRecord.

    public NdfeMessage create(String content){
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
            NdefRecord record = NdefRecord.createTextRecord("en", content);
            NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
            return msg;
        } else{
            return null;
        }
    }
    
    0 讨论(0)
提交回复
热议问题