forked from Elara6331/infinitime
		
	Log when too many disconnects occur rather than removing the device (Arsen6331/itd#10)
This commit is contained in:
		@@ -14,6 +14,7 @@ import (
 | 
			
		||||
	"github.com/muka/go-bluetooth/bluez/profile/adapter"
 | 
			
		||||
	"github.com/muka/go-bluetooth/bluez/profile/device"
 | 
			
		||||
	"github.com/muka/go-bluetooth/bluez/profile/gatt"
 | 
			
		||||
	"github.com/rs/zerolog"
 | 
			
		||||
	"go.arsenm.dev/infinitime/blefs"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -67,11 +68,15 @@ type Options struct {
 | 
			
		||||
	Whitelist        []string
 | 
			
		||||
	OnReqPasskey     func() (uint32, error)
 | 
			
		||||
	OnReconnect      func()
 | 
			
		||||
	Logger           zerolog.Logger
 | 
			
		||||
	LogLevel         zerolog.Level
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var DefaultOptions = &Options{
 | 
			
		||||
	AttemptReconnect: true,
 | 
			
		||||
	WhitelistEnabled: false,
 | 
			
		||||
	Logger:           zerolog.Nop(),
 | 
			
		||||
	LogLevel:         zerolog.Disabled,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Connect will attempt to connect to a
 | 
			
		||||
@@ -239,10 +244,10 @@ func reconnect(opts *Options, dev *device.Device1) {
 | 
			
		||||
			amtDisconnects = 0
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// If less than 3 seconds have past and more than 6
 | 
			
		||||
		// If less than 3 seconds have passed and more than 6
 | 
			
		||||
		// disconnects have occurred, remove the device and reset
 | 
			
		||||
		if secsSince <= 3 && amtDisconnects >= 6 {
 | 
			
		||||
			defaultAdapter.RemoveDevice(dev.Path())
 | 
			
		||||
			opts.Logger.Warn().Msg("At least 6 disconnects have occurred in the last three seconds. If this continues, try removing the InfiniTime device from bluetooth.")
 | 
			
		||||
			lastDisconnect = time.Unix(0, 0)
 | 
			
		||||
			amtDisconnects = 0
 | 
			
		||||
		}
 | 
			
		||||
@@ -254,7 +259,7 @@ func reconnect(opts *Options, dev *device.Device1) {
 | 
			
		||||
		for i := 0; i < 6; i++ {
 | 
			
		||||
			// If three tries failed, remove device
 | 
			
		||||
			if i == 3 {
 | 
			
		||||
				defaultAdapter.RemoveDevice(dev.Path())
 | 
			
		||||
				opts.Logger.Warn().Msg("Multiple connection attempts have failed. If this continues, try removing the InfiniTime device from bluetooth.")
 | 
			
		||||
			}
 | 
			
		||||
			// Connect to device
 | 
			
		||||
			newDev, err := connect(opts, false)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user