fbpx

Bluetooth Connectivity in iOS – Part 1

Bluetooth Connectivity in iOS 

Bluetooth is a technology that is used to exchange data packets between two devices. Hence Bluetooth Connectivity in iOS has become one of the important aspect in modern day applications with IOT.

In the last decade we have witnessed some incredible developments have come in the way we use our mobile phones and other technologies specially Bluetooth.

So like other mobile operating systems iOS too comes with an option to connect to the Bluetooth via the phone’s settings menu. It is seen that due to lack of correct information  .

So in this blog we have tried to explain you the different profiles available in the Bluetooth and its connectivity with iOS.

iOS supports and connects to two different type of Bluetooth devices – one that is termed as BLE – Bluetooth Low Energy device and the other is a Bluetooth classic device.

Well, you did not get it right ?????????????? Let’s explain it a bit.

First let’s cover the most common form of Bluetooth Device which is a classic Bluetooth device.

Bluetooth Classic is designed for continuous two-way data transfer with high Application throughput (up to 2.1 Mbps); highly effective, but only for short distances.

So, it’s a perfect solution in the case of streaming audio and video, or mice and other devices that need a continuous, broadband link. There are different Bluetooth profiles which comes under Bluetooth classic :

Serial Port Profile:

If you’re replacing a serial communication interface (like RS-232 or a UART) with Bluetooth, SPP is the profile for you. SPP is great for sending bursts of data between two devices. It’s is one of the more fundamental Bluetooth profiles (Bluetooth’s original purpose was to replace RS-232 cables after all).

Human Interface Device (HID):

HID is the go-to profile for Bluetooth-enabled user-input devices like mice, keyboards, and joysticks. It’s also used for a lot of modern video game controllers, for PS3 controllers.

HSP – HeadSet Profile & HFP – Hands Free Profile

HSP is a Bluetooth profile that enables wireless connection between a phone and a Bluetooth headset. It supports simultaneous two-way audio but not stereo.

HFP is used in the hands-free audio systems built into cars. It implements a few features on top of those in HSP to allow for common phone interactions (accepting/rejecting calls, hanging up, etc.) to occur while the phone remains in your pocket.

Advanced Audio Distribution Profile (A2DP)

Advanced audio distribution profile (A2DP) defines how audio can be transmitted from one Bluetooth device to another.

Where HFP and HSP send audio to and from both devices, A2DP is a one-way street, but the audio quality has the potential to be much higher.

A2DP is well-suited to wireless audio transmissions between an MP3 player and a Bluetooth-enabled stereo.

A/V Remote Control Profile (AVRCP)

The audio/video remote control profile (AVRCP) allows for remote controlling of a Bluetooth device.

It’s usually implemented alongside A2DP to allow the remote speaker to tell the audio-sending device to fast-forward, rewind, etc.

Another mode of communicating with Bluetooth is BLE ( Bluetooth Low Energy):

BLE is a wireless personal area network technology designed and marketed by the Bluetooth Special Interest Group (Bluetooth SIG) aimed at novel applications in the healthcare, fitness, beacons, security, and home entertainment industries.

Compared to Classic Bluetooth, Bluetooth Low Energy is intended to provide considerably reduced power consumption and cost while maintaining a similar communication range.

BLE finds application largely in IoT and also other utility applications. BLE uses GATT profile (General Attribute Profile). General Attribute Profile is built on top of Attribute Protocol (ATT) and establishes common operations and a framework for the data transported and stored by the Attribute Protocol.

Please refer to the below mentioned diagram to understand the Bluetooth stack.

 

Bluetooth Connectivity IOS

After WWDC 2019,  docs on apple developer portal claimed to connect to classic Bluetooth devices using one of its frameworks : Core Bluetooth. However this is pretty misleading, as in practice this is not quite feasible till iOS 13.3. Lets take a look

Apple allows Bluetooth connectivity in iOS using two of its frameworks

Core Bluetooth communicates with Bluetooth Low Energy devices. The official apple documentation claims that using Core Bluetooth you can connect to the BR/EDR devices.

However which is not the case. If you go through the video of WWDC 2019 and look at what their team has to say about it, you would know that apple allows you to scan and connect the BR/EDR “CLASSIC” devices via a BLE service ID.

This means that the BR/EDR device which you would want to connect via Core Bluetooth should be having GATT profiles. For the Bluetooth devices with dual- mode apple uses Cross Transport Delivery Key to connect to the BR/EDR device if connected to it via the GATT profile.

To summarise apple still do not have any apis or any method to connect to a non- GATT BR/EDR (classic) device using Core Bluetooth.

The second way of connecting the classic devices is using External Accessory framework. But a big caveat with this is that External accessory only recognises MFI compliant devices.

What is MFI :-

MFI is a licensing program for developers of hardware and software peripherals that work with Apple’s iPod, iPad and iPhone. The name is a shortened version of the long-form Made for iPod, the original program that ultimately became MFI.

When you are enroll with a MFi you get a protocol string or rather a unique string for the device you enrolled which you can pass in the delegate methods of EAAccessoryDelegate.

The steps and code to connect to the BLE using Core Bluetooth and MFi device using External Accessory framework is available in the second  part. You can refer to the below WWDC 2019 .

Article Written By :- Kumar Gaurav

 

Categories IOS

Customize Navigation Bar In IOS Using SubClass and StoryBoard

Customize Navigation Bar In IOS Using SubClassing and StoryBoard:-

Like most of you, I have been searching many times about Customize Navigation Bar In IOS Using SubClass and StoryBoard only and add some navigation items to it .Most of the  times I used to write  Customization code in App Delegate file which seemed inconvenient and i was not satisfied.

After a lot of research for one of my recent project in IOS .I decided to use subclass for customizing only navigation bar without the using UINavigationController. Here are few things we will do in this example.

  1. Setting the background color of Navigation Bar.
  2. Using background image in Navigation Bar.
  3. Customizing the color of UIBarButtons Items.
  4. Changing the font of Navigation Bar Title.
  5. Changing the font Size of Navigation Bar Title.
  6. Adding multiple UIBarButtons Items.
  7. Adding Actions or Click on UIBarButtons Items.
  8. Remove Gray Border From Navigation Bar.
  9. Setting Shadow Image to Navigation Bar.

1. Create Subclass Of UINavigationBar :-

UINavigationBar

 

2 Project Structure:- 

Once you have created the class you will have the project structure like below

Project Structure

3. Declare a Custom Navigation Function:-

override func awakeFromNib() {
        super.awakeFromNib()
}

we will use this function to call every customization function to customize our NavigationBar .This function will get called , when you set the class of your NavigationBar in StoryBoard .Please select your ViewController NavigationBar and go to identity Inspector and select Custom Class, then set Class name to Your Subclass name like image below.

Customize Navigation Bar In IOS Using SubClass and StoryBoard

 

4.Using Background Image in Navigation Bar :-

lets declare a function name to set the background Image of NavigationBar like below code snippet.

  func setBackgroundImageOfNavBar(imagenName:String){
        //pass the image name and set the image here
        self.setBackgroundImage(UIImage(named: imagenName), for: .default)
        //if you want white backgroung just remove the image
       // self.setBackgroundImage(UIImage(), for: .default)
        //if you want to set backgroung color
       // self.backgroundColor = UIColor.yellow
    }

In this function you can pass image name if you want to put image in Navigation Bar background .In case if you want to set color you can use backgroundColor to set background color of Navigation Bar.

5.Using Shadow Image in Navigation Bar:-

let’s set the shadow image of  Navigation Bar , we need to make another function for this like below

 func setShadowImageOfNavBar(imagenName:String){
        //pass the image name and set the image here
       // self.shadowImage = UIImage(named: imagenName)
        //if you want white backgroung just remove the image
        self.shadowImage = UIImage()
    }

You can pass the image for shadow or put it blank to get Transparent background of  Navigation Bar.

6.Changing the Font of Navigation Bar Title :-

It is One of the most important part .While we work with Navigation Bar we often stuck to make custom font size and custom font for Navigation Bar title we can use NSAttributedStringKey set the title color as well as font size by using the  “titleTextAttributes” properties of the Navigation Bar.

We can specify the font, text color, text shadow color, and text shadow offset for the title, using the following text attribute keys ::-

  • UITextAttributeFont – Key to the font
  • UITextAttributeTextColor – Key to the text color
  • UITextAttributeTextShadowColor – Key to the text shadow color
  • UITextAttributeTextShadowOffset – Key to the offset used for the text shadow

Here is the sample code snippets for altering the font style and font color of the navigation bar title:-

func setTitleAttribute(){
        //add NSAttributedStringKey as you want to customize title Color and font provide your app font family name open below commented code
       //        let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.black,NSAttributedStringKey.font: UIFont(name: "Enter Your App Font Name", size: 18)!]
        
        //set only title color of navigation bar by below code
        let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red]
        self.titleTextAttributes = textAttributes
    }

If you want to make transparent background make this boolean value true as below.

func setBarAsTranslucent(){
        self.isTranslucent = true
    }

lets call all the function from our first function like below

 override func awakeFromNib() {
        super.awakeFromNib()
        self.setBackgroundImageOfNavBar(imagenName: "nav.jpg")
        self.setShadowImageOfNavBar(imagenName: "")
        self.setTitleAttribute()
        self.setBarAsTranslucent()
    }

For background with clear and transparent make sure to set some property of NavigationBar .

self.setBackgroundImage(UIImage(), for: .default)
        //if you want to set backgroung color
        self.backgroundColor = UIColor.clear
        self.shadowImage = UIImage()
        self.isTranslucent = true

We can set transparent image to achieve transparent background

you can set the status bar style by using the UIApplication statusBarStyle method. But first you’ll need to opt out the “View controller-based status bar appearance”. In Your info.plist file the this row and set the value to NO.like below.

Customize Navigation Bar In IOS Using SubClass and StoryBoard

opt out the “View controller-based status bar appearance

opt out the View controller-based status bar appearance

 

7.Adding UIBarButton To Navigation Bar

  Open storyboard file and drag and drop UIBarButton item to Navigation Bar from the right side menu add change the title ,image ,tintcolor of bar button from property inspector like below.

Customize Navigation Bar In IOS Using SubClass and StoryBoard

Summary:-

For your complete reference, you can Download the source code of the demo project from here  Just uncomment any code snippets in the sample project to test out the change.

Like many of you, I’m still exploring all the new changes of IOS 11 SDK. I am by no means an expert on IOS . If you find any errors in the article, please do let me know. If you find any tips and tricks related to navigation bar and status bar, please also share with us by leaving comment below.

 

 

Categories IOS