我试图从我的iOS应用程序中连接到的BLE设备上发现服务,但是当我的didDiscoverServices函数被调用时,外围服务数组是空的。
应用程序连接到设备,运行peripheral.discoverServices(nil),然后调用didDiscoverServices函数,但没有返回任何服务。
我已经在网上阅读了许多与蓝牙相关的答案和其他示例,而且我能说的最好,我的代码与它应该是什么不同,除非它没有用。我觉得我必须在某个地方错过一些东西,但我不确定是什么。
我已经在下面添加了我的控制台日志,以了解运行代码时获得的内容以及底部的蓝牙代码以供参考。
Bluetooth initialised
BLE is powered on
Optional("Nordic_Blinky") found at -72
Scanning stopped
Connect request sent
Connected to <CBPeripheral: 0x1c0301a70, identifier = 0887CF7F-98C8-3FCF-2D10-873FFFFB2B65, name = Nordic_Blinky, state = connected>
Discovering services
Services -- Optional([]) and Error -- nil
我的BluetoothHandler类代码在
之下class BluetoothHandler : NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
// MARK: Properties
var manager: CBCentralManager!
var targetPeripheral: CBPeripheral!
// MARK: Shared Instance
static let sharedInstance = BluetoothHandler()
private override init() {
super.init()
self.startManager()
print("Bluetooth initialised")
}
// MARK: Functions
func startManager() {
manager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
var consoleMessage = ""
switch (central.state) {
case.poweredOn:
consoleMessage = "BLE is powered on"
manager.scanForPeripherals(withServices: nil, options: nil)
case.poweredOff:
consoleMessage = "BLE is powered off"
case.resetting:
consoleMessage = "BLE Resetting"
case.unknown:
consoleMessage = "BLE state unknown"
case.unsupported:
consoleMessage = "Device not supported by BLE"
case.unauthorized:
consoleMessage = "BLE not authorised"
}
print("\(consoleMessage)")
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if (peripheral.name == "Nordic_Blinky") {
print("\(String(describing: peripheral.name)) found at \(RSSI)")
self.stopScan()
if targetPeripheral != peripheral {
targetPeripheral = peripheral
targetPeripheral!.delegate = self
manager.connect(targetPeripheral, options: nil)
print("Connect request sent")
}
}
else if (peripheral.name != nil) {
print("\(String(describing: peripheral.name))")
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Connected to \(peripheral)")
peripheral.delegate = self
peripheral.discoverServices(nil)
print("Discovering services")
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Any) {
print("Connection failed", error)
}
func centralManager(_ central: CBCentralManager, didDisconnect peripheral: CBPeripheral) {
if self.targetPeripheral != nil {
self.targetPeripheral!.delegate = nil
self.targetPeripheral = nil
}
print("Connection disconnected")
self.startManager()
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
print("Services -- \(peripheral.services) and Error -- \(error)")
if let services = peripheral.services {
for service in services {
peripheral.discoverCharacteristics(nil, for: service)
}
}
}
答案 0 :(得分:0)
试试这个:swift 3
如果您的外围设备(BLE设备)有服务,则在日志中打印。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="w3-sand w3-large">
<div class="w3-container " id="schedule">
<div class="w3-content width-max ">
<h4 class="w3-center p2t p1b"><span class="w3-tag w3-wide ">SCHEDULE</span></h4>
</div>
<ul class="timeline ">
<li class="event" data-date="09:30 - 10:00 ">
<div onclick="document.getElementById('id01').style.display='block'">
<h3 class="event-title">Registration</h3>
<p>It is on first come first server basis. Pls hurry</p>
</div>
</li>
<li class="event" data-date="10:00 - 10:30 ">
<div>
<h3 class="event-title">Introduction</h3>
<p><strong> Speaker </strong>
<br />
</p>
<div class="speaker">
<img src="./avatar.png" alt="Boss" class="w3-circle">
<h3>Michael<br />Lee</h3>
</div>
</div>
</li>
<li class="event" data-date="10:30 - 11:15 ">
<div>
<h3 class="event-title">Speech 1</h3>
<p><strong> Speaker </strong>
<br />
</p>
<div class="speaker">
<img src="./avatar.png" alt="Boss" class="w3-circle">
<h3>David<br />Anderson</h3>
</div>
</div>
</li>
<li class="event" data-date="11:30 - 12:15 ">
<div>
<h3 class="event-title">Speech 2</h3>
<p><strong> Speakers </strong>
<br />
</p>
<div class="speaker">
<img src="./avatar.png" alt="Boss" class="w3-circle">
<h3>Steve<br />Newman</h3>
</div>
</div>
</li>
<li class="event" data-date="12:30 - 13:15 ">
<div>
<h3 class="event-title">Competetion</h3>
<p><strong> Speakers </strong>
<br />
</p>
<div class="speaker">
<img src="./avatar.png" alt="Boss" class="w3-circle">
<h3>Mark<br />Smith</h3>
</div>
</div>
</li>
<li class="event" data-date="14:00 - 14:45 ">
<div>
<h3 class="event-title">Prize Distribution</h3>
<p><strong> Speaker </strong>
<br />
</p>
<div class="speaker">
<img src="./avatar.png" alt="Boss" class="w3-circle">
<h3>John<br />Doe</h3>
</div>
</div>
</li>
</ul>
</div>
<!-- Modals -->
<div id="id01" class="w3-modal w3-grayscale">
<div class="w3-modal-content w3-card-4 w3-animate-opacity middle-modal">
<header class="w3-container w3-khaki w3-display-container">
<span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-khaki w3-display-topright"><i class="fa fa-remove"></i></span>
<h4>Register</h4>
</header>
<div class="w3-container bio">
<p>Registration link goes here</p>
</div>
</div>
</div>
<div id="id02" class="w3-modal">
<div class="vertical-alignment-helper">
<div class="vertical-align-center">
<div class="w3-modal-content w3-card-4 w3-animate-opacity">
<header class="w3-container w3-khaki w3-display-container">
<span class="w3-button w3-khaki w3-display-topright close-modal"><i class="fa fa-remove"></i></span>
<h4></h4>
</header>
<div class="w3-container bio">
<img alt="Speaker" class="w3-round modal-image">
<span></span>
</div>
</div>
</div>
</div>
</div>
</div>