Mrigyank

hindimain

Jul 30th, 2025
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.59 KB | None | 0 0
  1. package com.example.manopancreas;
  2.  
  3. import android.Manifest;
  4. import android.annotation.SuppressLint;
  5. import android.app.AlertDialog;
  6. import android.app.ProgressDialog;
  7. import android.bluetooth.BluetoothAdapter;
  8. import android.bluetooth.BluetoothDevice;
  9. import android.bluetooth.BluetoothSocket;
  10. import android.content.pm.PackageManager;
  11. import android.os.Bundle;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import androidx.annotation.NonNull;
  18. import androidx.appcompat.app.AppCompatActivity;
  19. import androidx.core.app.ActivityCompat;
  20. import androidx.core.content.ContextCompat;
  21.  
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.util.Set;
  25. import java.util.UUID;
  26.  
  27. public class MainHindiActivity extends AppCompatActivity {
  28.  
  29.     private EditText glucoseInput;
  30.     private TextView doseResult;
  31.     private Button sendBtn, connectBtn;
  32.  
  33.     private BluetoothAdapter bluetoothAdapter;
  34.     private BluetoothSocket bluetoothSocket;
  35.     private OutputStream outputStream;
  36.  
  37.     private static final int REQUEST_BLUETOOTH_PERMISSION = 1;
  38.     private static final UUID DEVICE_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  39.     private static final String DEVICE_NAME = "HC-05";
  40.  
  41.     @SuppressLint("SetTextI18n")
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_main_hindi);
  46.  
  47.         glucoseInput = findViewById(R.id.glucoseInput);
  48.         doseResult = findViewById(R.id.doseResult);
  49.         sendBtn = findViewById(R.id.sendBtn);
  50.         connectBtn = findViewById(R.id.connectBtn);
  51.  
  52.         bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  53.         if (bluetoothAdapter == null) {
  54.             Toast.makeText(this, "इस डिवाइस पर ब्लूटूथ समर्थित नहीं है", Toast.LENGTH_LONG).show();
  55.             finish();
  56.             return;
  57.         }
  58.  
  59.         // Request Bluetooth permission if not already granted
  60.         if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
  61.             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN}, REQUEST_BLUETOOTH_PERMISSION);
  62.         }
  63.  
  64.         connectBtn.setOnClickListener(v -> connectToBluetooth());
  65.  
  66.         sendBtn.setOnClickListener(v -> {
  67.             String glucoseStr = glucoseInput.getText().toString().trim();
  68.             if (glucoseStr.isEmpty()) {
  69.                 Toast.makeText(this, "कृपया ग्लूकोज स्तर दर्ज करें", Toast.LENGTH_SHORT).show();
  70.                 return;
  71.             }
  72.  
  73.             try {
  74.                 float glucose = Float.parseFloat(glucoseStr);
  75.                 float dose = calculateDose(glucose);
  76.                 doseResult.setText("इंसुलिन डोज़: " + dose + " µg");
  77.  
  78.                 // Add newline character for Arduino
  79.                 String signal = "DOSE:" + dose + "\n";
  80.                 sendBluetoothSignal(signal);
  81.  
  82.             } catch (NumberFormatException e) {
  83.                 Toast.makeText(this, "अमान्य इनपुट", Toast.LENGTH_SHORT).show();
  84.             }
  85.         });
  86.     }
  87.  
  88.     private float calculateDose(float glucose) {
  89.         if (glucose >= 180) return 8;
  90.         else if (glucose >= 150) return 6;
  91.         else if (glucose >= 120) return 4;
  92.         else if (glucose >= 100) return 2;
  93.         else return 0;
  94.     }
  95.  
  96.     @SuppressLint("MissingPermission")
  97.     private void connectToBluetooth() {
  98.         if (!bluetoothAdapter.isEnabled()) {
  99.             Toast.makeText(this, "कृपया ब्लूटूथ सक्षम करें", Toast.LENGTH_SHORT).show();
  100.             return;
  101.         }
  102.  
  103.         AlertDialog dialog = new AlertDialog.Builder(this)
  104.                 .setTitle("कनेक्ट हो रहा है")
  105.                 .setMessage("कृपया प्रतीक्षा करें जब तक एचसी-05 से कनेक्ट हो रहा है...")
  106.                 .setCancelable(false)
  107.                 .create();
  108.         dialog.show();
  109.  
  110.         new Thread(() -> {
  111.             BluetoothDevice device = null;
  112.             Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
  113.             if (pairedDevices.size() > 0) {
  114.                 for (BluetoothDevice btDevice : pairedDevices) {
  115.                     if (DEVICE_NAME.equals(btDevice.getName())) {
  116.                         device = btDevice;
  117.                         break;
  118.                     }
  119.                 }
  120.             }
  121.  
  122.             try {
  123.                 if (device != null) {
  124.                     bluetoothSocket = device.createRfcommSocketToServiceRecord(DEVICE_UUID);
  125.                     bluetoothSocket.connect();
  126.                     outputStream = bluetoothSocket.getOutputStream();
  127.                     runOnUiThread(() -> {
  128.                         Toast.makeText(MainHindiActivity.this, "HC-05 से कनेक्ट हो गया", Toast.LENGTH_SHORT).show();
  129.                         dialog.dismiss();
  130.                     });
  131.                 } else {
  132.                     runOnUiThread(() -> {
  133.                         Toast.makeText(MainHindiActivity.this, "HC-05 नहीं मिला। कृपया सेटिंग्स में जोड़ें।", Toast.LENGTH_LONG).show();
  134.                         dialog.dismiss();
  135.                     });
  136.                 }
  137.             } catch (IOException e) {
  138.                 runOnUiThread(() -> {
  139.                     Toast.makeText(MainHindiActivity.this, "कनेक्शन विफल: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  140.                     dialog.dismiss();
  141.                 });
  142.             }
  143.         }).start();
  144.     }
  145.  
  146.     private void sendBluetoothSignal(String signal) {
  147.         if (outputStream != null) {
  148.             try {
  149.                 outputStream.write(signal.getBytes());
  150.                 Toast.makeText(this, "सिग्नल भेजा गया", Toast.LENGTH_SHORT).show();
  151.             } catch (IOException e) {
  152.                 Toast.makeText(this, "भेजने में विफल: " + e.getMessage(), Toast.LENGTH_SHORT).show();
  153.             }
  154.         } else {
  155.             Toast.makeText(this, "डिवाइस से कनेक्ट नहीं है", Toast.LENGTH_SHORT).show();
  156.         }
  157.     }
  158.  
  159.     @Override
  160.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  161.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  162.         if (requestCode == REQUEST_BLUETOOTH_PERMISSION) {
  163.             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  164.                 Toast.makeText(this, "ब्लूटूथ की अनुमति दी गई", Toast.LENGTH_SHORT).show();
  165.             } else {
  166.                 Toast.makeText(this, "कनेक्ट करने के लिए ब्लूटूथ की अनुमति आवश्यक है", Toast.LENGTH_LONG).show();
  167.             }
  168.         }
  169.     }
  170.  
  171.     @Override
  172.     protected void onDestroy() {
  173.         super.onDestroy();
  174.         try {
  175.             if (outputStream != null) outputStream.close();
  176.             if (bluetoothSocket != null) bluetoothSocket.close();
  177.         } catch (IOException e) {
  178.             e.printStackTrace();
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment