2017-09-22 7 views
2

내 iOS 앱에서 연결 한 BLE 장치에서 서비스를 검색하려고하는데 내 didDiscoverServices 기능이 호출되면 주변 서비스 배열이 비어 있습니다.Swift 3 코어 블루투스가 서비스를 검색하지 않습니다.

응용 프로그램이 장치에 연결하고 peripheral.discoverServices (nil)를 실행 한 다음 didDiscoverServices 함수가 호출되었지만 반환 된 서비스가 없습니다.

나는 블루투스 관련 답변 및 기타 온라인 예를 많이 읽었으며 최선을 다해도 제 코드는 작동하지 않는다는 점에서 차이가 없습니다. 나는 어딘가에 뭔가를 놓쳤을 것 같은 느낌이 든다. 그러나 나는 무엇이 확실하지 않다.

코드를 실행할 때 아래에서 내 콘솔 로그를 추가했으며 참조 용으로 하단에 블루투스 코드를 추가했습니다.

Bluetooth initialised 
BLE is powered on 
Optional("Nordic_Blinky") found at -72 
Scanning stopped 
Connect request sent 
Connected to <CBPeripheral: 0x1c0301a70, identifier = 0887CF7F-98C8-3FCF-2D10-873FFFFB2B65, name = Nordic_Blinky, state = connected> 
Discovering services 
Services -- Optional([]) and Error -- nil 

내 BluetoothHandler 클래스 코드는

class BluetoothHandler : NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { 

// MARK: Properties 
var manager: CBCentralManager! 
var targetPeripheral: CBPeripheral! 


// MARK: Shared Instance 
static let sharedInstance = BluetoothHandler() 
private override init() { 
    super.init() 
    self.startManager() 
    print("Bluetooth initialised") 
} 

// MARK: Functions 

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

func centralManagerDidUpdateState(_ central: CBCentralManager) { 

    var consoleMessage = "" 
    switch (central.state) { 

    case.poweredOn: 
     consoleMessage = "BLE is powered on" 
     manager.scanForPeripherals(withServices: nil, options: nil) 

    case.poweredOff: 
     consoleMessage = "BLE is powered off" 

    case.resetting: 
     consoleMessage = "BLE Resetting" 

    case.unknown: 
     consoleMessage = "BLE state unknown" 

    case.unsupported: 
     consoleMessage = "Device not supported by BLE" 

    case.unauthorized: 
     consoleMessage = "BLE not authorised" 
    } 
    print("\(consoleMessage)") 

} 

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 

    if (peripheral.name == "Nordic_Blinky") { 

     print("\(String(describing: peripheral.name)) found at \(RSSI)") 
     self.stopScan() 

     if targetPeripheral != peripheral { 

      targetPeripheral = peripheral 
      targetPeripheral!.delegate = self 

      manager.connect(targetPeripheral, options: nil) 
      print("Connect request sent") 
     } 

    } 

    else if (peripheral.name != nil) { 
     print("\(String(describing: peripheral.name))") 
    } 
} 

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 
    print("Connected to \(peripheral)") 
    peripheral.delegate = self 
    peripheral.discoverServices(nil) 
    print("Discovering services") 
} 

func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Any) { 
    print("Connection failed", error) 
} 

func centralManager(_ central: CBCentralManager, didDisconnect peripheral: CBPeripheral) { 

    if self.targetPeripheral != nil { 
     self.targetPeripheral!.delegate = nil 
     self.targetPeripheral = nil 
    } 
    print("Connection disconnected") 
    self.startManager() 
} 

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 
    print("Services -- \(peripheral.services) and Error -- \(error)") 

    if let services = peripheral.services { 
     for service in services { 
      peripheral.discoverCharacteristics(nil, for: service) 
     } 
    } 
} 
+0

BlueLight.app와 같은 앱에 서비스가 있습니까? – Larme

+0

@Larme 예 Android 앱이 기기에 연결하여 모든 서비스를 수신했습니다. – AdamDWalker

+0

iOS에서도'BlueLight.app'가 사용됩니까? – Larme

답변

0

이 시도 아래 : 빠른 3

당신의 주변 (BLE 장치)의 경우이 서비스는 다음 로그에 인쇄 할 수 있습니다.

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { 

    if let services = peripheral.services as [CBService]!{ 

     for service in services{ 

      peripheral.discoverCharacteristics(nil, for: service) 
     } 
    } 
    print("==>",peripheral.services!) 
} 

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { 

    for newChar: CBCharacteristic in service.characteristics!{ 

     print(newChar) 
    } 
} 
+0

서비스를 인쇄하기 위해 이미 서비스를 인쇄하려고 시도하지만 서비스가 없기 때문에 빈 어레이 만 인쇄합니다 (코드 및 콘솔 로그 참조) – AdamDWalker