Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS216 MOBILE PROGRAMMING Android, Chapter 13 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (

Similar presentations


Presentation on theme: "CSS216 MOBILE PROGRAMMING Android, Chapter 13 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov ("— Presentation transcript:

1 CSS216 MOBILE PROGRAMMING Android, Chapter 13 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov ( andrey.bogdanchikov@sdu.edu.kz )

2 Outline - Bluetooth, Networks, and Wi-Fi Managing Bluetooth devices Discovering remote Bluetooth devices Managing discovery mode Communicating over Bluetooth Monitoring Internet connectivity Obeying user preferences for background data transfer Monitoring Wi-Fi and network details Configuring networks and Wi-Fi configurations Scanning for Wi-Fi access points

3 Introduction In this chapter you’ll continue to explore Android’s low-level communications APIs by examining the Bluetooth, network, and Wi-Fi packages. Android offers APIs to manage and monitor your Bluetooth device settings, to control discoverability, to discover nearby Bluetooth devices, and to use Bluetooth as a proximity- based peer-to-peer transport layer for your applications. A full network and Wi-Fi package is also available. Using these APIs you can scan for hotspots, create and modify Wi- Fi configuration settings, monitor your Internet connectivity, and control and monitor Internet settings and preferences.

4 Using Bluetooth In this section you’ll learn how to interact with the local Bluetooth device and communicate with remote devices on nearby phones. Using Bluetooth you can search for, and connect to, other devices within range. By initiating a communications link using Bluetooth Sockets you can then transmit and receive streams of data between devices from within your applications.

5 Bluetooth is a communications protocol designed for short- range, low-bandwidth peer-to-peer communications. As of Android 2.1, only encrypted communication is supported, meaning you can only form connections between paired devices. In Android, Bluetooth devices and connections are handled by the following classes: ➤ BluetoothAdapter The Bluetooth Adapter represents the local Bluetooth device—that is, the Android device on which your application is running. ➤ BluetoothDevice Each remote device with which you wish to communicate is represented as a BluetoothDevice. ➤ BluetoothSocket Call createRfcommSocketToServiceRecord on a remote Bluetooth Device object to create a Bluetooth Socket that will let you make a connection request to the remote device, and then initiate communications. ➤ BluetoothServerSocket By creating a Bluetooth Server Socket (using the listenUsingRfcommWithServiceRecord method) on your local Bluetooth Adapter, you can listen for incoming connection requests from Bluetooth Sockets on remote devices.

6 Accessing the Local Bluetooth Device Adapter The local Bluetooth device is controlled via the BluetoothAdapter class. To access the default Bluetooth adapter on the host device call getDefaultAdapter. It is possible that some Android devices will feature multiple Bluetooth adapters, though it is currently only possible to access the default device. BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); To read any of the local Bluetooth Adapter properties, initiate discovery, or find bonded devices you will need to include the BLUETOOTH manifest permission. In order to modify any of the local device properties the BLUETOOTH_ADMIN uses-permission is also required.

7 Managing Bluetooth Properties and State The Bluetooth Adapter offers methods for reading and setting properties of the local Bluetooth hardware. The Bluetooth Adapter properties can be read and changed only if the Bluetooth adapter is currently turned on (that is, if its device state is enabled). If the device is off, these methods will return null. If the Bluetooth Adapter is turned on, and you have included the BLUETOOTH permission in your manifest, you can access the Bluetooth Adapter’s friendly name (an arbitrary string that users can set and then use to identify a particular device) and hardware address.

8 Reading Bluetooth Adapter properties BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); String toastText; if (bluetooth.isEnabled()) { String address = bluetooth.getAddress(); String name = bluetooth.getName(); toastText = name + " : " + address; }else toastText = "Bluetooth is not enabled"; Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();

9 States If you also have the BLUETOOTH_ADMIN permission you can change the friendly name of the Bluetooth Adapter using the setName method:bluetooth.setName("Blackfang"); To find a more detailed description of the current Bluetooth Adapter state, use the getState method, which will return one of the following BluetoothAdapter constants: ➤ STATE_TURNING_ON ➤ STATE_ON ➤ STATE_TURNING_OFF ➤ STATE_OFF

10 Enable Bluetooth By default the Bluetooth adapter will be turned off. In order to conserve battery life and optimize security, most users will keep Bluetooth disabled unless it’s in use. To enable the Bluetooth Adapter you can start a system sub- Activity using the ACTION_REQUEST_ENABLE Bluetooth Adapter static constant as a startActivityForResult action string: String enableBT = BluetoothAdapter.ACTION_REQUEST_ENABLE; startActivityForResult(new Intent(enableBT), 0);

11 It is also possible to turn the Bluetooth Adapter on and off directly, using the enable and disable methods, if you include the BLUETOOTH_ADMIN permission in your manifest. Note that this should be done only when absolutely necessary and that the user should always be notified if you are manually changing the Bluetooth Adapter status on the user’s behalf. In most cases you should use the Intent mechanism described earlier.

12 Being Discoverable and Remote Device Discovery The process of two devices finding each other in order to connect is called discovery. Before you can establish a Bluetooth Socket for communications, the local Bluetooth Adapter must bond with the remote device. Before two devices can bond and connect, they first need to discover each other.

13 Managing Device Discoverability The Bluetooth Adapter’s discoverability is indicated by its scan mode. You can find the adapter’s scan mode by calling getScanMode on the BluetoothAdapter object. It will return one of the following BluetoothAdapter constants:. ➤ SCAN_MODE_CONNECTABLE_DISCOVERABLE Inquiry scan and page scan are both enabled, meaning that the device is discoverable from any Bluetooth device performing a discovery scan. ➤ SCAN_MODE_CONNECTABLE Page Scan is enabled but inquiry scan is not. This means that devices that have previously connected and bonded to the local device can find it during discovery, but new devices can’t. ➤ SCAN_MODE_NONE Discoverability is turned off. No remote devices can find the local adapter during discovery.

14 Discoverability For privacy reasons, Android devices will default to having discoverability disabled. To turn on discovery you had to obtain explicit permission from the user; you do this by starting a new Activity using the ACTION_REQUEST_DISCOVERABLE action: String aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE; startActivityForResult(new Intent(aDiscoverable), DISCOVERY_REQUEST); By default discoverability will be enabled for two minutes. You can modify this setting by adding an EXTRA_DISCOVERABLE_DURATION extra to the launch Intent, specifying the number of seconds you want discoverability to last.

15 Bluetooth Communications The Bluetooth communications APIs are wrappers around RFCOMM, the Bluetooth radio frequency communications protocol. RFCOMM supports RS232 serial communication over the Logical Link Control and Adaptation Protocol (L2CAP) layer. In practice, this alphabet soup provides a mechanism for opening communication sockets between two paired Bluetooth devices.

16 Connect You can establish an RFCOMM communication channel for bidirectional communications using the following classes. ➤ BluetoothServerSocket Used to establish a listening socket for initiating a link between devices. To establish a handshake, one device acts as a server to listen for, and accept, incoming connection requests. ➤ BluetoothSocket Used in creating a new client socket to connect to a listening Bluetooth Server Socket, and returned by the Server Socket once a connection is established. Once the connection is made, Bluetooth Sockets are used on both the server and client sides to transfer data streams.

17 Listen for connections final BluetoothServerSocket btserver = bluetooth.listenUsingRfcommWithServiceRecord(name, uuid); Thread acceptThread = new Thread(new Runnable() { public void run() { try { // Block until client connection established. BluetoothSocket serverSocket = btserver.accept(); // TODO Transfer data using the server socket } catch (IOException e) { Log.d("BLUETOOTH", e.getMessage()); }}});acceptThread.start();

18 Connecting to a remote Bluetooth server Try{ BluetoothDevice device = bluetooth.getRemoteDevice("00:23:76:35:2F:AA"); BluetoothSocket clientSocket = device.createRfcommSocketToServiceRecord(uuid); clientSocket.connect(); // TODO Transfer data using the Bluetooth Socket } catch (IOException e) { Log.d("BLUETOOTH", e.getMessage()); }

19 Managing network connectivity The incredible growth of Internet services and the ubiquity of mobile devices have made mobile Internet access an increasingly prevalent feature on mobile phones. With the speed, reliability, and cost of Internet connectivity dependent on the network technology being used (Wi-Fi, GPRS, 3G), letting your applications know and manage these connections can help to ensure they run efficiently and responsively. Android broadcasts Intents that describe changes in network connectivity and offers APIs that provide control over network settings and connections. Just as importantly, users can specify their connectivity preferences — particularly in the case of allowing background data transfers.

20 Introducing the Connectivity Manager The ConnectivityManager represents the Network Connectivity Service. It’s used to monitor the state of network connections, configure failover settings, and control the network radios. To access the Connectivity Manager, use getSystemService, passing in Context.CONNECTIVITY_SERVICE as the service name.

21 Accessing the Connectivity Manager String service = Context.CONNECTIVITY_SERVICE; ConnectivityManager connectivity = (ConnectivityManager)getSystemService(service); To use the Connectivity Manager, your application needs read and write network state access permissions. Add each to your manifest, as shown here:

22 Reading User Preferences for Background Data Transfer One of the most important pieces of information available via the Connectivity Manager is the user’s preference for background data transfers. Users can select to enable or disable background data transfers through the Settings ➪ Accounts & sync settings ➪ Background data setting

23 While your applications are not forced to obey the user’s preference for background data transfers, not doing so is likely to earn vocal criticism from users who installed your application and were rewarded with a significant mobile data bill.

24 Finding and Configuring Network Preferences and Controlling Hardware Radios The Connectivity Manager can also be used to control network hardware and configure failover preferences. Android will attempt to connect to the preferred network whenever an authorized application requests an Internet connection. You can find the current, and set the preferred, network using the getNetworkPreference and setNetworkPreference methods, respectively, as shown in the following code snippet: int networkPreference = connectivity.getNetworkPreference(); connectivity.setNetworkPreference(NetworkPreference.PREFE R_WIFI);

25 Preferable If the preferred connection is unavailable, or connectivity on this network is lost, Android will automatically attempt to connect to the secondary network. You can control the availability of the network types using the setRadio method. This method lets you set the state of the radio associated with a particular network (Wi-Fi, mobile, etc.). For example, in the following code snippet the Wi-Fi radio is turned off and the mobile radio is turned on: connectivity.setRadio(NetworkType.WIFI, false); connectivity.setRadio(NetworkType.MOBILE, true);

26 Monitoring Network Connectivity One of the most useful functions of the Connectivity Manager is to notify applications of changes in network connectivity. To monitor network connectivity create your own Broadcast Receiver implementation that listens for ConnectivityManager.CONNECTIVITY_ACTION broadcast Intents. Such Intents include several extras that provide additional details on the change to the connectivity state. You can access each extra using one of the static constants available from the ConnectivityManager class: ➤ EXTRA_IS_FAILOVER ➤ EXTRA_NO_CONNECTIVITY ➤ EXTRA_REASON ➤ EXTRA_NETWORK_INFO ➤ EXTRA_OTHER_NETWORK_INFO ➤ EXTRA_EXTRA_INFO

27 Managing your Wi-Fi The WifiManager represents the Android Wi-Fi Connectivity Service. It can be used to configure Wi-Fi network connections, manage the current Wi-Fi connection, scan for access points, and monitor changes in Wi-Fi connectivity. As with the ConnectivityManager, you access the Wi- FiManager using the getSystemService method,passing in the Context.WIFI_SERVICE constant

28 Accessing the Wi-Fi Manager String service = Context.WIFI_SERVICE; WifiManager wifi = (WifiManager)getSystemService(service); To use the Wi-Fi Manager your application must have uses- permissions for accessing and changing the Wi-Fi state included in its manifest.

29 Enable Wi-Fi You can use the Wi-Fi Manager to enable or disable your Wi-Fi hardware using the setWifiEnabled method, or request the current Wi-Fi state using the getWifiState or isWifiEnabled methods. if (!wifi.isWifiEnabled()) if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING) wifi.setWifiEnabled(true);

30 Monitoring Wi-Fi Connectivity The Wi-Fi Manager broadcasts Intents whenever the connectivity status of the Wi-Fi network changes, using an action from one of the following constants defined in the WifiManager class: ➤ WIFI_STATE_CHANGED_ACTION Indicates that the Wi-Fi hardware status has changed, moving between enabling, enabled, disabling, disabled, and unknown. ➤ SUPPLICANT_CONNECTION_CHANGE_ACTION This Intent is broadcast whenever the connection state with the active supplicant (access point) changes. It is fired when a new connection is established or an existing connection is lost. ➤ NETWORK_STATE_CHANGED_ACTION Fired whenever the Wi-Fi connectivity state changes. This Intent includes two extras—the first EXTRA_NETWORK_INFO includes a NetworkInfo object that details the current network state. ➤ RSSI_CHANGED_ACTION You can monitor the current signal strength of the connected Wi-Fi network by listening for the RSSI_CHANGED_ACTION Intent.

31 Querying the active network connection WifiInfo info = wifi.getConnectionInfo(); if (info.getBSSID() != null) { int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5); int speed = info.getLinkSpeed(); String units = WifiInfo.LINK_SPEED_UNITS; String ssid = info.getSSID(); String cSummary = String.format("Connected to %s at %s%s. Strength %s/5“, ssid, speed, units, strength); }

32 Managing Wi-Fi Configurations You can use the Wi-FiManager to manage the configured network settings and control which networks to connect to. Once connected, you can interrogate the active network connection to get additional details of its configuration and settings. Get a list of the current network configurations using getConfiguredNetworks. The list of WifiConfiguration objects returned includes the network ID, SSID, and other details for each configuration.

33 Activating a network connection // Get a list of available configurations List configurations = wifi.getConfiguredNetworks(); // Get the network ID for the first one. if (configurations.size() > 0) { int netID = configurations.get(0).networkId; // Enable that network. boolean disableAllOthers = true; wifi.enableNetwork(netID, disableAllOtherstrue); }

34 Creating Wi-Fi Network Configurations To connect to a Wi-Fi network you need to create and register a configuration. Normally, your users would do this using the native Wi-Fi configuration settings, but there’s no reason you can’t expose the same functionality within your own applications. Network configurations are stored as WifiConfiguration objects. The following is a non-exhaustive list of some of the public fields available for each Wi-Fi configuration: ➤ BSSID The BSSID for an access point ➤ SSID The SSID for a particular network ➤ networkId A unique identifier used to identify this network configuration on the current device ➤ priority The network configuration’s priority to use when ordering the list of potential access points to connect to ➤ status The current status of this network connection, which will be one of the following: WifiConfiguration.Status.ENABLED, WifiConfiguration.Status.DISABLED, or WifiConfiguration.Status.CURRENT

35 The configuration object also contains the supported authentication techniques, as well as the keys used previously to authenticate with this access point. The addNetwork method lets you specify a new configuration to add to the current list; similarly, updateNetwork lets you update a network configuration by passing in a WifiConfiguration that’s sparsely populated with a network ID and the values you want to change. You can also use removeNetwork, passing in a network ID, to remove a configuration. To persist any changes made to the network configurations, you must call saveConfiguration.

36 THE END Thank you


Download ppt "CSS216 MOBILE PROGRAMMING Android, Chapter 13 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov ("

Similar presentations


Ads by Google