Download - Android accessories session 2
Android
Accessories
Sathish. R
Roadmap
Bluetooth
Wi-Fi
Sensors Motion Sensors Position Sensors
Environment Sensors
Send/ Receive SMS
Shared Preference
Hands-on
Android
Accessories
Session 3
13-Nov-13
Tenet Technetronics
Bluetooth o Which allows a device to wirelessly exchange
data with other Bluetooth devices.
o The application framework provides access
to the Bluetooth functionality through the
Android Bluetooth APIs.
o Bluetooth APIs let applications wirelessly
connect to other Bluetooth devices.
13-Nov-13
Tenet Technetronics
Bluetooth Cont. o Scan for other Bluetooth devices
o Query the local Bluetooth adapter for paired Bluetooth devices
o Establish RFCOMM channels
o Connect to other devices through service discovery
o Transfer data to and from other devices
13-Nov-13
Tenet Technetronics
Bluetooth Cont. All of the Bluetooth APIs are available in the android. Bluetooth package.
The four major tasks necessary to communicate using Bluetooth.
setting up Bluetooth, finding devices that are either paired or available in
the local area, connecting devices &
transferring data between devices.
13-Nov-13
Tenet Technetronics
Bluetooth Cont.
The classes and interfaces you will need to
create Bluetooth connections:
Bluetooth Adapter.
Bluetooth Device.
Bluetooth Socket.
Bluetooth Server Socket.
13-Nov-13
Tenet Technetronics
Bluetooth Permissions.
o We must declare the Bluetooth
permission BLUETOOTH.
o To initiate device discovery or manipulate
Bluetooth settings, we must also declare
the BLUETOOTH_ADMIN permission.
13-Nov-13
Tenet Technetronics
Setting Up Bluetooth
Verify that Bluetooth is supported on the
device.
If Bluetooth is not supported, then disable
any Bluetooth features.
o Get the BluetoothAdapter.
o Enable Bluetooth.
13-Nov-13
Tenet Technetronics
Setting Up Bluetooth
Get Bluetooth Adapter:
The BluetoothAdapter is required for any
and all Bluetooth activity.
13-Nov-13
Tenet Technetronics
Setting Up Bluetooth Enable Bluetooth.
o Ensure that Bluetooth is enabled.
o Call isEnabled() to check whether
Bluetooth is currently enable. If this
method returns false, then Bluetooth is
disabled.
13-Nov-13
Tenet Technetronics
Finding Devices o Find remote Bluetooth devices either through device
discovery or by querying the list of paired (bonded) devices.
o Device discovery is a scanning procedure that searches the local area for Bluetooth enabled devices.
o A Bluetooth device within the local area will respond to a discovery request only if it is currently enabled to be discoverable
13-Nov-13
Tenet Technetronics
Querying paired devices
o Before performing device discovery, its
worth querying the set of paired devices.
o call getBondedDevices().
13-Nov-13
Tenet Technetronics
Discovering devices o simply call startDiscovery().
o The method will immediately return with a
Boolean indicating whether discovery has
successfully started.
o Involves an inquiry scan of about 12 seconds,
followed by a page scan of each found
device to retrieve its Bluetooth name.
13-Nov-13
Tenet Technetronics
Discovering devices o Register a Broadcast Receiver for
the ACTION_FOUND Intent in order to receive
information about each device discovered.
o This Intent carries the extra
fields EXTRA_DEVICE and EXTRA_CLASS,
containing a BluetoothDevice and a
BluetoothClass.
13-Nov-13
Tenet Technetronics
Discovering devices
13-Nov-13
Tenet Technetronics
Connecting Devices o To create a connection between two
devices, implement both the server-side and client-side mechanisms.
o If the two devices have not been previously paired, then the Android framework will automatically show a pairing request notification or dialog to the user during the connection procedure.
13-Nov-13
Tenet Technetronics
Connecting as a server o One must act as a server by holding an
open BluetoothServerSocket.
o The server socket is to listen for incoming connection requests.
Get a BluetoothServerSocket by calling the listenUsingRfcommWithServiceRecord(String, UUID).
Start listening for connection requests by calling accept().
Unless you want to accept additional connections, call close().
13-Nov-13
Tenet Technetronics
13-Nov-13
Tenet Technetronics
Connecting as a client o To initiate a connection with a remote
device , obtain the remote device.
o Using the Bluetooth Device, get a BluetoothSocket by calling createRfcommSocketToServiceRecord (UUID).
Note : The UUID passed here must match the UUID used by the server device when it opened its BluetoothServerSocket
13-Nov-13
Tenet Technetronics
Connecting as a client o Initiate the connection by calling connect().
o We should always ensure that the device is
not performing device discovery when you
call connect().
o If discovery is in progress, then the
connection attempt will be significantly
slowed and is more likely to fail.
13-Nov-13
Tenet Technetronics
13-Nov-13
Tenet Technetronics
Managing a Connection
Once Both Devices are connected
Get the InputStream and OutputStream that
handle transmissions through the socket,
Read and write data to the streams.
13-Nov-13
Tenet Technetronics
Wi-Fi
o The Primary API for managing all aspects
of Wi-Fi connectivity.
o It deals with several categories of items:
The list of configured networks.
The currently active Wi-Fi network.
Results of access point scans.
13-Nov-13
Tenet Technetronics
Turn On/Off Wi-Fi
o Need to use WiFiManager class.
o Create an Object of WiFiManager class to
get the services of Wi-Fi.
13-Nov-13
Tenet Technetronics
Permission in Manifest
o When performing Wi-Fi specific
operations, we must declare permissions
in manifest file
13-Nov-13
Tenet Technetronics
Sensors o Most Android-powered devices
have built-in sensors that measure motion, orientation, and various environmental conditions.
o These sensors are capable of providing raw data with high precision and accuracy, and are useful
13-Nov-13
Tenet Technetronics
Sensors The Android platform supports three broad categories of sensors:
Motion sensors (accelerometers, gravity sensors, gyroscopes)
Environmental sensors (pressure, illumination, and humidity)
Position sensors ( orientation sensors and magnetometers)
We can access sensors available on the device and acquire raw
sensor data by using the Android sensor framework.
13-Nov-13
Tenet Technetronics
Sensors We can use the sensor framework to do the following:
Determine which sensors are available on a device.
Determine an individual sensor's capabilities, such as its maximum range, manufacturer, power requirements, and resolution.
Acquire raw sensor data and define the minimum rate at which you acquire sensor data.
Register and unregister sensor event listeners that monitor sensor changes.
13-Nov-13
Tenet Technetronics
Identifying Sensors and Sensor
Capabilities
o The Android sensor framework provides
several methods to determine at runtime
which sensors are on a device.
o To identify the sensors that are on a
device you first need to get a reference
to the sensor service.
13-Nov-13
Tenet Technetronics
Access Sensors:
o We can get a listing of every sensor on a
device by calling
the getSensorList() method
13-Nov-13
Tenet Technetronics
Monitoring Sensor Events
To monitor raw sensor data you need to
implement two callback methods of
SensorEventListener interface.
onAccuracyChanged() and
onSensorChanged().
13-Nov-13
Tenet Technetronics
13-Nov-13
Tenet Technetronics
Send SMS
o Add permission in manifest.xml
o To send sms we need sms manager.
13 November 2013
Tenet Technetronics
33
Send SMS The sendTextMessages() method takes five arguments:
o destinationAddress
o scAddress
o Content of the SMS message
o sentIntent
o deliveryIntent
13 November 2013
Tenet Technetronics
34
Receive SMS o Declare Broadcast receiver in Android
Manifest.xml
o Give read SMS permission in AndroidManifest
o Get the object of SmsManager to find out received sms details
13 November 2013
Tenet Technetronics
35
Receive SMS
13 November 2013
Tenet Technetronics
36
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context c, Intent in) {
if(in.getAction().equals(RECEIVED_ACTION)) {
Bundle bundle = in.getExtras();
if(bundle!=null) {
Object[] pdus = (Object[])bundle.get(“pdus”);
SmsMessage[] msgs = new SmsMessage[pdus.length];
for(int i = 0; i<pdus.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
// reply();
}
}
}};
Shared Preferences o Android provides several options to save
persistent application data
o Store private primitive data in key-value pairs.
o Application shared preferences allows you to save and retrieve key, value pair data.
o Boolean, float, int, long and string.
o These data will persist even app is killed
13 November 2013
Tenet Technetronics
37
Shared Preferences
o Application shared preferences can be
fetched using getSharedPreferences()
method.
o We need an editor to edit and save the
changes in shared preferences.
13 November 2013
Tenet Technetronics
38
Shared Preferences Storing Data:
o You can save data into shared
preferences using editor.
o Call editor. Commit() in order to save
changes to shared preferences.
13 November 2013
Tenet Technetronics
39
Shared Preferences
Retrieving Data
o Data can be retrieved from saved
preferences by calling getString() (For
string) method.
13 November 2013
Tenet Technetronics
40
Shared Preferences Clearing / Deleting Data
o To delete that particular value, call
remove(“key_name”).
o If we want to delete all the data,
call clear().
13 November 2013
Tenet Technetronics
41
13-Nov-13
Tenet Technetronics
13-Nov-13
Tenet Technetronics