Android Bluetooth On/Off
Diğer Yazılarımız
MainActivity Java Kodu
public class MainActivity extends AppCompatActivity { BluetoothAdapter myBluetooth; //etrafımızdaki cihazları taryıp ekliyeceğimiz bir dizi oluşturuyoruz //V1 Button toggle_button; //V1 @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 toggle_button.setOnClickListener(new View.OnClickListener() { //V1 @Override public void onClick(View v) { toggleBluetooth(); //method oluşturuldu //V1 } }); } 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"/> </RelativeLayout>