微信小程序nfc指令异常记录

小程序nfc相关代码: 

readEvent(){
    
    wx.getNFCAdapter().startDiscovery({
      success:(res)=>{
        console.log('--------------start--------')
        console.log(res);
        wx.getNFCAdapter().onDiscovered(
          callback=>{
            console.log('------------onDiscovered----------')
            console.log(callback);
            console.log(Array.from(new Uint8Array(callback.id)))
            console.log('------------getNfcA----------')
            console.log('------------connect----------')
            wx.getNFCAdapter().getNfcA().connect({
              success:(res)=>{
                console.info(res)
                console.log('------------transceive----------')
                wx.getNFCAdapter().getNfcA().transceive({
                  data: new Uint8Array([0x30, 0x04]).buffer, // 必须要传入 ArrayBuffer
                  success: (res) => {
                      // res.data 是 ArrayBuffer,转成数组方便查看
                      console.log(Array.from(new Uint8Array(res.data)));
                  },
                  fail:(err)=>{
                    console.log(err)
                  }
              })
              
              },
              fail:(err)=>{
                console.error(err);
              }
            })
          }
        );
      },
      fail:(err)=>{

      }
    });
    
  },

connect 成功但是指令报错,报错信息:

transceive:fail:system internal error:tag was lost

解决方式,替换NfcA

readEvent(){
    
    wx.getNFCAdapter().startDiscovery({
      success:(res)=>{
        console.log('--------------start--------')
        console.log(res);
        wx.getNFCAdapter().onDiscovered(
          callback=>{
            console.log('------------onDiscovered----------')
            console.log(callback);
            console.log(Array.from(new Uint8Array(callback.id)))
            console.log('------------getNfcA----------')
            console.log('------------connect----------')
            wx.getNFCAdapter().getIsoDep().connect({
              success:(res)=>{
                console.info(res)
                console.log('------------transceive----------')
                wx.getNFCAdapter().getIsoDep().transceive({
                  data: new Uint8Array([0x30, 0x04]).buffer, // 必须要传入 ArrayBuffer
                  success: (res) => {
                      // res.data 是 ArrayBuffer,转成数组方便查看
                      console.log(Array.from(new Uint8Array(res.data)));
                  },
                  fail:(err)=>{
                    console.log(err)
                  }
              })
              
              },
              fail:(err)=>{
                console.error(err);
              }
            })
          }
        );
      },
      fail:(err)=>{

      }
    });
    
  },

你可能感兴趣的:(微信小程序,小程序)