WIFI and Bluetooth Application Dr Sandip Mal WIFI
WIFI and Bluetooth Application Dr. Sandip Mal
WIFI activity_main. xml <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" xmlns: app="http: //schemas. android. com/apk/res-auto" xmlns: tools="http: //schemas. android. com/tools" android: layout_width="match_parent" android: layout_height="match_parent" android: orientation="vertical" tools: context=". Main. Activity"> <Button android: id="@+id/button 1" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_align. Parent. Left="true" android: layout_align. Parent. Top="true" android: layout_margin. Left="76 dp" android: layout_margin. Top="67 dp" android: text="Enable Wifi" /> <Button android: id="@+id/button 2" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_align. Left="@+id/button 1" android: layout_below="@+id/button 1" android: layout_margin. Top="44 dp" android: text="Disable Wifi" /> </Linear. Layout>
Androidmanifest. xml Add the following permissions <uses-permission android: name="android. permission. ACCESS_WIFI_STATE" /> <uses-permission android: name="android. permission. INTERNET" /> <uses-permission android: name="android. permission. CHANGE_WIFI_STATE"/>
Edit Java file • • Declare Button enable. Button, disable. Button; Define Button enable. Button=(Button)find. View. By. Id(R. id. button 1); disable. Button=(Button)find. View. By. Id(R. id. button 2); • enable. Button. set. On. Click. Listener(new On. Click. Listener(){ public void on. Click(View v){ Wifi. Manager wifi = (Wifi. Manager) get. System. Service(Context. WIFI_SERVICE); wifi. set. Wifi. Enabled(true); } }); • disable. Button. set. On. Click. Listener(new On. Click. Listener(){ public void on. Click(View v){ Wifi. Manager wifi = (Wifi. Manager) get. System. Service(Context. WIFI_SERVICE); wifi. set. Wifi. Enabled(false); } });
Bluetooth • • • Bluetooth. Adapter class provides many constants. Some of them are as follows: String ACTION_REQUEST_ENABLE String ACTION_REQUEST_DISCOVERABLE String ACTION_DISCOVERY_STARTED String ACTION_DISCOVERY_FINISHED Commonly used methods of Bluetooth. Adapter class are as follows: • static synchronized Bluetooth. Adapter get. Default. Adapter() returns the instance of Bluetooth. Adapter. • boolean enable() enables the bluetooth adapter if it is disabled. • boolean is. Enabled() returns true if the bluetooth adapter is enabled. • boolean disable() disables the bluetooth adapter if it is enabled. • String get. Name() returns the name of the bluetooth adapter. • boolean set. Name(String name) changes the bluetooth name. • int get. State() returns the current state of the local bluetooth adapter. • Set<Bluetooth. Device> get. Bonded. Devices() returns a set of paired (bonded) Bluetooth. Device objects. • boolean start. Discovery() starts the discovery process.
Activity_main. xml <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/andr oid" xmlns: app="http: //schemas. android. com/apk/res-auto" xmlns: tools="http: //schemas. android. com/tools" android: layout_width="match_parent" android: layout_height="match_parent" android: orientation="vertical" tools: context=". Main. Activity"> <Text. View android: text="" android: id="@+id/out" android: layout_width="wrap_content" android: layout_height="wrap_content"> </Text. View> <Button android: id="@+id/button 1" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_align. Parent. Left="true" android: layout_align. Parent. Top="true" android: layout_margin. Left="30 dp" android: layout_margin. Top="49 dp" android: text="TURN_ON" /> • <Button android: id="@+id/button 2" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_align. Left="@+id/button 1" android: layout_below="@+id/button 1" android: layout_margin. Top="27 dp" android: text="DISCOVERABLE" /> <Button android: id="@+id/button 3" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_align. Left="@+id/button 2" android: layout_below="@+id/button 2" android: layout_margin. Top="28 dp" android: text="TURN_OFF" /> </Linear. Layout>
Androidmanifest. xml Add Permission <uses-permission android: name="android. permission. BLUETOOTH" /> <uses-permission android: name="android. permission. BLUETOOTH_ADMIN" />
Java File Declare private static final int REQUEST_ENABLE_BT = 0; private static final int REQUEST_DISCOVERABLE_BT = 0; • Define buttons and text views final Text. View out=(Text. View)find. View. By. Id(R. id. out); final Button button 1 = (Button) find. View. By. Id(R. id. button 1); final Button button 2 = (Button) find. View. By. Id(R. id. button 2); final Button button 3 = (Button) find. View. By. Id(R. id. button 3); final Bluetooth. Adapter m. Bluetooth. Adapter = Bluetooth. Adapter. get. Default. Adapter(); – if (m. Bluetooth. Adapter == null) { out. append("device not supported"); } – button 1. set. On. Click. Listener(new View. On. Click. Listener() { public void on. Click(View v) { if (!m. Bluetooth. Adapter. is. Enabled()) { Intent enable. Bt. Intent = new Intent(Bluetooth. Adapter. ACTION_REQUEST_ENABLE); start. Activity. For. Result(enable. Bt. Intent, REQUEST_ENABLE_BT); } } }); •
– button 2. set. On. Click. Listener(new View. On. Click. Listener() { @Override public void on. Click(View arg 0) { if (!m. Bluetooth. Adapter. is. Discovering()) { //out. append("MAKING YOUR DEVICE DISCOVERABLE"); Toast. make. Text(get. Application. Context(), "MAKING YOUR DEVICE DISCOVERABLE", Toast. LENGTH_LONG); Intent enable. Bt. Intent = new Intent(Bluetooth. Adapter. ACTION_REQUEST_DISCOVERABLE); start. Activity. For. Result(enable. Bt. Intent, REQUEST_DISCOVERABLE_BT); } } – button 3. set. On. Click. Listener(new View. On. Click. Listener() { @Override public void on. Click(View arg 0) { m. Bluetooth. Adapter. disable(); //out. append("TURN_OFF BLUETOOTH"); Toast. make. Text(get. Application. Context(), "TURNING_OFF BLUETOOTH", Toast. LENGTH_LONG); }); }
- Slides: 9