| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"log" |
| 8 |
|
| 9 |
"github.com/paypal/gatt" |
| 10 |
"github.com/paypal/gatt/examples/option" |
| 11 |
) |
| 12 |
|
| 13 |
func onStateChanged(d gatt.Device, s gatt.State) { |
| 14 |
fmt.Println("State:", s) |
| 15 |
switch s { |
| 16 |
case gatt.StatePoweredOn: |
| 17 |
fmt.Println("scanning...") |
| 18 |
d.Scan([]gatt.UUID{}, false) |
| 19 |
return |
| 20 |
default: |
| 21 |
d.StopScanning() |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) { |
| 26 |
fmt.Printf("\nPeripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name()) |
| 27 |
fmt.Println(" Local Name =", a.LocalName) |
| 28 |
fmt.Println(" TX Power Level =", a.TxPowerLevel) |
| 29 |
fmt.Println(" Manufacturer Data =", a.ManufacturerData) |
| 30 |
fmt.Println(" Service Data =", a.ServiceData) |
| 31 |
} |
| 32 |
|
| 33 |
func main() { |
| 34 |
d, err := gatt.NewDevice(option.DefaultClientOptions...) |
| 35 |
if err != nil { |
| 36 |
log.Fatalf("Failed to open device, err: %s\n", err) |
| 37 |
return |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
d.Handle(gatt.PeripheralDiscovered(onPeriphDiscovered)) |
| 42 |
d.Init(onStateChanged) |
| 43 |
select {} |
| 44 |
} |