-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ndef.go
58 lines (47 loc) · 1.25 KB
/
ndef.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"bytes"
"encoding/binary"
"github.com/hsanjuan/go-ndef"
)
var NDEF_END = []byte{0xFE}
var NDEF_START = []byte{0x54, 0x02, 0x65, 0x6E}
func ParseRecordText(blocks []byte) string {
// Find the text NDEF record
startIndex := bytes.Index(blocks, NDEF_START)
endIndex := bytes.Index(blocks, NDEF_END)
if startIndex != -1 && endIndex != -1 {
tagText := string(blocks[startIndex+4 : endIndex])
return tagText
}
return ""
}
func BuildMessage(text string) ([]byte, error) {
msg := ndef.NewTextMessage(text, "en")
var payload, err = msg.Marshal()
if err != nil {
return nil, err
}
var header, _ = CalculateNdefHeader(payload)
if err != nil {
return nil, err
}
payload = append(header, payload...)
payload = append(payload, []byte{0xFE}...)
return payload, nil
}
func CalculateNdefHeader(ndefRecord []byte) ([]byte, error) {
var recordLength = len(ndefRecord)
if recordLength < 255 {
return []byte{0x03, byte(len(ndefRecord))}, nil
}
// NFCForum-TS-Type-2-Tag_1.1.pdf Page 9
// > 255 Use three consecutive bytes format
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, uint16(recordLength))
if err != nil {
return nil, err
}
var header = []byte{0x03, 0xFF}
return append(header, buf.Bytes()...), nil
}