`adapter.Connect()` never exits due to CoreBluetooth hang on Darwin
I'm trying to facilitate simple connections to BLE devices in a relatively noisy BT environment, and am getting occasional failures to connect. This isn't terribly surprising to me, however the Darwin gap code has some behavior that causes my application to stall irrecoverably.
Specifically, it seems that the implementation of adapter.Connect() relies on ConnectionTimeout to only trigger CoreBluetooth's cancelPeripheralConnection - however, it then blocks forever waiting for a positive disconnect signal from the CoreBluetooth stack. In my case, I never see this signal come in; and since the timeout has already been waited on, we stay blocked forever. My guess is that cancelPeripheralConnection does not reliably fire back didDisconnectPeripheral in all connecting states
A couple of thoughts
- if ConnectionTimeout is going to be consumed to trigger a disconnect, there needs to be a way to time-bound the actual 'disconnect'-ing part.
- it would be nice if
adapter.Connectcould rely on a context for better cancel propagation. In this case, perhaps a context could be used to guard the forever-waiting case and ConnectionTimeout can remain as-is.
I'm developing a app through the broadcast bluetooth protocol, first time the connection is correct.But when device faraway from the computer,the EnableNotifications circulation do not invoke any more with no doubt. So I use a func to detect lastupdatetime for reconnect the bluetooth device.But the program will hang at adapter.Connect forever.
Did there have any method to reconnect device from disconnected state?
func connectAndSubscribe(addr bluetooth.Address, serviceUUids []bluetooth.UUID, topicUUids []bluetooth.UUID, results []*int, lastDataTime *time.Time) {
var err error
device, err = adapter.Connect(addr, bluetooth.ConnectionParams{
ConnectionTimeout: bluetooth.NewDuration(10 * time.Second),
Timeout: bluetooth.NewDuration(10 * time.Second)})
if err != nil {
println("connet error")
fmt.Sprintf("connet error%v", err)
*lastDataTime = time.Now()
return
}
var services, serviceErr = device.DiscoverServices(serviceUUids)
if serviceErr != nil {
*lastDataTime = time.Now()
println("service error")
return
}
for i, serviceSingle := range services {
chars, characterErr := serviceSingle.DiscoverCharacteristics([]bluetooth.UUID{topicUUids[i]})
if characterErr != nil {
*lastDataTime = time.Now()
println("characterErr error")
return
}
err = chars[0].EnableNotifications(func(buf []byte) {
*lastDataTime = time.Now()
if i == 0 {
*results[i] = int(buf[1])
fmt.Printf("[%s] heartbeat: %d \n", time.Now().Format("2006-01-02 15:04:05"), *results[i])
}
if i == 1 {
parseRSC1(buf)
}
return
})
if err != nil {
*lastDataTime = time.Now()
println("notification error")
}
}
}
go func() {
for {
time.Sleep(10 * time.Second)
if !garminLastDataTime.IsZero() && time.Since(garminLastDataTime) > 5*time.Second {
fmt.Println("garmin设备断开")
// device.Disconnect()
heartRate = 60
cadence = 70
connectAndSubscribe(garminBleAddr, []bluetooth.UUID{garminServiceUUid, garminCadenceServiceUUid},
[]bluetooth.UUID{garminTopicUUid, garminCadenceTopicUUid}, []*int{&heartRate, &cadence}, &garminLastDataTime)
}
}
}()