2017-08-23 24 views
0

私はBluetoothとSwiftの両方が新しく、今はBluetoothデバイス(Arduino HM-10)に接続してサービスを取得しようとしています。私が抱えている問題は、デバイスに接続した後、centralManager didConnectPeripheralメソッドが呼び出されないことです。ここに私のコードはiOS Swift CentralManager didConntectPeripheralは決して呼び出されません

import UIKit 
import CoreBluetooth 

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { 

    var manager:CBCentralManager! 
    var peripheral:CBPeripheral! 


    let BubbleWallBluetoothName = "TestBluetooth" 
    let BEAN_SCRATCH_UUID = 
     CBUUID(string: "a495ff21-c5b1-4b44-b512-1370f02d74de") 
    let BEAN_SERVICE_UUID = 
     CBUUID(string: "a495ff20-c5b1-4b44-b512-1370f02d74de") 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     manager = CBCentralManager(delegate: self, queue: nil) 
    } 

    func centralManagerDidUpdateState(_ central: CBCentralManager){ 

     switch (central.state) 
     { 
     case . unsupported: 
      print("BLE is unsupported") 
     case.unauthorized: 
      print("BLE is unauthorised") 
     case.unknown: 
      print("BLE is unknown") 
     case.resetting: 
      print("BLE is resetting") 
     case.poweredOff: 
      print("BLE is powered off") 
     case.poweredOn: 
      print("BLE is powered on") 
      central.scanForPeripherals(withServices: nil, options: nil) 
     } 

    } 

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { 
     print("Found Device " + peripheral.name!) 


     let device = (advertisementData as NSDictionary).object(forKey: CBAdvertisementDataLocalNameKey) as? NSString 

     if (device?.contains(BubbleWallBluetoothName) == true) { 
      self.manager.stopScan() 

      self.peripheral = peripheral 

      self.peripheral.delegate = self 

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


    //This function never gets called 
    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { 
     peripheral.discoverServices(nil) 
     print("We are connected to BT"); 
    } 
} 

答えて

0

OKですので、私はそれがとても

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { 
     peripheral.discoverServices(nil) 
     print("We are connected to BT"); 
    } 
+0

それが機能するようになりましたとの最後の方法を置き換えることで動作するように管理しましたか? –

関連する問題