如何在ios swift中连接蓝牙低功耗? [英] how to connect with bluetooth low energy in ios swift?

查看:157
本文介绍了如何在ios swift中连接蓝牙低功耗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接外设。
但是我的代码没有调用didConect函数

I want to connect with ble peripheral. But my code doesn't call didConect function

这是我的代码:

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 {

            print("Did discover peripheral", peripheral)

            self.bluetoothManager.stopScan()
            self._peripheral = peripheral
            self._peripheral.delegate = self
            central.connect(peripheral, options: nil)
        }
}


func centralManager( central: CBCentralManager, didConnect peripheral: CBPeripheral) { //cant call this
        print("connected to \(BEAN_NAME)")
        peripheral.discoverServices(nil)
    }

日志:

BLE service is powered on
Did discover peripheral <CBPeripheral: 0x1740eef00, identifier = 4872623B-F872-443A-8A96-F4E1F84D6841, name = GoDoor in  :), state = disconnected>


推荐答案

我已经解决了这个问题。

只需更改:

I've solved this.
just need to change :

func centralManager(central: CBCentralManager, didConnect peripheral: CBPeripheral)

进入:

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)

reference:Swift 3:无法通过BLE连接到外围设备

reference : Swift 3: Can't connect to peripheral via BLE

这篇关于如何在ios swift中连接蓝牙低功耗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆