我知道上面提到的断开连接错误在堆栈中被问过很多次,但是接受的答案并不是实际的解决方案
我正在尝试将MI Band 3与我的快速App连接起来。它已成功连接,但一段时间后会自动断开连接
Error: Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us." UserInfo={NSLocalizedDescription=The specified device has disconnected from us
我的应用程序要求:我需要从Apple Watch,Fitbit和MI Smart Watch获取心率和步行距离
我尝试在多个设备上连接相同的MI Band,结果相同。但是那个特定的MI乐队可以与自己的应用完美配合
--> Connect Device - Kept reference of connected Device
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Connected With Peripheral: \(peripheral)")
selectedPeripheral=peripheral
self.delegate?.scannedPeripherals(Is: peripheral)
/// Discover Services Provided By Device
selectedPeripheral?.delegate=self
selectedPeripheral?.discoverServices([heartRateServiceCBUUID])
}
---> After Discovering services With heart Rate CBUUID
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else { return }
selectedPeripheral=peripheral
for char in characteristics {
print("Characterstics: \(char)")
if char.properties.contains(.read) {
}
if char.properties.contains(.notify) {
/// ------------- Setting Notify to true but not never call required delegates ----------------
peripheral.setNotifyValue(true, for: char)
print("\(char.uuid): properties contains .notify")
peripheral.readValue(for: char)
}
}
}
Calculating BPM from delegate
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
switch characteristic.uuid {
case bodySensorLocationCharacteristicCBUUID:
let bodySensorLocation = bodyLocation(from: characteristic)
print(bodySensorLocation)
default:
/// --------- here characteristic value is nil --------------
let bpm = heartRate(from: characteristic)
print("BPM: \(bpm)")
}
}
答案 0 :(得分:0)
在func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
尝试删除peripheral.readValue(for: char)
,然后再次测试!