I'm new to working with BLE, currently trying to make a simple application which would connect to my custom BLE device. I am able to discover the BLE device, but for some reason i can't connect to it. I tried to check it with 'Light Blue', it shows my device as connectable and seems to work fine. But in my app after i discover the device, CB manager tries to connect to it and seems to 'freeze'? Function 'didConnect peripheral' is never triggered, and state of peripheral is forever 'connecting'.
How can i identify the problem? Is there any options i can include in connection method, or somehow track the connection process?
I would appreciate any advice where to search for problems.
Working in XCode 8.2.1, using Swift 3. iOS 10.2.1 installed on the testing phone
Here's my code:
import UIKit
import CoreBluetooth
class InfoPageViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var manager:CBCentralManager!
var peripheral:CBPeripheral!
let BEAN_NAME = "MyDevice"
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
let device = (advertisementData as NSDictionary)
.object(forKey: CBAdvertisementDataLocalNameKey)
as? NSString
if device?.contains(BEAN_NAME) == true {
self.manager.stopScan()
self.peripheral = peripheral
self.peripheral.delegate = self
manager.connect(peripheral, options: nil)
print("discovered \(BEAN_NAME)")
}
}
func centralManager(
central: CBCentralManager,
didConnect peripheral: CBPeripheral) {
print("connected to \(BEAN_NAME)")
peripheral.discoverServices(nil)
}