Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.manopancreas;
- import android.Manifest;
- import android.annotation.SuppressLint;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothSocket;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityCompat;
- import androidx.core.content.ContextCompat;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Set;
- import java.util.UUID;
- public class MainHindiActivity extends AppCompatActivity {
- private EditText glucoseInput;
- private TextView doseResult;
- private Button sendBtn, connectBtn;
- private BluetoothAdapter bluetoothAdapter;
- private BluetoothSocket bluetoothSocket;
- private OutputStream outputStream;
- private static final int REQUEST_BLUETOOTH_PERMISSION = 1;
- private static final UUID DEVICE_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- private static final String DEVICE_NAME = "HC-05";
- @SuppressLint("SetTextI18n")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main_hindi);
- glucoseInput = findViewById(R.id.glucoseInput);
- doseResult = findViewById(R.id.doseResult);
- sendBtn = findViewById(R.id.sendBtn);
- connectBtn = findViewById(R.id.connectBtn);
- bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- if (bluetoothAdapter == null) {
- Toast.makeText(this, "इस डिवाइस पर ब्लूटूथ समर्थित नहीं है", Toast.LENGTH_LONG).show();
- finish();
- return;
- }
- // Request Bluetooth permission if not already granted
- 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_BLUETOOTH_PERMISSION);
- }
- connectBtn.setOnClickListener(v -> connectToBluetooth());
- sendBtn.setOnClickListener(v -> {
- String glucoseStr = glucoseInput.getText().toString().trim();
- if (glucoseStr.isEmpty()) {
- Toast.makeText(this, "कृपया ग्लूकोज स्तर दर्ज करें", Toast.LENGTH_SHORT).show();
- return;
- }
- try {
- float glucose = Float.parseFloat(glucoseStr);
- float dose = calculateDose(glucose);
- doseResult.setText("इंसुलिन डोज़: " + dose + " µg");
- // Add newline character for Arduino
- String signal = "DOSE:" + dose + "\n";
- sendBluetoothSignal(signal);
- } catch (NumberFormatException e) {
- Toast.makeText(this, "अमान्य इनपुट", Toast.LENGTH_SHORT).show();
- }
- });
- }
- private float calculateDose(float glucose) {
- if (glucose >= 180) return 8;
- else if (glucose >= 150) return 6;
- else if (glucose >= 120) return 4;
- else if (glucose >= 100) return 2;
- else return 0;
- }
- @SuppressLint("MissingPermission")
- private void connectToBluetooth() {
- if (!bluetoothAdapter.isEnabled()) {
- Toast.makeText(this, "कृपया ब्लूटूथ सक्षम करें", Toast.LENGTH_SHORT).show();
- return;
- }
- AlertDialog dialog = new AlertDialog.Builder(this)
- .setTitle("कनेक्ट हो रहा है")
- .setMessage("कृपया प्रतीक्षा करें जब तक एचसी-05 से कनेक्ट हो रहा है...")
- .setCancelable(false)
- .create();
- dialog.show();
- new Thread(() -> {
- BluetoothDevice device = null;
- Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
- if (pairedDevices.size() > 0) {
- for (BluetoothDevice btDevice : pairedDevices) {
- if (DEVICE_NAME.equals(btDevice.getName())) {
- device = btDevice;
- break;
- }
- }
- }
- try {
- if (device != null) {
- bluetoothSocket = device.createRfcommSocketToServiceRecord(DEVICE_UUID);
- bluetoothSocket.connect();
- outputStream = bluetoothSocket.getOutputStream();
- runOnUiThread(() -> {
- Toast.makeText(MainHindiActivity.this, "HC-05 से कनेक्ट हो गया", Toast.LENGTH_SHORT).show();
- dialog.dismiss();
- });
- } else {
- runOnUiThread(() -> {
- Toast.makeText(MainHindiActivity.this, "HC-05 नहीं मिला। कृपया सेटिंग्स में जोड़ें।", Toast.LENGTH_LONG).show();
- dialog.dismiss();
- });
- }
- } catch (IOException e) {
- runOnUiThread(() -> {
- Toast.makeText(MainHindiActivity.this, "कनेक्शन विफल: " + e.getMessage(), Toast.LENGTH_SHORT).show();
- dialog.dismiss();
- });
- }
- }).start();
- }
- private void sendBluetoothSignal(String signal) {
- if (outputStream != null) {
- try {
- outputStream.write(signal.getBytes());
- Toast.makeText(this, "सिग्नल भेजा गया", Toast.LENGTH_SHORT).show();
- } catch (IOException e) {
- Toast.makeText(this, "भेजने में विफल: " + e.getMessage(), Toast.LENGTH_SHORT).show();
- }
- } else {
- Toast.makeText(this, "डिवाइस से कनेक्ट नहीं है", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (requestCode == REQUEST_BLUETOOTH_PERMISSION) {
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- Toast.makeText(this, "ब्लूटूथ की अनुमति दी गई", Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(this, "कनेक्ट करने के लिए ब्लूटूथ की अनुमति आवश्यक है", Toast.LENGTH_LONG).show();
- }
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- try {
- if (outputStream != null) outputStream.close();
- if (bluetoothSocket != null) bluetoothSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment