green-code-initiative / ecoCode-challenge

Emboard in the hackhatons serie for improving ecoCode
3 stars 4 forks source link

[IOS] Stop scanning when you found your device #24

Open hgroschaus opened 1 year ago

hgroschaus commented 1 year ago

When you call the scanForPeripheralsWithServices:options method of the CBCentralManager class to discover remote peripherals that are advertising services, the device uses its radio to listen for advertising devices until explicitly told to stop.

Unless you need to discover more devices, stop scanning for other devices once you have found one you want to connect to. Use the stopScan method of the CBCentralManager class to stop scanning for other devices

func beginScanningForDevice() {
    // Create a Core Bluetooth Central Manager object
    self.myCentralManager = CBCentralManager(delegate: self, queue: nil, options: nil)

    // Scan for peripherals
    self.myCentralManager.scanForPeripheralsWithServices(nil, options: nil)
}

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: 
                                  CBPeripheral, advertisementData: [NSObject: AnyObject]!, RSSI: NSNumber!) {
    // Connect to the newly discovered device

    // Stop scanning for devices
    self.myCentralManager.stopScan()
}
fredfoc commented 1 year ago

\newpage

Stop scanning when you found your device

Platform

OS OS version Langage
IOS - Swift

Main caracteristics

ID Title Category Sub-category
- Stop scanning when you found your device - -

Severity / Remediation Cost

Severity Remediation Cost
Minor Minor

Rule short description

Rule complete description

When you call the scanForPeripheralsWithServices:options method of the CBCentralManager class to discover remote peripherals that are advertising services, the device uses its radio to listen for advertising devices until explicitly told to stop.

Unless you need to discover more devices, stop scanning for other devices once you have found one you want to connect to. Use the stopScan method of the CBCentralManager class to stop scanning for other devices.

Text

When you call the scanForPeripheralsWithServices:options method of the CBCentralManager class to discover remote peripherals that are advertising services, the device uses its radio to listen for advertising devices until explicitly told to stop.

Unless you need to discover more devices, stop scanning for other devices once you have found one you want to connect to. Use the stopScan method of the CBCentralManager class to stop scanning for other devices.

func beginScanningForDevice() {
    // Create a Core Bluetooth Central Manager object
    self.myCentralManager = CBCentralManager(delegate: self, queue: nil, options: nil)

    // Scan for peripherals
    self.myCentralManager.scanForPeripheralsWithServices(nil, options: nil)
}

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: 
                                  CBPeripheral, advertisementData: [NSObject: AnyObject]!, RSSI: NSNumber!) {
    // Connect to the newly discovered device

    // Stop scanning for devices
    self.myCentralManager.stopScan()
}

Coming from : https://developer.apple.com/library/archive/documentation/Performance/Conceptual/EnergyGuide-iOS/BluetoothBestPractices.html#//apple_ref/doc/uid/TP40015243-CH28-SW1

HTML

<p>When you call the <code>scanForPeripheralsWithServices:options</code> method of the <code>CBCentralManager</code>class to discover remote peripherals that are advertising services, the device uses its radio to listen for advertising devices until explicitly told to stop.</p>
<p>Unless you need to discover more devices, stop scanning for other devices once you have found one you want to connect to. Use the <code>stopScan</code> method of the <code>CBCentralManager</code> class to stop scanning for other devices.</p>
<h2>Compliant Solution</h2>
<pre>
    func beginScanningForDevice() {
        // Create a Core Bluetooth Central Manager object
        self.myCentralManager = CBCentralManager(delegate: self, queue: nil, options: nil)

        // Scan for peripherals
        self.myCentralManager.scanForPeripheralsWithServices(nil, options: nil)
    }

   func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: 
                                      CBPeripheral, advertisementData: [NSObject: AnyObject]!, RSSI: NSNumber!) {
        // Connect to the newly discovered device

        // Stop scanning for devices
        self.myCentralManager.stopScan()
    }
</pre>

Implementation principle