连接ble
This commit is contained in:
parent
a96ba8549c
commit
8800620f7e
2
Makefile
2
Makefile
|
|
@ -7,4 +7,6 @@ arm:
|
||||||
release: arm
|
release: arm
|
||||||
ssh pi@192.168.0.21 "sudo systemctl stop ble.service"
|
ssh pi@192.168.0.21 "sudo systemctl stop ble.service"
|
||||||
scp ./ble-ibeacon pi@192.168.0.21:~/
|
scp ./ble-ibeacon pi@192.168.0.21:~/
|
||||||
|
|
||||||
|
aaa:
|
||||||
ssh pi@192.168.0.21 "sudo systemctl start ble.service"
|
ssh pi@192.168.0.21 "sudo systemctl start ble.service"
|
||||||
255
main.go
255
main.go
|
|
@ -1,139 +1,164 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"flag"
|
||||||
"encoding/hex"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/influxdata/influxdb/client/v2"
|
|
||||||
"github.com/paypal/gatt"
|
"github.com/paypal/gatt"
|
||||||
"github.com/paypal/gatt/examples/option"
|
"github.com/paypal/gatt/examples/option"
|
||||||
"myschools.me/suguo/ble-ibeacon/influx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type IBeancon struct {
|
var done = make(chan struct{})
|
||||||
Uuid string
|
|
||||||
Major uint16
|
|
||||||
Minor uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewiBeacon(data []byte) (*IBeancon, error) {
|
func onStateChanged(d gatt.Device, s gatt.State) {
|
||||||
if len(data) < 25 || binary.BigEndian.Uint32(data) != 0x4c000215 {
|
fmt.Println("State:", s)
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
chn chan *client.Point
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
chn = make(chan *client.Point, 500)
|
|
||||||
}
|
|
||||||
|
|
||||||
func onPerhipheralDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
|
|
||||||
// if p.ID() != "78:2C:29:16:F2:78" {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// if p.ID() != "90:98:38:F5:DA:60" {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// if p.ID() != "E4:78:41:19:10:98" {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// if !(p.ID() == "C6:1A:05:02:06:C0" || p.ID() == "90:98:38:F5:DA:60") {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// fmt.Println("NAME:", a.LocalName, "T:", time.Now(), "MAC:", p.ID(), "RSSI:", rssi, "LEN:", len(a.ManufacturerData))
|
|
||||||
|
|
||||||
tags := make(map[string]string)
|
|
||||||
tags["mac"] = p.ID()
|
|
||||||
|
|
||||||
fields := make(map[string]interface{})
|
|
||||||
fields["name"] = a.LocalName
|
|
||||||
fields["rssi"] = rssi
|
|
||||||
fields["pl"] = strconv.Itoa(len(a.ManufacturerData)) //包长度
|
|
||||||
fields["power"] = a.TxPowerLevel
|
|
||||||
|
|
||||||
point, err := client.NewPoint(
|
|
||||||
"band",
|
|
||||||
tags,
|
|
||||||
fields,
|
|
||||||
time.Now(),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err.Error())
|
|
||||||
}
|
|
||||||
chn <- point
|
|
||||||
}
|
|
||||||
|
|
||||||
func onStateChanged(device gatt.Device, s gatt.State) {
|
|
||||||
switch s {
|
switch s {
|
||||||
case gatt.StatePoweredOn:
|
case gatt.StatePoweredOn:
|
||||||
device.Scan([]gatt.UUID{}, true)
|
fmt.Println("Scanning...")
|
||||||
|
d.Scan([]gatt.UUID{}, false)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
device.StopScanning()
|
d.StopScanning()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
|
||||||
|
id := strings.ToUpper(flag.Args()[0])
|
||||||
|
if strings.ToUpper(p.ID()) != id {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop scanning once we've got the peripheral we're looking for.
|
||||||
|
p.Device().StopScanning()
|
||||||
|
|
||||||
|
fmt.Printf("\nPeripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
|
||||||
|
fmt.Println(" Local Name =", a.LocalName)
|
||||||
|
fmt.Println(" TX Power Level =", a.TxPowerLevel)
|
||||||
|
fmt.Println(" Manufacturer Data =", a.ManufacturerData)
|
||||||
|
fmt.Println(" Service Data =", a.ServiceData)
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
|
p.Device().Connect(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func onPeriphConnected(p gatt.Peripheral, err error) {
|
||||||
|
fmt.Println("Connected")
|
||||||
|
defer p.Device().CancelConnection(p)
|
||||||
|
|
||||||
|
if err := p.SetMTU(500); err != nil {
|
||||||
|
fmt.Printf("Failed to set MTU, err: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Discovery services
|
||||||
|
ss, err := p.DiscoverServices(nil)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to discover services, err: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range ss {
|
||||||
|
msg := "Service: " + s.UUID().String()
|
||||||
|
if len(s.Name()) > 0 {
|
||||||
|
msg += " (" + s.Name() + ")"
|
||||||
|
}
|
||||||
|
fmt.Println(msg)
|
||||||
|
|
||||||
|
// Discovery characteristics
|
||||||
|
cs, err := p.DiscoverCharacteristics(nil, s)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to discover characteristics, err: %s\n", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cs {
|
||||||
|
msg := " Characteristic " + c.UUID().String()
|
||||||
|
if len(c.Name()) > 0 {
|
||||||
|
msg += " (" + c.Name() + ")"
|
||||||
|
}
|
||||||
|
msg += "\n properties " + c.Properties().String()
|
||||||
|
fmt.Println(msg)
|
||||||
|
|
||||||
|
// Read the characteristic, if possible.
|
||||||
|
if (c.Properties() & gatt.CharRead) != 0 {
|
||||||
|
b, err := p.ReadCharacteristic(c)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to read characteristic, err: %s\n", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Printf(" value %x | %q\n", b, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Discovery descriptors
|
||||||
|
ds, err := p.DiscoverDescriptors(nil, c)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to discover descriptors, err: %s\n", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, d := range ds {
|
||||||
|
msg := " Descriptor " + d.UUID().String()
|
||||||
|
if len(d.Name()) > 0 {
|
||||||
|
msg += " (" + d.Name() + ")"
|
||||||
|
}
|
||||||
|
fmt.Println(msg)
|
||||||
|
|
||||||
|
// Read descriptor (could fail, if it's not readable)
|
||||||
|
b, err := p.ReadDescriptor(d)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to read descriptor, err: %s\n", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Printf(" value %x | %q\n", b, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscribe the characteristic, if possible.
|
||||||
|
if (c.Properties() & (gatt.CharNotify | gatt.CharIndicate)) != 0 {
|
||||||
|
f := func(c *gatt.Characteristic, b []byte, err error) {
|
||||||
|
fmt.Printf("notified: % X | %q\n", b, b)
|
||||||
|
}
|
||||||
|
if err := p.SetNotifyValue(c, f); err != nil {
|
||||||
|
fmt.Printf("Failed to subscribe characteristic, err: %s\n", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Waiting for 5 seconds to get some notifiations, if any.\n")
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
func onPeriphDisconnected(p gatt.Peripheral, err error) {
|
||||||
|
fmt.Println("Disconnected")
|
||||||
|
close(done)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
influx.Init(&influx.Config{
|
flag.Parse()
|
||||||
Address: "http://127.0.0.1:8086",
|
if len(flag.Args()) != 1 {
|
||||||
DBName: "wristband",
|
log.Fatalf("usage: %s [options] peripheral-id\n", os.Args[0])
|
||||||
Username: "abc",
|
|
||||||
Password: "abc",
|
|
||||||
Timeout: 3,
|
|
||||||
})
|
|
||||||
|
|
||||||
// if err := influx.CreateDB("wristband", 10); err != nil {
|
|
||||||
// log.Fatalln("influx.CreateDB", err.Error())
|
|
||||||
// }
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
db, err := influx.New()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
|
|
||||||
Database: "wristband",
|
|
||||||
Precision: "ms",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err.Error())
|
|
||||||
}
|
|
||||||
for i := 1; i < 50; i++ {
|
|
||||||
if d, ok := <-chn; ok {
|
|
||||||
bp.AddPoint(d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := db.Write(bp); err != nil {
|
|
||||||
log.Println("db.Write: ", err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
dev, err := gatt.NewDevice(option.DefaultClientOptions...)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to open device, err:%s\n", err)
|
|
||||||
}
|
}
|
||||||
dev.Handle(
|
|
||||||
gatt.PeripheralDiscovered(onPerhipheralDiscovered),
|
d, err := gatt.NewDevice(option.DefaultClientOptions...)
|
||||||
gatt.CentralConnected(func(c gatt.Central) { fmt.Println("Connect: ", c.ID()) }),
|
if err != nil {
|
||||||
gatt.CentralDisconnected(func(c gatt.Central) { fmt.Println("Disconnect: ", c.ID()) }),
|
log.Fatalf("Failed to open device, err: %s\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register handlers.
|
||||||
|
d.Handle(
|
||||||
|
gatt.PeripheralDiscovered(onPeriphDiscovered),
|
||||||
|
gatt.PeripheralConnected(onPeriphConnected),
|
||||||
|
gatt.PeripheralDisconnected(onPeriphDisconnected),
|
||||||
)
|
)
|
||||||
dev.Init(onStateChanged)
|
|
||||||
select {}
|
d.Init(onStateChanged)
|
||||||
|
<-done
|
||||||
|
fmt.Println("Done")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue