From 90f73feb28331ddd83f0508adbdb58934c44c535 Mon Sep 17 00:00:00 2001 From: dimohamdy Date: Sat, 30 Apr 2016 03:17:19 +0200 Subject: [PATCH] receive more bytes receive more bytes 20 bytes convert Data to chunks --- BLEFramework/BLE/BLE.swift | 60 ++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/BLEFramework/BLE/BLE.swift b/BLEFramework/BLE/BLE.swift index 0ab1297..9c4218a 100644 --- a/BLEFramework/BLE/BLE.swift +++ b/BLEFramework/BLE/BLE.swift @@ -1,14 +1,14 @@ /* - -Copyright (c) 2015 Fernando Reynoso - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -*/ + + Copyright (c) 2015 Fernando Reynoso + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + */ import Foundation import CoreBluetooth @@ -236,17 +236,39 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { } func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) { - - if error != nil { - - print("[ERROR] Error updating value. \(error!.description)") - return + #if BLE_DEBUG + print("In BLE.peripheral didUpdateValueForCharacteristic") + #endif + if error == nil { + if characteristic.UUID.UUIDString == RBL_CHAR_TX_UUID { + #if BLE_DEBUG + print("characteristic.value=\(characteristic.value)") + #endif + let length = characteristic.value!.length + let chunkSize = 20 // 1mb chunk sizes + var offset = 0 + + repeat { + // get the length of the chunk + let thisChunkSize = ((length - offset) > chunkSize) ? chunkSize : (length - offset); + + // get the chunk + let chunk = characteristic.value!.subdataWithRange(NSMakeRange(offset, thisChunkSize)) + + // ----------------------------------------------- + // do something with that chunk of data... + // ----------------------------------------------- + self.delegate?.bleDidReceiveData(chunk) + + // update the offset + offset += thisChunkSize; + + } while (offset < length); + } + } else { + print("[ERROR] Update Value For Characteristic failed!. Error: \(error!.description)") } - if characteristic.UUID.UUIDString == RBL_CHAR_TX_UUID { - - self.delegate?.bleDidReceiveData(characteristic.value) - } } func peripheral(peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: NSError?) {