Skip to content

Commit

Permalink
Merge pull request #60 from richbl/dev
Browse files Browse the repository at this point in the history
refactor(app): ♻️ Minor refactor to GetBLECharacteristics(): s…
  • Loading branch information
richbl authored Dec 30, 2024
2 parents 7a2438e + dc077a5 commit f3d9725
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
8 changes: 7 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,14 @@ func scanForBLECharacteristic(ctx context.Context, controllers appControllers) e
return
}

serviceResult, err := controllers.bleController.GetBLEServices(connectResult)
if err != nil {
errChan <- err
return
}

// Get the BLE characteristic from the connected device
err = controllers.bleController.GetBLECharacteristic(connectResult)
err = controllers.bleController.GetBLECharacteristics(serviceResult)
errChan <- err
}()

Expand Down
18 changes: 12 additions & 6 deletions internal/ble/sensor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,32 @@ func (m *BLEController) ConnectToBLEPeripheral(device bluetooth.ScanResult) (blu
return connectedDevice, nil
}

// GetBLECharacteristic scans a connected BLE peripheral for CSC services/characteristics
// returning the speed characteristic
func (m *BLEController) GetBLECharacteristic(device bluetooth.Device) error {
// GetBLEServices discovers services on a connected BLE peripheral
func (m *BLEController) GetBLEServices(device bluetooth.Device) ([]bluetooth.DeviceService, error) {

logger.Debug(logger.BLE, "discovering CSC services", bluetooth.New16BitUUID(0x1816).String())

// Discover CSC services
svc, err := device.DiscoverServices([]bluetooth.UUID{bluetooth.New16BitUUID(0x1816)})
if err != nil {
logger.Error(logger.BLE, "CSC services discovery failed:", err.Error())
return err
return nil, err
}

logger.Debug(logger.BLE, "found CSC service", svc[0].UUID().String())
return svc, nil
}

// GetBLECharacteristics scans a connected BLE peripheral for CSC services/characteristics
// returning the speed characteristic
func (m *BLEController) GetBLECharacteristics(device []bluetooth.DeviceService) error {

logger.Debug(logger.BLE, "discovering CSC characteristics", bluetooth.New16BitUUID(0x2A5B).String())

// Discover CSC characteristics
char, err := svc[0].DiscoverCharacteristics([]bluetooth.UUID{bluetooth.New16BitUUID(0x2A5B)})
char, err := device[0].DiscoverCharacteristics([]bluetooth.UUID{bluetooth.New16BitUUID(0x2A5B)})
if err != nil {
logger.Warn(logger.BLE, "CSC characteristics discovery failed:", err.Error())
logger.Error(logger.BLE, "CSC characteristics discovery failed:", err.Error())
return err
}

Expand Down

0 comments on commit f3d9725

Please sign in to comment.