Android Bluetooth



Yüklə 151,3 Kb.
səhifə1/3
tarix02.08.2022
ölçüsü151,3 Kb.
#62983
  1   2   3
Документ Microsoft Word


Android Bluetooth
Ko'p usullar orasida Bluetooth ikki xil qurilma o'rtasida ma'lumotlarni yuborish yoki qabul qilish usulidir. Android platformasi qurilmaga boshqa Bluetooth qurilmalari bilan simsiz ma'lumot almashish imkonini beruvchi Bluetooth ramkasini qo'llab-quvvatlaydi.
Android ushbu turli operatsiyalarni bajarish uchun Bluetooth API-ni taqdim etadi.

  • Boshqa Bluetooth qurilmalarini qidiring

  • Ulangan qurilmalar roʻyxatini oling

  • Xizmatni aniqlash orqali boshqa qurilmalarga ulaning

Android Bluetooth bilan muloqot qilish uchun BluetoothAdapter sinfini taqdim etadi. GetDefaultAdapter() statik usulini chaqirish orqali ushbu chaqiruv ob'ektini yarating. Uning sintaksisi quyida keltirilgan.
private BluetoothAdapter BA;
BA = BluetoothAdapter.getDefaultAdapter();
Qurilmangizning Bluetooth-ni yoqish uchun quyidagi Bluetooth doimiysi ACTION_REQUEST_ENABLE bilan maqsadni chaqiring. Uning sintaksisi.
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Bluetooth ni yoqqaningizdan so'ng getBondedDevices() usuliga qo'ng'iroq qilib, ulangan qurilmalar ro'yxatini olishingiz mumkin. U bluetooth qurilmalari to'plamini qaytaradi. Uning sintaksisi.
private SetpairedDevices;
pairedDevices = BA.getBondedDevices();
Mana src/MainActivity.java ning mazmuni
package com.example.sairamkrishna.myapplication;

import android.app.Activity;


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 Activity {


Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private SetpairedDevices;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1 = (Button) findViewById(R.id.button);


b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);

BA = BluetoothAdapter.getDefaultAdapter();


lv = (ListView)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(), "Turned on",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
}
}

public void off(View v){


BA.disable();
Toast.makeText(getApplicationContext(), "Turned off" ,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){


pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();

for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());


Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();

final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);


lv.setAdapter(adapter);
}
}

Yüklə 151,3 Kb.

Dostları ilə paylaş:
  1   2   3




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin