Назар аударыңыз. Бұл материалды сайт қолданушысы жариялаған. Егер материал сіздің авторлық құқығыңызды бұзса, осында жазыңыз. Біз ең жылдам уақытта материалды сайттан өшіреміз
Жақын арада сайт әкімшілігі сізбен хабарласады
Бонусты жинап картаңызға (kaspi Gold, Halyk bank) шығарып аласыз
Arduino boinwa
Дипломдар мен сертификаттарды алып үлгеріңіз!
Материалдың толық нұсқасын
жүктеп алып көруге болады
Book Title <Chapter No> V1 - MM/DD/2010
ffirs.indd iiffirs.indd ii 5/2/2012 6:22:26 PM5/2/2012 6:22:26 PM
Book Title <Chapter No> V1 - MM/DD/2010
PROFESSIONAL
Android™ Sensor Programming
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvii
PART I LOCATION SERVICES
CHAPTER 1 Introducing the Android Location Service . . . . . . . . . . . . . . . . . . . . . . . . . . 3
CHAPTER 2 Determining a Device’s Current Location . . . . . . . . . . . . . . . . . . . . . . . . . . 11
CHAPTER 3 Tracking Device Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
CHAPTER 4 Proximity Alerts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
PART II INFERRING INFORMATION FROM PHYSICAL SENSORS
CHAPTER 5 Overview of Physical Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
CHAPTER 6 Errors and Sensor Signal Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
CHAPTER 7 Determining Device Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
CHAPTER 8 Detecting Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147
CHAPTER 9 Sensing the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
CHAPTER 10 Android Open Accessory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
PART III SENSING THE AUGMENTED, PATTERN-RICH EXTERNAL
WORLD
CHAPTER 11 Near Field Communication (NFC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
CHAPTER 12 Using the Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
CHAPTER 13 Image-Processing Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
CHAPTER 14 Using the Microphone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
PART IV SPEAKING TO ANDROID
CHAPTER 15 Designing a Speech-Enabled App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
CHAPTER 16 Using Speech Recognition and Text-To-Speech APIs . . . . . . . . . . . . . . 349
CHAPTER 17 Matching What Was Said . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
CHAPTER 18 Executing Voice Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
CHAPTER 19 Implementing Speech Activation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
ffirs.indd iffirs.indd i 5/11/2012 9:36:42 AM5/11/2012 9:36:42 AM
Book Title <Chapter No> V1 - MM/DD/2010
ffirs.indd iiffirs.indd ii 5/11/2012 9:36:43 AM5/11/2012 9:36:43 AM
Book Title <Chapter No> V1 - MM/DD/2010
PROFESSIONAL
Android™ Sensor Programming
ffirs.indd iiiffirs.indd iii 5/11/2012 9:36:43 AM5/11/2012 9:36:43 AM
ffirs.indd ivffirs.indd iv 5/11/2012 9:36:43 AM5/11/2012 9:36:43 AM
Book Title <Chapter No> V1 - MM/DD/2010
PROFESSIONAL
Android™ Sensor Programming
Greg Milette
Adam Stroud
ffirs.indd vffirs.indd v 5/11/2012 9:36:43 AM5/11/2012 9:36:43 AM
Book Title <Chapter No> V1 - MM/DD/2010
Professional Android™ Sensor Programming
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2012 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-18348-9
ISBN: 978-1-118-22745-9 (ebk)
ISBN: 978-1-118-24045-8 (ebk)
ISBN: 978-1-118-26505-5 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the
Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-
6008, or online at
http://www.wiley.com/go/permissions .
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect
to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including without
limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or promotional
materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the
understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional
assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author
shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation
and/or a potential source of further information does not mean that the author or the publisher endorses the information the
organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web
sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with
standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media
such as a CD or DVD that is not included in the version you purchased, you may download this material at
http://
booksupport.wiley.com
. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2012936847
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other countries,
and may not be used without written permission. Android is a trademark of Google, Inc. All other trademarks are the prop-
erty of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.
ffirs.indd viffirs.indd vi 5/11/2012 9:36:46 AM5/11/2012 9:36:46 AM
Book Title <Chapter No> V1 - MM/DD/2010
For Tanya and Madison, my inspiration!!!
—G.M.
To Sabrina, Abigail and Elizabeth...I love you.
—A.S.
ffirs.indd viiffirs.indd vii 5/11/2012 9:36:46 AM5/11/2012 9:36:46 AM
ffirs.indd viiiffirs.indd viii 5/11/2012 9:36:47 AM5/11/2012 9:36:47 AM
Book Title <Chapter No> V1 - MM/DD/2010 Page ix
ABOUT THE AUTHORS
GREG MILETTE is a professional Android developer and founder of Gradison Technologies, an app
development company. He enjoys building practical apps like Digital Recipe Sidekick and contribut-
ing to StackOverfl ow.
ADAM STROUD is the lead developer for the Android version of RunKeeper. He is a self-proclaimed
“phandroid” and is an active participant in the Android virtual community on StackOverfl ow and
Android Google groups.
ABOUT THE CONTRIBUTORS
DAVID N. HUTCHISON (http://davidnhutch.com) was born and raised in New Zealand, and is cur-
rently a PhD candidate in physics at Cornell University, where he is developing next-generation inertial
sensors. He loves to hack up microcontroller-enabled gadgets in the machine shop, ride his motorcycle,
and start companies. David wrote Chapters 5 and 6, and contributed to Chapters 7 and 10.
JON WEBB, the developer of Jon’s Java Imaging Library, has been developing software professionally
for over three decades. He enjoys programming image processing on Android as it brings back fond
memories of his early days. Jon wrote Chapters 12 and 13.
PEARL CHEN takes a cross-disciplinary approach to her work, from HTML to LEDs, Android to
Arduino. Both an educator and developer, Pearl teaches programming and electronics, while also
acting as CTO of http://thehungryveg.com . To fi nd out more about Pearl’s upcoming workshops
and Arduino kits, visit http://klab.ca/arduino. Pearl wrote Chapter 11 and contributed to
Chapter 10.
ffirs.indd ixffirs.indd ix 5/11/2012 9:36:47 AM5/11/2012 9:36:47 AM
ffirs.indd xffirs.indd x 5/11/2012 9:36:47 AM5/11/2012 9:36:47 AM
Book Title <Chapter No> V1 - MM/DD/2010
EXECUTIVE EDITOR
Robert Elliot
PROJECT EDITOR
Brian Herrmann
TECHNICAL EDITOR
Jim Steele
PRODUCTION EDITOR
Christine Mugnolo
COPY EDITOR
Kimberly A. Cofer
EDITORIAL MANAGER
Mary Beth Wakefi eld
FREELANCER EDITORIAL MANAGER
Rosemarie Graham
ASSOCIATE DIRECTOR OF MARKETING
David Mayhew
MARKETING MANAGER
Ashley Zurcher
BUSINESS MANAGER
Amy Knies
PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND EXECUTIVE GROUP
PUBLISHER
Richard Swadley
VICE PRESIDENT AND EXECUTIVE
PUBLISHER
Neil Edde
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Katie Crocker
PROOFREADER
Josh Chase, Word One New York
INDEXER
Robert Swanson
COVER DESIGNER
Ryan Sneed
COVER IMAGE
© Antonis Papantoniou / iStockPhoto
CREDITS
ffirs.indd xiffirs.indd xi 5/11/2012 9:36:47 AM5/11/2012 9:36:47 AM
ffirs.indd xiiffirs.indd xii 5/11/2012 9:36:47 AM5/11/2012 9:36:47 AM
Book Title <Chapter No> V1 - MM/DD/2010
ACKNOWLEDGMENTS
WE WOULD LIKE TO THANK David Hutchinson, Pearl Chen, and Jon Webb for providing content and
guidance throughout the process of authoring this book. Their expertise in physics, AOA, NFC, and
image processing allowed us to describe some exciting ways to use Android in a level of detail we
would have otherwise been unable to achieve.
We would like to thank our editors for inspiring us to write the book and their hard work in making
us sound more human-like and less like little green robots.
Finally, we could not have written this book without the help of people in the Android developer
community who share and help us all work towards a common goal. We hope this book and its code
help to repay the favor.
ffirs.indd xiiiffirs.indd xiii 5/11/2012 9:36:47 AM5/11/2012 9:36:47 AM
ffirs.indd xivffirs.indd xiv 5/11/2012 9:36:47 AM5/11/2012 9:36:47 AM
Book Title <Chapter No> V1 - MM/DD/2010
CONTENTS
INTRODUCTION xxvii
PART I: LOCATION SERVICES
CHAPTER 1: INTRODUCING THE ANDROID LOCATION SERVICE 3
Methods Used to Determine Location 3
GPS Provider 4
How It Works 4
GPS Improvements 5
Limitations 6
Controlling GPS 7
Network Provider 8
Using Wireless Network Access Points 8
Using Cell IDs 9
Summary 10
CHAPTER 2: DETERMINING A DEVICE’S CURRENT LOCATION 11
Know Your Tools 12
LocationManager 12
LocationProvider 13
Location 13
Criteria 13
LocationListener 15
Setting up the Android Manifest 15
Determining the Appropriate Location Provider 15
GPS Location Provider 16
Network Location Provider 16
Passive Location Provider 16
Accuracy versus Battery Life 17
Receiving Location Updates 18
Receiving Location Updates with a LocationListener 18
Receiving Location Updates with a Broadcast Intent 18
Implementing the Example App 18
Implementing LocationListener 18
onLocationChanged() 19
ftoc.indd xvftoc.indd xv 5/10/2012 2:18:10 PM5/10/2012 2:18:10 PM
Book Title <Chapter No> V1 - MM/DD/2010
xvi
CONTENTS
onProviderDisabled() and onProviderEnabled() 19
onStatusChanged() 20
Obtaining a Handle to LocationManager 20
Requesting Location Updates 23
Cleaning up After Yourself 25
Launching the Location Settings Activity 25
Summary 26
CHAPTER 3: TRACKING DEVICE MOVEMENT 27
Collecting Location Data 28
Receiving Location Updates with a Broadcast Receiver 28
Extending BroadcastReceiver 29
Registering the BroadcastReceiver with Android 30
Requesting Location Updates with a PendingIntent 32
One Intent, Multiple Receivers 33
Why Not Use a Service? 34
Viewing the Tracking Data 35
Google Map Library Components 36
MapView 37
OverlayItem 37
ItemizedOverlay 38
MapActivity 40
Filtering Location Data 40
Continuous Location Tracking and Battery Life 43
Reducing Location Update Frequency 43
Limiting Location Providers 44
Summary 44
CHAPTER 4: PROXIMITY ALERTS 45
App Structure 45
Geocoding 46
android.location.Geocoder 46
Reading the Geocoded Response 48
Setting a Proximity Alert 50
Responding to a Proximity Alert 53
Proximity Alert Limitations 55
Battery Life 55
Permissions 56
More Effi cient Proximity Alert 56
ftoc.indd xviftoc.indd xvi 5/10/2012 2:18:11 PM5/10/2012 2:18:11 PM
10 Book Title <Chapter No> V1 - MM/DD/2010
xvii
CONTENTS
ProximityAlertService 56
Summary 61
PART II: INFERRING INFORMATION FROM PHYSICAL SENSORS
CHAPTER 5: OVERVIEW OF PHYSICAL SENSORS 65
Defi nitions 66
Android Sensor API 68
SensorManager 68
Sensor 68
Sensor Rates 69
Sensor Range and Resolution 70
SensorEventListener 71
SensorEvent 71
Sensor List 72
The Manifest File 72
SensorListActivity 73
SensorSelectorFragment 74
SensorDisplayFragment 76
Sensing the Environment 84
Sensor.TYPE_LIGHT 84
Sensor.TYPE_PROXIMITY 85
Sensor.TYPE_PRESSURE 86
Absolute Altitude 87
Relative Altitude 87
Mean Sea-Level Pressure (MSLP) 87
Where to Find MSLP 88
Sensor Units 88
Sensor Range 88
Common Use Cases 88
Sensor.TYPE_RELATIVE_HUMIDITY 89
Sensor.TYPE_AMBIENT_TEMPERATURE 89
Sensor.TYPE_TEMPERATURE 89
Sensing Device Orientation and Movement 90
Coordinate Systems 90
Global Coordinate System 91
Device Coordinate System 91
Angles 91
Sensor.TYPE_ ACCELEROMETER, .TYPE_GRAVITY, and
.TYPE_LINEAR_ACCELERATION 92
ftoc.indd xviiftoc.indd xvii 5/10/2012 2:18:11 PM5/10/2012 2:18:11 PM
Book Title <Chapter No> V1 - MM/DD/2010
xviii
CONTENTS
Sensor Units and Resolution 93
Sensor.TYPE_GYROSCOPE 94
Sensor Units 94
Sensor Range 94
Sensor.TYPE_MAGNETIC_FIELD 94
Sensor Units, Range, and Resolution 95
Sensor.TYPE_ROTATION_VECTOR 97
SensorManager.getOrientation() 98
SensorManager.getInclination() 101
Sensor Fusion Schemes 101
Summary 102
CHAPTER 6: ERRORS AND SENSOR SIGNAL PROCESSING 103
Defi nitions 104
Accuracy and Precision 104
Types of Errors 105
Human Error, Systematic Error, and Random Error 105
Noise 105
Drift 105
Zero Off set (or “Off set,” or “Bias”) 105
Time Delays and Dropped Data 105
Integration Error 106
Techniques to Address Error 107
Re-zeroing 107
Filters 107
Sensor Fusion 107
Filters 107
Low-Pass 107
Weighted Smoothing 108
Simple Moving Average (SMA) 108
Choosing the Smoothing Parameter 111
Averaging: Smoothness vs. Response Time 111
Simple Moving Median (SMM) 111
High-Pass 111
Inverse Low-Pass Filter 112
Bandpass 113
Introducing Kalman Filters 114
A Better Determination of Orientation
by Using Sensor Fusion 115
Sensor Fusion: Simple vs. Proprietary 115
ftoc.indd xviiiftoc.indd xviii 5/10/2012 2:18:11 PM5/10/2012 2:18:11 PM
10 Book Title <Chapter No> V1 - MM/DD/2010
xix
CONTENTS
Proprietary Sensor Fusion 116
Simple Sensor Fusion: The Balance Filter 117
Summary 119
CHAPTER 7: DETERMINING DEVICE ORIENTATION 121
Previewing the Example App 121
Determining Device Orientation 122
Gravity Sensor 123
Accelerometer and Magnetometer 123
Gravity Sensor and Magnetometer 124
Rotation Vector 124
Implementation Details 125
Processing Gravity Sensor Data 130
Processing Accelerometer and Magnetic Field Data 130
Processing Rotation Vector Data 132
Notifying the User of Orientation Changes 135
NorthFinder 143
Summary 146
CHAPTER 8: DETECTING MOVEMENT 147
Acceleration Data 148
Accelerometer Data 148
Linear Acceleration Sensor Data 150
Data While Device Is in Motion 150
Total Acceleration 153
Implementation 153
DetermineMovementActivity 153
AccelerationEventListener 156
Summary 159
CHAPTER 9: SENSING THE ENVIRONMENT 161
Barometer vs. GPS for Altitude Data 162
Example App Overview
162
Implementation Details 163
GPS-Based Altitude 169
Barometric Pressure–Based Altitude 170
Relative Altitude 177
Summary 187
ftoc.indd xixftoc.indd xix 5/10/2012 2:18:11 PM5/10/2012 2:18:11 PM
Book Title <Chapter No> V1 - MM/DD/2010
xx
CONTENTS
CHAPTER 10: ANDROID OPEN ACCESSORY 189
A Short History of AOA 189
USB Host Versus USB Accessory 190
Electrical Power Requirements 190
Supported Android Devices 190
The Android Development Kit (ADK) 191
Hardware Components 194
Software Components 195
AOA Sensors versus Native Device Sensors 196
AOA Beyond Sensors 196
AOA Limitations 196
AOA and Sensing Temperature 197
Implementation 198
Requirements 198
Getting Started with the Arduino Software 198
Arduino Sketch 199
Android Code 205
Communication between Arduino and Android 208
Taking an Android Accessory to the Consumer Market 215
Summary 216
PART III: SENSING THE AUGMENTED, PATTERN-RICH EXTERNAL WORLD
CHAPTER 11: NEAR FIELD COMMUNICATION (NFC) 219
What Is RFID? 220
What Is NFC? 222
The NDEF Data Format 223
How and Where to Buy NFC Tags 224
NDEF-compatible NFC Tags 224
Storage Size versus Price versus Security Trade-off 224
Write Protection 226
Form Factor 226
Retailers 227
General Advantages and Disadvantages of NFC 227
Low Power and Proximity Based 228
Small, Short Data Bursts 228
Singular Scanning 229
Security 229
Card Emulation 229
ftoc.indd xxftoc.indd xx 5/10/2012 2:18:11 PM5/10/2012 2:18:11 PM
10 Book Title <Chapter No> V1 - MM/DD/2010
xxi
CONTENTS
Android-specifi c Advantage: Intents 229
Required Hardware 230
Building an Inventory Tracking System 230
The Scenario 230
The NFC Inventory Demonstration App 230
Enabling NFC in the Settings 231
Debugging Your Tags with Apps 232
Android APIs 233
In Your AndroidManifest.xml File 233
Permissions and Minimum API Level 233
Intent Filters 233
Custom MIME Type Intent Filters 234
URI-based Intent Filters 235
In Your Main Activity Class 236
NfcManager 237
NfcAdapter 237
Foreground Dispatching 237
Foreground NDEF Push 241
Reacting to an NDEF Tag 241
NdefMessage and NdefRecord 243
Parsing and Reading NDEF Tags 245
Getting Ready to Write to a Tag 246
Writing to the Tag 248
Putting it All Together 250
Future Considerations 251
NFC N-Mark 251
Peer-to-Peer NFC Sharing 251
Peer-to-Peer Android APIs 252
Go Forth and NFC! 253
Summary 254
CHAPTER 12: USING THE CAMERA 255
Using the Camera Activity 255
Controlling the Camera with Your Own Activity 256
Claiming and Releasing a Camera 257
The Preview View 258
Controlling the Camera 261
Orientation 261
Zoom 263
Focus 264
ftoc.indd xxiftoc.indd xxi 5/10/2012 2:18:12 PM5/10/2012 2:18:12 PM
Book Title <Chapter No> V1 - MM/DD/2010
xxii
CONTENTS
Switching Cameras 264
Flash 264
Other Camera Parameters 265
Creating a Simple Barcode Reader 267
Understanding Barcodes 267
Parity and Implied First Digit 269
The Check Digit 270
Right Half of the Barcode 271
Autofocus 272
Using the Camera Preview Image and Detecting the Barcode 273
Debugging Image Processing Programs on Android 275
Detecting the Barcode 276
Summary 279
CHAPTER 13: IMAGE-PROCESSING TECHNIQUES 281
The Structure of Image-Processing Programs 281
The Image-Processing Pipeline 281
Common Image-Processing Operations 282
Image-to-Image Operations 282
Image-to-Object Operations 284
Jon’s Java Imaging Library (JJIL) 284
Image 285
PipelineStage 285
Sequence 288
Ladder 289
JJIL and Detecting the Android Logo 291
Choose the Right Image Size 293
Improving Reliability in Image Processing 296
Detecting Faces 299
Image-Processing Resources 300
Summary 301
CHAPTER 14: USING THE MICROPHONE 303
Introducing the Android Clapper 303
Using MediaRecorder to Analyze Maximum Amplitude 304
Recording Maximum Amplitude 305
Asynchronous Audio Recording 310
Implementing a Clapper 312
Analyzing Raw Audio 314
ftoc.indd xxiiftoc.indd xxii 5/10/2012 2:18:12 PM5/10/2012 2:18:12 PM
10 Book Title <Chapter No> V1 - MM/DD/2010
xxiii
CONTENTS
Setting Audio Input Parameters 315
Preparing AudioRecord 316
Recording Audio 317
Using OnRecordPositionUpdateListener 317
Using Loud Noise Detection 323
Using Consistent Frequency Detection 324
Estimating Frequency 325
Implementing the Singing Clapper 327
Summary 329
PART IV: SPEAKING TO ANDROID
CHAPTER 15: DESIGNING A SPEECH-ENABLED APP 333
Know Your Tools 334
User Interface Screen Flow 336
Voice Action Types 337
Voice User Interface (VUI) Design 338
Deciding Appropriate Tasks for Voice Actions 339
Designing What the App and Users Will Say 340
Constrain Speech Input to Increase Accuracy 340
Train Users to Know What They Can Say 340
Prompt the Users so They Know What to Say 341
Confi rm Success and Help Users Recover from Errors 342
Help Users Recover from Accidental Speech Activation 343
Teach Users Proper Speech Hygiene 344
Use Menus Cautiously 344
After the Design 345
Testing Your Design 346
Summary 347
References 347
CHAPTER 16: USING SPEECH RECOGNITION
AND TEXT-TO-SPEECH APIS 349
Text-To-Speech 349
Initialization 350
Initialization with Locale 351
Check TTS Data Action 361
Speaking 366
Speaking a Script 369
ftoc.indd xxiiiftoc.indd xxiii 5/10/2012 2:18:12 PM5/10/2012 2:18:12 PM
Book Title <Chapter No> V1 - MM/DD/2010
xxiv
CONTENTS
Speech Recognition 377
Initializing 377
Using the RecognizerIntent 382
The Speech Recording Process 384
Confi guring and Processing the Result 385
RecognizerIntent Use Cases 386
Implementation 391
Direct Speech Recognition Using SpeechRecognizer 403
Summary 405
CHAPTER 17: MATCHING WHAT WAS SAID 407
Parts of a Voice Command 407
Word Spotting 409
Indexing to Improve Word Spotting 411
Stemming 412
Phonetic Indexing 414
Matching Command Words in Persistent Storage 418
SQLite Full Text Search 418
Using the LIKE Operator 419
Using the FTS MATCH Operator 420
Implementing FTS 421
Word Searching with Lucene 426
Multi-part Commands 431
Ignoring Potential Collisions 432
Considering Ordering 434
Using a Grammar 438
Summary 438
CHAPTER 18: EXECUTING VOICE ACTIONS 441
Food Dialogue VUI Design 442
Defi ning and Executing Voice Actions 443
Executing VoiceActionCommands 448
Implementing an AlertDialog for VoiceActions 451
Implementing Multi-Turn Voice Actions 455
Implementing Multi-Turn AddFood 455
Implementing Multi-Turn RemoveFood 459
Making a Best Guess 461
Relaxing Strictness Between Commands 463
Making an Educated Guess 464
Responding When Recognition Fails 466
ftoc.indd xxivftoc.indd xxiv 5/10/2012 2:18:12 PM5/10/2012 2:18:12 PM
10 Book Title <Chapter No> V1 - MM/DD/2010
xxv
CONTENTS
Determining Not a Command 468
Determining Inaccurate Recognition 469
Not Understanding 469
Summary 469
CHAPTER 19: IMPLEMENTING SPEECH ACTIVATION 471
Implementing Speech Activation 472
Starting Speech Recognition 473
Implementing Speech Activation within an Activity 475
Activating Speech Recognition with Movement Detection 479
Activating Speech Recognition with the Microphone 481
Activating Speech Recognition with
Continuous Speech Recognition 483
Activating Speech Recognition with NFC 487
Implementing Persistent Speech Activation 488
Using a Service for Persistent Speech Activation 489
Summary 494
INDEX 495
ftoc.indd xxvftoc.indd xxv 5/10/2012 2:18:12 PM5/10/2012 2:18:12 PM
flast.indd xxviflast.indd xxvi 5/10/2012 2:17:55 PM5/10/2012 2:17:55 PM
INTRODUCTION
ANDROIDS ARE ALIVE. THEY CAN LOCATE THEMSELVES, see, listen, and understand speech. They can
sense radio signals and detect orientation, movement, and environmental properties. Can your com-
puter do all of that?
The availability of sensors is one feature Android devices have that makes them different from other
computers. Without sensors, an Android device is just an underpowered, mobile web browser with a
screen that is too small and has an awkward input mechanism.
Sensors also allow apps to do amazing things. For example, sensors can help save users from pain-
fully slow manual input and manipulation, and sensors can help users do tasks that they could never
do before. Because of this, it may be essential for an app to incorporate sensors to be successful.
Sensors will continue to be an important part of the Android platform. As the hardware specifi ca-
tions of Android devices improve, so do the number of available sensors and their quality. While
this happens, users will continue to expect apps to use any existing and new sensors when possible.
Therefore, using Android’s sensors is a crucial skill for any Android programmer to master. This
book gives you the knowledge and code you need to develop this skill and make great apps that use
sensors.
PROGRAMMING WITH ANDROID SENSORS
Writing apps that use Android’s sensors involves understanding the sensing capabilities of an
Android device, selecting which sensors to use in an app, and implementing an app that can acquire
sensor data and interpret it.
Android’s Sensing Capabilities
An Android device can have a wide variety of sensors. This book uses a defi nition of sensor that
incorporates many of an Android device’s capabilities. In this book a sensor is:
A capability that can capture measurements about the device and its external
environment.
Sensing capabilities are derived from the available hardware on Android devices and from creative
use of it. A capability may use values directly from hardware that can measure physical quantities,
such as the magnetic fi eld sensor. It may use hardware that the user typically interacts with, such as
the camera and microphone. A capability may even use a combination of hardware and server-based
processing, such as speech recognition. Whatever the source, the resulting data can inform an app
about the device’s state and the environment in which it resides.
flast.indd xxviiflast.indd xxvii 5/10/2012 2:17:56 PM5/10/2012 2:17:56 PM