我正在尝试通过BLE发送大数据包。为此,我创建了一个特征,该特征应在数据更改时通知中央。问题在于,显然外围设备会在特性中堆叠数据,但是很明显,一旦特性达到552字节,便无法存储更多数据。
中央仅接收552个字节并调用函数didReceiveRead
3次(我一次传送200个字节,所以3次是600字节,但是只有552个字节使它通过(我认为是自iOS 10起的限制))。
这段代码也只打印Unhandled Characteristic UUID: 00000000-0000-0000-0000-000000000000
一次,并设法将setNotifyValue
的特征复制到true
。 didUpdateNotificationStateFor
也仅被调用一次。
我想知道为什么我的特征是堆积数据,而不像我希望的那样一次发送200个字节。
外围设备的代码使用为: (函数块仅获取我们想要的块的大小的数据和整数,然后返回该块和减去块后的数据)
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
switch peripheral.state {
case .unknown:
print("peripheral.state is .unknown")
case .resetting:
print("peripheral.state is .resetting")
case .unsupported:
print("peripheral.state is .unsupported")
case .unauthorized:
print("peripheral.state is .unauthorized")
case .poweredOff:
print("peripheral.state is .poweredOff")
case .poweredOn:
print("peripheral.state is .poweredOn")
let myService = CBMutableService(type: serviceUUID, primary: true)
let uuid: CBUUID
uuid = CBUUID(string: "00000000-0000-0000-0000-000000000000")
let myCharacetristic = CBMutableCharacteristic(type: uuid, properties: [CBCharacteristicProperties.read,CBCharacteristicProperties.notify,CBCharacteristicProperties.write], value: nil, permissions: [CBAttributePermissions.readable, CBAttributePermissions.writeable])
chara = myCharacetristic
myService.characteristics = [myCharacetristic]
print(myService)
myPeripheral.add(myService)
let advertisingData = [CBAdvertisementDataLocalNameKey:"my-peripheral", CBAdvertisementDataServiceUUIDsKey: [serviceUUID]] as [String : Any]
myPeripheral.startAdvertising(advertisingData)
print(advertisingData.description)
}
}
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
var max = 0
if (data.count - 200) > 0{
max = 200
}else{
max = data.count
}
var subdata: Data //subdata is what I want to send
(subdata, data) = chunck(json: data, max: max)
print(request.value)
request.value = subdata
myPeripheral?.respond(to: request, withResult: CBATTError.success)
}
对于中央代码是:
public func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else { return }
print("Characteristic:")
for characteristic in characteristics {
print(characteristic)
if characteristic.properties.contains(.read) {
print("\(characteristic.uuid): properties contains .read")
peripheral.readValue(for: characteristic)
}
if characteristic.properties.contains(.notify) {
print("\(characteristic.uuid): properties contains .notify")
peripheral.setNotifyValue(true, for: characteristic)
print("ok for: \(characteristic)")
}
}
}
public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
print("Unhandled Characteristic UUID: \(characteristic.uuid)")
print(error)
}
public func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
print(characteristic)
print(characteristic.value)
}
随时询问更多代码
谢谢。