خرید بک لینک

Vote count: 0

First off, I know this question has been asked a lot on SO. And I went through all of them trying to resolve it but to no avail.

I have a simple app that scans for Bluetooth enabled devices. Here's my code.

import CoreBluetooth
import UIKit

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

    var manager: CBCentralManager!
    var peripheral: CBPeripheral!

    override func viewDidLoad() {
        super.viewDidLoad()

        manager = CBCentralManager(delegate: self, queue: nil)
    }

    // MARK: - CBCentralManagerDelegate
    func centralManagerDidUpdateState(central: CBCentralManager) {
        print(#function)

        switch central.state {
        case .Unsupported:
            print("Unsupported")
        case .Unauthorized:
            print("Unauthorized")
        case .PoweredOn:
            print("Powered On")
            central.scanForPeripheralsWithServices(nil, options: nil)
        case .Resetting:
            print("Resetting")
        case .PoweredOff:
            print("Powered Off")
        case .Unknown:
            print("Unknown")
        }
    }

    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
        print(#function)

        print("Discovered (peripheral.name) at (RSSI)")

        if peripheral.name!.containsString(name) {
            manager.stopScan()

            self.peripheral = peripheral
            self.peripheral.delegate = self

            manager.coectPeripheral(peripheral, options: nil)
        }
    }

    func centralManager(central: CBCentralManager, didCoectPeripheral peripheral: CBPeripheral) {
        print(#function)
    }

    func centralManager(central: CBCentralManager, didDiscoectPeripheral peripheral: CBPeripheral, error: NSError?) {
        central.scanForPeripheralsWithServices(nil, options: nil)
    }


    // MARK: - CBPeripheralDelegate
    func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
        print(#function)

        guard let services = peripheral.services else {
            retu
        }

        for service in services {
            print(service.UUID)
            if service.UUID == serviceUUID {
                peripheral.discoverCharacteristics(nil, forService: service)
            }
        }
    }

    func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
        print(#function)

        guard let characteristics = service.characteristics else {
            retu
        }

        for characteristic in characteristics {
            print(characteristic.UUID)
            if characteristic.UUID == scratchUUID {
                peripheral.setNotifyValue(true, forCharacteristic: characteristic)
            }
        }
    }

    func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
        print(#function)

        var count: UInt32 = 0
        if characteristic.UUID == scratchUUID {
            characteristic.value!.getBytes(&count, length: sizeof(UInt32))
            print(String(format: "%llu", count))
        }
    }

}

I ran it in my iPhone 5 (iOS 9.3.4). I had a laptop ruing Windows, an Android phone, an iPad Air 2 and my Mac all Bluetooth tued on. But none of those devices were discovered. centralManagerDidUpdateState method gets called and scaing starts but that's it. didDiscoverPeripheral delegate method never gets called.

I repeated the process, this time ran it in the iPad Air 2 but same result.

If I go to the Bluetooth menu on the device I do see other devices being discovered.

In some SO answers, I saw that ruing it in the main queue would work. Like so, manager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue()). But that did not work for me either.

I'd really appreciate any help on this.

asked 1 min ago

برچسب: bluetooth doesn't find devices,bluetooth doesn't find devices iphone,bluetooth doesn't find devices windows 8 1,bluetooth doesn't find devices iphone 5,ipad bluetooth doesn't discover devices,android bluetooth doesn't find devices,mac bluetooth doesn't find devices, نویسنده: استخدام کار تاريخ: يکشنبه 17 مرداد 1395 ساعت: 17:11

صفحه بندی