forked from tinygo-org/bluetooth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8dc1e15
commit 20f0ce6
Showing
4 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// This example advertises for 5 minutes after | ||
// - boot | ||
// - disconnect | ||
// and then stops advertising. | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
"tinygo.org/x/bluetooth" | ||
) | ||
|
||
var adapter = bluetooth.DefaultAdapter | ||
|
||
var advUntil = time.Now().Add(5 * time.Minute) | ||
var advState = true | ||
|
||
func main() { | ||
must("enable BLE stack", adapter.Enable()) | ||
adv := adapter.DefaultAdvertisement() | ||
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{ | ||
LocalName: "Go Bluetooth", | ||
})) | ||
adapter.SetConnectHandler(func(device bluetooth.Addresser, connected bool) { | ||
if connected { | ||
println("connected, not advertising...") | ||
advState = false | ||
} else { | ||
println("disconnected, advertising...") | ||
advState = true | ||
advUntil = time.Now().Add(5 * time.Minute) | ||
} | ||
}) | ||
must("start adv", adv.Start()) | ||
|
||
println("advertising...") | ||
address, _ := adapter.Address() | ||
for { | ||
if advState && time.Now().After(advUntil) { | ||
println("timeout, not advertising...") | ||
advState = false | ||
must("stop adv", adv.Stop()) | ||
} | ||
println("Go Bluetooth /", address.MAC.String()) | ||
time.Sleep(time.Second) | ||
} | ||
} | ||
|
||
func must(action string, err error) { | ||
if err != nil { | ||
panic("failed to " + action + ": " + err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters