Skip to main content

Bluetooth Paired Device Search

 MainActivity.java

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 MainActivity extends Activity{

private static final int REQUEST_ENABLE_BT = 1;
private static final int REQUEST_PAIRED_DEVICE = 2;

   /** Called when the activity is first created. */
Button btnListPairedDevices;
TextView stateBluetooth;
BluetoothAdapter bluetoothAdapter;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
     
       btnListPairedDevices = (Button)findViewById(R.id.listpaireddevices);
     
       stateBluetooth = (TextView)findViewById(R.id.listpaireddevices);
       bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
     
       CheckBlueToothState();
     
       btnListPairedDevices.setOnClickListener(btnListPairedDevicesOnClickListener);
   }
 
   private void CheckBlueToothState(){
    if (bluetoothAdapter == null){
        stateBluetooth.setText("Bluetooth NOT support");
       }else{
        if (bluetoothAdapter.isEnabled()){
         if(bluetoothAdapter.isDiscovering()){
          stateBluetooth.setText("Bluetooth is currently in device discovery process.");
         }else{
          stateBluetooth.setText("Bluetooth is Enabled.");
          btnListPairedDevices.setEnabled(true);
         }
        }else{
         stateBluetooth.setText("Bluetooth is NOT Enabled!");
         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) {
  // TODO Auto-generated method stub
  Intent intent = new Intent();
  intent.setClass(MainActivity.this, ListPairedDevicesActivity.class);
  startActivityForResult(intent, REQUEST_PAIRED_DEVICE);
 }};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 // TODO Auto-generated method stub
 if(requestCode == REQUEST_ENABLE_BT){
  CheckBlueToothState();
 }if (requestCode == REQUEST_PAIRED_DEVICE){
  if(resultCode == RESULT_OK){

  }
 }
}
}

ListPairedDevicesActivity.java

import java.util.Set;

import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothClass.Device;
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);

 ArrayAdapter<String> btArrayAdapter  = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1);

 BluetoothAdapter bluetoothAdapter  = BluetoothAdapter.getDefaultAdapter();
 Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

 if (pairedDevices.size() > 0) {
     for (BluetoothDevice device : pairedDevices) {
      String deviceBTName = device.getName();
      String deviceBTId =device.getAddress();
       String deviceBTMajorClass = getBTMajorDeviceClass(device .getBluetoothClass()
         .getMajorDeviceClass());
      btArrayAdapter.add(deviceBTName + "\n"+deviceBTId+ "\n"+ deviceBTMajorClass);
     }
 }
 setListAdapter(btArrayAdapter);

}

private String getBTMajorDeviceClass(int major){
 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) {
 // TODO Auto-generated method stub
 super.onListItemClick(l, v, position, id);

    Intent intent = new Intent();
    setResult(RESULT_OK, intent);
    finish();
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="hello"
    />
<TextView
    android:id="@+id/bluetoothstate"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/listpaireddevices"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="List Paired Devices"
    android:enabled="false"
    />
<TextView
    android:id="@+id/bluetoothstate"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluetooth_paireddevice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

     <uses-permission android:name="android.permission.BLUETOOTH" />
 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.bluetooth_paireddevice.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ListPairedDevicesActivity"
      android:label="AndroidBluetooth: List of Paired Devices"/>

    </application>

</manifest>

Comments

Popular posts from this blog

Bluetooth Chat Example

Manifest File <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.example.android.BluetoothChat"       android:versionCode="1"       android:versionName="1.0">     <uses-sdk minSdkVersion="7" />     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />     <uses-permission android:name="android.permission.BLUETOOTH" />     <application android:label="@string/app_name"                  android:icon="@drawable/app_icon" >         <activity android:name=".BluetoothChat"                   android:label="@string/app_name"                   android:configChanges="orientation|keyboardHidden">             <intent-filter> ...

Google Map v2 signed map isssue fixed

Hi Step 1: Develop Map Application  using android studio Step 2: For Map viewing we need to have google api key .in order to get api key we need to register application in google api console  https://console.developers.google.com/apis/ Step 3; For getting SHA1 Key please follow the steps            1) Go the java installed folder location , open cmd                 Eg: C:\Program Files\Java\jdk1.7.0_04\bin            2)  Type                   Windows keytool -list -v -keystore "CHANGE WITH OUR KEYSTORE LOC" -alias ALIASNAME -storepass PASSWORD -keypass PASSWORD                   Mac & Linux  keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android Ref:  http://www.truiton.com/2015/04/obtaining-sha1...

Store Image to Google Cloud Store using Google App engine java

Here is sample code for uploading image files to cloud storage bucket in google cloud platform Step 1 Create a google cloud project in cloud (https://console.cloud.google.com/)  enable cloud storage Step 2: Create a new web application project I am using Eclipse with Java for App engine Project Step 3: create a POST web service method the below code for upload image to the existing bucket in google cloud and the uploaded image url will be returned Code   @Path("/addres")  @POST       @Consumes(MediaType.MULTIPART_FORM_DATA)    public String addRestaurant( @FormDataParam("imgname")  String imagename,            @FormDataParam("file") InputStream uploadedInputStream,         @FormDataParam("file") FormDataContentDisposition restImages     ) throws IOException  {   try { // Create your service object Storage storage = St...