Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Developing Bluetooth Application

 

In this video we are going to develop a Bluetooth application.

First of all we will design the Layout of our application, so for that we will go to main.xml file. We have taken one linear layout and a textview.

We need to work with Bluetooth so we have to specify some certain conditions. So for that we will go to our androidmanifest.xml file and give the following permissions,

ACCESS_NETWORK_STATE

ACCESS_WIFI_STATE

BLUETOOTH

BLUETOOTH_ADMIN

Now we will design a MainActivity and also a class named as 'Connections'. Inside that we will first declare a variable 'state' and assign it's value as false.

private static boolean state=false;

Now we will design a bluetooth method and then write the code for it in Connections.java ,

public static boolean blueTooth()

{

BluetoothAdapter bluetooth=BluetoothAdapter.getDefaultAdapter();

if(!bluetooth.isEnabled())

{

System.out.println("Bluetooth is Disable...");

state=true;

}

else if (bluetooth.isEnabled())

{

String address=bluetooth.getAddress();

String name=bluetooth.getName();

System.out.println(name + " : " + address);

state=false;

}

return state;

}

Now we will design in our MainActivity.java file,

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

out=(TextView)findViewById(R.id.tvBluetoothInfo);

}

private void setBluetoothData()

{

adapter=BluetoothAdapter.getDefaultAdapter();

out.append("\\nAdapter: " + adapter.toString() + "\\n\\nName: " + adapter.getName() + "\\nAddress: " + adapter.getAddress());

if(adapter==null)

Toast.makeText(this,"Bluetooth NOT supported.Aborting.",

Toast.LENGTH_LONG).show();

}

out.append("\\n\\nStarting discovery...");

adapter.startDiscovery();

out.append("\\nDone with discovery...\\n");

Now we will call the method for checking that is our Bluetooth enable or disable in MainActivity.java,

setBluetoothData();

if(Connections.blueTooth())

{

Intent enableBtIntent=new Intent(

BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, Request_ENABLE_BT);

}

}

Now we will run our code and execute on a real device. Our code is running successfully and all the information regarding devices bluetooth appears in front of our screen.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere