2016-07-04 13 views
0

私はテーブルからBLEデバイスを接続するためのコードを開発してきました。私はデバイスを発見してテーブルにロードすることができます。テーブル内で行を選択すると、選択したデバイスを接続するように要求されます。しかし、didConnectPeripheralが呼び出されることはありません...centralManager didConnectPeripheralは決して呼び出されません

任意の考え:私はオブザーバーをできるように、コードがテーブルから呼び出されている知っている

import UIKit 
import CoreBluetooth 

@objc protocol BLEDelegate: class { 

    func srgDiscoverServices(sender: BLEDiscovery, peripheral: CBPeripheral) 

} 

let bleDiscoverySharedInstance = BLEDiscovery() 

//MARK: - UUIDS for StingRay Genessis M (SRG) 
let StingRayGenesisMUUID = CBUUID (string: "346D0000-12A9-11CF-1279-81F2B7A91332") //Core UUID 

//MARK: - Device and Characteristic Registers 
var BLEDevices   : [CBPeripheral] = []   //Device Array 
var BLECharDictionary = [String: CBCharacteristic]() //Characteristic Dictionary 


class BLEDiscovery: NSObject, CBCentralManagerDelegate { 

    private var centralManager : CBCentralManager? 

    weak var delegate: BLEDelegate? 

    override init() { 
     super.init() 

     let centralQueue = dispatch_queue_create("com.stingray", DISPATCH_QUEUE_SERIAL) 
     centralManager = CBCentralManager(delegate: self, queue: centralQueue) 

    } 

    // MARK: - CBCentralManager 
    func centralManagerDidUpdateState(central: CBCentralManager) { 
     switch (central.state) { 

      case CBCentralManagerState.PoweredOff: 
       print("CBCentralManagerState.PoweredOff") 

      case CBCentralManagerState.Unauthorized: 
       // Indicate to user that the iOS device does not support BLE. 
       print("CBCentralManagerState.Unauthorized") 
       break 

      case CBCentralManagerState.Unknown: 
       // Wait for another event 
       print("CBCentralManagerState.Unknown") 
       break 

      case CBCentralManagerState.PoweredOn: 
       print("CBCentralManagerState.PoweredOn") 
       self.startScanning() 

      case CBCentralManagerState.Resetting: 
       print("CBCentralManagerState.Resetting") 

      case CBCentralManagerState.Unsupported: 
       print("CBCentralManagerState.Unsupported") 
       break 
     } 
    } 

    // MARK: - Start scanning for StringRay devices with the appropriate UUID 
    func startScanning() { 
     if let central = centralManager { 
      central.scanForPeripheralsWithServices([StingRayGenesisMUUID], options: nil) 
     } 
    } 

    // MARK: - CB Central Manager - Did discover peripheral (follows : startScanning) 
    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) { 

     print("BLEDiscovery :: didDiscoverPeripheral :: ", peripheral.name) 

     //Check if new discovery and append to BLEDevices where required 
     if BLEDevices.contains(peripheral) { 

     } 
     else{ 
      BLEDevices.append(peripheral) 
     } 

     //Change to BLEDevices - therefore update MianViewController, but check that the view is loaded 
     if MainViewController().deviceTableView != nil { 

      print("BLEDiscovery :: deviceTableView :: ") 
      MainViewController().relaodDeviceTable() 
     } 


    } 

    // MARK: - CB Central Manager - Connect and Disconnet BLE Devices 

    func connectBLEDevice (peripheral: CBPeripheral){ 

     print("BLEDiscovery :: connectBLEDevice :: ", peripheral.name) 

     //Connect 
     let peripheralConnect : CBPeripheral = peripheral 
     self.centralManager!.connectPeripheral(peripheralConnect, options: nil) 
    } 

    func disconnectBLEDevice (peripheral: CBPeripheral){ 

     print("BLEDiscovery :: disconnectBLEDevice :: ", peripheral.name) 

     //Disconnect 
     let peripheralDisconnect : CBPeripheral = peripheral 
     self.centralManager?.cancelPeripheralConnection(peripheralDisconnect) 
    } 

    // MARK: - CB Central Manager - Did Connect Device 

    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { 

     print("BLEDiscovery :: didConnectPeripheral :: ", peripheral.name) 

     delegate?.srgDiscoverServices(self, peripheral: peripheral) 

    } 

    func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) { 

     //error handling 
     if (error != nil) { 
      print("!!Error - BLE Discovery - didDisconnectPeripheral - Error :: \(error)") 
      return 
     } 

     //On disconnect remove device from register 
     if let index = BLEDevices.indexOf(peripheral) { 
      BLEDevices.removeAtIndex(index) 
     } 

     //Change to BLEDevices - therefore update MianViewController 
     MainViewController().relaodDeviceTable() 

    } 

    func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) { 

     //error handling 
     if (error != nil) { 
      print("!!Error - BLE Discovery - didFailToConnectPeripheral - Error :: \(error)") 
      return 
     } 

     //Change to BLEDevices - therefore update MianViewController 
     MainViewController().relaodDeviceTable() 

    } 


} 

"BLEDiscovery :: connectBLEDevice ::"、peripheral.name」このようBLEDiscoveryが最高実装されているあなたのよう

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     print("MainViewController :: didSelectRowAtIndexPath :: Row :: ", deviceTableView.indexPathForSelectedRow?.row) 

     let peripheral : CBPeripheral = BLEDevices[(deviceTableView.indexPathForSelectedRow?.row)!] 

     switch peripheral.state{ 

     case .Connected: 
      //Disconnect as device is connected 
      BLEDiscovery().disconnectBLEDevice(peripheral) 
     case .Disconnected: 
      //Connect as device as disconnected 
      BLEDiscovery().connectBLEDevice(peripheral) 

     default: break 

     } 

    } 
+0

これは問題ではありませんが、メインの表示コントローラはシングルトンですか?そうでなければ、MainViewController()を使用しても、必要なインスタンスは得られません。 'connectBLEDevice'と呼ぶ場所を表示できますか? 'didSelectRowAtIndexPath'では?あなたの 'BLEDiscovery'オブジェクトはシングルトンでなければなりません。周辺機器の配列は、グローバルではなくこのクラスのプロパティでなければなりません – Paulw11

答えて

0

オブジェクト:。私はからの接続と切断を呼び出すどこログウィンドウに

ですシングルトンとして、またはDependency Injectionを使用することができますが、主なものはクラスの単一のインスタンスを持つことです。

これを達成するためにグローバルを使用していますが、didSelectRowAtIndexPath機能ではスリップしました。あなたは

case .Connected: 
    //Disconnect as device is connected 
    BLEDiscovery().disconnectBLEDevice(peripheral) 

を言うときあなたは、独自のCBCentralManagerが含まれており、これはあなたが接続を実行するように依頼中心であるBLEDiscoveryの新しい、ローカル、インスタンスを作成します。 caseステートメントを終了するとすぐに、このローカルBLEDiscoveryが解放されるので、デリゲートメソッドは決して呼び出されません。グローバル配列を使用するのではなく、BLEDiscoveryクラス内に周辺機器の配列をカプセル化した場合は、配列にアクセスする前にBLEDiscovery参照を取得しなければならないため、このエラーを発見した可能性があります。空であっただろう。

あなたがシングルトンとグローバルを排除するために、あなたのBLEDiscoveryを再構築することができます

class BLEDiscovery: NSObject, CBCentralManagerDelegate { 

    static let sharedInstance = BLEDiscovery() 
    private static var initialised = false 

    private var centralManager : CBCentralManager! 

    weak var delegate: BLEDelegate? 

    //MARK: - UUIDS for StingRay Genesis M (SRG) 
    let stingRayGenesisMUUID = CBUUID (string: "346D0000-12A9-11CF-1279-81F2B7A91332") //Core UUID 

    //MARK: - Device and Characteristic Registers 
    var bleDevices   : [CBPeripheral] = []   //Device Array 
    var bleCharDictionary = [String: CBCharacteristic]() //Characteristic Dictionary 

    override init() { 
     assert(!BLEDiscovery.initialised, "Illegal call to initializer - use sharedInstance") 

     BLEDiscovery.initialised = true 

     super.init() 

     let centralQueue = dispatch_queue_create("com.stingray", DISPATCH_QUEUE_SERIAL) 
     centralManager = CBCentralManager(delegate: self, queue: centralQueue) 
} 

// Rest of methods largely unchanged, although you should use `self.bleDevices` etc 

、あなたがBLEDiscoveryのインスタンスをしたいとき、あなたはBLEDiscovery.sharedInstance例えばを使用することができます

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    print("MainViewController :: didSelectRowAtIndexPath :: Row :: ", deviceTableView.indexPathForSelectedRow?.row) 

    let bleDiscovery = BLEDiscovery.sharedInstance 

    let peripheral = bleDiscovery.bleDevices[indexPath.row] 

    switch peripheral.state{ 

    case .Connected: 
     //Disconnect as device is connected 
     bleDiscovery.disconnectBLEDevice(peripheral) 
    case .Disconnected: 
     //Connect as device as disconnected 
     bleDiscovery.connectBLEDevice(peripheral) 

    default: break 

    } 

} 
+0

フィードバックありがとうございます。私は今日後であなたの推薦を更新し、テストします。あなたは大きな助けになりました。私は概念的に何が間違っていたのか分かりませんでしたが、それを見ることはできませんでした... – user5308141

+0

もう1つの質問ですが、この影響はBLEDiscoveryへの代議員を持つでしょうか? – user5308141

+0

はい、デリゲートとシングルトンは適切な組み合わせではありません。私はあなたが任意の数の加入者ができるようにNSNotificationを使用します – Paulw11

関連する問題