Android accessories session 2


Published on

Published in: Education, Technology, Business
  • Be the first to comment

  • Be the first to like this

Android accessories session 2

  1. 1. Android Accessories Sathish. R
  2. 2. 13-Nov-13    Roadmap Bluetooth Wi-Fi Sensors       Motion Sensors Position Sensors Environment Sensors Send/ Receive SMS Shared Preference Hands-on Android Accessories Session 3 Tenet Technetronics
  3. 3. 13-Nov-13 Bluetooth o o o Which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. Bluetooth APIs let applications wirelessly connect to other Bluetooth devices. Tenet Technetronics
  4. 4. 13-Nov-13 Bluetooth Cont. o o o o o Scan for other Bluetooth devices Query the local Bluetooth adapter for paired Bluetooth devices Establish RFCOMM channels Connect to other devices through service discovery Transfer data to and from other devices Tenet Technetronics
  5. 5. 13-Nov-13 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. Tenet Technetronics
  6. 6. 13-Nov-13 Bluetooth Cont. The classes and interfaces you will need to create Bluetooth connections:     Bluetooth Adapter. Bluetooth Device. Bluetooth Socket. Bluetooth Server Socket. Tenet Technetronics
  7. 7. 13-Nov-13 Bluetooth Permissions. o o We must declare the Bluetooth permission BLUETOOTH. To initiate device discovery or manipulate Bluetooth settings, we must also declare the BLUETOOTH_ADMIN permission. Tenet Technetronics
  8. 8. 13-Nov-13 Setting Up Bluetooth Verify that Bluetooth is supported on the device. If Bluetooth is not supported, then disable any Bluetooth features. o o Get the BluetoothAdapter. Enable Bluetooth. Tenet Technetronics
  9. 9. 13-Nov-13 Setting Up Bluetooth Get Bluetooth Adapter: The BluetoothAdapter is required for any and all Bluetooth activity. Tenet Technetronics
  10. 10. 13-Nov-13 Setting Up Bluetooth Enable Bluetooth. o o Ensure that Bluetooth is enabled. Call isEnabled() to check whether Bluetooth is currently enable. If this method returns false, then Bluetooth is disabled. Tenet Technetronics
  11. 11. 13-Nov-13 Finding Devices o o o Find remote Bluetooth devices either through device discovery or by querying the list of paired (bonded) devices. Device discovery is a scanning procedure that searches the local area for Bluetooth enabled devices. A Bluetooth device within the local area will respond to a discovery request only if it is currently enabled to be discoverable Tenet Technetronics
  12. 12. 13-Nov-13 Querying paired devices o o Before performing device discovery, its worth querying the set of paired devices. call getBondedDevices(). Tenet Technetronics
  13. 13. 13-Nov-13 Discovering devices o o o simply call startDiscovery(). The method will immediately return with a Boolean indicating whether discovery has successfully started. Involves an inquiry scan of about 12 seconds, followed by a page scan of each found device to retrieve its Bluetooth name. Tenet Technetronics
  14. 14. 13-Nov-13 Discovering devices o o Register a Broadcast Receiver for the ACTION_FOUND Intent in order to receive information about each device discovered. This Intent carries the extra fields EXTRA_DEVICE and EXTRA_CLASS, containing a BluetoothDevice and a BluetoothClass. Tenet Technetronics
  15. 15. 13-Nov-13 Discovering devices Tenet Technetronics
  16. 16. 13-Nov-13 Connecting Devices o o To create a connection between two devices, implement both the server-side and client-side mechanisms. 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. Tenet Technetronics
  17. 17. 13-Nov-13 Connecting as a server o o One must act as a server by holding an open BluetoothServerSocket. 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(). Tenet Technetronics
  18. 18. 13-Nov-13 Tenet Technetronics
  19. 19. 13-Nov-13 Connecting as a client 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 o Tenet Technetronics
  20. 20. 13-Nov-13 Connecting as a client o o o Initiate the connection by calling connect(). We should always ensure that the device is not performing device discovery when you call connect(). If discovery is in progress, then the connection attempt will be significantly slowed and is more likely to fail. Tenet Technetronics
  21. 21. 13-Nov-13 Tenet Technetronics
  22. 22. 13-Nov-13 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. Tenet Technetronics
  23. 23. 13-Nov-13 Wi-Fi o o The Primary API for managing all aspects of Wi-Fi connectivity. It deals with several categories of items:    The list of configured networks. The currently active Wi-Fi network. Results of access point scans. Tenet Technetronics
  24. 24. 13-Nov-13 Turn On/Off Wi-Fi o o Need to use WiFiManager class. Create an Object of WiFiManager class to get the services of Wi-Fi. Tenet Technetronics
  25. 25. 13-Nov-13 Permission in Manifest o When performing Wi-Fi specific operations, we must declare permissions in manifest file Tenet Technetronics
  26. 26. 13-Nov-13 Sensors o o Most Android-powered devices have built-in sensors that measure motion, orientation, and various environmental conditions. These sensors are capable of providing raw data with high precision and accuracy, and are useful Tenet Technetronics
  27. 27. 13-Nov-13 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. Tenet Technetronics
  28. 28. 13-Nov-13 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. Tenet Technetronics
  29. 29. 13-Nov-13 Identifying Sensors and Sensor Capabilities o o The Android sensor framework provides several methods to determine at runtime which sensors are on a device. To identify the sensors that are on a device you first need to get a reference to the sensor service. Tenet Technetronics
  30. 30. 13-Nov-13 Access Sensors: o We can get a listing of every sensor on a device by calling the getSensorList() method Tenet Technetronics
  31. 31. 13-Nov-13 Monitoring Sensor Events To monitor raw sensor data you need to implement two callback methods of SensorEventListener interface.  onAccuracyChanged() and  onSensorChanged(). Tenet Technetronics
  32. 32. 13-Nov-13 Tenet Technetronics
  33. 33. 33 13 November 2013 Send SMS o Add permission in manifest.xml o To send sms we need sms manager. Tenet Technetronics
  34. 34. 34 13 November 2013 Send SMS The sendTextMessages() method takes five arguments: o destinationAddress o scAddress o Content of the SMS message o sentIntent o deliveryIntent Tenet Technetronics
  35. 35. 35 13 November 2013 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 Tenet Technetronics
  36. 36. 36 13 November 2013 Receive SMS 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(); } } }}; Tenet Technetronics
  37. 37. 37 13 November 2013 Shared Preferences o o o o o Android provides several options to save persistent application data Store private primitive data in key-value pairs. Application shared preferences allows you to save and retrieve key, value pair data. Boolean, float, int, long and string. These data will persist even app is killed Tenet Technetronics
  38. 38. 38 13 November 2013 Shared Preferences o o Application shared preferences can be fetched using getSharedPreferences() method. We need an editor to edit and save the changes in shared preferences. Tenet Technetronics
  39. 39. 39 13 November 2013 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. Tenet Technetronics
  40. 40. 40 13 November 2013 Shared Preferences Retrieving Data o Data can be retrieved from saved preferences by calling getString() (For string) method. Tenet Technetronics
  41. 41. 41 13 November 2013 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(). Tenet Technetronics
  42. 42. 13-Nov-13 Tenet Technetronics
  43. 43. 13-Nov-13 Tenet Technetronics