Home>
I want to exchange data using Swift's Core Bluetooth, but I don't understand the mechanism of data exchange between peripherals and central. I want to send a UIImage from a peripheral and Central receives it and displays it.
Could you tell me something small?
I wrote to change UIImage to data with the SendImage () function on the peripheral side and send it to central with updatevalue.
I don't know how the central side should receive this data.
extension UIImage {
public func toPNGData ()->Data {
guard let data = self.pngData () else {
return Data ()
}
return data
}
}
private func sendImage () {
guard let transferCharacteristic = transferCharacteristic else {
return
}
let chunk = pngData
os_log ("% d bytes from image", chunk.count)
peripheralManager.updateValue (pngData, for: transferCharacteristic, onSubscribedCentrals: nil)
os_log ("Sent data of image")
}
//abridgement
func peripheralManager (_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
os_log ("Central subscribed to characteristic")
imageToSend = UIImage (named: "turtlerock")!
pngData = imageToSend.toPNGData ()
sendDataIndex = 0
connectedCentral = central
sendImage ()
}
Supplementary information
peripheral os_log
139102 bytes from image
Sent data of image
Is displayed, but it seems that it has not been successfully sent to Central.
-
Answer # 1
Related articles
- [swift ui] questions about core data templates
- python - get exchange chart data with api
- core data - about swiftui toggle
- ios - detection of terminals within the bluetooth range and acquisition of information
- core data - exc_bad_instruction appears in fetchedrequestwrappedvaluegetter
- ios - i want to save data using sqlite
- Detailed iOS data storage
- ios - bluetooth module not visible from iphone
- ASPNET Core Data Protection (Part I)
- ASPNET Core Data Protection (Part I)
- ios - how to get data of firestore sub-collection
- ios - save data with swift (todolist)
- i want to transmit data via bluetooth on ios
Related questions
- ios - data of authauth () currentuser? uid cannot be deleted even if the application is deleted
- ios : UIButton title doesn't work
- ios : PROFILE PROFILE DOESNT INCLUDE SIGNING CERTIFICATE
- swift : UIDocumentPickerViewController can't get the files in the iCloud document
- ios - i don't see the xcode simulator
- ios - about the mechanism that can pass by value at the time of screen transition in segue
- ios - it is about line breaks when using uitextview wrapped in swift ui
- ios : CHILDVIEWCONTROLLER.VIEW.FRAME changes in screen rotation
- swift : Screen transition using Segue in SKview
- ios - thread 1: exception: error is displayed!
The data size that can be sent at one time via Bluetooth is 27 bytes up to version 4.1. From Version 4.2, it's 255 bytes, so you should think that you can't send images.