CoreBluetooth:无法找到iPad [英] CoreBluetooth: Can't find iPad

查看:182
本文介绍了CoreBluetooth:无法找到iPad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I don't know how to use CoreBluetooth and I'm trying to find my iPad and get info of gps, accelerometer, gyroscope from that iPad (don't ask why). I know how to get gps location but I don't know how to get accelerometer and gyroscope too. Plz help me. App must be in swift.

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.
    }

}


推荐答案

确定我在LighBlue应用程序中创建了虚拟外设,并且此代码正常工作:

Ok I made virtual Peripheral in LighBlue app and this code works:

  //
  //  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编程指南转换。

This code works with LightBlue app and was converted from CoreBluetooth Objective C Programming guide.

这篇关于CoreBluetooth:无法找到iPad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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