Bluetooth:
Bluetooth connectivity is a common way to share data from one device to another.Normally Bluetooth is used to share Media files like mp3 songs images or videos or files.
Bluetooth Device Class:
Bluetooth class, which describes general characteristics and capabilities of a device. For example, Generally when we connect to a Bluetooth Device like Computer, Laptop, Bluetooth Headset and Bluetooth other peripherals.And the Bluetooth class will specify the general device type such as a phone, a computer, or headset, and whether it's capable of services such as audio or telephony.
And today I am adding an other basic tutorial to get List of Paired Bluetooth Devices and get the Major Class (Headset, Computer or other Peripheral) of the Paired Bluetooth Devices:
OutPut:
Create new Android Project
Project Name: BlueToothPairing
//tested from 2.3.3 to current android sdk
Build Target: Android 2.3.3 //or greater than that
Application Name: BlueToothPairing
Package Name: com.shaikhhamadali.blogspot.bluetoothpairing
Create Layout file: activity_pair
1. code of Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tVBluetoothState"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnListPairedDevices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="List Paired Devices"
android:enabled="false"
/>
</LinearLayout>
2. code of activityies:
PairingActivity:
package com.shaikhhamadali.blogspot.bluetoothpairing;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class PairingActivity extends Activity {
//Create variables
private static final int REQUEST_ENABLE_BT = 1;
private static final int REQUEST_PAIRED_DEVICE = 2;
//declare views/controls
Button btnListPairedDevices;
TextView tVBluetoothState;
//declare Bluetooth Adapter
BluetoothAdapter btAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pair);
//get reference of the UI Controls
btnListPairedDevices = (Button)findViewById(R.id.btnListPairedDevices);
tVBluetoothState = (TextView)findViewById(R.id.tVBluetoothState);
//Get Default bluetooth adapter
btAdapter = BluetoothAdapter.getDefaultAdapter();
//Check the state of bluetooth
CheckBlueToothState();
//set listner for the button.
btnListPairedDevices.setOnClickListener(btnListPairedDevicesOnClickListener);
}
private void CheckBlueToothState(){
//does device support Bluetooth
if (btAdapter == null){
tVBluetoothState.setText("Bluetooth NOT support");
}else{
//is Bluetooth enable
if (btAdapter.isEnabled()){
//is in discovery mode
if(btAdapter.isDiscovering()){
tVBluetoothState.setText("Bluetooth is currently in device discovery process.");
}else{
tVBluetoothState.setText("Bluetooth is Enabled.");
btnListPairedDevices.setEnabled(true);
}
}else{
tVBluetoothState.setText("Bluetooth is NOT Enabled!");
//Enable Bluetooth by intent
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
private Button.OnClickListener btnListPairedDevicesOnClickListener= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// Create instance of intent
Intent intent = new Intent();
//set class for intent
intent.setClass(PairingActivity.this, ListPairedDevicesActivity.class);
//and start list activity for result
startActivityForResult(intent, REQUEST_PAIRED_DEVICE);
}};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// is the request code for enable bluetooth or the paired devices
if(requestCode == REQUEST_ENABLE_BT){
//check Bluetooth state
CheckBlueToothState();
}
if (requestCode == REQUEST_PAIRED_DEVICE){
//do nothing is result is ok
if(resultCode == RESULT_OK){
}
}
}
}
ListPairedDevicesActivity:
package com.shaikhhamadali.blogspot.bluetoothpairing;
import java.util.Set;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListPairedDevicesActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Create ArrayAdapter to store the list of paired Bluetooth Devices
ArrayAdapter<String> btArrayAdapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
//Create instance of Bluetooth Adapter
BluetoothAdapter bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
//create Set instance to get the set of Paired devices
Set<BluetoothDevice> pairedDevices= bluetoothAdapter.getBondedDevices();
//check that device has paired devices list or not
if (pairedDevices.size() > 0) {
//loop through the every paired device.
for (BluetoothDevice device : pairedDevices) {
//Get Name of the paired Device
String deviceBTName = device.getName();
//Get Class of the Paired Device
String deviceBTMajorClass= getBTMajorDeviceClass(device.getBluetoothClass().getMajorDeviceClass());
//Add name and class of the Paired device
btArrayAdapter.add(deviceBTName + "\n" + deviceBTMajorClass);
}
}
//Set list Adapter to the ListActivity
setListAdapter(btArrayAdapter);
}
private String getBTMajorDeviceClass(int major){
//Types of the Bluetooth Classes
switch(major){
case BluetoothClass.Device.Major.AUDIO_VIDEO:
return "AUDIO_VIDEO";
case BluetoothClass.Device.Major.COMPUTER:
return "COMPUTER";
case BluetoothClass.Device.Major.HEALTH:
return "HEALTH";
case BluetoothClass.Device.Major.IMAGING:
return "IMAGING";
case BluetoothClass.Device.Major.MISC:
return "MISC";
case BluetoothClass.Device.Major.NETWORKING:
return "NETWORKING";
case BluetoothClass.Device.Major.PERIPHERAL:
return "PERIPHERAL";
case BluetoothClass.Device.Major.PHONE:
return "PHONE";
case BluetoothClass.Device.Major.TOY:
return "TOY";
case BluetoothClass.Device.Major.UNCATEGORIZED:
return "UNCATEGORIZED";
case BluetoothClass.Device.Major.WEARABLE:
return "AUDIO_VIDEO";
default: return "unknown!";
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//set Result OK and finish the list activity
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}
3. note that:
-
Good practice is to always check that is bluetooth supported, is Bluetooth ON or not, and is it in discovering mode.
- As you can see above I have used some common class types that are supported by Bluetooth are listed here on developer.android.com.
- You can connect to any device after getting list of all paired devices.
- Learn more about Bluetooth, Enable Disable Bluetooth.
Good practice is to always check that is bluetooth supported, is Bluetooth ON or not, and is it in discovering mode.
4. conclusion:
- Some information about how to get Bluetooth paired devices list.
- know how to get Class type of Bluetooth paired devices.
- Know how to Create List Activity.
- How to generate list of dynamic items.
- Some information about how to get Bluetooth paired devices list.
- know how to get Class type of Bluetooth paired devices.
- Know how to Create List Activity.
- How to generate list of dynamic items.
5. About the post:
5. About the post:
- The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
- Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
- Hope you enjoy it!
6. Source Code:
you can download the source code from: GoogleDrive, Github
Cheers,
Hamad Ali Shaikh
- The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
- Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
- Hope you enjoy it!
6. Source Code:
you can download the source code from: GoogleDrive, Github
Cheers,
Hamad Ali Shaikh