package com.mq.mylibrary.rfid.io.service;

public class NdefPacket {
	
	static String MB = "1"; //说明这是首记录  11010001
	static String ME = "1";//说明这是末记录(说明消息只有一个记录)
	static String CF = "0"; //说明记录未被切块
	static String SR = "1";//说明这是短记录，PAYLOAD_LENGTH 占1字节
	static String IL = "0";//说明没有 ID_LENGTH 和 ID 域
	static String TNF = "001"; //TNf占三个位
	
	static byte[] HEAD = {0x44,0x03}; //第五页的前两个字节
	static byte	TOTAL_LENGTH;  //内容的总长度
	private static byte FirstByte = Tools.hexStringToBytes(
			Integer.toHexString(Integer.parseInt(MB+ME+CF+SR+IL+TNF, 2)))[0];//写入内容的第一个字节
	private  byte TYPE_LENGTH = 0x02; //类型的长度
	private  byte PAYLOAD_LENGTH;//payload的长度
	private  byte[] TYPE = Tools.hexStringToBytes(Tools.str2HexStr("Sp"));// 内容的类型
	
	private PayLoad payLoad;
	
	/**
	 * ndef的数据包
	 * @param strUri
	 * @return
	 */
	public byte[] getNdefPacket(String strUri){
		
		byte[] ndefPacket;
		payLoad = new PayLoad(strUri);
		byte[] uriPayload = payLoad.getPayLoad();
		byte[] textPayload = new NdefText().getNdefText();
		ndefPacket = new byte[uriPayload.length+8+textPayload.length];
		PAYLOAD_LENGTH = (byte)(uriPayload.length+textPayload.length);
		
		ndefPacket[0] = HEAD[0];
		ndefPacket[1] = HEAD[1];
		ndefPacket[3] = FirstByte;
		ndefPacket[4] = TYPE_LENGTH;
		ndefPacket[5] = PAYLOAD_LENGTH;
		ndefPacket[6] = TYPE[0];
		ndefPacket[7] = TYPE[1];
		
		for(int i = 0;i<textPayload.length;i++){
			ndefPacket[i+8] = textPayload[i];
		}
		
		for(int i = 0;i<uriPayload.length;i++){
			ndefPacket[i+textPayload.length+8] = uriPayload[i];
		}
		TOTAL_LENGTH = (byte)(ndefPacket.length-3);
		ndefPacket[2] = TOTAL_LENGTH; 
		return ndefPacket;
	}
	
}
