Repository navigation
Expand file tree
/
Copy pathAndroidBluetoothActivity.java
More file actions
138 lines (117 loc) · 5.29 KB
/
Copy pathAndroidBluetoothActivity.java
File metadata and controls
138 lines (117 loc) · 5.29 KB
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
package com.disasterlink.mobile;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import org.json.JSONObject;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class EmergencyActivity extends AppCompatActivity {
private static final UUID BLUETOOTH_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothAdapter bluetoothAdapter;
private FusedLocationProviderClient fusedLocationClient;
private EditText messageInput;
private Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emergency);
messageInput = findViewById(R.id.messageInput);
sendButton = findViewById(R.id.sendButton);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
sendButton.setOnClickListener(v -> sendEmergencyData());
requestPermissions();
}
private void requestPermissions() {
String[] permissions = {
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
if (!hasPermissions(permissions)) {
ActivityCompat.requestPermissions(this, permissions, 1);
}
}
private boolean hasPermissions(String[] permissions) {
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
private void sendEmergencyData() {
String message = messageInput.getText().toString().trim();
if (message.isEmpty()) {
Toast.makeText(this, "Please enter emergency message", Toast.LENGTH_SHORT).show();
return;
}
// Get current location
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, location -> {
if (location != null) {
sendViaBluetooth(message, location);
} else {
// Use default location if GPS unavailable
Location defaultLocation = new Location("");
defaultLocation.setLatitude(28.6139);
defaultLocation.setLongitude(77.2090);
sendViaBluetooth(message, defaultLocation);
}
});
}
}
private void sendViaBluetooth(String message, Location location) {
try {
// Find paired DisasterLink devices
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
BluetoothDevice disasterLinkDevice = null;
for (BluetoothDevice device : pairedDevices) {
if (device.getName() != null && device.getName().contains("DisasterLink")) {
disasterLinkDevice = device;
break;
}
}
if (disasterLinkDevice == null) {
Toast.makeText(this, "No DisasterLink device found. Please pair first.", Toast.LENGTH_LONG).show();
return;
}
// Create JSON data
JSONObject emergencyData = new JSONObject();
emergencyData.put("type", "emergency");
emergencyData.put("content", message);
emergencyData.put("location", location.getLatitude() + "," + location.getLongitude());
emergencyData.put("timestamp", System.currentTimeMillis());
emergencyData.put("deviceId", android.provider.Settings.Secure.getString(
getContentResolver(), android.provider.Settings.Secure.ANDROID_ID));
emergencyData.put("severity", "critical");
// Send via Bluetooth
BluetoothSocket socket = disasterLinkDevice.createRfcommSocketToServiceRecord(BLUETOOTH_UUID);
socket.connect();
OutputStream outputStream = socket.getOutputStream();
outputStream.write(emergencyData.toString().getBytes());
outputStream.flush();
socket.close();
Toast.makeText(this, "✅ Emergency data sent successfully!", Toast.LENGTH_SHORT).show();
messageInput.setText("");
} catch (Exception e) {
Toast.makeText(this, "❌ Failed to send data: " + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}