Android Studio-da Bluetooth bilan ishlash


ACTION_REQUEST_DISCOVERABLE



Download 24,23 Kb.
bet2/2
Sana12.07.2022
Hajmi24,23 Kb.
#780640
1   2
Bog'liq
Android Studio

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">


    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:onClick="on"
    android:text="Yoqish" />


    <Button
    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" />


    <Button
    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" />


    <Button
    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" />


    <TextView
    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" />


    <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;


    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);
    }
    }
    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.
    Download 24,23 Kb.

    Do'stlaringiz bilan baham:
  • 1   2




    Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
    ma'muriyatiga murojaat qiling

    kiriting | ro'yxatdan o'tish
        Bosh sahifa
    юртда тантана
    Боғда битган
    Бугун юртда
    Эшитганлар жилманглар
    Эшитмадим деманглар
    битган бодомлар
    Yangiariq tumani
    qitish marakazi
    Raqamli texnologiyalar
    ilishida muhokamadan
    tasdiqqa tavsiya
    tavsiya etilgan
    iqtisodiyot kafedrasi
    steiermarkischen landesregierung
    asarlaringizni yuboring
    o'zingizning asarlaringizni
    Iltimos faqat
    faqat o'zingizning
    steierm rkischen
    landesregierung fachabteilung
    rkischen landesregierung
    hamshira loyihasi
    loyihasi mavsum
    faolyatining oqibatlari
    asosiy adabiyotlar
    fakulteti ahborot
    ahborot havfsizligi
    havfsizligi kafedrasi
    fanidan bo’yicha
    fakulteti iqtisodiyot
    boshqaruv fakulteti
    chiqarishda boshqaruv
    ishlab chiqarishda
    iqtisodiyot fakultet
    multiservis tarmoqlari
    fanidan asosiy
    Uzbek fanidan
    mavzulari potok
    asosidagi multiservis
    'aliyyil a'ziym
    billahil 'aliyyil
    illaa billahil
    quvvata illaa
    falah' deganida
    Kompyuter savodxonligi
    bo’yicha mustaqil
    'alal falah'
    Hayya 'alal
    'alas soloh
    Hayya 'alas
    mavsum boyicha


    yuklab olish