BluetoothKit icon indicating copy to clipboard operation
BluetoothKit copied to clipboard

Couldn't work properly on iOS 10.

Open WebMobi59 opened this issue 8 years ago • 7 comments

I'd like to use this framework for my app, but I faced following issue on IOS10 with Swift3.0.

Error from scanning: The operation couldn't be completed. (BluetoothKit.BKError error 3.)

Also I checked this on iOS 9(XCode 7.3, iPhone 5s, Gimbal Beacon Series 21) , but occurred the same issue. (I tried with this framework https://github.com/tinydream12/Bluetoothkit, but the same result.)

Does anyone know how to avoid this? Thank you.

WebMobi59 avatar Nov 07 '16 03:11 WebMobi59

Please show your code?

rhummelmose avatar Nov 08 '16 19:11 rhummelmose

  • CentralViewController.swift in the BKExample
...
    func scan() {
        central.scanContinuouslyWithChangeHandler({ changes, discoveries in
            let indexPathsToRemove = changes.filter({ $0 == .remove(discovery: nil) }).map({ NSIndexPath(row: self.discoveries.index(of: $0.discovery)!, section: 0) })
            self.discoveries = discoveries
            let indexPathsToInsert = changes.filter({ $0 == .insert(discovery: nil) }).map({ NSIndexPath(row: self.discoveries.index(of: $0.discovery)!, section: 0) })
            if !indexPathsToRemove.isEmpty {
                self.discoveriesTableView.deleteRows(at: indexPathsToRemove as [IndexPath], with: UITableViewRowAnimation.automatic)
            }
            if !indexPathsToInsert.isEmpty {
                self.discoveriesTableView.insertRows(at: indexPathsToInsert as [IndexPath], with: UITableViewRowAnimation.automatic)
            }
            for insertedDiscovery in changes.filter({ $0 == .insert(discovery: nil) }) {
                Logger.log("Discovery: \(insertedDiscovery)")
            }
        }, stateHandler: { newState in
            if newState == .scanning {
                self.activityIndicator?.startAnimating()
                return
            } else if newState == .stopped {
                self.discoveries.removeAll()
                self.discoveriesTableView.reloadData()
            }
            self.activityIndicator?.stopAnimating()
        }, errorHandler: { error in
            Logger.log("Error from scanning: \(error.localizedDescription)")
        })
    }
...
  • BKCentral.swift in the BluetoothKit
...
   public func scanContinuouslyWithChangeHandler(_ changeHandler: @escaping ContinuousScanChangeHandler, stateHandler: ContinuousScanStateHandler?, duration: TimeInterval = 3, inBetweenDelay: TimeInterval = 3, errorHandler: ContinuousScanErrorHandler?) {
        do {
            try stateMachine.handleEvent(.scan)
            continuousScanner.scanContinuouslyWithChangeHandler(changeHandler, stateHandler: { newState in
                if newState == .stopped && self.availability == .available {
                    _ = try? self.stateMachine.handleEvent(.setAvailable)
                }
                stateHandler?(newState)
            }, duration: duration, inBetweenDelay: inBetweenDelay, errorHandler: { error in
                errorHandler?(.internalError(underlyingError: error))
            })
        } catch let error {
            errorHandler?(.internalError(underlyingError: error))
        }
    }
...

I tested with both https://github.com/rhummelmose/BluetoothKit/tree/master/Example and https://github.com/tinydream12/BluetoothKit/tree/master/Example, on iOS 9 and 10, but the same error occurs.

Thank you.

WebMobi59 avatar Nov 10 '16 02:11 WebMobi59

I have just met the same error, and when I dig into the code and find out that the state is 'scanning' in this function:

    private func handleScanEvent(_ event: Event) throws {
        switch state {
        case .available:
            state = .scanning
        default:
            throw BKError.transitioning(currentState: state, validStates: [ .available ])
        }
    }

in BluetoothKit > Source > BKCentralStateMachine.swift > line99.

Could anyone give some advice?

BottleMan avatar Nov 28 '16 12:11 BottleMan

Isn't it just calling scan twice? I think it is just the example app that does that, it is no problem.

rhummelmose avatar Nov 28 '16 18:11 rhummelmose

@BottleMan ?

rhummelmose avatar Nov 30 '16 09:11 rhummelmose

@rhummelmose sorry for replying late, I just comment the 'scan' method in viewDidAppear and it goes alright.

    internal override func viewDidAppear(_ animated: Bool) {
        //scan()
    }

BottleMan avatar Dec 02 '16 04:12 BottleMan

Then it won't always start scanning when you navigate back and forth. That is why it is there :)

rhummelmose avatar Dec 02 '16 08:12 rhummelmose