Mobile Programming Dr. Mohsin Ali Memon
Bluetooth in Android Bluetooth is a way to send or receive data between two different devices. Android platform includes support for the Bluetooth framework that allows a device to wirelessly exchange data with other Bluetooth devices. Android provides Bluetooth API to perform these different operations. Scan for other Bluetooth devices Get a list of paired devices Connect to other devices through service discovery
BluetoothAdapter Class Android provides BluetoothAdapter class to communicate with Bluetooth. Create an object of this calling by calling the static method getDefaultAdapter(). private BluetoothAdapter BA; BA = BluetoothAdapter.getDefaultAdapter();
Calling Intent In order to enable the Bluetooth of your device, call the intent with the following Bluetooth constant ACTION_REQUEST_ENABLE. Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnOn, 0);
Calling other Bluetooth Intents Apart from this constant , there are other constants provided the API , that supports different tasks. ACTION_REQUEST_DISCOVERABLE This constant is used for turn on discovering of bluetooth ACTION_STATE_CHANGED This constant will notify that Bluetooth state has been changed ACTION_FOUND This constant is used for receiving information about each device that is discovered
List of Paired devices Once you enable the Bluetooth , you can get a list of paired devices by calling getBondedDevices() method. It returns a set of bluetooth devices. private Set<BluetoothDevice>pairedDevices; pairedDevices = BA.getBondedDevices();
Methods in Bluetooth API Apart form the pariredDevices , there are other methods in the API that gives more control over Blueetooth. enable() This method enables the adapter if not enabled isEnabled() This method returns true if adapter is enabled disable() This method disables the adapter
Methods in Bluetooth API cont’d getName() This method returns the name of the Bluetooth adapter setName(String name) This method changes the Bluetooth name getState() This method returns the current state of the Bluetooth Adapter. startDiscovery() This method starts the discovery process of the Bluetooth for 120 seconds.
Example This example provides demonstration of BluetoothAdapter class to manipulate Bluetooth and show list of paired devices by the Bluetooth. To experiment with this example , you need to run this on an actual device.
Main Activity
Main Activity cont’d
Main Activity cont’d
Layout
Layout cont’d
Layout cont’d
Manifest
Snapshots
Snapshots
Android Services Services in Android is a component that runs in the background without any user interaction or interface. Unlike Activities, services do not have any graphical interface. Services run invisibly performing long running tasks - doing Internet look ups, playing music, triggering notifications etc. Services run with a higher priority than inactive or invisible activities and therefore it is less likely that the Android system terminates them for resource management.
Services Template The only reason Android will stop a Service prematurely is to provide additional resources for a foreground component usually an Activity. When this happens, your Service can be configured to restart automatically.
Two forms of services Services takes Two Forms Started A Service is started when a application component such as an activity invokes it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed Add the service to AndroidManifest.xml
Two forms of services cont’d Bound If the activity wants to interact with the service directly, it can use the bindService() method to start the service. This method requires a ServiceConnection object as a parameter which is called on the service start and when finishing its onBind() method. This method returns a IBinder object to the ServiceConnection. This IBinder object can be used by the activity to communicate with the service.
Main Activity
Example Layout
Services class
Manifest
Snapshots
Snapshots cont’d
Android Fragments A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity which enable more modular activity design. A fragment has its own layout and its own behavior with its own lifecycle callbacks. You can add or remove fragments in an activity while the activity is running. You can combine multiple fragments in a single activity to build a multi-pane UI.
Fragments cont’d A fragment can be used in multiple activities. Fragment life cycle is closely related to the lifecycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped. Fragment are more flexible and removed the limitation of having a single activity on the screen at a time
Tablet and Handset Fragment
Fragment Lifecycle
Fragment Lifecycle cont’d
How to use Fragments First of all decide how many fragments you want to use in an activity. Next based on number of fragments, create classes which will extend the Fragment class. The Fragment class has above mentioned callback functions. You can override any of the functions based on your requirements. Corresponding to each fragment, you will need to create layout files. Finally modify activity file to define the actual logic of replacing fragments based on your requirement.
Methods to Override onCreate() The system calls this when creating the fragment. You should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed. onCreateView() It is called when it's time for the fragment to draw user interface for the first time. onPause() The system calls this method as the first indication that the user is leaving the fragment. This is usually where you should commit any changes that should be persisted beyond the current user session.
Managing Fragments To manage the fragments in your activity, you need to use FragmentManager. To get it, call getFragmentManager() from your activity. With fragments you can Get fragments that exist in the activity, with findFragmentById() (for fragments that provide a UI in the activity layout) or findFragmentByTag() (for fragments that do or don't provide a UI). Pop fragments off the back stack, with popBackStack() (simulating a Back command by the user). Register a listener for changes to the back stack, with addOnBackStackChangedListener().
Performing Fragment Transactions A great feature about using fragments in your activity is the ability to add, remove, replace, and perform other actions with them, in response to user interaction. Each set of changes that you commit to the activity is called a transaction and you can perform one using APIs in FragmentTransaction. You can also save each transaction to a back stack managed by the activity, allowing the user to navigate backward through the fragment changes (similar to navigating backward through activities).
Performing Fragment Transactions Each transaction is a set of changes that you want to perform at the same time. You can set up all the changes you want to perform for a given transaction using methods such as add(), remove(), and replace(). Then, to apply the transaction to the activity, you must call commit(). Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. The back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.
Example
Fragment classes
Fragment Layouts
Main Activity Layout
String.xml