Iphone How to know if Bluetooth headset connected - iphone

using iphone sdk 3.1.2.
Is there anyway of knowing if a Bluetooth headset is connected to the device? Don't need any info except if its connected or not. This is different from knowing if one was plugged in or not which one can do via a Property Listener of an Audio Session.
Thanks

Call this method to find out the bluetooth headset is connected or not.
First import this framework #import <AVFoundation/AVFoundation.h>
- (BOOL) isBluetoothHeadsetConnected
{
AVAudioSession *session = [AVAudioSession sharedInstance];
AVAudioSessionRouteDescription *routeDescription = [session currentRoute];
NSLog(#"Current Routes : %#", routeDescription);
if (routeDescription)
{
NSArray *outputs = [routeDescription outputs];
if (outputs && [outputs count] > 0)
{
AVAudioSessionPortDescription *portDescription = [outputs objectAtIndex:0];
NSString *portType = [portDescription portType];
NSLog(#"dataSourceName : %#", portType);
if (portType && [portType isEqualToString:#"BluetoothA2DPOutput"])
{
return YES;
}
}
}
return NO;
}

Have you looked at the: kAudioSessionProperty_AudioRoute?
Also see this post:
How can I find out if an external headset is connected to an iPhone?

Related

connecting 2 devices over different WiFi network using GKSession

I am trying to connect 2 devices using GKSession. When the devices are on same WiFi network it is working but when i am trying to connect devices on different WiFi network it is not working.
This is my code for GKSession
chatSession = [[GKSession alloc] initWithSessionID:AppName displayName:name sessionMode:GKSessionModePeer];
[chatSession setDataReceiveHandler:self withContext:nil];
chatSession.delegate = self;
chatSession.available = YES;
-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{
NSLog(#"state=%d", state);
if(state == GKPeerStateDisconnected)
{
// A peer disconnected
chatSession.available = YES;
NSLog(#"Disconnected");
[data removeAllObjects];
chatInput.hidden = YES;
}
else if(state == GKPeerStateConnected)
{
// You can now send messages to the connected peer(s)
NSData *imgData = UIImageJPEGRepresentation(myImage, 0.5);
if(imgData==nil)
NSLog(#"myImage is nil");
NSError *err;
//NSLog(#"pid=%#", pID);
NSLog(#"before sending image");
[chatSession sendData:imgData toPeers:[NSArray arrayWithObject:pID] withDataMode:GKSendDataReliable error:&err];
NSLog(#"after send data");
if(err)
NSLog(#"error:%#", err.description);
}
else if (state == GKPeerStateAvailable)
{
pID = peerID;
[session connectToPeer:peerID withTimeout:60*120];
}
}
-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID{
// We can now decide to deny or accept
bool shouldAccept = YES;
if(shouldAccept)
{
pID = peerID;
[session acceptConnectionFromPeer:peerID error:nil];
}
else
{
[session denyConnectionFromPeer:peerID];
}}
I am not sure if it is possible using GKSession.
According to Apple
A GKSession object provides the ability to discover and connect to nearby iOS devices using Bluetooth or Wi-fi.
Please help me ia m trying to create a chat app using GKSession.
iOS' GameKit most probably uses Bonjour for this, which works by doing broadcasts on a local network. It won't work between two separate Wi-Fi networks.
This means you'll need a central server (at least during connection set-up). Have a look at XMPP, if you haven't done so already.
Good luck!

iOS - How to record a video using a bluetooth headset as audio input?

I'm trying to record a video using DIYCam and setting the audio route for a bluetooth headset as audio input, but seems like the routing isn't working. As I understand, I just need to route the audio input to my bluetooth headset, and I don't need to change anything about DIYCam, am I right? Here is my code:
When the view is loading I create the DIYCam instance:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self.navigationController setNavigationBarHidden:YES];
self.camera = [[DIYCam alloc] initWithFrame:self.view.bounds];
self.camera.delegate = self;
[self.camera setupWithOptions:nil]; // Check DIYAV.h for options
[self.camera setCamMode:DIYAVModeVideo];
[self.view addSubview:self.camera];
[self.view sendSubviewToBack:self.camera];
if( self.bluetoothInput ) {
[self setBluetoothAudioInput];
}
}
This is my bluetooth routing function:
- (void)setBluetoothAudioInput
{
// create and set up the audio session
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setDelegate:self];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
[audioSession setActive:YES error:nil];
// set up for bluetooth microphone input
UInt32 allowBluetoothInput = 1;
OSStatus stat = AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
sizeof (allowBluetoothInput),
&allowBluetoothInput
);
}
And these are the IBActions for starting and stoping:
- (IBAction)startRecording:(id)sender
{
[self.camera startSession];
[self.camera captureVideoStart];
}
- (IBAction)stopRecording:(id)sender
{
[self.camera captureVideoStop];
[self.camera stopSession];
}
I believe you need to have the user manually set what input device to use for audio. I just had a similar issue and used an MPVolumeView with the showsVolumeSlider boolean set to NO. This provides a clickable button to set the desired input from a list of available ones.
Check out:
AudioSession input from bluetooth output to line out or speaker
try this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];

How do you make a call from your app? [duplicate]

I need to call programmatically in my app in a button click.
for that i found code like this.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:1-800-555-1212"]];
Is it work in iphone sdk 3.0 and iphone 2.0 also
Can any pls help
Thank u in advance.
Keep the phone number in a separate string.
NSString *phoneNumber = #"1-800-555-1212"; // dynamically assigned
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];
NSLog(#"Phone calling...");
UIDevice *device = [UIDevice currentDevice];
NSString *cellNameStr = [NSString stringWithFormat:#"%#",self.tableCellNames[indexPath.row]];
if ([[device model] isEqualToString:#"iPhone"] ) {
NSString *phoneNumber = [#"tel://" stringByAppendingString:cellNameStr];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
} else {
UIAlertView *warning =[[UIAlertView alloc] initWithTitle:#"Note" message:#"Your device doesn't support this feature." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warning show];
}
// VKJ
The following code snippet checks if SIM card is present or not as well if the device is capable of making the call such as non-sim ios devices
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"tel://"]]) {
// Check if iOS Device supports phone calls
CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netInfo subscriberCellularProvider];
NSString *mnc = [carrier mobileNetworkCode];
// User will get an alert error when they will try to make a phone call in airplane mode.
if (([mnc length] == 0)) {
// Device cannot place a call at this time. SIM might be removed.
} else {
// iOS Device is capable for making calls
}
} else {
// iOS Device is not capable for making calls
}
if ( ! [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"sms:"]]) {
// iOS Device is not capable to send SMS messages.
}
Don't forget to add the CoreTelephony framework
Credit

Game Center Host

I am currently creating a Game Center game, and am using the peer-to-peer functionality available. However, I would like to select one of the players (2 players) to act as a host, to ensure synchronization.
What is the best way to select a host (even randomly) from the players available and ensure that the other device knows which host was chosen?
This is a nice, simple tutorial with code that demonstrates how to do this. They randomly choose one of the peers to be the host. Hope that Helps!
already posted answer , still here is sample code to do that thing
NSString *uid = [[UIDevice currentDevice] uniqueIdentifier];
CoinTossID = [uid hash];
now in delegate Function
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
NSMutableArray *ReceivedArray = [[NSMutableArray alloc] init];
ReceivedArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
int flag = [[ReceivedArray objectAtIndex:0] intValue];
[ReceivedArray removeObjectAtIndex:0];
int CoinValue = [ReceivedCoinTossID intValue];
if(CoinValue > CoinTossID)
{
isPlayer1 = YES;
}
else
{
isPlayer1 = NO;
}
}

How to make a call programmatically?

I need to call programmatically in my app in a button click.
for that i found code like this.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:1-800-555-1212"]];
Is it work in iphone sdk 3.0 and iphone 2.0 also
Can any pls help
Thank u in advance.
Keep the phone number in a separate string.
NSString *phoneNumber = #"1-800-555-1212"; // dynamically assigned
NSString *phoneURLString = [NSString stringWithFormat:#"tel:%#", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];
NSLog(#"Phone calling...");
UIDevice *device = [UIDevice currentDevice];
NSString *cellNameStr = [NSString stringWithFormat:#"%#",self.tableCellNames[indexPath.row]];
if ([[device model] isEqualToString:#"iPhone"] ) {
NSString *phoneNumber = [#"tel://" stringByAppendingString:cellNameStr];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
} else {
UIAlertView *warning =[[UIAlertView alloc] initWithTitle:#"Note" message:#"Your device doesn't support this feature." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warning show];
}
// VKJ
The following code snippet checks if SIM card is present or not as well if the device is capable of making the call such as non-sim ios devices
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"tel://"]]) {
// Check if iOS Device supports phone calls
CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netInfo subscriberCellularProvider];
NSString *mnc = [carrier mobileNetworkCode];
// User will get an alert error when they will try to make a phone call in airplane mode.
if (([mnc length] == 0)) {
// Device cannot place a call at this time. SIM might be removed.
} else {
// iOS Device is capable for making calls
}
} else {
// iOS Device is not capable for making calls
}
if ( ! [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"sms:"]]) {
// iOS Device is not capable to send SMS messages.
}
Don't forget to add the CoreTelephony framework
Credit