MainActivity.java
package kantapp.android_bluetoothtutorial; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.Set; public class MainActivity extends AppCompatActivity { Button b1,b2,b3,b4; private BluetoothAdapter BA; private Set<BluetoothDevice> pairedDevices; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button) findViewById(R.id.button); b2=(Button)findViewById(R.id.button2); b3=(Button)findViewById(R.id.button3); b4=(Button)findViewById(R.id.button4); BA = BluetoothAdapter.getDefaultAdapter(); lv = (ListView)findViewById(R.id.listView); } public void on(View v){ if (!BA.isEnabled()) { Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnOn, 0); Toast.makeText(getApplicationContext(),"Turned on",Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(),"Already on", Toast.LENGTH_LONG).show(); } } public void off(View v){ BA.disable(); Toast.makeText(getApplicationContext(),"Turned off" ,Toast.LENGTH_LONG).show(); } public void visible(View v){ Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(getVisible, 0); } public void list(View v){ pairedDevices = BA.getBondedDevices(); ArrayList list = new ArrayList(); for(BluetoothDevice bt : pairedDevices) list.add(bt.getName()); Toast.makeText(getApplicationContext(),"Showing Paired Devices",Toast.LENGTH_SHORT).show(); final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="kantapp.android_bluetoothtutorial.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|top" android:text="Bluetooth Example" android:textAppearance="?android:attr/textAppearanceLarge" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bluetooth" /> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:onClick="on" android:text="Turn On" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="off" android:text="turn off" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="visible" android:text="Get Visible" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="list" android:text="list Device" /> </LinearLayout> </HorizontalScrollView> <LinearLayout android:layout_width="200dp" android:layout_height="wrap_content" android:orientation="horizontal"> <ListView android:id="@+id/listView" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/textView2" android:layout_width="200dp" android:layout_height="wrap_content" android:text="Paired devices:" android:textColor="#ff34ff06" android:textSize="25dp" /> </LinearLayout> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="kantapp.android_bluetoothtutorial"> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Complete Poject : https://userscloud.com/1r4p6n3b3thq
Only Source Code: https://userscloud.com/ibu56o1784xi
Only Source Code: https://userscloud.com/ibu56o1784xi
No comments:
Post a Comment