|
| 1 | +// Test for setting connection parameters. |
| 2 | +// |
| 3 | +// To test this feature, run this either on a desktop OS or by flashing it to a |
| 4 | +// device with TinyGo. Then connect to it from a BLE connection debugger, for |
| 5 | +// example nRF Connect on Android. After a second, you should see in the log of |
| 6 | +// the BLE app that the connection latency has been updated. It might look |
| 7 | +// something like this: |
| 8 | +// |
| 9 | +// Connection parameters updated (interval: 510.0ms, latency: 0, timeout: 10000ms) |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "time" |
| 14 | + |
| 15 | + "tinygo.org/x/bluetooth" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + adapter = bluetooth.DefaultAdapter |
| 20 | + newDevice chan bluetooth.Device |
| 21 | +) |
| 22 | + |
| 23 | +func main() { |
| 24 | + must("enable BLE stack", adapter.Enable()) |
| 25 | + |
| 26 | + newDevice = make(chan bluetooth.Device, 1) |
| 27 | + adapter.SetConnectHandler(func(device bluetooth.Device, connected bool) { |
| 28 | + // If this is a new device, signal it to the separate goroutine. |
| 29 | + if connected { |
| 30 | + select { |
| 31 | + case newDevice <- device: |
| 32 | + default: |
| 33 | + } |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + // Start advertising, so we can be found. |
| 38 | + const name = "Go BLE test" |
| 39 | + adv := adapter.DefaultAdvertisement() |
| 40 | + adv.Configure(bluetooth.AdvertisementOptions{ |
| 41 | + LocalName: name, |
| 42 | + }) |
| 43 | + adv.Start() |
| 44 | + println("advertising:", name) |
| 45 | + |
| 46 | + for device := range newDevice { |
| 47 | + println("connection from device:", device.Address.String()) |
| 48 | + |
| 49 | + // Discover services and characteristics. |
| 50 | + svcs, err := device.DiscoverServices(nil) |
| 51 | + if err != nil { |
| 52 | + println(" failed to resolve services:", err) |
| 53 | + } |
| 54 | + for _, svc := range svcs { |
| 55 | + println(" service:", svc.UUID().String()) |
| 56 | + chars, err := svc.DiscoverCharacteristics(nil) |
| 57 | + if err != nil { |
| 58 | + println(" failed to resolve characteristics:", err) |
| 59 | + } |
| 60 | + for _, char := range chars { |
| 61 | + println(" characteristic:", char.UUID().String()) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Update connection parameters (as a test). |
| 66 | + time.Sleep(time.Second) |
| 67 | + err = device.SetConnectionLatency(bluetooth.ConnectionLatency{ |
| 68 | + MinInterval: bluetooth.NewDuration(495 * time.Millisecond), |
| 69 | + MaxInterval: bluetooth.NewDuration(510 * time.Millisecond), |
| 70 | + Timeout: bluetooth.NewDuration(10 * time.Second), |
| 71 | + }) |
| 72 | + if err != nil { |
| 73 | + println(" failed to update connection parameters:", err) |
| 74 | + continue |
| 75 | + } |
| 76 | + println(" updated connection parameters") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func must(action string, err error) { |
| 81 | + if err != nil { |
| 82 | + panic("failed to " + action + ": " + err.Error()) |
| 83 | + } |
| 84 | +} |
0 commit comments