Slide 1

Slide 1 text

klab.ca/spotlightandroid Google+: klab.ca/+ Twitter: @PearlChen Open Accessory (Or "Android: The Loveable Mobile Platform That Plays Well with External Devices") NFC Bluetooth Better with Friends Pearl Chen [email protected]

Slide 2

Slide 2 text

klab.ca/spotlightandroid The future is already here — it's just not very evenly distributed. William Gibson

Slide 3

Slide 3 text

About me ( Illustration created using modified output from visualize.me and my LinkedIn profile. ) Some Comp Sci classes back there too

Slide 4

Slide 4 text

Some of my web projects

Slide 5

Slide 5 text

Some of non-web my projects

Slide 6

Slide 6 text

What inspires me A-Nerve by CuteCircuit

Slide 7

Slide 7 text

The Third Industrial Revolution From The Economist, April 2012 A number of remarkable technologies are converging: clever software, novel materials, more dexterous robots, new processes (notably 3D printing) and a whole range of web-based services. The factory of the past was based on cranking out zillions of identical products. The factory of the future will focus on mass customisation.

Slide 8

Slide 8 text

Microcontrollers 1975: Microchip PIC MaKey MaKey Arduino Basic Stamp

Slide 9

Slide 9 text

Hardware startups becoming viable [The founder] also shared a photo of the first Pebble prototype using an Arduino and some similarly cobbled-together hardware. That was four years ago. Pebble Kickstarter campaign as of June 2012 via Slashgear

Slide 10

Slide 10 text

Looking at the hardware trends • Small, yet mighty • Multi-screen • Always on • $$$ ⇾ $ Steve Mann - world’s first cyborg Project Glass by Google

Slide 11

Slide 11 text

...and the software trends Location, location, location. Context, context, context.

Slide 12

Slide 12 text

And how is this related to Android? This talk will cover: • External hardware integration with Android Open Accessory • NFC for low-friction interactions (not related to mobile payments) • Combining with Bluetooth

Slide 13

Slide 13 text

klab.ca/spotlightandroid Professional Android Sensor Programming Disclaimer: I authored chapter 11 (NFC) & co-authored 10 (AOA) (more info here)

Slide 14

Slide 14 text

klab.ca/spotlightandroid Android Open Accessory

Slide 15

Slide 15 text

klab.ca/spotlightandroid Android Open Accessory - Demo!

Slide 16

Slide 16 text

klab.ca/spotlightandroid Android Development Kit (ADK) Arduino Mega ADK

Slide 17

Slide 17 text

klab.ca/spotlightandroid More Android Development Kits! SparkFun IOIO ODroid ADK Microchip PIC24F ADK

Slide 18

Slide 18 text

Basic firmware/sketch on Arduino ADK #include #include #include AndroidAccessory acc("Manufacturer", "Model", "Description", "1.0", "http://www.android.com", "0000000012345678"); Google-supplied C++ library USB and USB host libraries Create a new instance of the AndroidAccessory void setup() { acc.powerOn(); } void loop() { byte msg[0]; if (acc.isConnected()) { //send something to the Android app acc.write(msg, 1); //or read something int len = acc.read(msg, sizeof(msg), 1); } } Note: You can get a fully working but barebones Arduino ADK sketch here: iheartrobotics.com/2011/07/arduino-mega-adk-setup-notes.html Convenience method that simply calls the powerOn() method in the Max3421e library. Continually check for connection to Android app Create data that the Android app can read Read data from Android app into msg variable

Slide 19

Slide 19 text

Accessory Filter Resource Needs to match the Arduino sketch exactly In xml/accessory_filter.xml:

Slide 20

Slide 20 text

Android Manifest In AndroidManifest.xml (3.1+): From previous slide Use intent filter to launch this activity when ADK is plugged in

Slide 21

Slide 21 text

Activity import com.android.hardware.usb.UsbAccessory; import com.android.hardware.usb.UsbManager; //... UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); ParcelFileDescriptor fileDescriptor = manager.openAccessory(accessory); FileDescriptor fd = fileDescriptor.getFileDescriptor(); FileInputStream inputStream = new FileInputStream(fd); byte[] buffer = new byte[16384]; int ret = inputStream.read(buffer); FileOutputStream outputStream = new FileOutputStream(fd); mOutputStream.write(buffer); In the .java file for your Activity (3.1+): Read data from the Arduino via a FileInputStream Write data to the Arduino via a FileOutputStream

Slide 22

Slide 22 text

Project ideas So what can you make with the Open Accessory APIs and an Arduino? 1 + 1 = 3 Things already available on your phone Electronics components you can add Super awesome stuff!

Slide 23

Slide 23 text

klab.ca/spotlightandroid Things already available on your phone touch screen camera wifi/3G accelerometer GPS mass storage proximity/light sensors temperature sensor microphone NFC

Slide 24

Slide 24 text

klab.ca/spotlightandroid Electronics components you can add Electronics inputs and outputs using LittleBits as an example of various components

Slide 25

Slide 25 text

klab.ca/spotlightandroid Super awesome stuff! Music Beta: “Now Playing” by Chris Juergen Aerogarden Monitor by Sam Steele Space Spheres by NASA Floating Sensor Project by UC Berkeley

Slide 26

Slide 26 text

klab.ca/spotlightandroid Why Open Accessories are “open” • iOS developer fees: $99/yr. • Approval required to be in MFi program; must sign NDAs. • Additional Apple licensing fees and 3rd party certification. • Final product needs to be approved by Apple • One supplier (Avent) produces the connectors. • Fill out zero forms to start developing. • Pay nothing to Google -- now or ever. • No approval process from Google to go to market. • USB connectors and drivers are readily available.

Slide 27

Slide 27 text

Speaking of connectors...

Slide 28

Slide 28 text

Are there wireless ADKs? Bluetooth Shields Coming soon? Wifi Shield w/ integrated chip IOIO w/ USB Bluetooth dongle Coming soon? ADK2012

Slide 29

Slide 29 text

klab.ca/spotlightandroid NFC (near field communications)

Slide 30

Slide 30 text

What is NFC? Radiowaves NFC Scanner NFC Sticker: antenna and flash storage

Slide 31

Slide 31 text

How is it different than RFID? Low frequency radio waves can travel far distances. High frequency waves cannot. NFC works within 10cm only. 13.56MHz = NFC

Slide 32

Slide 32 text

How is it different than RFID? Storage with RFID is also limited to simple IDs versus the complex data NFC tags can hold. NFC Forum Type Popular Products of This Type Operations Specifications Rewrite Capabilities Available Memory Communication Speed Price Range (price per unit) 1 Broadcom Topaz ISO 14443A User rewritable; can be marked as read-only by user 96 bytes, expandable to 2KB 106kbit/s Low (~$1-2 USD) 2 MIFARE UltraLight ISO 14443A User rewritable; can be marked as read-only by user 48 bytes, 144 bytes is common, expandable to 2KB 106kbit/s Low (~$1-2 USD) 3 Sony FeliCa JIS X 6319-4 Manufacture pre- configured to be read- only or re-writable. variable, theoretical 1MB 212kbit/s or 424kbit/s High (~$8-10 USD or higher) 4 NXP DESFire, NXP SmartFX ISO 14443A, ISO 14443B Manufacture pre- configured to be read- only or rewritable. 4KBfor DESFire, up to 32KBfor SmartFX Up to 424kbit/s Medium-High (~$3-4 USD) MIFARE Type Operations Specifications Rewrite Capabilities Available Memory Communicatio n Speed Price Range (price per unit) Classic 1K ISO 14443A compatible, but NDEF is formatted using a proprietary protocol User rewritable; only manufacturer can mark as read-only 752 bytes 106kbit/s Low (~$1 USD) Classic 4K ISO 14443A compatible, but NDEF is formatted using a proprietary protocol User rewritable; only manufacturer can mark as read-only 3440 bytes 106kbit/s Low-Medium (~$2 USD)

Slide 33

Slide 33 text

klab.ca/spotlightandroid NFC - Demo!

Slide 34

Slide 34 text

Android Manifest In AndroidManifest.xml (4.0+): Could be custom MIME Type like: application/root.gast.playground.nfc

Slide 35

Slide 35 text

Activity - Reacting to NFC scans import android.nfc.NfcAdapter; import android.nfc.NfcAdapter.CreateNdefMessageCallback; import android.nfc.NfcEvent; //... NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this); Boolean nfcEnabled = mNfcAdapter.isEnabled(); mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mReadTagFilters, null); mNfcAdapter.setNdefPushMessageCallback(this,this); In the .java file for your Activity: Give priority to the foreground activity to receive NFC scan intents. Enable Android Beam for peer-to-peer

Slide 36

Slide 36 text

Activity - Formatting to NDEF Format import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.tech.Ndef; import android.nfc.tech.NdefFormatable; //... // get the values from the form's text fields: Editable nameField = mName.getText(); JSONObject computerSpecs = new JSONObject(); computerSpecs.put("name", nameField); String data = computerSpecs.toString(); // create a new NDEF record w/ NDEF message using the app's custom MIME type: String mimeType = "application/root.gast.playground.nfc"; byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8")); byte[] dataBytes = data.getBytes(Charset.forName("UTF-8")); byte[] id = new byte[0]; NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, dataBytes); NdefMessage m = new NdefMessage(new NdefRecord[] { record }); In the .java file for your Activity: JSON for readability, not performance Unique to your app Not hard at all!

Slide 37

Slide 37 text

Activity - Reading NDEF Format import android.nfc.Tag; //... Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage[] msgs = new NdefMessage[rawMsgs.length]; for (int i = 0; i < rawMsgs.length; i++) { msgs[i] = (NdefMessage) rawMsgs[i]; } NdefRecord record = msgs[0].getRecords()[0]; byte[] payload = record.getPayload(); In the .java file for your Activity: The info you wanted. (e.g. string or json)

Slide 38

Slide 38 text

klab.ca/spotlightandroid A case for NFC • Create low friction interactions • Augment physical objects (even offline) • Make your software app more tangible • Engage others to share and connect • Increase the viralness of your app • Possibility to be platform agnostic

Slide 39

Slide 39 text

klab.ca/spotlightandroid Mobile OS support for NFC Chart updated for Sept 27, 2012

Slide 40

Slide 40 text

klab.ca/spotlightandroid Don’t worry, this isn’t Apple bashing... By touching phones? Hey! What did you just do there? Oh, I just sent him a playlist.

Slide 41

Slide 41 text

klab.ca/spotlightandroid Thank you! Oh, and check out some upcoming Arduino workshops! Sign up for mailing list on karma-laboratory.com. Google+: klab.ca/+ Twitter: @PearlChen Pearl Chen [email protected]