|
| 1 | +// This example scans and then connects to a specific Bluetooth peripheral |
| 2 | +// that can provide the Heart Rate Service (HRS). |
| 3 | +// |
| 4 | +// Once connected, it subscribes to notifications for the data value, and |
| 5 | +// displays it. |
| 6 | +// |
| 7 | +// To run this on a desktop system: |
| 8 | +// |
| 9 | +// go run ./examples/heartrate-monitor EE:74:7D:C9:2A:68 |
| 10 | +// |
| 11 | +// To run this on a microcontroller, change the constant value in the file |
| 12 | +// "mcu.go" to set the MAC address of the device you want to discover. |
| 13 | +// Then, flash to the microcontroller board like this: |
| 14 | +// |
| 15 | +// tinygo flash -o circuitplay-bluefruit ./examples/heartrate-monitor |
| 16 | +// |
| 17 | +// Once the program is flashed to the board, connect to the USB port |
| 18 | +// via serial to view the output. |
| 19 | +// |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "tinygo.org/x/bluetooth" |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + adapter = bluetooth.DefaultAdapter |
| 28 | + |
| 29 | + heartRateServiceUUID = bluetooth.New16BitUUID(0x180D) |
| 30 | + heartRateCharacteristicUUID = bluetooth.New16BitUUID(0x2A37) |
| 31 | +) |
| 32 | + |
| 33 | +func main() { |
| 34 | + println("enabling") |
| 35 | + |
| 36 | + // Enable BLE interface. |
| 37 | + must("enable BLE stack", adapter.Enable()) |
| 38 | + |
| 39 | + ch := make(chan bluetooth.ScanResult, 1) |
| 40 | + |
| 41 | + // Start scanning. |
| 42 | + println("scanning...") |
| 43 | + err := adapter.Scan(func(adapter *bluetooth.Adapter, result bluetooth.ScanResult) { |
| 44 | + println("found device:", result.Address.String(), result.RSSI, result.LocalName()) |
| 45 | + if result.Address.String() == connectAddress() { |
| 46 | + adapter.StopScan() |
| 47 | + ch <- result |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + var device *bluetooth.Device |
| 52 | + select { |
| 53 | + case result := <-ch: |
| 54 | + device, err = adapter.Connect(result.Address, bluetooth.ConnectionParams{}) |
| 55 | + if err != nil { |
| 56 | + println(err.Error()) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + println("connected to ", result.Address.String()) |
| 61 | + } |
| 62 | + |
| 63 | + // get services |
| 64 | + println("discovering services/characteristics") |
| 65 | + srvcs, err := device.DiscoverServices([]bluetooth.UUID{heartRateServiceUUID}) |
| 66 | + must("discover services", err) |
| 67 | + |
| 68 | + if len(srvcs) == 0 { |
| 69 | + panic("could not find heart rate service") |
| 70 | + } |
| 71 | + |
| 72 | + srvc := srvcs[0] |
| 73 | + |
| 74 | + println("found service", srvc.UUID().String()) |
| 75 | + |
| 76 | + chars, err := srvc.DiscoverCharacteristics([]bluetooth.UUID{heartRateCharacteristicUUID}) |
| 77 | + if err != nil { |
| 78 | + println(err) |
| 79 | + } |
| 80 | + |
| 81 | + if len(chars) == 0 { |
| 82 | + panic("could not find heart rate characteristic") |
| 83 | + } |
| 84 | + |
| 85 | + char := chars[0] |
| 86 | + println("found characteristic", char.UUID().String()) |
| 87 | + |
| 88 | + char.EnableNotifications(func(buf []byte) { |
| 89 | + println("data:", uint8(buf[0])) |
| 90 | + }) |
| 91 | + |
| 92 | + select {} |
| 93 | +} |
| 94 | + |
| 95 | +func must(action string, err error) { |
| 96 | + if err != nil { |
| 97 | + panic("failed to " + action + ": " + err.Error()) |
| 98 | + } |
| 99 | +} |
0 commit comments