-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
86 lines (69 loc) · 2.31 KB
/
MainActivity.java
File metadata and controls
86 lines (69 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
Button Sync;
ImageView search_bt;
TextView ListTxt;
ListView listview;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Sync = findViewById(R.id.sync);
search_bt = findViewById(R.id.search_bt);
ListTxt = findViewById(R.id.ListTxt);
listview = findViewById(R.id.listview);
ListTxt.setText(getLocalBluetoothName());
BA = BluetoothAdapter.getDefaultAdapter();
if (BA == null) {
Toast.makeText(this, "Bluetooth not supported", Toast.LENGTH_SHORT).show();
finish();
}
else{
}
Sync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list();
}
});
}
private void list() {
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for (BluetoothDevice bt : pairedDevices) {
list.add(bt.getName());
}
Toast.makeText(this, "Showing Devices", Toast.LENGTH_SHORT).show();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
}
public String getLocalBluetoothName() {
if (BA == null) {
BA = BluetoothAdapter.getDefaultAdapter();
}
String name = BA.getName();
if (name == null){
name = BA.getAddress();
}
return name;
}
}