【问题标题】:Unexpected prefix is added to URL/URI when writing to NFC tag写入 NFC 标签时,向 URL/URI 添加了意外的前缀
【发布时间】:2018-06-09 06:41:04
【问题描述】:

我正在尝试将 URI/URL 写入 NFC 标签。我能够成功地做到这一点,但是有一个前缀会自动添加到我编写的 URI 中。

例如:如果我要写的URL是“https://indies.net.in”,那么标签上实际写的URL是“https://www.enindies.net.in” ”。

谁能告诉我我在这里做错了什么?

创建消息:

private NdefMessage createNdefMessage(String content){
    NdefRecord ndefRecord= createTextRecord(content);
    NdefMessage ndefMessage=new NdefMessage(new NdefRecord[]{ndefRecord});
    return ndefMessage;
}

创建 URL 记录:

private NdefRecord createUrlRecord(String content) {
    try{
        byte[] language;
        language= Locale.getDefault().getLanguage().getBytes();
        final byte[] text=content.getBytes("UTF-8");
        final int languageSize=language.length;
        final int textLength=text.length;
        final ByteArrayOutputStream payload= new ByteArrayOutputStream(1+languageSize+textLength);

        payload.write((byte) (languageSize & 0x1F));
        payload.write(language,0,languageSize);
        payload.write(text,0,textLength);

        return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,NdefRecord.RTD_URI,new byte[0],payload.toByteArray());
    }catch (Exception e){
        Log.e("createTextRecord",e.getMessage());
    }
    return  null;
}

写信息:

private void writeNdefMessage(Tag tag, NdefMessage ndefMessage){
    try {
        if (tag== null){
            Toast.makeText(this," Tag object cannot be null",Toast.LENGTH_SHORT).show();
            return;
        }

        Ndef ndef=Ndef.get(tag);
        if (ndef==null){
            formatTag(tag,ndefMessage);
        } else {
            ndef.connect();
            if (!ndef.isWritable()){
                Toast.makeText(this," Tag cannot be Written",Toast.LENGTH_SHORT).show();
                ndef.close();
                return;
            }
            ndef.writeNdefMessage(ndefMessage);
            ndef.close();
            Toast.makeText(this," Tag Written!",Toast.LENGTH_SHORT).show();
        }

【问题讨论】:

    标签: android format uri nfc ndef


    【解决方案1】:

    您将 URL 写入 NFC 标签的方法的问题在于,您似乎从最初用于编写 NFC 论坛 文本 记录的代码中复制了该方法。您的方法似乎最初被称为“createTextRecord”这一事实也表明了这一点(至少您在 createNdefMessage 中是这样称呼的)。

    现在的问题是文本记录和 URI 记录具有完全不同的格式。因此,如果您将文本记录的有效负载放入 URI 记录中,您将得到意想不到的结果。在您的情况下,状态字节(文本记录的第一个字节,包含语言字段的大小)被映射到 URI 记录的标识符代码。由于该字段的值为 0x02,因此将其转换为 URI 前缀“https://www.”。此外,语言字段本身(包含语言代码“en”)已添加到 URI。

    一般来说,您会希望使用NdefRecord.createUri() 方法为您的网址创建正确的URI 记录。这将根据 NFC 论坛 URI 记录类型定义的压缩方案自动处理标准化和压缩 URI。

    NdefRecord ndefRecord= NdefRecord.createUri("https://indies.net.in");
    

    但是,如果您需要支持 API 14 之前的平台(引入此方法),您也可以手动创建 URI 记录。例如,如果您不关心压缩,则可以使用:

    private NdefRecord createUrlRecord(String url) {
        byte[] uriBytes = content.getBytes("UTF-8");
    
        byte[] payload = new byte[uriBytes.length + 1];
        payload[0] = (byte)0x00;
        System.arraycopy(uriBytes, 0, payload, 1, uriBytes.length);
    
        return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                              NdefRecord.RTD_URI,
                              null,
                              payload);
    }
    
    NdefRecord ndefRecord= createUrlRecord("https://indies.net.in");
    

    【讨论】:

    • 谢谢迈克尔...是的,我确实使用了一个教程作为基础并且卡住了。谢谢你回答这个菜鸟。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2013-02-01
    • 2013-06-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多