<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Carpé Cocoa &#187; GameKit</title>
	<atom:link href="http://carpe-cocoa.com/tag/gamekit/feed/" rel="self" type="application/rss+xml" />
	<link>http://carpe-cocoa.com</link>
	<description>My journey into iPhone development</description>
	<lastBuildDate>Tue, 24 Nov 2009 21:03:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Detecting when Bluetooth is disabled with GKSession</title>
		<link>http://carpe-cocoa.com/2009-07-29/detecting-when-bluetooth-is-disabled-with-gksession/</link>
		<comments>http://carpe-cocoa.com/2009-07-29/detecting-when-bluetooth-is-disabled-with-gksession/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 21:47:45 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[iPhone Development]]></category>
		<category><![CDATA[GameKit]]></category>

		<guid isPermaLink="false">http://carpe-cocoa.com/?p=304</guid>
		<description><![CDATA[Edit: As noted in the comments, this technique no longer works in iPhone OS 3.1.
If you are writing an iPhone GameKit app without using GKPeerPickerController (as I did in PhotoBeamer), you need to detect when Bluetooth is disabled and ask the user to turn it on in the settings app. Apple doesn&#8217;t provide a way [...]]]></description>
			<content:encoded><![CDATA[<p><em>Edit: As noted in the comments, this technique no longer works in iPhone OS 3.1.</em></p>
<p>If you are writing an iPhone GameKit app without using <code>GKPeerPickerController</code> (as I did in <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=321814330&amp;mt=8">PhotoBeamer</a>), you need to detect when Bluetooth is disabled and ask the user to turn it on in the settings app. Apple doesn&#8217;t provide a way for a developer to enable Bluetooth other than through <code>GKPeerPickerController</code>.</p>
<p>This is pretty easy to do. First, create your <code>GKSession</code> and set it to be available:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">    session <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>GKSession alloc<span style="color: #002200;">&#93;</span> initWithSessionID<span style="color: #002200;">:</span>kMyAppSessionID
                                       displayName<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span>
                                       sessionMode<span style="color: #002200;">:</span>GKSessionModeClient<span style="color: #002200;">&#93;</span>;
    session.delegate <span style="color: #002200;">=</span> self;
    <span style="color: #002200;">&#91;</span>session setDataReceiveHandler<span style="color: #002200;">:</span>self withContext<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    session.available <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;</pre></div></div>

<p>Next, set up your <code lang="objc">- (void)session:(GKSession *)session didFailWithError:(NSError *)error</code> delegate method to detect when Bluetooth is not available and alert the user:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// GKSessionErrorDomain causes link error (rdar://problem/7014349)</span>
<span style="color: #6e371a;">#if 0</span>
<span style="color: #6e371a;">#define kGKSessionErrorDomain GKSessionErrorDomain</span>
<span style="color: #6e371a;">#else</span>
<span style="color: #6e371a;">#define kGKSessionErrorDomain @&quot;com.apple.gamekit.GKSessionErrorDomain&quot;</span>
<span style="color: #6e371a;">#endif</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>session<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>GKSession <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>session didFailWithError<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>error <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>error domain<span style="color: #002200;">&#93;</span> isEqual<span style="color: #002200;">:</span>kGKSessionErrorDomain<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp;
        <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>error code<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> GKSessionCannotEnableError<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Bluetooth disabled, prompt the user to turn it on</span>
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Some other error, get the description from the NSError object</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// destroy the GKSession and clean up</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The <code lang="objc">GKSessionErrorDomain</code> symbol causes a link error for me, so I&#8217;m temporarily using the constant string instead. When Apple fixes the SDK, I&#8217;ll change that <code lang="objc">#if 0</code> to use the official symbolic name.</p>
<p>Note that this error will sometimes occur on a reconnection attempt even when Bluetooth is enabled, presumably because the old session is still partially active.</p>
<p>It would be nice to have a more user-friendly way to turn on Bluetooth without leaving the app and without using the full <code lang="objc">GKPeerPickerController</code> UI. I&#8217;ve filed a bug report (rdar://problem/7061502) asking for this functionality. You should <a title="Apple Bug Reporter" href="https://bugreport.apple.com/" target="_blank">file one too</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://carpe-cocoa.com/2009-07-29/detecting-when-bluetooth-is-disabled-with-gksession/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
