-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
151 lines (87 loc) · 3.53 KB
/
MainActivity.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.example.bluetoothlist;
import java.util.ArrayList;
import java.util.Set;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private int REQUEST_EN_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox ch1 = (CheckBox) findViewById(R.id.checkBox1);
CheckBox ch2 = (CheckBox) findViewById(R.id.checkBox2);
ch1.setText("Server");
ch2.setText("Client");
BluetoothAdapter myBlue = BluetoothAdapter.getDefaultAdapter();
Button btn = (Button) findViewById(R.id.button1);
if(!myBlue.isDiscovering()) {
btn.setText("Not Discoverable");
}
if(myBlue.isDiscovering())
{
btn.setText("Discoverable");
}
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent makeDiscoverable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
makeDiscoverable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(makeDiscoverable);
// TODO Auto-generated method stub
}
});
/* if (myBlue == null){
Toast.makeText(getApplicationContext(), "This device does not support Bluetooth", Toast.LENGTH_SHORT).show();
} */
if (!myBlue.isEnabled()){
Intent enBlue = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enBlue,REQUEST_EN_BT);
}
ArrayList<String> Devices = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,Devices);
Set<BluetoothDevice> pairedDevices = myBlue.getBondedDevices();
if (pairedDevices.size() > 0) {
Toast.makeText(getApplicationContext(), "Device Detected", Toast.LENGTH_SHORT).show();
for (BluetoothDevice bluetoothDevice : pairedDevices) {
Devices.add(bluetoothDevice.getName() + "\n" + bluetoothDevice.getAddress());
}
setListAdapter(arrayAdapter);
}
ch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean state) {
if (state==true)
{
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_EN_BT && requestCode == RESULT_OK)
{
}
}
}