List Paired Bluetooth Devices, Too Read Its Information

Posted on

This event cheque BlueTooth back upwardly of the device, in addition to then cheque if it is enabled, in addition to listing paired devices inwards ListView. When user click on whatever device, display it details.


MainActivity.java

package com.example.androidbluetooth;  import java.util.ArrayList; import java.util.Set;  import android.support.v7.app.ActionBarActivity; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast;  world flat MainActivity extends ActionBarActivity {    person static lastly int REQUEST_ENABLE_BT = 1;    BluetoothAdapter bluetoothAdapter;    ArrayList<BluetoothDevice> pairedDeviceArrayList;    ListView listViewPairedDevice;  ArrayAdapter<BluetoothDevice> pairedDeviceAdapter;   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);   listViewPairedDevice = (ListView)findViewById(R.id.pairedlist);      if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)){    Toast.makeText(this,      "FEATURE_BLUETOOTH NOT support",      Toast.LENGTH_LONG).show();             finish();             return;   }      bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();   if (bluetoothAdapter == null) {    Toast.makeText(this,      "Bluetooth is non supported on this hardware platform",      Toast.LENGTH_LONG).show();             finish();             return;   }  }   @Override  protected void onStart() {   super.onStart();      //Turn ON BlueTooth if it is OFF   if (!bluetoothAdapter.isEnabled()) {             Intent enableIntent = novel Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);             startActivityForResult(enableIntent, REQUEST_ENABLE_BT);         }      setup();  }    person void setup() {   Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();   if (pairedDevices.size() > 0) {    pairedDeviceArrayList = novel ArrayList<BluetoothDevice>();        for (BluetoothDevice device : pairedDevices) {     pairedDeviceArrayList.add(device);             }        pairedDeviceAdapter = novel ArrayAdapter<BluetoothDevice>(this,                  android.R.layout.simple_list_item_1, pairedDeviceArrayList);    listViewPairedDevice.setAdapter(pairedDeviceAdapter);        listViewPairedDevice.setOnItemClickListener(new OnItemClickListener(){      @Override     world void onItemClick(AdapterView<?> parent, View view,       int position, long id) {      BluetoothDevice device =         (BluetoothDevice)parent.getItemAtPosition(position);       Toast.makeText(MainActivity.this,         "Name: " + device.getName() + "\n"        + "Address: " + device.getAddress() + "\n"        + "BondState: " + device.getBondState() + "\n"        + "BluetoothClass: " + device.getBluetoothClass() + "\n"        + "Class: " + device.getClass(),        Toast.LENGTH_LONG).show();     }});   }  }   @Override  protected void onActivityResult(int requestCode, int resultCode, Intent data) {      if(requestCode==REQUEST_ENABLE_BT){    if(resultCode == Activity.RESULT_OK){     setup();    }else{     Toast.makeText(this,       "BlueTooth NOT enabled",       Toast.LENGTH_SHORT).show();              finish();    }   }   } } 

/res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:orientation="vertical"     tools:context="com.example.androidbluetooth.MainActivity" >      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />          <ListView          android:id="@+id/pairedlist"         android:layout_width="match_parent"         android:layout_height="match_parent"/>  </LinearLayout> 

“android.permission.BLUETOOTH” is needed inwards AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.androidbluetooth"     android:versionCode="1"     android:versionName="1.0" >      <uses-sdk         android:minSdkVersion="8"         android:targetSdkVersion="21" />     <uses-permission android:name="android.permission.BLUETOOTH"/>      <application         android:allowBackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/AppTheme" >         <activity             android:name=".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>     </application>  </manifest> 

This event cheque BlueTooth back upwardly of the device List paired BlueTooth devices, in addition to read its informationDownload the files.

Next:
– Make BlueTooth connector betwixt Android devices
– Bluetooth communication betwixt Android devices