From ef51dbe795d4ee1ce7875548b135dab1f5cf3c83 Mon Sep 17 00:00:00 2001 From: "suguo.yao" Date: Thu, 16 Dec 2021 14:19:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0RSSI=E5=92=8CMAC=E5=88=A4?= =?UTF-8?q?=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index f548dcc..fbc4c44 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,49 @@ package main import ( + "encoding/binary" + "encoding/hex" + "errors" "fmt" "log" + "strings" "github.com/paypal/gatt" "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) { - 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) {