<?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>Silverball Software</title>
	<atom:link href="http://silverballsoftware.com/feed" rel="self" type="application/rss+xml" />
	<link>http://silverballsoftware.com</link>
	<description></description>
	<lastBuildDate>Mon, 27 Feb 2012 20:10:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Androcade Handheld</title>
		<link>http://silverballsoftware.com/androcade-handheld</link>
		<comments>http://silverballsoftware.com/androcade-handheld#comments</comments>
		<pubDate>Fri, 15 Jul 2011 18:55:29 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Androcade]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://silverballsoftware.com/?p=259</guid>
		<description><![CDATA[After making the original Androcade with real arcade controls I felt that I needed a smaller device that was a little more portable. I decided to use a joystick/button shield for prototyping this device. Taking the same configuration I had in the original Androcade I added support to the arduino program to allow for an [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://silverballsoftware.com/wp-content/uploads/2011/07/androcadehandheld.png"><img class="size-thumbnail wp-image-262 alignleft" title="androcadehandheld" src="http://silverballsoftware.com/wp-content/uploads/2011/07/androcadehandheld-150x150.png" alt="" width="150" height="150" /></a>After making the original <a title="Androcade Android Arcade Controller (PART 1)" href="http://silverballsoftware.com/androcade-android-arcade-controller-part-1">Androcade</a> with real arcade controls I felt that I needed a smaller device that was a little more portable. I decided to use a joystick/button shield for prototyping this device.</p>
<p>Taking the same configuration I had in the original Androcade I added support to the arduino program to allow for an analog joystick to control the up/down/left/right. This was pretty straight forward, instead of triggering a serial send via a button I just looked to the value of the analog input and triggered based on thresholds.</p>
<p>The device is a little large at this point because of having to stack a few shields on top of each other but, it works great! The next plan would be to shrink down the prototype into something that fits into your hands. I&#8217;ve had a couple ideas &#8211; like taking an existing controller and replacing the insides with an Arduino mini but that&#8217;s left for another time.</p>
<p><iframe width="480" height="390" src="http://www.youtube.com/embed/Z2X_SICybRQ?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p><a href="http://youtu.be/Z2X_SICybRQ">Androcade Handheld</a></p>
<p><span id="more-259"></span>Here are some of the details of the build:</p>
<p>Hardware:</p>
<ul>
<li>Arduino UNO</li>
<li>Bluetooth serial shield &#8211; (I used the following from <a href="http://iteadstudio.com/store/index.php?main_page=product_info&amp;cPath=18&amp;products_id=307">iteadstudio</a>)</li>
<li>Joystick shield &#8211; ( here is the one I used from <a href="http://iteadstudio.com/store/index.php?main_page=product_info&amp;cPath=18&amp;products_id=307">iteadstudio</a>)</li>
</ul>
<p>The software on the Arduino side is very simple. I listen for a button push or the analog threshold to be reached and send out a serial command. The serial data is then sent through the bluetooth device out to the Android software.</p>
<p>On the Android side I have created software the reads the serial data and sends out keyboard characters to the system.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://silverballsoftware.com/androcade-handheld/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Androcade Android Arcade Controller (PART 1)</title>
		<link>http://silverballsoftware.com/androcade-android-arcade-controller-part-1</link>
		<comments>http://silverballsoftware.com/androcade-android-arcade-controller-part-1#comments</comments>
		<pubDate>Wed, 29 Jun 2011 19:00:47 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Androcade]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://silverballsoftware.com/?p=212</guid>
		<description><![CDATA[I vividly remember the days of putting my hard earned quarters into the latest video game at the the local arcade (Machine Shed). Disparately tying to get my initials on the high score board, hearing that familiar knock when someone got that extra game on the pinball machine&#8230; Well it&#8217;s not the 1980&#8242;s anymore and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://silverballsoftware.com/wp-content/uploads/2011/06/logo.png"><img class="aligncenter size-full wp-image-214" title="logo" src="http://silverballsoftware.com/wp-content/uploads/2011/06/logo.png" alt="" width="500" height="181" /></a></p>
<p>I vividly remember the days of putting my hard earned quarters into the latest video game at the the local arcade (Machine Shed). Disparately tying to get my initials on the high score board, hearing that familiar knock when someone got that extra game on the pinball machine&#8230;</p>
<p>Well it&#8217;s not the 1980&#8242;s anymore and those great games of the past have been replaced by XBox live and mobile handsets.</p>
<p>I&#8217;m not one to shy away from the latest in new technology, in fact, I welcome every new piece of techno-wizardry that comes my way. Although, I do miss the look of that retro-joystick and the familiar feel of the plastic button under my fingers. So out of my need to recapture some of my youth (maybe a bit of a stretch) and my love of new technology I set out to create a simple retro gaming controller for the Android platform.</p>
<p>Here&#8217;s a video preview of the final project:</p>
<p><iframe width="425" height="349" src="http://www.youtube.com/embed/4nhah_YujOM?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p><a href="http://www.youtube.com/watch?v=4nhah_YujOM">Androcade Preview</a></p>
<p><span id="more-212"></span></p>
<p><strong>Project Investigation</strong></p>
<p>I wanted the project to meet a certain set of criteria:</p>
<ol>
<li>The controller had to work 	seamlessly with almost every Android device. This was the biggest 	hurdle as there is so many different devices to have to support&#8230;</li>
<li>It  had to be simple to use. 	Sometimes easier said than done.</li>
<li>It had to support a joystick and 	at least 3 buttons.</li>
</ol>
<p>Seems simple enough, right?</p>
<p>I started to investigate different ways to build my device. I began my journey down the path of using the Android ADK. This is a great development environment that allows you to use your Android device to interact with physical hardware. It is based on the Arduino which I was already familiar with! After further investigation I found that even though this platform was great it did not meet my needs, for a couple of reasons.</p>
<ol>
<li>It needs a physical connection 	with the device. I wanted to avoid having to plug in the Android 	device to my controller.</li>
<li>It needs to be supported by 	whatever program is running on the Android device. In other words it 	was not a simple task to send commands to other Android programs 	using this platform.</li>
</ol>
<p>So I scrapped the whole ADK idea, but, I did like the idea of using the Arduino for the input device. I already knew how to program it, it was simple to use and provided a great platform to prototype with. So I decided that this was the route to go for interacting with the controller.</p>
<p>My next task was to find a way for the Arduino to communicate with the Android device. After eliminating USB the next logical solution was Bluetooth.</p>
<p>I set out to find a inexpensive shield for the Arduino that would allow bluetooth communication to my Android device. Combing the internet I found several bluetooth shields for the Arduino but, they all cost around $40-$60 and being thrifty (OK – I&#8217;ll admit it&#8230; I&#8217;m cheap!) I wanted a less expensive solution.</p>
<p>After more searching I found what I think is the perfect solution! I found a $20 bluetooth shield that acts as a serial communication device. The product can be found here:</p>
<p><a href="http://iteadstudio.com/store/index.php?main_page=product_info&amp;cPath=18∏ucts_id=307">http://iteadstudio.com/store/index.php?main_page=product_info&amp;cPath=18&amp;products_id=307</a> ﻿</p>
<p>This device will work perfectly for my needs! I would simply send different characters over the serial port from the Arduino to the Android device and interrupt those commands on the Android to control games.</p>
<p>The next thing I had to figure out was how I was going to accept those commands from the bluetooth shield and send them to the games on the Android.</p>
<p>After a little searching and reading I settled upon writing a IME (Input Method Editor) for the Android that would take in the commands over bluetooth and send them out to the system as keyboard commands. This will work perfectly as a lot of the games that I wanted to play supported using the keyboard as an input device!</p>
<h2><strong><a title="Fun With Motion Detection Software Part 2" href="http://silverballsoftware.com/fun-with-motion-detection-software-part-2">Read Part 2</a> of the ANDROCADE build</strong></h2>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://silverballsoftware.com/androcade-android-arcade-controller-part-1/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Androcade Android Arcade Controller (PART 2)</title>
		<link>http://silverballsoftware.com/androcade-android-arcade-controller-part-2</link>
		<comments>http://silverballsoftware.com/androcade-android-arcade-controller-part-2#comments</comments>
		<pubDate>Wed, 29 Jun 2011 18:59:38 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Androcade]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://silverballsoftware.com/?p=219</guid>
		<description><![CDATA[In part 1 we started to introduce the ANDROCADE and I gave a little background on what I was looking for. In Part 2 I will talk a little about the software. Writing the Software This was the most involved piece of the entire build. I had to write essentially two different programs, one for [...]]]></description>
			<content:encoded><![CDATA[<p>In <a title="Fun With Motion Detection Software Part 1" href="http://silverballsoftware.com/fun-with-motion-detection-software-part-1">part 1</a> we started to introduce the ANDROCADE and I gave a little background on what I was looking for. In Part 2 I will talk a little about the software.</p>
<p><a href="http://silverballsoftware.com/wp-content/uploads/2011/06/side.jpg"><img class="alignleft size-medium wp-image-213" title="side" src="http://silverballsoftware.com/wp-content/uploads/2011/06/side-300x255.jpg" alt="" width="300" height="255" /></a></p>
<p><strong>Writing the Software</strong></p>
<p>This was the most involved piece of the entire build. I had to write essentially two different programs, one for the Arduino to interact with the buttons and one for the Android that would receive the bluetooth commands and send them out as keyboard commands.</p>
<p>I hate to disappoint anyone reading this but, I&#8217;m not going to get into great detail about the code and it is not publicly available – yet&#8230; I still need to clean it up and test some more before I decide what to do with it. I will however give an overview of what I did.</p>
<p>Interfacing with the Arduino</p>
<p>This is actually the simplest part. The controls are just buttons that open and close a loop to a digital pin on the Arduino. The Arduino senses the change and sends out a serial command based on which pin was triggered. Each of the buttons was connected to a digital pin via a pull up resistor. When the button is pushed the pin goes low and the software sends out a serial command based on the which pin was triggered. Here is a simple example to show what I&#8217;m talking about:</p>
<p><span id="more-219"></span></p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/***********<br />
Simple pin detection<br />
***********/</span><br />
<br />
<span style="color: #666666; font-style: italic;">//define the pins</span><br />
<span style="color: #000066; font-weight: bold;">int</span> BUTTON_A <span style="color: #339933;">=</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">int</span> BUTTON_B <span style="color: #339933;">=</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">//define the characters the pin will send over</span><br />
<br />
<span style="color: #666666; font-style: italic;">//the serial port</span><br />
<span style="color: #000066; font-weight: bold;">char</span> BTN_A_CHAR <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">char</span> BTN_B_CHAR <span style="color: #339933;">=</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//initial set up</span><br />
<span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">//enable serial communication</span><br />
Serial.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">//set the pin modes</span><br />
pinMode<span style="color: #009900;">&#40;</span>BUTTON_A, INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
pinMode<span style="color: #009900;">&#40;</span>BUTTON_B, INPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Here is the loop to listen for button pushes</span><br />
<span style="color: #000066; font-weight: bold;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">//read the pin if it is low then send the command</span><br />
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>digitalRead<span style="color: #009900;">&#40;</span>BUTTON_A <span style="color: #339933;">==</span> LOW<span style="color: #009900;">&#41;</span><br />
Serial.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>BTN_A_CHAR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>digitalRead<span style="color: #009900;">&#40;</span>BUTTON_B <span style="color: #339933;">==</span> LOW<span style="color: #009900;">&#41;</span><br />
Serial.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>BTN_B_CHAR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
delay<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>That&#8217;s essentially what I did for the Arduino code. The nice thing about this is that the bluetooth shield takes care of the hard part of sending the serial command out to the Android.</p>
<p>Android Code</p>
<p>The code for the Android side of things is a little more complicated. The program consists of a few components put together:</p>
<ol>
<li>Input method service</li>
<li>Bluetooth service</li>
<li>Preference activity</li>
</ol>
<p>The preference activity is where the user interacts with the program. This allows you to enable the bluetooth, find the and select the bluetooth device, active the input method, and configure the key commands.</p>
<p><a href="http://silverballsoftware.com/wp-content/uploads/2011/06/screen_shot.png"><img class="aligncenter size-full wp-image-254" title="screen_shot" src="http://silverballsoftware.com/wp-content/uploads/2011/06/screen_shot.png" alt="" width="240" height="410" /></a></p>
<p>The real work comes in the input method service and the bluetooth service. These are the programs that talk with the bluetooth shield and then send the commands out to the system as keyboard commands.</p>
<p>The bluetooth service is a background program that handles all the bluetooth communication. It will essentially run separate threads to threads to connect and communicate with the bluetooth device. There is a handler that is passed to the bluetooth service which then sends data back to the input method service letting it know what commands to send to the system.</p>
<p>There is a great bluetooth example that Google has on it&#8217;s developer site <a href="http://developer.android.com/resources/samples/BluetoothChat/index.html">http://developer.android.com/resources/samples/BluetoothChat/index.html</a></p>
<p>This is a bluetooth chat program and it has all the necessary code for you to create your own bluetooth programs.</p>
<p>The most important service in the Android code is the input method service. This is the program that sends out the keyboard commands based on th input from the bluetooth device. An input method editor or IME is basically a keyboard on the Android platform. To write one you extend the InputMethodService and override some supplied methods.</p>
<p>The nice thing about this for me was that I did not have to create any screen elements for my “keyboard”. This type of service has methods for displaying a view which is your keyboard but, since I did not want to display a user interface all I had to do was process the commands from the bluetooth service and send out key commands.</p>
<p>To find out more about creating an IME here is the developer page from Google: <a href="http://developer.android.com/resources/articles/creating-input-method.html">http://developer.android.com/resources/articles/creating-input-method.html</a></p>
<h3><a href="http://silverballsoftware.com/androcade-andr…troller-part-3">Read Part 3</a> of the ANDROCADE Build</h3>
]]></content:encoded>
			<wfw:commentRss>http://silverballsoftware.com/androcade-android-arcade-controller-part-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Androcade Android Arcade Controller (PART 3)</title>
		<link>http://silverballsoftware.com/androcade-android-arcade-controller-part-3</link>
		<comments>http://silverballsoftware.com/androcade-android-arcade-controller-part-3#comments</comments>
		<pubDate>Wed, 29 Jun 2011 18:57:18 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Androcade]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://silverballsoftware.com/?p=225</guid>
		<description><![CDATA[In parts 1 and 2 I talked about the concept and the software now it&#8217;s time to move on to the actual controller! Controller Design The next phase was to design something to house the controls. I first gravitated to building a mini-arcade cabinet. These are really cool! They are just scaled down versions of [...]]]></description>
			<content:encoded><![CDATA[<p>In <a title="Fun With Motion Detection Software Part 1" href="http://silverballsoftware.com/fun-with-motion-detection-software-part-1">parts 1</a> and <a title="Fun With Motion Detection Software Part 2" href="http://silverballsoftware.com/fun-with-motion-detection-software-part-2">2</a> I talked about the concept and the software now it&#8217;s time to move on to the actual controller!<a href="http://silverballsoftware.com/wp-content/uploads/2011/06/top_tablet.jpg"><img class="alignleft size-medium wp-image-226" title="top_tablet" src="http://silverballsoftware.com/wp-content/uploads/2011/06/top_tablet-196x300.jpg" alt="" width="196" height="300" /></a></p>
<p><strong>Controller Design</strong></p>
<p>The next phase was to design something to house the controls. I first gravitated to building a mini-arcade cabinet. These are really cool! They are just scaled down versions of the real arcade cabinets. After toying with a few designs I wasn&#8217;t happy with the way they were turning out.</p>
<p>What I didn&#8217;t like about the arcade design was the fact that I wanted to use any type of android device from tablets to phones. The arcade would have to be rather large to accommodate tablets in both landscape and portrait mode but, then it would be overbearing for a phone sitting in the large cabinet.</p>
<p>I also wanted to convey the idea that this is an Android specific controller – I&#8217;m a big Android fan and since this was designed specifically for that platform it had to scream Android!</p>
<p>I sat down and tried to tap my artistic side, which is very limited by the way, and come up with a unique controller design. After a few failed attempts I hit on the idea of using the Android mascot Andy as the base for my controller. I sketched out a few designs on paper to see if the idea was viable and from what I could see on paper – I had my design!</p>
<p><span id="more-225"></span></p>
<p>Next was out to the garage to try and put together the first prototype. I broke out the trusty power tools and started making saw dust fly. In the midst of all the measuring, cutting, drilling, sanding and painting I realized that I did not take any pictures of the build process&#8230; sorry.</p>
<p>After all the saw dust had cleared and the paint had dried I was left with one last task before I could call it complete – it needed a name. Reaching into the depths of my creativity (I didn&#8217;t have far to reach&#8230;) I dubbed the new controller ANDROCADE!!</p>
<p>Here are some final build pictures:</p>
<p><a href="http://silverballsoftware.com/wp-content/uploads/2011/06/top.jpg"></a><a href="http://silverballsoftware.com/wp-content/uploads/2011/06/top_tablet2.jpg"><img title="top_tablet2" src="http://silverballsoftware.com/wp-content/uploads/2011/06/top_tablet2.jpg" alt="" width="300" height="521" /></a><img title="top" src="http://silverballsoftware.com/wp-content/uploads/2011/06/top.jpg" alt="" width="300" height="452" /><a href="http://silverballsoftware.com/wp-content/uploads/2011/06/side1.jpg"><img class="alignnone size-full wp-image-227" title="side" src="http://silverballsoftware.com/wp-content/uploads/2011/06/side1.jpg" alt="" width="440" height="375" /></a><a href="http://silverballsoftware.com/wp-content/uploads/2011/06/top_tablet.jpg"><img class="alignnone size-full wp-image-226" title="top_tablet" src="http://silverballsoftware.com/wp-content/uploads/2011/06/top_tablet.jpg" alt="" width="300" height="458" /></a><a href="http://silverballsoftware.com/wp-content/uploads/2011/06/side2.jpg"><img class="alignnone size-full wp-image-228" title="side2" src="http://silverballsoftware.com/wp-content/uploads/2011/06/side2.jpg" alt="" width="458" height="350" /></a></p>
<p>&nbsp;</p>
<p>Testing the ANDROCADE</p>
<p>After final assembly I had to get the answers to a few questions that need to be answered after every creation. The first one being does it work? Yes, it does! I was able to turn it on and start up the configuration and after a few minutes I was playing games! The down side is that a lot of games that are on mobile devices do not support keyboard input – damn those silly touch screens&#8230;</p>
<p>Needless to say I was able to find games that do support keyboard input and the controller has worked flawlessly!</p>
<p>The next thing I wanted to do was give the controller a workout. Enter my teenage boys. After giving them a 30 second run down on how it works they put it through it paces. I think that it was a success because the only thing that stopped them from playing was a dead battery on my tablet!</p>
<p><strong>Conclusion</strong></p>
<p>You thought that we&#8217;d never get here&#8230;</p>
<p>It&#8217;s been really fun building this project – it&#8217;s always nice to see something go from an idea to reality. I will continue to improve upon the ANDROCADE device as there are always ways to improve a project!</p>
<p>I will try to update my site with more information as I make changes or improvements.</p>
<p>I&#8217;m also open to answer questions about the build as time permits leave a comment below or drop me an email at androcade at gmail dot com. I&#8217;ll do my best to get back to you!</p>
<p>Thanks for visiting!!</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://silverballsoftware.com/androcade-android-arcade-controller-part-3/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Android Monitor WiFi State With BroadcastReceiver</title>
		<link>http://silverballsoftware.com/android-monitor-wifi-state-with-broadcastreceiver</link>
		<comments>http://silverballsoftware.com/android-monitor-wifi-state-with-broadcastreceiver#comments</comments>
		<pubDate>Thu, 05 May 2011 13:22:42 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://silverballsoftware.com/?p=190</guid>
		<description><![CDATA[In a recent project I had to be able to monitor the state of the WiFi connection. I needed to be able to notify the user about what state the connection was in in real time. There were three states that I was interested in notifying the users of my app about: Is WiFi available on the [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project I had to be able to monitor the state of the WiFi connection. I needed to be able to notify the user about what state the connection was in in real time.</p>
<p>There were three states that I was interested in notifying the users of my app about:</p>
<ol>
<li>Is WiFi available on the device</li>
<li>Is the WiFi enabled</li>
<li>Is the WiFi connected to an access point</li>
</ol>
<p>One of the features in Android is the concept of listeners. These are classes that allow your app to receive and process events based on the state of something on the device. For example there are listeners that allow you to monitor GPS updates or accelerometer changes. So the off I went to search for a listener for WiFi changes&#8230; well to my surprise there is no listener for WiFi state.</p>
<p><span id="more-190"></span></p>
<p>Not to be discouraged I set off in search of an alternative and that&#8217;s where the <a href="http://developer.android.com/reference/android/content/BroadcastReceiver.html">BroadcastReceiver </a>comes into play. Android has a concept of sending off intent actions which you can register to receive. This is perfect for our situation as we can register for the broadcasts related to the WiFi state and within our receiver we can update our application.</p>
<p>Here is the code to create a receiver class and register it to receive WiFi state changes:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> ConnectionChangeReceiver <span style="color: #000000; font-weight: bold;">extends</span> BroadcastReceiver <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span> <span style="color: #003399;">Context</span> context, Intent intent <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag, <span style="color: #0000ff;">&quot;Inside Broadcast Reciever&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
CheckWifiStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> RegisterWifiWatcher<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>wifiWatcher <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
wifiWatcher <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ConnectionChangeReceiver<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
IntentFilter intentFilter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> IntentFilter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
intentFilter.<span style="color: #006633;">addAction</span><span style="color: #009900;">&#40;</span>WifiManager.<span style="color: #006633;">NETWORK_STATE_CHANGED_ACTION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
intentFilter.<span style="color: #006633;">addAction</span><span style="color: #009900;">&#40;</span>WifiManager.<span style="color: #006633;">SUPPLICANT_CONNECTION_CHANGE_ACTION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
intentFilter.<span style="color: #006633;">addAction</span><span style="color: #009900;">&#40;</span>WifiManager.<span style="color: #006633;">WIFI_STATE_CHANGED_ACTION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
intentFilter.<span style="color: #006633;">addAction</span><span style="color: #009900;">&#40;</span>WifiManager.<span style="color: #006633;">SUPPLICANT_STATE_CHANGED_ACTION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
registerReceiver<span style="color: #009900;">&#40;</span>wifiWatcher, intentFilter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>One thing with BroadcastReceivers you need to be aware of is that they should be registered in the onResume() method and unregistered in the onPause() method. According to the documentation your application will not receive the broadcast if it is paused so unregistering the receiver helps with system overhead.</p>
<p>Another thing that is specific to broadcast receivers it that you need to know is that a receiver is only valid for the duration of the call to its onReceive() method. This means that you should not do any asynchronous work from within that method. Instead if you need to you can launch a service or send notifications through the notification manager.</p>
<p>NOTE: The broadcast intents that we receive for the different WiFi states have extras along with them that you can access to determine the different states of the WiFi connection. I have chosen to use a different method of testing states.</p>
<p>With that being said if you remember we wanted to check three states.</p>
<p>The first one is only needed to be checked on the start of our applicaiton. If the WiFi is not available on this device the I doubt it will become available at a later time. We simply create a WifiManager object and check if it is null &#8211; if it is then the device has no WiFi.</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">WifiManager wifiMgr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>WifiManager<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getSystemService</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">WIFI_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>wifiMgr <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getApplicationContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;No WiFi available on device.&quot;</span>,Toast.<span style="color: #006633;">LENGTH_LONG</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>The second one is if the WiFi is enabled. Here we use the same WifiManager object and if it is not null we check to see if the WiFi is enabled.</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>wifiMgr.<span style="color: #006633;">isEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getApplicationContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;WiFi enabled.&quot;</span>,Toast.<span style="color: #006633;">LENGTH_LONG</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>The last piece is to check if we have connectivity to an actual wireless access point. Remember that even though the wireless is enabled it may not be connected and we will not be able to send data.</p>
<p>This is a little more involved as we have to iterate through an array to find the WiFi connection then we can determine it&#8217;s state.</p>
<p>First we get a ConnecticityManager and with that we retrieve a NetworkInfo array. We then iterate this array looking for the NetworkInfo object for the WiFI after that we check if that object is connected, if so, we are ready to send data!</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ConnectivityManager conMgr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ConnectivityManager<span style="color: #009900;">&#41;</span>getSystemService<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #006633;">CONNECTIVITY_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
NetworkInfo<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> netInf <span style="color: #339933;">=</span> conMgr.<span style="color: #006633;">getAllNetworkInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>NetworkInfo inf <span style="color: #339933;">:</span> netInf<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>inf.<span style="color: #006633;">getTypeName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;WIFI&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>inf.<span style="color: #006633;">isConnected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getApplicationContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;WiFi is connected.&quot;</span>,Toast.<span style="color: #006633;">LENGTH_LONG</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getApplicationContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;WiFi NOT connected.&quot;</span>,Toast.<span style="color: #006633;">LENGTH_LONG</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>There you have it. With this you should be able to detect the state of the WiFi!</p>
]]></content:encoded>
			<wfw:commentRss>http://silverballsoftware.com/android-monitor-wifi-state-with-broadcastreceiver/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get Your IP Address Android Code</title>
		<link>http://silverballsoftware.com/get-your-ip-address-android-code</link>
		<comments>http://silverballsoftware.com/get-your-ip-address-android-code#comments</comments>
		<pubDate>Mon, 25 Apr 2011 09:30:33 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://silverballsoftware.com/?p=156</guid>
		<description><![CDATA[I recently had a need to know the IP address of my phone when it was attached to my local network. I thought that this would be easy &#8211; just use the WifiManager &#8211; right? Get the WifiInfo then call getIPAddress() and that should give me what I needed. Well I was wrong&#8230; The documentation [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had a need to know the IP address of my phone when it was attached to my local network. I thought that this would be easy &#8211; just use the WifiManager &#8211; right? Get the WifiInfo then call getIPAddress() and that should give me what I needed. Well I was wrong&#8230;</p>
<p><span id="more-156"></span></p>
<p>The documentation in the developers area does not show any information for the method <a href="http://developer.android.com/reference/android/net/wifi/WifiInfo.html#getIpAddress()">getIPAddress()</a> all it states is that it returns an int? I was not sure that this would give me what I needed &#8211; maybe I would have to parse the number to return a logical display of the IP.</p>
<p>Well&#8230; after a few hours of trying different ways to display the number and looking at different ways that others have tried, I could not come up with a way to display the number returned by this method.</p>
<p>Back to searching for another way to do this. I abandon looking through the Android docs for a way and moved on to just look at how others have done this through Java calls &#8211; and wouldn&#8217;t you know I found a way.</p>
<p>By simply iterating over the network interfaces I was  able to get the IP that I was looking for. I can&#8217;t take full credit for this code as others have done this before me. One thing I need to remeber is that if there is not a way directly in the Android implementation &#8211; there might just be a simple Java method to solve my issue!</p>
<p>Here is the code that I used to get my local IP:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> GetLocalIpAddress<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Enumeration<span style="color: #339933;">&lt;</span>NetworkInterface<span style="color: #339933;">&gt;</span> en <span style="color: #339933;">=</span> NetworkInterface.<span style="color: #006633;">getNetworkInterfaces</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> en.<span style="color: #006633;">hasMoreElements</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NetworkInterface intf <span style="color: #339933;">=</span> en.<span style="color: #006633;">nextElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Enumeration<span style="color: #339933;">&lt;</span>InetAddress<span style="color: #339933;">&gt;</span> enumIpAddr <span style="color: #339933;">=</span> intf.<span style="color: #006633;">getInetAddresses</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> enumIpAddr.<span style="color: #006633;">hasMoreElements</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">InetAddress</span> inetAddress <span style="color: #339933;">=</span> enumIpAddr.<span style="color: #006633;">nextElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>inetAddress.<span style="color: #006633;">isLoopbackAddress</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> inetAddress.<span style="color: #006633;">getHostAddress</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SocketException</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;ERROR Obtaining IP&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;No IP Available&quot;</span><span style="color: #339933;">;</span> &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://silverballsoftware.com/get-your-ip-address-android-code/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding Android Activity Life-cycle</title>
		<link>http://silverballsoftware.com/understanding-android-activity-life-cycle</link>
		<comments>http://silverballsoftware.com/understanding-android-activity-life-cycle#comments</comments>
		<pubDate>Mon, 25 Oct 2010 09:18:11 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[activity]]></category>
		<category><![CDATA[life-cycle]]></category>

		<guid isPermaLink="false">http://chrisleitner.com/?p=100</guid>
		<description><![CDATA[When I first starting programming for the Android I was quite confused about the life-cycle of activities. It seemed almost strange how the application started, stopped, and paused without seemingly any logical reasoning. The activity would be destroyed and re-created when the screen was rotated and paused when other applications were started&#8230; I soon learned [...]]]></description>
			<content:encoded><![CDATA[<p>When I first starting programming for the Android I was quite confused about the life-cycle of activities. It seemed almost strange how the application started, stopped, and paused without seemingly any logical reasoning. The activity would be destroyed and re-created when the screen was rotated and paused when other applications were started&#8230;</p>
<p>I soon learned that having a good grasp on the life-cycle of an activity was critical. Making an efficient application and knowing when to save critical data related to your activities state, let alone restoring your application to a particular state is all a part of the activity life-cycle.</p>
<p>There is a lot of different articles and information available on the internet about the life-cycles of activities. The <a href="http://developer.android.com/guide/topics/fundamentals.html">Google documentation</a> has good information on this as well. But I&#8217;m always better at understanding things if I see them for myself.</p>
<p>So I decided that I would write a simple activity that prints out debug statements each time one of the functions of the life-cycle was executed! Then I would be able to see exactly what is happening during each phase and when each function occurs during different situations.<br />
<span id="more-100"></span><br />
It&#8217;s a very simple application, there is literally nothing that it does except print out debug statements on activity changes. Set up a filter in your LogCat called LIFECYCLE to get a clean output of just teh debug statements from this activity.</p>
<p>A couple of things that I found helpful were knowing if the bundle that was passed into the onCreate method had was null or not. This helped me determine when data was being saved and passed back to the activity.</p>
<p>Another item to note is the <a href="http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29">onRetainNonConfigurationInstance</a> this function can come in handy in situations where you need to retain an object between destroying and re-creating your application. This can be used instead of passing items through a bundle or writing objects out to files or even re-querying a database. The thing you need to remember about this function is when it is called and why. The documentation states that this function is not guaranteed to be called and is only called when configuration changes take place (i.e. screen rotations) &#8211; it is not designed for any persistant data storage but is very helpful in retaining objects between activity changes. If you look closely at this function you can save just about anything from threads, network connections, and even images in memory.</p>
<p>Load up the code below and play around, rotate the screen, go to the home screen, start a new activity, click the back button. All these will help you get a better understanding of where and when you should perform certain clean up and data saving routines within the activity life-cycle.</p>
<p>Here&#8217;s the code for the whole application put this in the main activity class:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> tag <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;LIFECYCLE&quot;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span><br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Debug check bundle</span><br />
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>savedInstanceState <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag, <span style="color: #0000ff;">&quot;onCreate Bundle == null&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">else</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag, <span style="color: #0000ff;">&quot;onCreate Bundle == NOT null&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onPause<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag,<span style="color: #0000ff;">&quot;onPause&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onPause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onResume<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag,<span style="color: #0000ff;">&quot;onResume&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onResume</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onStart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag,<span style="color: #0000ff;">&quot;onStart&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onStart</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onStop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag,<span style="color: #0000ff;">&quot;onStop&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onStop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onRestart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag,<span style="color: #0000ff;">&quot;onRestart&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onRestart</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onDestroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag,<span style="color: #0000ff;">&quot;onDestroy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onDestroy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onSaveInstanceState<span style="color: #009900;">&#40;</span>Bundle outState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag, <span style="color: #0000ff;">&quot;OnSaveInstance&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onRestoreInstanceState<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag, <span style="color: #0000ff;">&quot;OnRestoreInstance&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> onRetainNonConfigurationInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>tag, <span style="color: #0000ff;">&quot;OnRetainNonConfig&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://silverballsoftware.com/understanding-android-activity-life-cycle/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paint.NET &#8211; Great Image Editing Software &#8211; Free!</title>
		<link>http://silverballsoftware.com/paint-net-great-image-editing-software-free</link>
		<comments>http://silverballsoftware.com/paint-net-great-image-editing-software-free#comments</comments>
		<pubDate>Tue, 19 Oct 2010 08:17:13 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Ramblings...]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[image editing]]></category>
		<category><![CDATA[paint]]></category>

		<guid isPermaLink="false">http://chrisleitner.com/?p=88</guid>
		<description><![CDATA[I’ve always loved to create my own graphics for websites, videos, even the family photos. One thing that stopped me was the price of good editing software. In my search for a great editing package I’ve come across a real gem. Paint.NET (sorry mac users – windows only) This software has everything and then some. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://silverballsoftware.com/wp-content/uploads/2010/10/paintnetl.jpg"><img class="alignleft size-full wp-image-123" title="paintnetl" src="http://silverballsoftware.com/wp-content/uploads/2010/10/paintnetl.jpg" alt="" width="237" height="58" /></a>I’ve always loved to create my own graphics for websites, videos, even the family photos. One thing that stopped me was the price of good editing software.</p>
<p>In my search for a great editing package I’ve come across a real gem. <a href="http://getpaint.net/" target="_blank">Paint.NET</a> (sorry mac users – windows only) This software has everything and then some.</p>
<p>The interface is extremely intuitive everything is laid out simply allowing you to easily find the tools you need. It has a thumbnail interface allowing you work with multiple images.</p>
<p><span id="more-88"></span></p>
<p><a href="http://silverballsoftware.com/wp-content/uploads/2011/04/paintnet.png"><img class="aligncenter size-full wp-image-122" title="paintnet" src="http://silverballsoftware.com/wp-content/uploads/2011/04/paintnet.png" alt="" width="567" height="90" /></a></p>
<p>&nbsp;</p>
<p>One of the things that is lacking in most imaging programs is layers. This feature makes Paint.NET a step above the competition. Having multiple layers allows you to create some amazing effects.</p>
<p>Another great thing about this program is the <a href="http://paintdotnet.forumer.com/index.php" target="_blank">active community</a>. If you have a question or are looking for a technique on how to create a certain effect – there are hundreds of users that will answer your questions. They also have a huge set of tutorials created by the community allowing you to get up to speed fast on some of the more advances techniques.</p>
<p>One of the other great features of the program is the effects. You can achieve very professional results with just a few clicks with the effects interfaces.  Developers from all over have contributed some amazing features to the program through their plug-in feature and the greatest thing about them is they are all <strong>free</strong>!</p>
<p>If your interested in some great imaging software &#8211; check out <a href="http://getpaint.net">Paint.NET</a>!</p>
<p><img src="file:///C:/Users/TheBoys/AppData/Local/Temp/moz-screenshot.png" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://silverballsoftware.com/paint-net-great-image-editing-software-free/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun With Motion Detection Software Part 2</title>
		<link>http://silverballsoftware.com/fun-with-motion-detection-software-part-2</link>
		<comments>http://silverballsoftware.com/fun-with-motion-detection-software-part-2#comments</comments>
		<pubDate>Mon, 18 Oct 2010 08:11:28 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://chrisleitner.com/?p=77</guid>
		<description><![CDATA[If you haven&#8217;t read my first post about our new experiment with motion detection head o over and read part 1 &#8211; then come on back for part 2! Everything is working great with with the software set up! It easily detects motion and takes pictures of the events. When we last posted about this [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t read my first post about our new experiment with motion detection head o over and <a href="http://chrisleitner.com/fun-with-motion-detection-software-part-1/">read part 1</a> &#8211; then come on back for part 2!</p>
<p>Everything is working great with with the software set up! It easily detects motion and takes pictures of the events. When we last posted about this I was in the process of adding notification to the software. I was able to add a couple of simple yet fun features to notify us when the software has detected motion.</p>
<p>I wanted a way to let us know when something was detected when we were at home so we could walk over to the computer and see what it caught. What better way to do that then to have the software send us a text message! Most carries have a way for you to send a text message by simply emailing your phone number using the carrier specific email address. After some brief searching I found that my carrier uses this format: yourphonenumber @ vtext.com sending an email to this address triggers their system to send a text message to your phone. NOTE: You will be charged for these if you don&#8217;t have an unlimited text plan be careful!</p>
<p><span id="more-77"></span></p>
<p>All I had to do now is add some code to send out the message after detection. Luckily .NET has some simple libraries to allow you to send out emails &#8211; so with just a few simple lines of code I was able to send an email that generates a text message whenever motion is detected. Here is the email code:</p>
<div class="codecolorer-container csharp twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> sendTextMsg<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MailMessage msg <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailMessage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg<span style="color: #008000;">.</span><span style="color: #0000FF;">To</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>send to email<span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg<span style="color: #008000;">.</span><span style="color: #0000FF;">To</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>send to email <span style="color: #008080;">#2&amp;quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">From</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>yourfromaddress<span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg<span style="color: #008000;">.</span><span style="color: #0000FF;">Subject</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>New Motion Detection<span style="color: #008000;">.&amp;</span>quot<span style="color: #008000;">;;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> mMailServer <span style="color: #008000;">=</span> <span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>yourmailserver<span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>New Image Taken<span style="color: #008000;">!&amp;</span>quot<span style="color: #008000;">;;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SmtpClient mySmtpClient <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span>mMailServer<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Net</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">NetworkCredential</span> cred <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Net</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">NetworkCredential</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>userName<span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>, <span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span>password<span style="color: #008000;">&amp;</span>quot<span style="color: #008000;">;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mySmtpClient<span style="color: #008000;">.</span><span style="color: #0000FF;">Credentials</span> <span style="color: #008000;">=</span> cred<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mySmtpClient<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">&#40;</span>msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></div>
<p>This works great I added a boolean to the code that is turned on and off by a menu choice allowing us to activate and de-activate the sending of messages when we want. It works great &#8211; my son gets really excited when he gets a text message that a new motion was detected!</p>
<p><strong>That just wasn&#8217;t enough &#8211; there was something missing&#8230;</strong></p>
<p>After my son and I discussed it we decided that we needed a way to see the images after they were captured but, without having to go to the computer to see them.</p>
<p>Since we already had the ability to send out email &#8211; we thought why not email the pictures to us so we can view them on our phones from anywhere!! Needless to say we changed the code once again and added the ability to send out an email with the images attached to it.</p>
<p>A couple of things to note are that attaching large amounts of photos to am email can make it very large depending on the size of the photos and some email providers won&#8217;t accept emails that large also, the processing time can be a hog and delay the rest of the program. To fix these we designed it to only attach the first five images that it collects and we also have it run on a separate thread so the program can go along it&#8217;s merry way as the email is processed and sent!</p>
<p>If your wondering about the attachments &#8211; it&#8217;s simple just add them to the
<pre>Message</pre>
<p> object like this
<pre>msg.Attachment.Add(path to file)</pre>
<p>.</p>
<p>As for the threading, once again, .NET makes it easy to create simple threading with the
<pre>BackgroundWorker</pre>
<p> object. I will leave this for another day as this post is already way too long.</p>
<p>One last thing&#8230; After all the fun we&#8217;ve had with the motion detection we have decided to change things up so we can do time-lapse! We think it would be really neat to see how things happen over time. So stay tuned for some fun with time-lapse coming in some later posts!</p>
]]></content:encoded>
			<wfw:commentRss>http://silverballsoftware.com/fun-with-motion-detection-software-part-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun With Motion Detection Software Part 1</title>
		<link>http://silverballsoftware.com/fun-with-motion-detection-software-part-1</link>
		<comments>http://silverballsoftware.com/fun-with-motion-detection-software-part-1#comments</comments>
		<pubDate>Sat, 16 Oct 2010 18:11:05 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://chrisleitner.com/?p=55</guid>
		<description><![CDATA[I was looking through some of my accumulated items (junk) and I found a couple of small video monitoring cameras that I bought years ago. This was great timing as I had saw a post somewhere that talked about motion detection in video. I thought this would be a great opportunity to learn a little [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking through some of my accumulated items (junk) and I found a couple of small video monitoring cameras that I bought years ago. This was great timing as I had saw a post somewhere that talked about motion detection in video. I thought this would be a great opportunity to learn a little about motion detection and have a little fun.</p>
<p>The cameras have 25m of cable, some infrared leds (night vision), and an s-video out. It runs off a wall wart for power (I&#8217;m thinking about swapping this for batteries) so it&#8217;s limited to where we can put it. I&#8217;m using a USB EasyCAP device to take the s-video and bring it into the computer.</p>
<p><span id="more-55"></span></p>
<p>Time to do a little research and since I&#8217;m familiar with C# I figured I&#8217;d start looking for examples of software to use. I had a few criteria that I was looking for:</p>
<ul>
<li>Had to use C#</li>
<li>Had to be able to save files to disc</li>
<li>Should be event driven</li>
<li>Had to support my video input</li>
<li>Had to be simple&#8230;</li>
</ul>
<p>After about a half hour looking at different examples and rummaging through some source code I came across this one on codeproject &#8211; <a href="http://www.codeproject.com/KB/audio-video/Motion_Detection.aspx" target="_blank">Motion Detection Algorythms</a>. Even though it is a little old it fits all my criteria perfectly. It uses the AForge.NET Framework which is very actively worked on which allows me access to great documentation. I was able to download this and get it compiled and running in about a half hour!</p>
<p>Right away I noticed that the orginal source code saves the images to an AVI file when it senses motion. I just wanted to take still pictures and save them to disc whenever motion is detected. Since the AForge framework is so simple I was able to modify the code to write out bitmaps instead of avi files. Here is what I added&#8230; (this is just a simple fun program so alot of error checking and proper coding was skipped)</p>
<p>The camera control has a callback event that triggers on each frame, so originally the code would check to see if it was supposed to capture and if it was then it would write an avi file &#8211; here&#8217;s what I changed the callback function to:</p>
<div class="codecolorer-container csharp twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> camera_NewFrame<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">object</span> sender, <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EventArgs</span> e <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span> intervalsToSave <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span>amp<span style="color: #008000;">;</span><span style="color: #008000;">&#40;</span>saveOnMotion <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>saveInterval <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008080; font-style: italic;">// save the frame</span><br />
Camera camera <span style="color: #008000;">=</span> cameraWindow<span style="color: #008000;">.</span><span style="color: #0000FF;">Camera</span><span style="color: #008000;">;</span><br />
camera<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Lock</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
DateTime date <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span><br />
<span style="color: #6666cc; font-weight: bold;">String</span> fileName <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0}-{1}-{2} {3}-{4}-{5}.bmp&quot;</span>,date<span style="color: #008000;">.</span><span style="color: #0000FF;">Year</span>, date<span style="color: #008000;">.</span><span style="color: #0000FF;">Month</span>, date<span style="color: #008000;">.</span><span style="color: #0000FF;">Day</span>, date<span style="color: #008000;">.</span><span style="color: #0000FF;">Hour</span>, date<span style="color: #008000;">.</span><span style="color: #0000FF;">Minute</span>, date<span style="color: #008000;">.</span><span style="color: #0000FF;">Second</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
camera<span style="color: #008000;">.</span><span style="color: #0000FF;">LastFrame</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span>Application<span style="color: #008000;">.</span><span style="color: #0000FF;">StartupPath</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\i</span>mages<span style="color: #008080; font-weight: bold;">\&quot;</span> + fileName);<br />
camera.Unlock();<br />
saveInterval = mnuInterval;<br />
}<br />
saveInterval--;<br />
}<br />
}</span></div></div>
<p>This gave me a bitmap image that was uniquely named from the date/time and saved it to a folder called images that is in the same directory as the program. The saveInterval variable was also added. This allowed me to not have to save every frame when the motion was triggered. At 20-30 frames per second it was creating a lot of files that I didn&#8217;t need. I have been setting this to 10 or 20 and it seems to work for me (you can eliminate it to capture every frame if you want).</p>
<p>I almost forgot&#8230; one other thing that I changed was to eliminate the red lines that get drawn around the items that are detected. I didn&#8217;t care to see what was being detected &#8211; I just wanted clean images. In the  file MotionDetector3Optimized.cs all I had to do was comment out one line.</p>
<div class="codecolorer-container csharp twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">// Postprocess input image</span><br />
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> PostprocessInputImage<span style="color: #008000;">&#40;</span> BitmapData data, <span style="color: #6666cc; font-weight: bold;">int</span> width, <span style="color: #6666cc; font-weight: bold;">int</span> height, <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> buf <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008000;">.......</span><br />
<span style="color: #008080; font-style: italic;">// check if we need to highlight moving object</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>buf<span style="color: #008000;">&#91;</span>k<span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">255</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008080; font-style: italic;">// check for border</span><br />
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><br />
<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> x <span style="color: #008000;">%</span> <span style="color: #FF0000;">8</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span>amp<span style="color: #008000;">;</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> j <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span> buf<span style="color: #008000;">&#91;</span>k <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span><br />
<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> x <span style="color: #008000;">%</span> <span style="color: #FF0000;">8</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">7</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span>amp<span style="color: #008000;">;</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> j <span style="color: #008000;">==</span> lenWM1 <span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span> buf<span style="color: #008000;">&#91;</span>k <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span><br />
<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> y <span style="color: #008000;">%</span> <span style="color: #FF0000;">8</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span>amp<span style="color: #008000;">;</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> i <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span> buf<span style="color: #008000;">&#91;</span>k <span style="color: #008000;">-</span> len<span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span><br />
<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> y <span style="color: #008000;">%</span> <span style="color: #FF0000;">8</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">7</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span>amp<span style="color: #008000;">;</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span> i <span style="color: #008000;">==</span> lenHM1 <span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span> buf<span style="color: #008000;">&#91;</span>k <span style="color: #008000;">+</span> len<span style="color: #008000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008080; font-style: italic;">//src[RGB.R] = 255;</span><br />
<span style="color: #008080; font-style: italic;">//un-comment the above to draw the red outline of motion objects.</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">..............</span></div></div>
<p>So now we had a functioning program and it was collecting the images just like we wanted.</p>
<p>It was late and we wanted to see if the night vision part of the camera would work. So we set out the camera way out in the back yard (as far as the cable would reach out the window). Underneath the camera we placed some peanut butter on a rock and then went to bed.</p>
<p>The next morning we woke up and to our surprise we had some images &#8211; yes they are mice (we live out in a rural area). It was pretty neat to see the images and it was fun to get everything working together.</p>
<p>I created these animated gifs with the images we captured.</p>
<p><a href="http://silverballsoftware.com/wp-content/uploads/2011/04/mouse1.gif"><img class="aligncenter size-full wp-image-121" title="mouse1" src="http://silverballsoftware.com/wp-content/uploads/2011/04/mouse1.gif" alt="" width="320" height="240" /></a></p>
<p><a href="http://silverballsoftware.com/wp-content/uploads/2011/04/mouse.gif"><img class="aligncenter size-full wp-image-120" title="mouse" src="http://silverballsoftware.com/wp-content/uploads/2011/04/mouse.gif" alt="" width="320" height="240" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Even though we had a working system I wanted to try some other things with the software &#8211; I wanted to be able to know when it captured motion and be able to see the images on my phone. So in part two of this post I will show you how we are now notified by text message and sent an email with the pictures (I&#8217;m looking at having them upload to a web site as well) I&#8217;m also working on a larger array of infrared lights to hopefully get better pictures! Stay Tuned!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://silverballsoftware.com/fun-with-motion-detection-software-part-1/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

