|
| 1 | +package com.lenovo.bluetooth; |
| 2 | + |
| 3 | +import android.bluetooth.BluetoothAdapter; |
| 4 | +import android.bluetooth.BluetoothDevice; |
| 5 | +import android.content.Intent; |
| 6 | +import android.support.v7.app.AppCompatActivity; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.view.View; |
| 9 | +import android.widget.ArrayAdapter; |
| 10 | +import android.widget.ListView; |
| 11 | +import android.widget.Toast; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +public class MainActivity extends AppCompatActivity { |
| 17 | + |
| 18 | + BluetoothAdapter BA; |
| 19 | + public void bluetoothOff(View view) |
| 20 | + { |
| 21 | + BA.disable(); |
| 22 | + if (BA.isEnabled()) |
| 23 | + { |
| 24 | + Toast.makeText(getApplicationContext(),"BLuetooth coul not be disabled",Toast.LENGTH_SHORT).show(); |
| 25 | + |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + Toast.makeText(getApplicationContext(),"BLuetooth is off",Toast.LENGTH_SHORT).show(); |
| 30 | + |
| 31 | + } |
| 32 | + } |
| 33 | + public void Finddiscoverabledevices(View view) |
| 34 | + { |
| 35 | + Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); |
| 36 | + startActivity(i); |
| 37 | + } |
| 38 | + |
| 39 | + public void ViewpairedDevies(View view) |
| 40 | + { |
| 41 | + Set<BluetoothDevice> paireddevices = BA.getBondedDevices(); |
| 42 | + ListView paireddevicesListview = (ListView) findViewById(R.id.paireddevicesListview); |
| 43 | + ArrayList paireddevicesArraylist = new ArrayList(); |
| 44 | + for(BluetoothDevice bluetoothDevice:paireddevices) |
| 45 | + { |
| 46 | + paireddevicesArraylist.add(bluetoothDevice.getName()); |
| 47 | + |
| 48 | + } |
| 49 | + ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,paireddevicesArraylist); |
| 50 | + paireddevicesListview.setAdapter(arrayAdapter); |
| 51 | + } |
| 52 | + @Override |
| 53 | + |
| 54 | + protected void onCreate(Bundle savedInstanceState) { |
| 55 | + super.onCreate(savedInstanceState); |
| 56 | + setContentView(R.layout.activity_main); |
| 57 | + |
| 58 | + if(BA.isEnabled()) |
| 59 | + { |
| 60 | + Toast.makeText(getApplicationContext(),"Bluetooth is on",Toast.LENGTH_LONG).show(); |
| 61 | + } |
| 62 | + else { |
| 63 | + Intent i =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); |
| 64 | + startActivity(i); |
| 65 | + if(BA.isEnabled()) |
| 66 | + { |
| 67 | + Toast.makeText(getApplicationContext(),"Bluetooth is turned on",Toast.LENGTH_LONG).show(); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments