<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/selectDeviceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bluetooth"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/postureTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="스트레칭을 따라하세요!"
android:textSize="24sp"
android:layout_below="@id/selectDeviceButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
<ImageView
android:id="@+id/stretchImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/postureTextView"
android:layout_marginTop="20dp"
android:scaleType="centerInside"
android:src="@drawable/w25"/>
<ListView
android:id="@+id/paired_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/stretchImageView"
android:layout_marginTop="20dp"/>
</RelativeLayout>
package com.example.opensource;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
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 androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_ENABLE_BT = 1;
private static final int REQUEST_PERMISSIONS = 2;
private static final String TAG = "MainActivity";
private BluetoothAdapter bluetoothAdapter;
private BluetoothSocket bluetoothSocket;
private InputStream inputStream;
private Handler handler;
private TextView postureTextView;
private ImageView stretchImageView;
private Button selectDeviceButton;
private ListView listView;
private ArrayAdapter<String> pairedDevicesArrayAdapter;
private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postureTextView = findViewById(R.id.postureTextView);
stretchImageView = findViewById(R.id.stretchImageView);
selectDeviceButton = findViewById(R.id.selectDeviceButton);
listView = findViewById(R.id.paired_devices);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN}, REQUEST_PERMISSIONS);
}
}
selectDeviceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPairedDevices();
}
});
handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
String posture = msg.getData().getString("posture");
Log.d(TAG, "Received posture data: " + posture);
postureTextView.setText(posture);
showStretchingImages(posture);
return true;
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showPairedDevices();
} else {
Toast.makeText(this, "Bluetooth permissions are required to use this application", Toast.LENGTH_LONG).show();
finish();
}
}
}
private void showPairedDevices() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN}, REQUEST_PERMISSIONS);
return;
}
}