0

我正在尝试通过 BLE 发送大数据包。为此,我创建了一个特征,该特征应在数据更改时通知中心。问题是显然外设在特征中堆积数据,但很明显一旦特征达到552字节它就不能储存更多的数据。

中央只接收 552 个字节并调用该函数didReceiveRead3 次(我一次传输 200 个字节,所以 3 次是 600 个字节,但只有 552 个字节使它成为低谷(我认为这是自 iOS 10 以来的限制))。这段代码也只打印Unhandled Characteristic UUID: 00000000-0000-0000-0000-000000000000一次并管理到setNotifyValue我的特性true。也didUpdateNotificationStateFor只叫一次。

我想知道为什么我的特点是堆积数据而不像我想的那样一次发送 200 个字节。

外围设备使用的代码是:(函数块只取数据和我们想要的块大小的 int 并返回块和数据减去所取的块)

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)
}

随意询问更多代码

谢谢。

4

0 回答 0