实现RSSI和MAC判读
This commit is contained in:
parent
4cfb26b1aa
commit
ef51dbe795
36
main.go
36
main.go
|
|
@ -1,15 +1,49 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/paypal/gatt"
|
"github.com/paypal/gatt"
|
||||||
"github.com/paypal/gatt/examples/option"
|
"github.com/paypal/gatt/examples/option"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type IBeancon struct {
|
||||||
|
Uuid string
|
||||||
|
Major uint16
|
||||||
|
Minor uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewiBeacon(data []byte) (*IBeancon, error) {
|
||||||
|
if len(data) < 25 || binary.BigEndian.Uint32(data) != 0x4c000215 {
|
||||||
|
return nil, errors.New("Not an iBeacon")
|
||||||
|
}
|
||||||
|
beacon := new(IBeancon)
|
||||||
|
beacon.Uuid = strings.ToUpper(hex.EncodeToString(data[4:8]) + hex.EncodeToString(data[8:10]) + hex.EncodeToString(data[10:12]) + hex.EncodeToString(data[12:14]) + hex.EncodeToString(data[14:20]))
|
||||||
|
beacon.Major = binary.BigEndian.Uint16(data[20:22])
|
||||||
|
beacon.Minor = binary.BigEndian.Uint16(data[22:24])
|
||||||
|
return beacon, nil
|
||||||
|
}
|
||||||
|
|
||||||
func onPerhipheralDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
|
func onPerhipheralDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
|
||||||
fmt.Println(string(a.ManufacturerData))
|
// if p.ID() != "C6:1A:05:02:06:C0" {
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
if p.ID() != "90:98:38:F5:DA:60" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println("MAC:", p.ID(), "RSSI:", rssi, "LEN:", len(a.ManufacturerData))
|
||||||
|
|
||||||
|
b, err := NewiBeacon(a.ManufacturerData)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("UUID: %s Major: %d Minor: %d RSSI: %d\n", b.Uuid, b.Major, b.Minor, rssi)
|
||||||
}
|
}
|
||||||
|
|
||||||
func onStateChanged(device gatt.Device, s gatt.State) {
|
func onStateChanged(device gatt.Device, s gatt.State) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue