ACTION_REQUEST_DISCOVERABLE: Ushbu konstanta Bluetooth-ni topish uchun ishlatiladi.
ACTION_STATE_CHANGED: Ushbu konstanta Bluetooth holatining o'zgarishi to'g'risida xabar beradi.
ACTION_FOUND: Ushbu konstanta har bir topilgan qurilma haqida ma'lumot olish uchun ishlatiladi.
Bluetooth-ni yoqgandan so'ng, getBondedDevices() usulini chaqirib, bog'langan qurilmalar ro'yxatini olishingiz mumkin. Bunda bluetooth qurilmalar ro’yxati qaytariladi: private Set boglanganQurilmalar; boglanganQurilmalar = BA.getBondedDevices(); Bluetooth-ni ko'proq boshqarish imkoniyatini beradigan boshqa metodlar ham mavjud:
enable(): Ushbu usul adapter yoqilmagan bo'lsa, adapterni yoqadi.
isEnabled(): Agar adapter yoqilgan bo'lsa, bu usul true qiymatini qaytaradi.
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: <RelativeLayout 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:padding="8dp" android:transitionGroup="true" tools:context=".MainActivity">
<ListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView2"/> RelativeLayout> 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;
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); } } on() metodi Bluetooth-ni yoqadi, off() metodi esa Bluetooth-ni o’chiradi. list() metodi bog’langan qurilmalar ro’yxatini chiqarib beradi. Endi AndroidManifest.xml faylini kerakli ruxsatlarni qo'shish uchun quyidagicha o'zgartiring: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.bluetooth"> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
…………………………………………… Endi ilovani ishga tushiring. Agar sizni qurilmangizda Bluetooth yoqilmagan bo’lsa, sizdan Bluetooth-ni yoqish uchun ruxsat so'raydi:
Endi “KO’RINADIGAN QILISH” tugmasini bosing. Qurilmani boshqa Bluetooth qurilmalari uchun 120 soniyaga ko’rinadigan qilish uchun sizning ruxsatingizni so'ragan quyidagi dialog oynasi paydo bo'ladi:
Endi “QURILMALAR RO’YXATI” tugmasini bosing. U listView-da bog'langan qurilmalarning ro'yxatini ko'rsatadi. Menda faqat bitta ulangan qurilma bor:
Endi Bluetooth-ni o'chirib qo’yish uchun “O'CHIRISH” tugmasini bosing. Bluetooth-ni o'chirganingizda, Bluetooth o'chirilganligini ko'rsatuvchi quyidagi xabar paydo bo'ladi:
Sizlarga ulashmoqchi bo'lgan ma’lumotlarim shulardan iborat edi. Sizlarga foydali bo’ldi degan, umiddaman.