This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to write an iOS app for a simple BLE - UART in swift?

I'm writing a custom app to perform a UART service between an nRF51822 module and iOS device.

github.com/.../nRFSampleSwiftApp_iOS.git

Please follow the above link to modify/make changes and but feel free to download and play with it.

Based on nRF-iOS-Toolbox,

//This is my write code
let bytes : [UInt8] = [ 0x1A, 0x2B, 0x3C, 0x4D ]
        let Transmitdata = NSData(bytes: bytes, length: bytes.count)
        peripheral.writeValue(Transmitdata as Data, for: txCharacteristic!, type: CBCharacteristicWriteType.withoutResponse)
        print("Data Sent",

//To read the received bytes from Ble.

let ReceiveData = rxCharacteristic?.value
        if let ReceiveData = ReceiveData {
            let ReceivedNoOfBytes = ReceiveData.count
            var ReceivedByteArray = [UInt8](repeating: 0, count: ReceivedNoOfBytes)
            (ReceiveData as NSData).getBytes(&ReceivedByteArray, length: ReceivedNoOfBytes)
            print("Data Received ",ReceivedByteArray)

Now before you start, you will need to change the UUID.swift class to suit the uuid requirements for your ble. At this stage i have left the UUID to the standard UUID as mentioned in nRF-iOS-Toolbox App. Use the below link if you are not sure what they are.

github.com/.../NORServiceIdentifiers.swift

Now to keep it simple its just a UART Service peoformed in various views. I have added a UIStepper, UISlider and few UIButtons.

Idea is to send the slider/stepper value as peripheral.writeValue And also sending some data to ble upon button click.

I'm a beginner to Swift and Xcode so if you think i can do it better then feel free to update it in the git.

I am only using basic chat code as a reference as I am able to understand how things work and play around. But to my knowledge nRF-iOS-Toolbox is far more superior to my level as a beginner but is more stable and never ever crashes.

If you can workout the same using the nRF UART block from the nRF-iOS-Toolbox, please do update the git so many users like me can learn from it as a reference.

Regards nar

Related