简体   繁体   中英

Swift BLE function didConnectPeripheral isn't called in all cases

I have written an iOS App to transfer some data from my IPhone to an Bluetooth Low Energy Module. Until the day before yesterday, everything worked fine... but it seems that I did one bad change yesterday and now, my application doesn't work anymore. The didConnectPeripheral func isn't called. In the future I have to backup my code every day... anyway this is the function to connect to my device:

In class ScanTableView:

var bluetoothManager: BluetoothManager = BluetoothManager()

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    selectedPeripheralIndex = indexPath.row
    print(selectedPeripheralIndex!)
    bluetoothManager.CBmanager.connectPeripheral(peripheralArray[selectedPeripheralIndex].peripheral, options: nil)
}

Is there anything missing? When I use the connectPeripheral func in my BluetoothManager class, I'm able to connect without any problem... But I would like to be able to choose the right BLE device and so I have to connect after the user selected a row.

In class BluetoothManager

var CBmanager: CBCentralManager = CBCentralManager()

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
{
    let UUID = "\(peripheral.identifier)".substringFromIndex("\(peripheral.identifier)".startIndex.advancedBy(31))

    if !peripheralArray.contains({$0.UUID == UUID})
    {
        if peripheral.name != nil
        {
            peripheralArray.append(BluetoothPeripheral(name: peripheral.name!, UUID: UUID, RSSI: RSSI, peripheral: peripheral))

            if peripheral.name!.containsString(DEVICE_NAME)
            {
                CBmanager.connectPeripheral(peripheralArray[selectedPeripheralIndex].peripheral, options: nil)
            }
        }
    }
}

Any ideas?

Okay I solved the problem. It seems to be an issue with the global array peripheralArray . If I use a local array it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM