So i have 2 iOS devices : one is Peripheral and the other Central. I want that the data would be image. I have tried with a string value and it is working fine but with image i get this error:"read_user_chunkIDOT:1221: invalid PNG file: no valid iEnd chunk", also i can see that the bytes are different( Optional(526 bytes)), they are more larger when i get them.
This is the peripheral:
if let img = UIImage(named: "maiden") {
let data = UIImagePNGRepresentation(img)
let base64 = data?.base64EncodedData(options: .lineLength64Characters)
let char = CBMutableCharacteristic(type: CHAR_UUID, properties: [.read], value: base64!, permissions: [.readable])
let myRoei = CBMutableService(type: RX_UUID, primary: true)
myRoei.characteristics = [char]
cameraPeripheralManager.add(myRoei)
cameraPeripheralManager.startAdvertising([CBAdvertisementDataServiceUUIDsKey:[RX_UUID], CBAdvertisementDataLocalNameKey: advertisementData])
}
This is the Central inside didUpdateValueFor characteristic:
print(characteristic.value as Any)
switch characteristic.uuid {
case CHAR_UUID:
let image = UIImage(data: Data(base64Encoded: characteristic.value!, options: .ignoreUnknownCharacters)!)
self.imageView.image = image
_ = bodyLocation(from: characteristic)
case RX_UUID: break
// onHeartRateReceived(bpm)
default:
print("Unhandled Characteristic UUID: \(characteristic.uuid)")
}
Would like to know where i am wrong. Thanks in advanced!
So i have managed doing so with this code:
The code below is the client side:
Very useful code that helps you break the data into chunks and deliver the last chunk with a string to notify the central device that the transfer finished.