disable() : Ushbu usul adapterni o'chiradi.
getName() : Ushbu usul Bluetooth adapterining nomini qaytaradi.
setName(String name) : Ushbu usul Bluetooth nomini o'zgartiradi.
getState() : Ushbu usul Bluetooth adapterining joriy holatini qaytaradi.
startDiscovery() : Ushbu usul 120 soniya davomida Bluetooth-ni topish jarayonini boshlaydi.
Demak, Android Studio-da Bluetooth bilan ishlashni boshlaylik. Ushbu hozir yaratadigan ilovamiz Bluetooth-ni boshqarish va Bluetooth orqali bog'langan qurilmalar ro'yxatini ko'rsatish uchun BluetoothAdapter sinfidan foydalaniladi.
Android Studio-da yangi ilova yarating va uni “Bluetooth” deb nomlang. activity_main.xml faylini quyidagicha o’zgartiring:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:transitionGroup="true"
tools:context=".MainActivity">
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="on"
android:text="Yoqish" />
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:onClick="visible"
android:text="Ko'rinadigan qilish" />
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:onClick="list"
android:text="Qurilmalar ro'yxati" />
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/button3"
android:onClick="off"
android:text="O'chirish" />
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:text="Ulangan qurilmalar:"
android:textColor="#f34ff0"
android:textSize="25sp" />
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2" />
Bu kodda ilovaga 4-ta tugma, bitta TextView va bog’langan qurilmalar ro’yxatini ko’rsatuvchi ListView qo’shildi.
Endi tugmalar bilan ishlash uchun java kodini yozish kerak. Ya’ni, MainActivity.java faylini quyidagicha o’zgartiring:
package com.example.android.bluetooth;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
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 boglanganQurilmalar;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.button);
b2 = findViewById(R.id.button2);
b3 = findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
BA = BluetoothAdapter.getDefaultAdapter();
lv = 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(), "Yoqilgan", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Allaqachon yoqilgan", Toast.LENGTH_LONG).show();
}
}
public void off(View v) {
BA.disable();
Toast.makeText(getApplicationContext(), "O'chirilgan", 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) {
boglanganQurilmalar = BA.getBondedDevices();
ArrayList list = new ArrayList();
for (BluetoothDevice bt : boglanganQurilmalar) list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Ulangan qurilmalar ko'rsatilmoqda", Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
}
Do'stlaringiz bilan baham: