Bluetooth Paired List
Diğer Yazılarımız
MainActivity Java Kodu
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 {
BluetoothAdapter myBluetooth; //etrafımızdaki cihazları taryıp ekliyeceğimiz bir dizi oluşturuyoruz //V1
private Set<BluetoothDevice> pairedDevices; //V2
Button toggle_button; //V1
Button pair_button; //V2
ListView pairedlist; //diğer calass da kullanmak üzere bir EXTRA_ADDRESS belirlenir //V2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myBluetooth=BluetoothAdapter.getDefaultAdapter();//bu kod cihazımızın bluetooth özelliği olup olmadığını göstericek //V1
toggle_button=(Button) findViewById(R.id.button_toggle); //bu butona basılınca bluetooth durumuna göre aktif/pasif durumuna getiricez //V1
pair_button=(Button) findViewById(R.id.button_pair); //V2
pairedlist=(ListView) findViewById(R.id.device_list); //V2
toggle_button.setOnClickListener(new View.OnClickListener() { //V1
@Override
public void onClick(View v) {
toggleBluetooth(); //method oluşturuldu //V1
}
});
pair_button.setOnClickListener(new View.OnClickListener() { //V2
@Override
public void onClick(View v) { //V2
listdevice(); //V2
}
});
}
private void listdevice() { //V2
pairedDevices = myBluetooth.getBondedDevices();//eşleşmiş cihazları pairedDevices içine attık //V2
ArrayList list = new ArrayList(); //V2
if (pairedDevices.size() > 0)//eşleşmiş cihazların boyutu sıfırdan büyükse //V2
{
for (BluetoothDevice bt: pairedDevices) //V2
{
list.add(bt.getName()+"\n"+ bt.getAddress()); //V2
}
}
else
{
Toast.makeText(getApplicationContext(),"Eşleşmiş Cihaz Yok!",Toast.LENGTH_SHORT).show(); //V2
}
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list); //V2
pairedlist.setAdapter(adapter); //V2
}
private void toggleBluetooth() { //yapmamız gereken işlemi bluetooth özelliğinin açık olup olmadığını tespid ederek yapıcaz //V1
if (myBluetooth==null){
Toast.makeText(getApplicationContext(), "Bluetooth Cihazı Yok!",Toast.LENGTH_SHORT).show(); //V1
}
if (!myBluetooth.isEnabled())
{
Intent enableBTIntent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBTIntent); //burada bluetooth açma isteği gönderilmiş olacak //V1
}
if (myBluetooth.isEnabled())
{
myBluetooth.disable();
}
}
}
activty_main.xml Kodu
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button_toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:background="@android:color/holo_blue_light"
android:text="Bluetooth On/Off"
android:layout_alignParentTop="true"/>
<Button
android:id="@+id/button_pair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:padding="5dp"
android:background="@android:color/holo_green_light"
android:text="Bluetooth Cihazları Listele"
android:layout_below="@id/button_toggle"
android:gravity="center"/>
<ListView
android:id="@+id/device_list"
android:layout_marginTop="30dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:padding="5dp"
android:layout_below="@id/button_pair"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"/>
</RelativeLayout>
Business vector created by pinnacleanimates - www.freepik.com