我们如何使用BLE将garmin设备连接到iOS应用程序

时间:2017-05-18 10:15:14

标签: ios swift bluetooth bluetooth-lowenergy garmin

我正在为心率监测开发一个iOS应用程序,并希望使用蓝牙将garmin设备连接到我的应用程序。

但我无法使用(180D服务UUID)在Swift中列出任何使用Core蓝牙的Garmin设备。

  func startUpCentralManager() {
        print("Initializing central manager")
        centralManager = CBCentralManager(delegate: self, queue: nil)
    }

    func discoverDevices() {
        print("discovering devices")
        let services = [CBUUID(string: HRM_HEART_RATE_SERVICE_UUID),CBUUID(string:HRM_DEVICE_INFO_SERVICE_UUID),CBUUID(string:HRM_DEVICE_BATTERY_LEVEL_SERVICE_UUID),CBUUID(string:CALORIE_SERVICE)]
        centralManager.scanForPeripherals(withServices: services, options: nil)
    }

    //MARK: CBCentralManagerDelegate
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print("checking state")
        switch (central.state) {
        case .poweredOff:
            print("CoreBluetooth BLE hardware is powered off")

        case .poweredOn:
           print("CoreBluetooth BLE hardware is powered on and ready")
           blueToothReady = true;

        case .resetting:
            print("CoreBluetooth BLE hardware is resetting")

        case .unauthorized:
            print("CoreBluetooth BLE state is unauthorized")

        case .unknown:
            print("CoreBluetooth BLE state is unknown");

        case .unsupported:
            print("CoreBluetooth BLE hardware is unsupported on this platform");

        }
        if blueToothReady {
            discoverDevices()
        }
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

        let nameOfDeviceFound = (advertisementData as NSDictionary).object(forKey: CBAdvertisementDataLocalNameKey) as? NSString
        print("\(String(describing: nameOfDeviceFound))")
        print("Discovered Peripheral name : ", peripheral.name ?? String())

           if !Utilities.allPeripheralList.contains(peripheral)
           {
               Utilities.allPeripheralList.add(peripheral)
        }
        let deviceObj = DeviceModel()
        deviceObj.deviceName = peripheral.name ?? String()
        deviceObj.UDID = String(format: "%@",(peripheral.identifier).uuidString)
        self.addingPeripheralToGlobalSearchArray(obj: deviceObj)

    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        peripheral.delegate = self
        peripheral.discoverServices(nil)
        self.connected = peripheral.state == CBPeripheralState.connected ? "YES" : "NO"
        print("Connection Status : %@", self.connected)
        if self.connected == "YES"{

    Utilities.sharedInstance.goToDashboardTabBarScreen(viewController: self)
        }
    }


func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
    //        central.scanForPeripherals(withServices: nil, options: nil)
}

func addingPeripheralToGlobalSearchArray(obj:DeviceModel){
    var isFound:Bool = false
    for i in 0..<Utilities.allDeviceList.count{
        let deviceObj = Utilities.allDeviceList[i] as! DeviceModel
        if obj.UDID == deviceObj.UDID{
            isFound = true
        }
    }

    if !isFound{
        Utilities.allDeviceList.add(obj)
    }
}

以上是我用来列出BLE设备的代码。我能够使用相同的代码安静而不是Garmin设备找到Mio-Fuse设备。

1 个答案:

答案 0 :(得分:0)

您需要使用Garmin Standard SDK。联系Garmin并注册为开发人员。您无法使用BLE直接连接,而必须通过Garmin SDK API连接。