-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
205 lines (157 loc) · 7.05 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.ederdoski.appexample;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothProfile;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.ederdoski.appexample.adapters.BasicList;
import com.ederdoski.simpleble.interfaces.BleCallback;
import com.ederdoski.simpleble.models.BluetoothLE;
import com.ederdoski.simpleble.utils.BluetoothLEHelper;
import com.ederdoski.simpleble.utils.Constants;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Logger;
public class MainActivity extends AppCompatActivity {
BluetoothLEHelper ble;
AlertDialog dAlert;
ListView listBle;
Button btnScan;
Button btnWrite;
Button btnRead;
private AlertDialog setDialogInfo(String title, String message, boolean btnVisible){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_standard, null);
TextView btnNeutral = view.findViewById(R.id.btnNeutral);
TextView txtTitle = view.findViewById(R.id.txtTitle);
TextView txtMessage = view.findViewById(R.id.txtMessage);
txtTitle.setText(title);
txtMessage.setText(message);
if(btnVisible){
btnNeutral.setVisibility(View.VISIBLE);
}else{
btnNeutral.setVisibility(View.GONE);
}
btnNeutral.setOnClickListener(view1 -> {
dAlert.dismiss();
});
builder.setView(view);
return builder.create();
}
private void setList(){
ArrayList<BluetoothLE> aBleAvailable = new ArrayList<>();
if(ble.getListDevices().size() > 0){ //장치 리스트 확인
for (int i=0; i<ble.getListDevices().size(); i++) {
aBleAvailable.add(new BluetoothLE(ble.getListDevices().get(i).getName(), ble.getListDevices().get(i).getMacAddress(), ble.getListDevices().get(i).getRssi(), ble.getListDevices().get(i).getDevice()));
}
BasicList mAdapter = new BasicList(this, R.layout.simple_row_list, aBleAvailable) {
@Override
public void onItem(Object item, View view, int position) {
TextView txtName = view.findViewById(R.id.txtText);
String aux = ((BluetoothLE) item).getName() + " " + ((BluetoothLE) item).getMacAddress();
txtName.setText(aux);
}
};
listBle.setAdapter(mAdapter);
listBle.setOnItemClickListener((parent, view, position, id) -> {
BluetoothLE itemValue = (BluetoothLE) listBle.getItemAtPosition(position);
ble.connect(itemValue.getDevice(), bleCallbacks());
});
}else{
dAlert = setDialogInfo("Ups", "We do not find active devices", true);
dAlert.show();
}
}
private BleCallback bleCallbacks(){
return new BleCallback(){
@Override
public void onBleConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onBleConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
runOnUiThread(() -> Toast.makeText(MainActivity.this, "Connected to GATT server.", Toast.LENGTH_SHORT).show());
}
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
runOnUiThread(() -> Toast.makeText(MainActivity.this, "Disconnected from GATT server.", Toast.LENGTH_SHORT).show());
}
}
@Override
public void onBleServiceDiscovered(BluetoothGatt gatt, int status) {
super.onBleServiceDiscovered(gatt, status);
if (status != BluetoothGatt.GATT_SUCCESS) {
Log.e("Ble ServiceDiscovered","onServicesDiscovered received: " + status);
}
}
@Override
public void onBleCharacteristicChange(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onBleCharacteristicChange(gatt, characteristic);
Log.i("BluetoothLEHelper","onCharacteristicChanged Value: " + Arrays.toString(characteristic.getValue()));
}
@Override
public void onBleRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onBleRead(gatt, characteristic, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.i("TAG", Arrays.toString(characteristic.getValue()));
runOnUiThread(() -> Toast.makeText(MainActivity.this, "onCharacteristicRead : "+Arrays.toString(characteristic.getValue()), Toast.LENGTH_SHORT).show());
}
}
};
}
private void scanCollars(){
if(!ble.isScanning()) { //스캔 상태 확인
dAlert = setDialogInfo("Scan in progress", "Loading...", false);
dAlert.show();
Handler mHandler = new Handler();
ble.scanLeDevice(true);
mHandler.postDelayed(() -> {
dAlert.dismiss();
setList();
},ble.getScanPeriod());
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void listenerButtons(){
btnScan.setOnClickListener(v -> {
if(ble.isReadyForScan()){ //권한 확인
scanCollars();
}else{
Toast.makeText(MainActivity.this, "You must accept the bluetooth and Gps permissions or must turn on the bluetooth and Gps", Toast.LENGTH_SHORT).show();
}
});
btnRead.setOnClickListener(v -> {
if(ble.isConnected()) { //연결 상태 확인
ble.read("6E400001-BA53-F393-E0A9-E50E24DCCA9E", "6E400003-BA53-F393-E0A9-E50E24DCCA9E");
// ble.read(Constants.SERVICE_COLLAR_INFO, Constants.CHARACTERISTIC_CURRENT_POSITION);
// 리드, 설정값 확인, 커넥션 <-> 리드 사이...,
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
ble.disconnect();
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--- Initialize BLE Helper
ble = new BluetoothLEHelper(this);
listBle = findViewById(R.id.listBle);
btnScan = findViewById(R.id.scanBle);
btnRead = findViewById(R.id.readBle);
listenerButtons();
}
}