How to scan and connect to Bluetooth A2DP device on iOS programmatically - ios

I have built an Android APP which handles the scan, return nearby devices, and connect steps on both BLE and Bluetooth A2DP, and it works well. Now I’m developing the iOS version which is with exactly the same functionalities. For the BLE part, I can use CoreBluetooth to perform what I need without any problems, yet I don’t know how to implement the steps of “scan -> return nearby discoverable devices -> connect” on iOS for Bluetooth A2DP device. The only solution I’ve found so far is to navigate to Settings page from my iOS APP and perform the connection on it. Is there any way to implement the Bluetooth A2DP connection process inside of my iOS APP programmatically?

in iOS, bluetooth works on central peripheral concept. below is the how it scan for nearby devices.
#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>
#interface MyViewController : UIViewController <CBPeripheralDelegate, CBCentralManagerDelegate>
{
CBCentralManager *mgr;
}
#property (readwrite, nonatomic) CBCentralManager *mgr;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog([NSString stringWithFormat:#"%#",[advertisementData description]]);
}
-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
NSLog(#"This is it!");
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSString *messtoshow;
switch (central.state) {
case CBCentralManagerStateUnknown:
{
messtoshow=[NSString stringWithFormat:#"State unknown, update imminent."];
break;
}
case CBCentralManagerStateResetting:
{
messtoshow=[NSString stringWithFormat:#"The connection with the system service was momentarily lost, update imminent."];
break;
}
case CBCentralManagerStateUnsupported:
{
messtoshow=[NSString stringWithFormat:#"The platform doesn't support Bluetooth Low Energy"];
break;
}
case CBCentralManagerStateUnauthorized:
{
messtoshow=[NSString stringWithFormat:#"The app is not authorized to use Bluetooth Low Energy"];
break;
}
case CBCentralManagerStatePoweredOff:
{
messtoshow=[NSString stringWithFormat:#"Bluetooth is currently powered off."];
break;
}
case CBCentralManagerStatePoweredOn:
{
messtoshow=[NSString stringWithFormat:#"Bluetooth is currently powered on and available to use."];
[mgr scanForPeripheralsWithServices:nil options:nil];
//[mgr retrieveConnectedPeripherals];
//--- it works, I Do get in this area!
break;
}
}
NSLog(messtoshow);
}

Related

Central manager method is not being called ..

i am new to iOS any one please give some suggestion to me.My process to get status about the bluetooth ring scanner. The bluetooth scanner is working fine and my app getting data form the scanner. But now my task is, whenever i open my app ring scanner should connect automatically. and also i want to display the bluetooth status icon in my app.
1) Ring scanner connected already or connected when my app launch - Bluetooth icon (Blue Colour)
2) Ring Scanner not connected - Bluetooth icon (Red colour)
3) Ring Scanner lost connection or sleep due to lack of activity - Bluetooth icon (Grey colour).
i have started working on this concept but centralManager method is not getting called. Here is my code. any one please help me
h.file
#import <CoreBluetooth/CoreBluetooth.h>
#property (nonatomic, strong) NSString *connected;
#property (nonatomic) CBCentralManager *bluetoothManager;
m.file
- (void)viewDidLoad
{
[super viewDidLoad];
_bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self
queue:nil
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *stateString = nil;
switch(_bluetoothManager.state)
{
case CBCentralManagerStateResetting: stateString = #"The connection with the system service was momentarily lost, update imminent."; break;
case CBCentralManagerStateUnsupported: stateString = #"The platform doesn't support Bluetooth Low Energy."; break;
case CBCentralManagerStateUnauthorized: stateString = #"The app is not authorized to use Bluetooth Low Energy."; break;
case CBCentralManagerStatePoweredOff: stateString = #"Bluetooth is currently powered off.";
{
UIAlertView *BluetoothOff = [[UIAlertView alloc] initWithTitle:#"Warning!!" message:#"Turn ON Bluetooth in setting!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[BluetoothOff show];
}
break;
case CBCentralManagerStatePoweredOn: stateString = #"Bluetooth is currently powered on and available to use.";
{
// [self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
[self startScan];
}
break;
default: stateString = #"State unknown, update imminent.";
break;
}
NSLog(#"Bluetooth State: %#",stateString);
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(#"DiscoverPeripheral#");
}
-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
NSLog(#"RetrievePeripherals#");
}
-(void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals
{
NSLog(#"RetrieveConnectedPeripherals#");
}
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(#"Connection Failed#");
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(#"Disconnected#");
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
self.connected = [NSString stringWithFormat:#"Connected: %#", peripheral.state == CBPeripheralStateConnected ? #"YES" : #"NO"];
}
- (void) startScan
{
NSLog(#"Start scanning");
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[self.bluetoothManager scanForPeripheralsWithServices:nil options:options];
}
You just save the UUID of the peripheral in your internal database and try to reconnect it after the application launched using the same UUID.
Save it in
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(#"peripheral.identifier=%#",peripheral.identifier.UUIDString);
}
After connecting with the peripheral you can read any value provided by the scanner.

Detect Siri Remote from iPhone via Bluetooth

Is it possible to detect the Apple TV 4 Siri Remote from an iOS application using CoreBluetooth? I'm able to detect the Apple TV, but I'm not having any luck detecting the Siri Remote. The Siri Remote uses Bluetooth 4.0 so I'm assuming it is detectable. Ideally, I'd like to detect the Siri Remote even if it's already paired with the Apple TV.
Simply being able to detect any signal from the Siri Remote/know it's in the vicinity of the users iPhone is what I'm after.
#import "ViewController.h"
#import CoreBluetooth;
#interface ViewController () <CBPeripheralDelegate, CBCentralManagerDelegate>
#end
#implementation ViewController {
CBCentralManager *btManager;
}
-(void)viewDidLoad {
[super viewDidLoad];
btManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
#pragma mark - CBCentralManagerDelegate Methods
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(#"peripheral name: %#", peripheral.name);
NSLog(#"peripheral services: %#", peripheral.services);
NSLog(#"peripheral identifier: %#", peripheral.identifier);
NSLog(#"peripheral state: %ld", (long)peripheral.state);
NSLog(#"RSSI: %# \n\n", RSSI);
}
-(void)centralManagerDidUpdateState:(CBCentralManager *)central {
NSString *nsLogMessage;
switch (central.state) {
case CBCentralManagerStateUnknown: {
nsLogMessage = [NSString stringWithFormat:#"State unknown, update imminent."];
break;
}
case CBCentralManagerStateResetting: {
nsLogMessage = [NSString stringWithFormat:#"The connection with the system service was momentarily lost, update imminent."];
break;
}
case CBCentralManagerStateUnsupported: {
nsLogMessage = [NSString stringWithFormat:#"The platform doesn't support Bluetooth Low Energy"];
break;
}
case CBCentralManagerStateUnauthorized: {
nsLogMessage = [NSString stringWithFormat:#"The app is not authorized to use Bluetooth Low Energy"];
break;
}
case CBCentralManagerStatePoweredOff: {
nsLogMessage = [NSString stringWithFormat:#"Bluetooth is currently powered off."];
break;
}
case CBCentralManagerStatePoweredOn: {
nsLogMessage = [NSString stringWithFormat:#"Bluetooth is currently powered on and available to use."];
NSDictionary *scanningOptions = #{CBCentralManagerScanOptionAllowDuplicatesKey: #YES};
[btManager scanForPeripheralsWithServices:nil options:scanningOptions];
break;
}
}
NSLog(#"%#", nsLogMessage);
}

Getting list of bluetoooth devices ios

I am developing a module in which I need to get all bleutooth devices and connect the selected device. I am using this
blueManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *messtoshow;
switch (central.state) {
case CBCentralManagerStateUnknown:
{
messtoshow=[NSString stringWithFormat:#"State unknown, update imminent."];
break;
}
case CBCentralManagerStateResetting:
{
messtoshow=[NSString stringWithFormat:#"The connection with the system service was momentarily lost, update imminent."];
break;
}
case CBCentralManagerStateUnsupported:
{
messtoshow=[NSString stringWithFormat:#"The platform doesn't support Bluetooth Low Energy"];
break;
}
case CBCentralManagerStateUnauthorized:
{
messtoshow=[NSString stringWithFormat:#"The app is not authorized to use Bluetooth Low Energy"];
break;
}
case CBCentralManagerStatePoweredOff:
{
messtoshow=[NSString stringWithFormat:#"Bluetooth is currently powered off."];
break;
}
case CBCentralManagerStatePoweredOn:
{
messtoshow=[NSString stringWithFormat:#"Bluetooth is currently powered on and available to use."];
[self.blueManager scanForPeripheralsWithServices:nil options:nil];
CBUUID *deviceInfoUUID = [CBUUID UUIDWithString:#"180A"];
NSLog(#"connected devices ios 8 code%# ",[blueManager retrieveConnectedPeripheralsWithServices:#[deviceInfoUUID]]);
}
and my bluetooth is on and connected to two devices which I can see from setting but I am not getting any device. I have checked in
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {}
method but it is not calling. And I know that two BLE devices is already connected to my device

didDiscoverPeripheral not been called after advertising another iOS device

basically i have two apps, one for scanning for bluetooth devices and the other for advertising, and what i am trying to do is to get a list of all iOS devices, within my proximity on the app which scans for the iOS device .
This is my code so far:
Scanning.m:
// Scan for all available CoreBluetooth LE devices
NSDictionary *scanOptions = #{CBCentralManagerScanOptionAllowDuplicatesKey:#(YES)};
NSArray *services = #[[CBUUID UUIDWithString:#"1CC024D6-E413-4B56-993C-831CAF033366"]];
[self.centralManager scanForPeripheralsWithServices:services options:scanOptions];
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(#"RSSI: %d", [RSSI intValue]);
}
// method called whenever the device state changes.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
// Determine the state of the peripheral
if ([central state] == CBCentralManagerStatePoweredOff) {
NSLog(#"CoreBluetooth BLE hardware is powered off");
}
else if ([central state] == CBCentralManagerStatePoweredOn) {
NSLog(#"CoreBluetooth BLE hardware is powered on and ready");
}
else if ([central state] == CBCentralManagerStateUnauthorized) {
NSLog(#"CoreBluetooth BLE state is unauthorized");
}
else if ([central state] == CBCentralManagerStateUnknown) {
NSLog(#"CoreBluetooth BLE state is unknown");
}
else if ([central state] == CBCentralManagerStateUnsupported) {
NSLog(#"CoreBluetooth BLE hardware is unsupported on this platform");
}
}
Advertising.m:
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
[self listenForRelays];
/** Required protocol method. A full app should take care of all the possible states,
* but we're just waiting for to know when the CBPeripheralManager is ready
*/
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
if (peripheral.state == CBPeripheralManagerStatePoweredOn)
{
// We're in CBPeripheralManagerStatePoweredOn state...
NSLog(#"self.peripheralManager powered on.");
// ... so build our service.
}
}
-(void) listenForRelays {
if(self.peripheralManager.state == CBPeripheralManagerStatePoweredOn)
{
NSDictionary *advertisingData = #{CBAdvertisementDataLocalNameKey:#"my-peripheral",
CBAdvertisementDataServiceUUIDsKey:#[[CBUUID UUIDWithString:#"1CC024D6-E413-4B56-993C-831CAF033322"]]};
// Start advertising over BLE
[self.peripheralManager startAdvertising:advertisingData];
}
}
When i run the different apps on different iphones, nothing happens !
This doesn't crash but didDiscoverPeripheral never gets called :( please help me out ! and yes i have gone through the documentation but still no good :((
cheers
You can't initiate your scan until after you are in the powered-on state - use code in your scanning app similar to that which is in your advertising app.
In iOS 7 starting the scan before you were powered on issued a warning on the console, but it still worked. In iOS 8 it doesn't work.

iPhone is not discoverable over bluetooth low energy

I am using CoreBluetooth Framework. I created the demo app to discover the bluetooth low energy devices. As iPhone 4s and above devices supports Bluetooth Low Energy, Am not able to discover another iOS devices over Bluetooth Low Energy from my demo app.
My iPhone has iOS7 running.
Here is my code. I am trying to discover another iPhone from my iOS app. I created Central Demo App. Do I need to create another demo app for peripheral and deploy it on another iPhone, which i wand to discover?.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
// You should test all scenarios
if (central.state == CBCentralManagerStateUnsupported) {
return;
}
if (central.state == CBCentralManagerStatePoweredOn) {
// Scan for devices
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
NSLog(#"Scanning started");
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(#"Discovered %# at %#", peripheral.name, RSSI);
}

Resources