[英]How can I see the BLE services of a peripheral device?
我將制作一個可以讓我從外圍設備下載一些數據的應用程序。 我可以與外圍設備連接,但是無法下載該設備支持的服務。 我沒有用作外圍設備的第二個應用程序。 第二個設備是iPad,它具有由LightBlue.app制造的虛擬外圍設備。 有時稱為Blank,有時稱為iPad,我不知道為什么。
這是我的代碼:
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)")
}
}
您需要在func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!)
中將外設的委托設置為self,以便在發現服務時回叫-
func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!)
{
connectingPeripheral.delegate=self
connectingPeripheral.discoverServices(nil)
println("Connected")
foundBLE.textColor = UIColor.redColor()
foundBLE.text = "Connected to: \(peripheral.name)"
}
然后,您將返回到func connectingPeripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!)
迅捷3
var peripheralDevice:CBPeripheral!
//if connected
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("connected")
self.peripheralDevice.discoverServices(nil)
}
//if disconnect
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("Disconnect")
}
//fail to connect
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print("Fail to connect, Please try again.")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.