- 
                Notifications
    You must be signed in to change notification settings 
- Fork 173
examples: add example that makes connections to multiple peripherals #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            deadprogram
  wants to merge
  1
  commit into
  dev
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
multiple-example
  
      
      
   
  
    
  
  
  
 
  
      
    base: dev
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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,146 @@ | ||
| // This example scans and then connects to multiple Bluetooth peripherals | ||
| // that provide the Heart Rate Service (HRS). | ||
| // | ||
| // Once connected to all the desired devices, it subscribes to notifications. | ||
| // | ||
| // To run on bare metal microcontroller: | ||
| // tinygo flash -target metro-m4-airlift -ldflags="-X main.Devices=D9:2A:A1:5C:ED:56,4D:A1:3C:24:F0:46" -monitor ./examples/multiples/ | ||
| // | ||
| // To run on OS: | ||
| // go run ./examples/multiples/ D9:2A:A1:5C:ED:56,64:0B:1D:46:D8:1D | ||
| package main | ||
|  | ||
| import ( | ||
| "context" | ||
| "os" | ||
| "slices" | ||
| "time" | ||
|  | ||
| "tinygo.org/x/bluetooth" | ||
| ) | ||
|  | ||
| var ( | ||
| adapter = bluetooth.DefaultAdapter | ||
|  | ||
| heartRateServiceUUID = bluetooth.ServiceUUIDHeartRate | ||
| heartRateCharacteristicUUID = bluetooth.CharacteristicUUIDHeartRateMeasurement | ||
|  | ||
| exitCtx context.Context | ||
| ) | ||
|  | ||
| func main() { | ||
| exitCtx = initExitHandler() | ||
|  | ||
| println("enabling") | ||
|  | ||
| // Enable BLE interface. | ||
| must("enable BLE stack", adapter.Enable()) | ||
|  | ||
| scanResults := make(map[string]bluetooth.ScanResult) | ||
| finished := make(chan bool, 1) | ||
|  | ||
| searchList, _ := connectAddresses() | ||
|  | ||
| // Start scanning. | ||
| println("scanning...") | ||
| err := adapter.Scan(func(adapter *bluetooth.Adapter, result bluetooth.ScanResult) { | ||
| print(".") | ||
| // is the scanned device one of the ones we want? | ||
| if slices.Contains(searchList, result.Address.String()) { | ||
| if _, ok := scanResults[result.Address.String()]; !ok { | ||
| println(".") | ||
| println("found device:", result.Address.String(), result.RSSI, result.LocalName()) | ||
| scanResults[result.Address.String()] = result | ||
| } | ||
|  | ||
| if len(scanResults) == len(searchList) { | ||
| println("All found.") | ||
| adapter.StopScan() | ||
| finished <- true | ||
| } | ||
| } | ||
| select { | ||
| case <-exitCtx.Done(): | ||
| println("exiting.") | ||
| os.Exit(0) | ||
| default: | ||
| } | ||
| }) | ||
| must("scan", err) | ||
|  | ||
| devices := []bluetooth.Device{} | ||
| select { | ||
| case <-time.After(5 * time.Second): | ||
| failMessage("timed out") | ||
| return | ||
| case <-exitCtx.Done(): | ||
| println("exiting.") | ||
| return | ||
| case <-finished: | ||
| } | ||
|  | ||
| defer func() { | ||
| for _, device := range devices { | ||
| device.Disconnect() | ||
| } | ||
| }() | ||
|  | ||
| // now connect to all devices | ||
| for _, result := range scanResults { | ||
| device, err := adapter.Connect(result.Address, bluetooth.ConnectionParams{}) | ||
| if err != nil { | ||
| failMessage(err.Error()) | ||
| return | ||
| } | ||
|  | ||
| println("connected to", result.Address.String()) | ||
| devices = append(devices, device) | ||
| } | ||
|  | ||
| // get services | ||
| println("discovering services/characteristics") | ||
|  | ||
| for _, device := range devices { | ||
| srvcs, err := device.DiscoverServices([]bluetooth.UUID{heartRateServiceUUID}) | ||
| must("discover services", err) | ||
|  | ||
| if len(srvcs) == 0 { | ||
| failMessage("could not find heart rate service") | ||
| return | ||
| } | ||
|  | ||
| srvc := srvcs[0] | ||
|  | ||
| println("found service", srvc.UUID().String(), "for device", device.Address.String()) | ||
|  | ||
| chars, err := srvc.DiscoverCharacteristics([]bluetooth.UUID{heartRateCharacteristicUUID}) | ||
| if err != nil { | ||
| failMessage(err.Error()) | ||
| return | ||
| } | ||
|  | ||
| if len(chars) == 0 { | ||
| failMessage("could not find heart rate characteristic") | ||
| return | ||
| } | ||
|  | ||
| char := chars[0] | ||
| addr := device.Address.String() | ||
| println("found characteristic", char.UUID().String(), "for device", addr) | ||
|  | ||
| char.EnableNotifications(func(buf []byte) { | ||
| println(addr, "data:", uint8(buf[1])) | ||
| }) | ||
| } | ||
|  | ||
| // wait for exit | ||
| <-exitCtx.Done() | ||
| println("exiting.") | ||
| } | ||
|  | ||
| func must(action string, err error) { | ||
| if err != nil { | ||
| failMessage("failed to " + action + ": " + err.Error()) | ||
| return | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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,37 @@ | ||
| //go:build baremetal | ||
|  | ||
| package main | ||
|  | ||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|  | ||
| // Devices are the MAC addresses of the Bluetooth peripherals you want to connect to. | ||
| // Replace this by using -ldflags="-X main.Devices='[MAC ADDRESS],[MAC ADDRESS]'" | ||
| // where [MAC ADDRESS] is the actual MAC address of the peripheral. | ||
| // For example: | ||
| // tinygo flash -target nano-rp2040 -ldflags="-X main.Devices='7B:36:98:8C:41:1C,7B:36:98:8C:41:1D" ./examples/heartrate-monitor/ | ||
| var Devices string | ||
|  | ||
| func initExitHandler() context.Context { | ||
| return context.Background() | ||
| } | ||
|  | ||
| func connectAddresses() ([]string, error) { | ||
| addrs := strings.Split(Devices, ",") | ||
| if len(addrs) == 0 { | ||
| return nil, errors.New("no devices specified") | ||
| } | ||
|  | ||
| return addrs, nil | ||
| } | ||
|  | ||
| func failMessage(msg string) { | ||
| for { | ||
| println(msg) | ||
| time.Sleep(1 * time.Second) | ||
| } | ||
| } | ||
  
    
      This file contains hidden or 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,49 @@ | ||||||
| //go:build !baremetal | ||||||
|  | ||||||
| package main | ||||||
|  | ||||||
| import ( | ||||||
| "context" | ||||||
| "errors" | ||||||
| "os" | ||||||
| "os/signal" | ||||||
| "strings" | ||||||
| "syscall" | ||||||
| ) | ||||||
|  | ||||||
| func initExitHandler() context.Context { | ||||||
| return contextWithSignal(context.Background()) | ||||||
| } | ||||||
|  | ||||||
| // ContextWithSignal creates a context canceled when SIGINT or SIGTERM are notified | ||||||
| func contextWithSignal(ctx context.Context) context.Context { | ||||||
| newCtx, cancel := context.WithCancel(ctx) | ||||||
| signals := make(chan os.Signal) | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
 My editor warned me the channel shall be buffered here. | ||||||
| signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) | ||||||
| go func() { | ||||||
| select { | ||||||
| case <-signals: | ||||||
| cancel() | ||||||
| } | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole block can be simplified:  | ||||||
| }() | ||||||
| return newCtx | ||||||
| } | ||||||
|  | ||||||
| func connectAddresses() ([]string, error) { | ||||||
| if len(os.Args) < 2 { | ||||||
| println("usage: multiples [address],[address]") | ||||||
| os.Exit(1) | ||||||
| } | ||||||
|  | ||||||
| addrs := strings.Split(os.Args[1], ",") | ||||||
| if len(addrs) == 0 { | ||||||
| return nil, errors.New("no devices specified") | ||||||
| } | ||||||
|  | ||||||
| return addrs, nil | ||||||
| } | ||||||
|  | ||||||
| func failMessage(msg string) { | ||||||
| println(msg) | ||||||
| exitCtx.Done() | ||||||
| } | ||||||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.