Bluetooth on Android

Transcription

Bluetooth on Android
17066 CBA
Developing Android™ and iPhone®
Applications to Control
®
Bluetooth Accessories
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
1
Objectives
By the end of the class you will be
able to:
 Find all the tools needed to
develop Android™ and iOS™
apps
 Know which programming
languages to use
 Understand how the OS
supports Bluetooth®
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
2
Agenda


Introduction
Development Environments


Languages



Eclipse™ and Xcode®
Java®, Objective-C®, XML
Bluetooth®
Summary
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
3
Agenda


Introduction
Development Environments


Languages



Eclipse and Xcode
Java, Objective-C, XML
Bluetooth®
Summary
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
4
Introduction



RN-42 module makes embedded
code easy
PC, Tablet, or Phone app is more
difficult for embedded designers
Android™ & iOS dominate

Smartphone share

Tablet share
Android 57%
 iOS 40%
Android 75%
 iOS 17%


Source: IDC’s Worldwide Mobile Phone Tracker® and Worldwide Tablet Tracker®, May 1, 2013
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
5
Market Share
iOS
17%
Android - 75%
iOS - 17%
Windows - 3%
BlackBerry - 3%
Linux - 1%
Symbian - 1%
Android
75%
Others - 0%
Source: IDC’s Worldwide Mobile Phone Tracker®
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
6
Agenda


Introduction
Development Environments


Languages



Eclipse and Xcode
Java, Objective-C, XML
Bluetooth®
Summary
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
7
Development Environments

Android developers use Eclipse


Can use NetBeans™ or MPLAB® X
IDE
Apple® developers use Xcode
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
8
Eclipse

Widely used


Runs on many platforms


To develop Android, BlackBerry®,
Windows®, Linux® applications
Windows, Linux, OS X®
Open


Uses many different components
Many plug-ins available
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
9
Eclipse

Complicated install procedure






Java Development Kit (JDK)
Eclipse IDE
ADT plug-in for Eclipse
Android SDK
Build Targets for SDK
USB Driver
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
10
Eclipse



Tutorials on Android website
No registration or license fee
required to develop
Register for a Google Play™
publisher account


To publish apps on Play Store
Can distribute APK (install) files
without publishing
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
11
Getting Started
http://developer.android.com/training/index.html
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
12
Demo 1:
Creating a new Android project
in Eclipse
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
13
New Android™ Project
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
14
Xcode

Single development environment



iOS (iPhone, iPad, iPod)
OS X (Mac, MacBook)
Free download from Apple
https://developer.apple.com

Runs on a Mac

Will not run on Windows or Linux
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
15
Xcode

Single download




IDE, Compiler, Simulator, Examples
Tutorials on Apple website
No license fee to develop on the
simulator
License required to run on a real
device and publish your app

Every device must be provisioned
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
16
Getting Started
https://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/chapters/Introduction.html
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
17
Demo 2:
Creating a new iOS project
in Xcode
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
18
New iOS Project
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
19
Agenda


Introduction
Development Environments


Languages



Eclipse and Xcode
Java, Objective-C, XML
Bluetooth®
Summary
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
20
Programming Languages

10 types of languages*



Java, Objective-C are procedural


Procedural
Declarative
Describe procedures to be followed
XML, HTML are declarative

Declare what you want done
* For people who understand binary!
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
21
Programming Languages

Two types of languages



Objective-C, C, C++ are compiled


Compiled
Interpreted
Compile source to executable
Java is compiled and interpreted


Compile source to bytecode
Interpreter (like JRE) runs bytecode
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
22
Programming Languages

Objective-C is used for iOS



Java is used for Android™



Created in early 1980’s
Pass messages to objects
Created in early 1990’s
Call methods for objects
XML used for Android and iOS

To describe views (screen layout)
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
23
Java Language

Object-oriented


Classes and Objects
All source code is in .java files
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
24
Java Code (.java)
package com.microchip.example;
public class Die {
Instance Variable
private byte spots;
public Die() {
Constructor
spots = 1 + (byte)(Math.random() * 6);
}
Method
public byte Roll() {
spots = 1 + (byte)(Math.random() * 6);
return spots;
}
public byte View() {
return spots;
private Die redDie;
redDie = new Die();
private byte myNumber = 0;
myNumber = redDie.Roll();
}
}
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
25
Objective-C

Object-oriented


Classes and Objects
Interfaces and implementations


Interface described in .h header file
Implementation in .m source file
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
26
Interface Code (.h)
@interface Die : NSObject {
int spots;
}
-(id)init;
-(int)Roll;
-(int)View;
Instance Variable
Initializer
Method
@end
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
27
Implementation Code (.m)
@implementation Die
-(id)init {
self = [super init];
if (self)
}
Die *redDie;
redDie = [[Die
spots = arc4random_uniform(6)
+ 1; alloc] init];
int myNumber = 0;
return self;
myNumber = [redDie Roll];
-(int)Roll {
spots = arc4random_uniform(6) + 1;
return spots;
Die *redDie;
redDie = Die.alloc.init;
}
int myNumber = 0;
-(int)View { return spots; }
myNumber = redDie.Roll;
@end
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
28
XML Programming

XML used to declare the layout of
the display



Buttons, Text, Pictures, Sliders, etc.
Called a XIB file or Storyboard in
Xcode
Invoked when view is loaded
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
29
Demo 3:
Using XML and XIB Layout Files
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
30
Help is Available
Three really good sites:
http://stackoverflow.com
http://developer.apple.com
http://developer.android.com
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
31
Agenda


Introduction
Development Environments


Languages



Eclipse and Xcode
Java, Objective-C, XML
Bluetooth®
Summary
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
32
Android™

BluetoothAdapter class


®
Bluetooth
Bluetooth radio in the Android device
BluetoothDevice class


Remote Bluetooth device
RN-42 in our case
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
33
RN-42 Demo App

Create a Handler for messages
private Handler handler = new Handler() {

Create a BluetoothManager
BluetoothManager btm;
btm = new BluetoothManager(handler, BLUETOOTH_MANAGER);

BluetoothManager enables the
BluetoothAdapter
private BluetoothAdapter mBluetoothAdapter = null;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

Start the Discovery process
btm.startDiscovery();
mBluetoothAdapter.startDiscovery();
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
34
RN-42 Demo App

BluetoothManager creates
BluetoothManagerMessage

Includes MessageType and BluetoothDevice
public enum MessageType {
DISCOVERY_COMPLETE,
READ,
Message of MessageType
ERROR,
DEVICE_FOUND results in
DEVICE_FOUND,
BluetoothDevice object added
CONNECTED,
to a ListView being displayed
READY
};
public BluetoothDevice device = null;
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
35
RN-42 Demo App
 BluetoothManager asked
btm.connect(currentDevice);

to connect
BluetoothManager starts a thread to
handle the connection
thread = new ConnectThread(device);
thread.start();

Thread opens a socket to the device
mmSocket =
mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
Discovered, Selected, Paired, and Connected !
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
36
Demo 4:
Android™ Bluetooth® App
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
37
iOS


®
Bluetooth
Developing accessories for
iPod®, iPhone® and iPad® using
one of the controlled interfaces*
requires a MFi license from Apple
For more information:
http://developer.apple.com/mfi
* iAP is a controlled interface
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
38
RN-42-APL Demo App

EAAccessory class


Bluetooth® or USB accessory
connected to iPhone/iPad
EAAccessoryManager class


Manages communication with
external accessory
RN42-APL in our case
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
39
RN-42-APL Demo App

Go to Settings, select Bluetooth®



Find Bluetooth device by name
Click to pair and connect
Find, Pair & Connect is done in
the settings, not in the App

App will not see the accessory unless
connected
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
40
RN-42-APL Demo App

Create an external accessory object
MFiAccessory *btDie;
btDie = [[MFiAccessory alloc] initWithProtocol:
@"com.microchip.btdemo"];

Get a list of connected accessories
NSArray *accessories = [[EAAccessoryManager
sharedAccessoryManager] connectedAccessories];

Find accessory and open a session
EAAccessory *accessory = nil;
EASession *session = nil;
session = [[EASession alloc] initWithAccessory:accessory
forProtocol:protocolString];
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
41
Demo 5:
iPhone Bluetooth® App
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
42
Development Tools
RN-41-EK & RN-42-EK
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
43
Development Tools
RN41APL-EVAL & RN42APL-EVAL

Requires MFi license to purchase
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
44
Agenda


Introduction
Development Environments


Languages



Eclipse and Xcode
Java, Objective-C, XML
Bluetooth®
Summary
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
45
What did we learn?

Xcode and Objective-C


For iOS Apps
Eclipse and Java
For Android™ Apps
Classes that access Bluetooth®
accessories in iOS and Android


© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
46
The End
Thank You
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
47
LEGAL NOTICE
SOFTWARE:
You may use Microchip software exclusively with Microchip products. Further, use of Microchip software is subject to the copyright notices, disclaimers, and any license
terms accompanying such software, whether set forth at the install of each program or posted in a header or text file.
Notwithstanding the above, certain components of software offered by Microchip and 3 rd parties may be covered by “open source” software licenses – which include
licenses that require that the distributor make the software available in source code format. To the extent required by such open source software licenses, the terms of
such license will govern.
NOTICE & DISCLAIMER:
These materials and accompanying information (including, for example, any software, and references to 3 rd party companies and 3rd party websites) are for informational
purposes only and provided “AS IS.” Microchip assumes no responsibility for statements made by 3 rd party companies, or materials or information that such 3 rd parties
may provide.
MICROCHIP DISCLAIMS ALL WARRANTIES, WHETHER EXPRESS, IMPLIED, OR STATUTORY, INCLUDING ANY IMPLIED WARRANTIES OF
NONINFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY DIRECT OR
INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL, OR CONSEQUENTIAL LOSS, DAMAGE, COST, OR EXPENSE OF ANY KIND RELATED TO THESE MATERIALS OR
ACCOMPANYING INFORMATION PROVIDED TO YOU BY MICROCHIP OR OTHER THIRD PARTIES, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
POSSIBLITY OF SUCH DAMAGES OR THE DAMAGES ARE FORESEEABLE.
TRADEMARKS:
The Microchip name and logo, the Microchip logo, dsPIC, FlashFlex, K EELOQ, KEELOQ logo, MPLAB, PIC, PICmicro, PICSTART, PIC32 logo, rfPIC, SST, SST Logo,
SuperFlash and UNI/O are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.
FilterLab, Hampshire, HI-TECH C, Linear Active Thermistor, MTP, SEEVAL and The Embedded Control Solutions Company are registered trademarks of Microchip
Technology Incorporated in the U.S.A.
Silicon Storage Technology is a registered trademark of Microchip Technology Inc. in other countries.
Analog-for-the-Digital Age, Application Maestro, BodyCom, chipKIT, chipKIT logo, CodeGuard, dsPICDEM, dsPICDEM.net, dsPICworks, dsSPEAK, ECAN,
ECONOMONITOR, FanSense, HI-TIDE, In-Circuit Serial Programming, ICSP, Mindi, MiWi, MPASM, MPF, MPLAB Certified logo, MPLIB, MPLINK, mTouch, Omniscient
Code Generation, PICC, PICC-18, PICDEM, PICDEM.net, PICkit, PICtail, REAL ICE, rfLAB, Select Mode, SQI, Serial Quad I/O, Total Endurance, TSHARC,
UniWinDriver, WiperLock, ZENA and Z-Scale are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
GestIC and ULPP are registered trademarks of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of Microchip Technology Inc., in other countries.
All other trademarks mentioned herein are property of their respective companies.
© 2013 Microchip Technology Incorporated. All Rights Reserved.
17066 CBA
Slide
48