Skip to content

Commit ca221a9

Browse files
committed
[FEAT] Sending through the id and room in JSON format, Changing IPs
1 parent dcc4914 commit ca221a9

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

app/src/main/java/com/example/blockchainaccess/MyHostApduService.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import android.content.Intent
66
import android.content.Context
77
import android.app.Service
88
import android.util.Log
9+
import org.json.JSONObject
910

1011
class MyHostApduService : HostApduService() {
1112

1213

1314
private var currentUserId: String = "unknown-user"
15+
private val roomName: String = "roomA"
1416

1517
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
1618
intent?.getStringExtra("NFC_USER_ID")?.let {
@@ -24,8 +26,22 @@ class MyHostApduService : HostApduService() {
2426
Log.d("HCE", "APDU Received: ${commandApdu.joinToString(" ") { "%02X".format(it) }}")
2527

2628
if (commandApdu.size >= 4 && commandApdu[1] == 0xA4.toByte()) {
27-
val responseData = currentUserId.toByteArray(Charsets.UTF_8)
28-
return responseData + byteArrayOf(0x90.toByte(), 0x00.toByte())
29+
try {
30+
// 3. Create a JSON object and add the key-value pai
31+
val jsonData = JSONObject()
32+
jsonData.put("id", currentUserId)
33+
jsonData.put("room", roomName)
34+
35+
// 4. Convert the JSON object to a UTF-8 byte array
36+
val responseData = jsonData.toString().toByteArray(Charsets.UTF_8)
37+
38+
// 5. Return the JSON data followed by the success status word (90 00)
39+
return responseData + byteArrayOf(0x90.toByte(), 0x00.toByte())
40+
41+
} catch (e: Exception) {
42+
Log.e("HCE", "Error creating JSON data", e)
43+
return byteArrayOf(0x6F.toByte(), 0x00.toByte()) // General error
44+
}
2945
}
3046
return byteArrayOf(0x6A.toByte(), 0x82.toByte()) // File not found
3147
}

app/src/main/java/com/example/blockchainaccess/NetworkClient.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ object NetworkClient {
4646

4747
suspend fun addWhitelistEntry(userId: String): Result<Unit> {
4848
return try {
49-
val url = "http://10.0.2.2:8081/new_whitelist_entry"
49+
val url = "http://192.168.0.198:8081/new_whitelist_entry"
5050
val response = client.post(url) {
5151
contentType(ContentType.Application.Json)
5252
setBody(WhitelistEntryRequest(userId))
@@ -64,7 +64,7 @@ object NetworkClient {
6464

6565
suspend fun removeWhitelistEntry(userId: String): Result<Unit> {
6666
return try {
67-
val url = "http://10.0.2.2:8081/remove_whitelist_entry"
67+
val url = "http://192.168.0.198:8081/remove_whitelist_entry"
6868
val response = client.post(url) {
6969
contentType(ContentType.Application.Json)
7070
setBody(WhitelistEntryRequest(userId))
@@ -82,7 +82,7 @@ object NetworkClient {
8282

8383
suspend fun createAndRegisterProfile(username: String, password: String): Result<Pair<String, Set<String>>> {
8484
try {
85-
val createProfileUrl = "http://10.0.2.2:8081/login"
85+
val createProfileUrl = "http://192.168.0.198:8081/login"
8686
val response: HttpResponse = client.post(createProfileUrl) {
8787
contentType(ContentType.Application.Json)
8888
setBody(CreateProfileRequest(username, password))
@@ -103,7 +103,7 @@ object NetworkClient {
103103

104104
println("Profile created. ID: $generatedId")
105105
// --- Step 2: Register user ID with 127.0.0.1:8081 ---
106-
val registerUrl = "http://10.0.2.2:8081/app_save_id"
106+
val registerUrl = "http://192.168.0.198:8081/app_save_id"
107107
val registerResponse: HttpResponse = client.post(registerUrl) {
108108
contentType(ContentType.Application.Json)
109109
setBody(UpdateUserRequest(generatedId))

0 commit comments

Comments
 (0)