shinyorg / shiny

.NET Framework for Backgrounding & Device Hardware Services (iOS, Android, & Catalyst)
https://shinylib.net
MIT License
1.43k stars 227 forks source link

[Bug]: IPeripheral's connection state is not updated after .Scan() running again #1404

Closed sleushunou closed 6 months ago

sleushunou commented 6 months ago

Component/Nuget

BluetoothLE Client (Shiny.BluetoothLE)

What operating system(s) are effected?

Version(s) of Operation Systems

Tested on iOS 16 iPhone 12

Hosting Model

Steps To Reproduce

Run .Scan() Receive IPeripheral from result of .Scan() Run .Scan() again Try to connect to received at step 2 IPeripheral and observe IPeripheral::WhenStatusChanged()

Expected Behavior

IPeripheral::WhenStatusChanged() - status changed when peripheral is connected

Actual Behavior

IPeripheral::WhenStatusChanged() - status not changed when peripheral is connected, subscription handler is not called when BUT IBleDelegate::OnPeripheralStateChanged() - state changed when peripheral is connected ✔️

Exception or Log output

instead of logs. seems like problem is here: this.Clear(); List of cached IPeripheral's on BleManager cleared every time after starting a new scan, and when connection status is changed, BleManager calls p.ReceiveStateChange(status);, where p - another, newly created, instance of IPeripheral that refers on the same physical device, and first instance of IPeripheral does not receive state change

public IObservable<ScanResult> Scan(ScanConfig? scanConfig = null) => Observable.Create<ScanResult>(ob =>
{
    if (this.IsScanning)
        throw new InvalidOperationException("There is already an existing scan");

    this.Clear();
    ......
}

void Clear() => this.peripherals
    .Where(x => x.Value.Status != ConnectionState.Connected)
    .ToList()
    .ForEach(x => this.peripherals.TryRemove(x.Key, out var device));

void RunStateChange(CBPeripheral peripheral, bool connected, NSError? error)
{
    this.logger.PeripheralStateChange(peripheral.Identifier, connected, error?.LocalizedDescription ?? "None");

    var p = this.GetPeripheral(peripheral);
    var status = connected ? ConnectionState.Connected : ConnectionState.Disconnected;
    p.ReceiveStateChange(status);

    this.services.RunDelegates<IBleDelegate>(x => x.OnPeripheralStateChanged(p), this.logger);
}

readonly ConcurrentDictionary<string, Peripheral> peripherals = new();
Peripheral GetPeripheral(CBPeripheral peripheral) => this.peripherals.GetOrAdd(
    peripheral.Identifier.ToString(),
    x => new Peripheral(this, peripheral, this.operations, this.peripheralLogger)
);

Code Sample

    private  IDisposable _subscription;

    private async Task Test(IBleManager bleManager)
    {
        var scanResult = await bleManager
            .Scan(new ScanConfig())
            .FirstAsync()
            .ToTask()
            .ConfigureAwait(false);

        var peripheral = scanResult.Peripheral;

        var scanResult2 = await bleManager
            .Scan(new ScanConfig())
            .FirstAsync()
            .ToTask()
            .ConfigureAwait(false);

        _subscription = peripheral.WhenStatusChanged().Subscribe(OnStatusChanged);
        peripheral.Connect(new ConnectionConfig(false));
    }

    private void OnStatusChanged(ConnectionState state)
    {

    }

Code of Conduct

sleushunou commented 6 months ago

https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html#//apple_ref/doc/uid/TP40013257-CH6-SW1

According to the Apple guide, scanning should be active only when it is really needed to save battery power.

business case: Once we have found all of our expected devices, we stop scanning. But, if we want to add another device to the device list, we run the scan again.

sleushunou commented 6 months ago

Are you actually connected to the devices while running a scan though?

usually not, but a business case is theoretically possible when we are connected to one device and are trying to find a second one

aritchie commented 6 months ago

Please read the previous issue. Don't keep a reference to peripheral across scans unless they're connected