Best practice: iBeacon


Welcome to the next part of the series of “How to deal with BLE in the background“.
In the previous part, I guided you how to keep your app alive as long as possible when your app enters to background mode by using State Preservation and Restoration technique supported by Apple. However, there are some usecases this technique can not handle, as described below (refer to Apple document: Conditions Under Which Bluetooth State Restoration Will Relaunch An App)

As you can see, there is a common case when users force quit the app from the multiple task view (Whether accidentally or intentionally), the Restoration technique can not awake your app. Let’s imagine that your app has a feature allows users to press a button on your BLE-connected devices to find where their phone is, but if your app is not running or is not able to wake up to handle the BLE signal sent from your devices, this feature would be useless.
In this post, I will show you a technique using iBeacon to deal with this case, which makes your app another chance to wake up despite it is terminated by users. Let’s drive-in!

Read More

Best practice: How to deal with Bluetooth Low Energy in background

Preface

When working with CoreBluetooth, have you ever concerned that how the BLE app on iOS can survive when it is terminated by the system? How can we bring it back to the background? Is there anything like a service on Android that can last forever? You can find the answer to all these questions in this post. Read on!

Read More

Working In Thread Safe on iOS


As you might know, the word “Thread safe” is referred to a computer science concept in the context of multi-thread programs. A code is called “Thread safe” if any shared data is accessed by only one thread at any given time. Notice these shared data are called critical sections in an operating system.
The point is Swift collection types like Array and Dictionary are not thread-safe when declared mutable (With var keyword).
In this post, we will discuss some techniques to make our code thread safe in iOS.

Read More

Big Endian vs Little Endian


In computer science, a bit is the smallest piece of information. It represents a digit of the binary numeral system. A string of 8 bits called a byte. There are two ways to store a string of data in computers: Big Endian and Little Endian. If your tasks are working with data in a piece of bytes, you ought to know how to deal with bytes in these two formats. In this post, I will explain how data is stored in computers, what are the main differences between these two, then provide some useful code to work with bytes in Swift and Objective-C.

Read More

Play Central And Peripheral Roles With CoreBluetooth

Introduction


As I mentioned in the previous post, CoreBluetooth allows us to create applications that can communicate with BLE devices such as heart rate monitors, body sensors, trackers, or hybrid devices.
There are two roles to play in the CoreBluetooth concepts: Central and peripheral.

  • Central: Obtain data from peripherals.
  • Peripheral: Publish data to be accessed by a central. We can make a Bluetooth device plays as a peripheral from either firmware-side or software-side.

In this post, I will show you how to create a peripheral by using our own identifiers. Also using another device, as a central, to connect and explore our services. Let’s get it started.

Read More

Asynchronous Programming in Swift

Promise Kit, one of the best frameworks to deal with asynchronous programming in Swift

In this post, I will use these following third parties to complete the project:

  • Alamofire: A HTTP networking framework in Swift.
  • SwiftyJSON: To process JSON data.
  • SwiftGifOrigin: An UIImage extension to display Gif files.
  • Bolts-Swift: Was designed by Parse and Facebook, I use it to create asynchronous methods.
  • PromiseKit: A framework helps us to simplify asynchronous programming.
  • Giphy’s APIs for searching and downloading gif images.
Read More

Grand Central Dispatch in Swift


Grand Central Dispatch, or GCD for short, is a low-level C APIs for managing concurrent tasks. It helps us improve our app performance by executing a block of code in reasonable threads, like perform computationally expensive tasks in the background. GCD provides several options for running tasks such as synchronously, asynchronously, after a certain delay, etc.
In this post I will explain more details about GCD and how it works, also provide some interesting points when we work with GCD. Let’s start.

Read More