我會製作應用程序,它允許我從外圍設備下載一些數據。我可以連接外圍設備,但無法下載此設備支持的服務。我沒有第二個作爲外設的應用程序。第二個設備是一個iPad,它具有在LightBlue.app中製作的虛擬外設。有時它被稱爲空白,有時被稱爲iPad,我不知道爲什麼。如何查看外圍設備的BLE服務?
這是我的代碼:
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBCentralManagerDelegate{
@IBOutlet var coreBluetooth: UILabel!
@IBOutlet var discoveredDevices: UILabel!
@IBOutlet var foundBLE: UILabel!
@IBOutlet var connected: 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() {
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: (NSDictionary), RSSI: NSNumber!) {
discoveredDevices.text = "Discovered \(peripheral.name)"
println("Discovered: \(peripheral.name)")
centralManager.stopScan()
if peripheral.name == "iPad" || peripheral.name == "Blank"
{
println("ok")
centralManager.connectPeripheral(peripheral, options: nil)
self.connectingPeripheral = peripheral
}
}
func centralManagerDidUpdateState(central: CBCentralManager!) { //BLE status
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()
}
}
func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!)
{
connectingPeripheral.discoverServices(nil)
println("Connected")
foundBLE.textColor = UIColor.redColor()
foundBLE.text = "Connected to: \(peripheral.name)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func scanBLE(sender: UIButton) {
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}
func connectingPeripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!)
{
println("Services \(connectingPeripheral.services)")
}
}
@JasonMArcher。 [我也是,刪除問題](http://data.stackexchange.com/stackoverflow/query/151579/unnecessary-question-labelling),但嘗試修復其他事情時,你談論它。 – TRiG 2014-09-13 22:36:17
@TRiG我會盡力改善這個問題。 – Spamizator 2014-09-14 15:14:09