CoreBluetooth:找不到iPad

时间:2014-08-27 13:42:38

标签: ios iphone ipad swift gps

我不知道如何使用CoreBluetooth而我正试图找到我的iPad并从iPad获取gps,加速度计,陀螺仪的信息(不要问为什么) 。我知道如何获得GPS位置,但我也不知道如何获得加速度计和陀螺仪。 Plz帮助我。 应用程序必须迅速。

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate {


    @IBOutlet var initializing: UILabel!
    @IBOutlet var disconvering: UILabel!
    @IBOutlet var checkingState: UILabel!
    @IBOutlet var coreBluetooth: UILabel!
    @IBOutlet var discoveredDevices: UILabel!




    var centralManager:CBCentralManager!
    var blueToothReady = false

    override func viewDidLoad() {
        super.viewDidLoad()
        startUpCentralManager()
    }

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

    func discoverDevices() {
        disconvering.text = "Discovering devices"
        centralManager.scanForPeripheralsWithServices(nil, options: nil)
    }

    func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
        discoveredDevices.text = "Discovered \(peripheral.name)"
    }

    func centralManagerDidUpdateState(central: CBCentralManager!) {
        checkingState.text = "Checking state"
        switch (central.state) {
        case .PoweredOff:
            coreBluetooth.text = "CoreBluetooth BLE hardware is powered off"

        case .PoweredOn:
            coreBluetooth.text = "CoreBluetooth BLE hardware is powered on and ready"
            blueToothReady = true;

        case .Resetting:
            coreBluetooth.text = "CoreBluetooth BLE hardware is resetting"

        case .Unauthorized:
            coreBluetooth.text = "CoreBluetooth BLE state is unauthorized"

        case .Unknown:
            coreBluetooth.text = "CoreBluetooth BLE state is unknown"

        case .Unsupported:
            coreBluetooth.text = "CoreBluetooth BLE hardware is unsupported on this platform"

        }
        if blueToothReady {
            discoverDevices()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

1 个答案:

答案 0 :(得分:0)

好的,我在LighBlue应用程序中创建了虚拟外设,这段代码可以运行:

  //
  //  ViewController.swift
  //  BLE
  //
  //  Created by Tomasz Kaczmarzyk on 01.09.2014.
  //  Copyright (c) 2014 Tomasz Kaczmarzyk. All rights reserved.
  //
  import UIKit
  import CoreBluetooth

  class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate{


@IBOutlet var errorSupport: UILabel!
@IBOutlet var servicesBLE: UILabel!


var centralManager:CBCentralManager!
var blueToothReady = false
var connectingPeripheral: CBPeripheral!

override func viewDidLoad() {
super.viewDidLoad()
startUpCentralManager()
}

func startUpCentralManager() {
centralManager = CBCentralManager(delegate: self, queue: nil)   
}

func discoverDevices() { //Szukanie BLE urzadzen
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: (NSDictionary), RSSI: NSNumber!) {

println("Discovered: \(peripheral.name)")
self.connectingPeripheral = peripheral
centralManager.stopScan()
if peripheral.name == "iPad" || peripheral.name == "Time" || peripheral.name == "Blank"
{
self.centralManager.connectPeripheral(peripheral, options: nil)
}
}

func centralManagerDidUpdateState(central: CBCentralManager!) { //BLE status
switch (central.state) {
case .PoweredOff:
    errorSupport.text = "CoreBluetooth BLE hardware is powered off"

case .PoweredOn:
    errorSupport.text = "CoreBluetooth BLE hardware is powered on and ready"
    blueToothReady = true;

case .Resetting:
    errorSupport.text = "CoreBluetooth BLE hardware is resetting"

case .Unauthorized:
    errorSupport.text = "CoreBluetooth BLE state is unauthorized"

case .Unknown:
    errorSupport.text = "CoreBluetooth BLE state is unknown"

case .Unsupported:
    errorSupport.text = "CoreBluetooth BLE hardware is unsupported on this platform"

}
if blueToothReady {
    discoverDevices()
    }
}

 func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!)
{
    peripheral.delegate = self
    peripheral.discoverServices(nil)
    println("Connected")

}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!)
{
    if let servicePeripherals = peripheral.services as? [CBService]
    {
        for servicePeripheral in servicePeripherals
        {
            servicesBLE.text = "Services: \(servicePeripheral)"
            println(servicePeripheral)
            peripheral.discoverCharacteristics(nil, forService: servicePeripheral)
        }
    }
}
@IBAction func refreshBLE(sender: UIButton) {
        centralManager.scanForPeripheralsWithServices(nil, options: nil)
}

func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
    if let charactericsx = service.characteristics  as? [CBCharacteristic]
    {
        for charactericsx in charactericsx
        {
            //servicesBLE.text = "Characterics: \(charactericsx)"
            println(charactericsx)
            peripheral.readValueForCharacteristic(charactericsx)
        }

    }
}

func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
    var data :NSData = characteristic.value
    println("Data \(characteristic.value)")
    }
}

此代码适用于LightBlue应用程序,并已从CoreBluetooth Objective C编程指南转换。