Last time I talked about turning a Data object into a single scalar. Now I’m going to turn a Data object into an array of scalars. Based on the last post, it’s tempting to try something like this: let data = Data(bytes: [0x01, 0x00, 0x00, … [Read more]
Turning Data into Numbers in Swift
When you read from a Bluetooth Device, Core Bluetooth returns a Data object with the contents. A Data object is basically a container for raw bytes without a type. How do you get that data into a proper Swift type like an Int or a Float? … [Read more]
Converting a Core Bluetooth CBUUID to a Foundation UUID
Here’s a short and sweet extension that converts a CoreBluetooth CBUUID to a Foundation UUID. It’s interesting mainly because of the time it took me to figure out how to use the raw data.1 I had to get familiar with pointer functions in … [Read more]
How to Get a Response from a CoreBluetooth Write
You write to a Bluetooth device in CoreBluetooth using the CBPeripheral method writeValue(_:for:type:). You can ask the peripheral for confirmation by indicating the type, which is either .withResponse or .withoutResponse. If you pass .withResponse, … [Read more]
Why You Can’t Assign nil to Just Anything in Swift
Swift takes a strong stance on which types can be assigned nil. For example, this is an error: var i = 1 i = nil //error: nil cannot be assigned to type 'Int' // It is also an error for objects: var obj = NSObject() obj = nil … [Read more]
Read From A Bluetooth LE Device with CoreBluetooth
Apple’s CoreBluetooth Programming Guide is pretty good, but it can be overwhelming if you’re diving into Bluetooth LE (BLE) for the first time. One potential point of confusion is that some objects are needed to connect to another device, … [Read more]