我正在创建连接到自定义控制器的应用程序并通过蓝牙读取一些数据,应该是非常简单的东西。 应用程序具有地址和数据特征: - 我正在写地址来解决特性(这样我告诉控制器我希望从给定的地址获得哪些值) - 控制器使用给定地址的值更新数据特征
我会在12秒的时间间隔内不断更新地址,以便从不同的地址接收不同的值。 应用程序工作正在从控制器读取数据。但是在一段时间后,数据地址上的值不会更新,并且控制器会为不同的地址返回相同的值。 我们知道控制器的工作原理,因为该应用程序的Android版本已经制作完成并且它就像一个魅力。
我想知道是否有人知道可能出现什么问题?有没有办法蓝牙连接改变服务/特性,所以我也必须更新它们?
EDIT 之前无法添加代码,所以这里是主要部分: 把它们加起来: 我在写作时使用.readValue和.writeWithResponse手动读取值。
internal func nextIteration() {
if _writeQueue.count > 0 {
self.writeData()
} else {
self.readData()
}
}
internal func writeData() {
let writeProperty = _writeQueue[0]
self.writeDataCharacteristics(writeProperty: writeProperty)
_writeQueue.remove(at: 0)
}
internal func readData() {
if _resetReadParameters || _currentPropertiesNumber != _selectedPropertiesNumber {
if _selectedPropertiesNumber >= _readQueue.count || _selectedPropertiesNumber < 0 {
_selectedPropertiesNumber = 0
}
_currentProperties = _readQueue[_selectedPropertiesNumber]
writeAddressCharacteristics()
_resetReadParameters = false
_currentPropertiesNumber = _selectedPropertiesNumber
return
}
readDataCharacteristics()
_selectedPropertiesNumber += 1
if _selectedPropertiesNumber >= _readQueue.count {
_selectedPropertiesNumber = 0
}
}
internal func writeAddressCharacteristics() {
if _currentProperties.count != Constants.maxProperties {
print("Not enough current properties.")
return
}
_addressBytes.removeAll()
for property in _currentProperties {
let a = property.address.rawValue
_addressBytes.append(UInt8(a & 0xFF))
_addressBytes.append(UInt8(a >> 8))
}
peripheral?.writeValue(Data(bytes: _addressBytes), for: _address, type: .withResponse)
}
internal func writeDataCharacteristics(writeProperty: Property) {
_writeBytes?.removeAll()
let d = UInt16(bitPattern: Int16(writeProperty.value!))
_writeBytes = [UInt8(d & 0xFF), UInt8(d >> 8), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
peripheral?.writeValue(Data(bytes: _writeBytes!), for: _data, type: .withResponse)
}
internal func readDataCharacteristics() {
_communicatingData = true
peripheral?.readValue(for: _data)
}
internal func parseValueBroadcast(bytes: [UInt8], property: Property, byteIndex: Int) {
let value = Int(Int16(bitPattern: UInt16(bytes[byteIndex]) | UInt16(bytes[byteIndex + 1]) << 8))
property.value = value
_communicationDelegate?.didReceiveProperty(property: property)
}
internal func setWriteProperty(address: Address, value: Int) {
let newProperty = Property.get(address)
newProperty.value = value
_writeQueue.append(newProperty)
if _pausedReading {
nextIteration()
}
}
// MARK: CBPeripheralDelegate
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
if let writeError = error {
print("Error writing: \(writeError.localizedDescription)")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 4, execute: {
self.nextIteration()
})
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
print("READ ERROR:", error.localizedDescription)
return
}
guard let value = characteristic.value else {
print("READ ERROR: NO DATA")
return
}
let bytes = [UInt8](value)
if !_pausedReading {
var n = 0
for property in _currentProperties {
parseValueBroadcast(bytes: bytes, property: property, byteIndex: n)
n += 2
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 4, execute: {
self.nextIteration()
})
}
}