저는 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
}
}
이것은 문제가 아니지만 기본보기 컨트롤러가 싱글 톤입니까? MainViewController()를 사용하지 않으면 필요한 인스턴스가 제공되지 않습니다. 'connectBLEDevice'라고 부를 수 있습니까? 'didSelectRowAtIndexPath'에? 귀하의 'BLEDiscovery' 객체는 싱글 톤이어야하며 주변 배열은 전역이 아닌이 클래스의 속성이어야합니다 – Paulw11