sumit
9/22/2019 - 2:52 PM

BLE ios

Code Tutorials:

Reading Characterstics as a Central

Services can be read from a connected Central

  • Retrieve By Identifier
open class CBCentralManager : CBManager{
  open func retrievePeripherals(withIdentifiers identifiers: [UUID]) -> [CBPeripheral]
}
  • Retreive Connected Device
open class CBCentralManager : CBManager{
  open func retrieveConnectedPeripherals(withServices serviceUUIDs: [CBUUID]) -> [CBPeripheral]
}

Enhanced Reliability using Background Modes

  • Uses Bluetooth LE accessories Tick this option if you want to use bluetooth in background mode as a central

  • Acts as a Bluetooth LE accessory Tick this option if you want to use bluetooth in background mode as a Peripheral

CBCentralManager restoration

Central operations can continue when your app is not running

  • scan for new devices with services
  • connect to an already known service
// Initialiae CBManager with RestoreIdentifier
let CBCentralManagerOptionRestoreIdentifierKey: String
Discussion
/*A string (an instance of NSString) containing a unique identifier (UID) for the central manager that is being instantiated.
The system uses this UID to identify a specific central manager. As a result, the UID must remain the same for subsequent executions of the app in order for the central manager to be successfully restored.

@seealso 
CBCentralManagerRestoredStatePeripheralsKey;
CBCentralManagerRestoredStateScanServicesKey;
CBCentralManagerRestoredStateScanOptionsKey;
*/

optional func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any])

State preservation and Restoration

  • Now it also works even when device reboot

  • Even if thre are Bluetooth system level events that previously would have stopped these things from occuring on your behalf, we'll keep doing them for you.

  • In order to keep the power profile of these features low, we actually do have to do a lot of things in Hardware. So we are limited to number of things we can look for at a time, especially the number of services we can scan for. So the less things you ask for, the better the chances that we can keep your actions running all the time.

  • There's no UI to control what applications are allowed to do what in the background, so we will be stopped if

    • user force quits the app through the task switcher.
    • user disables the Bluetooth manually through Bluetooth settings Then we will stop doing background activities on your behalf

Best Practices

  1. Another thing that we see is that if you've previously used an accessory and you want to reconnect to that accessory you don't have to scan every time. If you know that the device is advertising or you just want to connect as quickly as possible, connect directly to the accessory. If you have the identifier, you can call retrievePeripherals (WithIdentifiers) and directly get a CBPeripheral object that you can connect to. If you were to scan for your accessory and then connect to it it's actually going to take us twice as long to do that operation as if we could have connected the first time we found it.

Different iOS app working

  1. BLE Scan app: this use case describes situation where you want to discover and test any BLE peripheral in the surroundings. Examples of iOS app that suit this use case are: BlueCap and LightBlue.
  • Peripheral devices advertises services
  • Central devices scan for any advertising peripherals and displays it to the user
  • The user to select a peripheral from the list of available one
  • The central connects to the selected peripheral and displays its available services to the user
  • The user choses a service
  • The central asks the peripheral for the characteristics of the selected services and displays them to the user
  • The user choses a characteristic and perform some actions depending on the operations allowed: read and/or write and/or be notified of.
  1. Apps that handle specific BLE hardware follow this type of use case. For example, we have the MI Band official app. This type of use case is more straightforward and does require user intervention only for the first connection.
  • The iOS app scans for peripherals which advertise specific services.
  • The iOS app connects to a specific peripheral. For example: a smart band.
  • The app then collects information using specific characteristics and predetermined services. For example: the number of footsteps.
  • The app disconnects from the peripheral and reconnects to it directly the next time

video tutorials