Skip to content

Commit 4f8491d

Browse files
committed
examples: add example showing how to create a peripheral using the Device Information Service
Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent 71e737c commit 4f8491d

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// example that demonstrates how to create a BLE peripheral device with the Device Information Service.
2+
package main
3+
4+
import (
5+
"time"
6+
7+
"tinygo.org/x/bluetooth"
8+
)
9+
10+
var adapter = bluetooth.DefaultAdapter
11+
12+
var (
13+
localName = "TinyGo Device"
14+
15+
manufacturerName = "TinyGo"
16+
manufacturerNameCharacteristic bluetooth.Characteristic
17+
18+
modelNumber = "Model 1"
19+
modelNumberCharacteristic bluetooth.Characteristic
20+
21+
serialNumber = "123456"
22+
serialNumberCharacteristic bluetooth.Characteristic
23+
24+
softwareVersion = "1.0.0"
25+
softwareVersionCharacteristic bluetooth.Characteristic
26+
27+
firmwareVersion = "1.0.0"
28+
firmwareVersionCharacteristic bluetooth.Characteristic
29+
30+
hardwareVersion = "1.0.0"
31+
hardwareVersionCharacteristic bluetooth.Characteristic
32+
)
33+
34+
func main() {
35+
println("starting")
36+
must("enable BLE stack", adapter.Enable())
37+
adv := adapter.DefaultAdvertisement()
38+
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
39+
LocalName: localName,
40+
ServiceUUIDs: []bluetooth.UUID{bluetooth.ServiceUUIDDeviceInformation},
41+
}))
42+
must("start adv", adv.Start())
43+
44+
must("add service", adapter.AddService(&bluetooth.Service{
45+
UUID: bluetooth.ServiceUUIDDeviceInformation,
46+
Characteristics: []bluetooth.CharacteristicConfig{
47+
{
48+
Handle: &manufacturerNameCharacteristic,
49+
UUID: bluetooth.CharacteristicUUIDManufacturerNameString,
50+
Value: []byte(manufacturerName),
51+
Flags: bluetooth.CharacteristicReadPermission,
52+
},
53+
{
54+
Handle: &modelNumberCharacteristic,
55+
UUID: bluetooth.CharacteristicUUIDModelNumberString,
56+
Value: []byte(modelNumber),
57+
Flags: bluetooth.CharacteristicReadPermission,
58+
},
59+
{
60+
Handle: &serialNumberCharacteristic,
61+
UUID: bluetooth.CharacteristicUUIDSerialNumberString,
62+
Value: []byte(serialNumber),
63+
Flags: bluetooth.CharacteristicReadPermission,
64+
},
65+
{
66+
Handle: &softwareVersionCharacteristic,
67+
UUID: bluetooth.CharacteristicUUIDSoftwareRevisionString,
68+
Value: []byte(softwareVersion),
69+
Flags: bluetooth.CharacteristicReadPermission,
70+
},
71+
{
72+
Handle: &firmwareVersionCharacteristic,
73+
UUID: bluetooth.CharacteristicUUIDFirmwareRevisionString,
74+
Value: []byte(firmwareVersion),
75+
Flags: bluetooth.CharacteristicReadPermission,
76+
},
77+
{
78+
Handle: &hardwareVersionCharacteristic,
79+
UUID: bluetooth.CharacteristicUUIDHardwareRevisionString,
80+
Value: []byte(hardwareVersion),
81+
Flags: bluetooth.CharacteristicReadPermission,
82+
},
83+
},
84+
}))
85+
86+
for {
87+
time.Sleep(time.Second)
88+
}
89+
}
90+
91+
func must(action string, err error) {
92+
if err != nil {
93+
panic("failed to " + action + ": " + err.Error())
94+
}
95+
}

0 commit comments

Comments
 (0)