checkpoint
This commit is contained in:
1 parent
0ac0b4c901
commit
fd1898f555
4 files changed
+30
-17
No files matched your search
@@ -37,6 +37,8 @@ object BLEManager{
|
||||
private var bluetoothGatt: BluetoothGatt? = null
|
||||
private lateinit var bluetoothManager: BluetoothManager
|
||||
lateinit var bluetoothAdapter: BluetoothAdapter
|
||||
private val writeQueue = ArrayDeque<WriteRequest>()
|
||||
private var isWriting = false
|
||||
val gattCallback = object : BluetoothGattCallback() {
|
||||
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
|
||||
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
|
||||
@@ -68,6 +70,11 @@ object BLEManager{
|
||||
val deviceName: String,
|
||||
val deviceMac: String
|
||||
)
|
||||
private data class WriteRequest(
|
||||
val serviceUuid: UUID,
|
||||
val characteristicUuid: UUID,
|
||||
val data: ByteArray
|
||||
)
|
||||
@RequiresApi(Build.VERSION_CODES.S)
|
||||
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
|
||||
fun init(context: Context, activity: Activity) {
|
||||
@@ -134,7 +141,8 @@ object BLEManager{
|
||||
Log.d("BLE_DEBUG", "Permission BLUETOOTH_CONNECT error!")
|
||||
}
|
||||
}
|
||||
fun writeToEsp32(context: Context, dataString: String?, SERVICE_UUID: UUID?, CHARACTERISTIC_UUID: UUID?) {
|
||||
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
|
||||
fun writeToEsp32(dataString: String?, SERVICE_UUID: UUID?, CHARACTERISTIC_UUID: UUID?) {
|
||||
val gatt = bluetoothGatt
|
||||
if (gatt == null) {
|
||||
Log.d("ESP32_BLE", "Error: No GATT object existing (not connected).")
|
||||
@@ -158,11 +166,6 @@ object BLEManager{
|
||||
return
|
||||
}
|
||||
|
||||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
||||
Log.d("ESP32_BLE", "Error: BLUETOOTH_CONNECT permission not granted.")
|
||||
return
|
||||
}
|
||||
|
||||
characteristic.value = dataString.toByteArray(Charsets.UTF_8)
|
||||
|
||||
characteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
|
||||
@@ -174,6 +177,7 @@ object BLEManager{
|
||||
Log.d("ESP32_BLE", "Failed to start writing (gatt busy or invalid state).")
|
||||
}
|
||||
}
|
||||
@androidx.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
|
||||
fun syncTime(context: Context) {
|
||||
val currentTime = LocalTime.now()
|
||||
val formatterTime = DateTimeFormatter.ofPattern("HH mm ss")
|
||||
@@ -186,7 +190,6 @@ object BLEManager{
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
writeToEsp32(
|
||||
context = context,
|
||||
dataString = formattedTime,
|
||||
SERVICE_UUID = deviceUUIDs.SERVICE_UUID,
|
||||
CHARACTERISTIC_UUID = deviceUUIDs.CHAR_TIME_UUID)
|
||||
@@ -194,7 +197,6 @@ object BLEManager{
|
||||
delay(150.milliseconds)
|
||||
|
||||
writeToEsp32(
|
||||
context = context,
|
||||
dataString = formattedDate,
|
||||
SERVICE_UUID = deviceUUIDs.SERVICE_UUID,
|
||||
CHARACTERISTIC_UUID = deviceUUIDs.CHAR_DATE_UUID)
|
||||
|
||||
@@ -3,11 +3,13 @@ package com.example.smart_watch_android.BLE
|
||||
import java.util.UUID
|
||||
|
||||
object DeviceUUIDs {
|
||||
val SERVICE_UUID: UUID? = UUID.fromString("791cadd7-de87-49f9-8986-94b7ae11940b")
|
||||
val CHAR_MESSAGE_UUID: UUID? = UUID.fromString("3e4eac5b-eaa2-4e0c-a705-dcac4f606e01")
|
||||
val CHAR_TIME_UUID:UUID? = UUID.fromString("3e4eac5b-eaa2-4e0c-a705-dcac4f606e02")
|
||||
val CHAR_DATE_UUID:UUID? = UUID.fromString("3e4eac5b-eaa2-4e0c-a705-dcac4f606e03")
|
||||
val CHAR_NOTIF_APP_UUID:UUID? = UUID.fromString("9ff3d4a9-2082-424d-bf34-8dfaf752ff90")
|
||||
val CHAR_NOTIF_TITLE_UUID:UUID? = UUID.fromString("9ff3d4a9-2082-424d-bf34-8dfaf752ff91")
|
||||
val CHAR_NOTIF_TEXT_UUID: UUID? = UUID.fromString("9ff3d4a9-2082-424d-bf34-8dfaf752ff92")
|
||||
val SERVICE_UUID: UUID? = UUID.fromString("00004d43-0000-1000-8000-00805F9B34FB")
|
||||
val CHAR_MESSAGE_UUID: UUID? = UUID.fromString("0000b331-0000-1000-8000-00805F9B34FB")
|
||||
val CHAR_TIME_UUID:UUID? = UUID.fromString("00003c4b-0000-1000-8000-00805F9B34FB")
|
||||
val CHAR_DATE_UUID:UUID? = UUID.fromString("00006ecf-0000-1000-8000-00805F9B34FB")
|
||||
|
||||
val SERVICE_NOTIFICATION_UUID:UUID? = UUID.fromString("0000a7fa-0000-1000-8000-00805F9B34FB")
|
||||
val CHAR_NOTIF_APP_UUID:UUID? = UUID.fromString("0000375f-0000-1000-8000-00805F9B34FB")
|
||||
val CHAR_NOTIF_TITLE_UUID:UUID? = UUID.fromString("00006e40-0000-1000-8000-00805F9B34FB")
|
||||
val CHAR_NOTIF_TEXT_UUID: UUID? = UUID.fromString("00002eef-0000-1000-8000-00805F9B34FB")
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
package com.example.smart_watch_android.Notification
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.service.notification.StatusBarNotification
|
||||
import android.util.Log
|
||||
import android.service.notification.NotificationListenerService
|
||||
import androidx.annotation.RequiresPermission
|
||||
import com.example.smart_watch_android.BLE.BLEManager
|
||||
import com.example.smart_watch_android.BLE.DeviceUUIDs
|
||||
|
||||
class MyNotificationListener : NotificationListenerService() {
|
||||
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
|
||||
override fun onNotificationPosted(sbn: StatusBarNotification?) {
|
||||
super.onNotificationPosted(sbn)
|
||||
val packageName = sbn?.packageName
|
||||
@@ -14,6 +19,11 @@ class MyNotificationListener : NotificationListenerService() {
|
||||
val text = extras?.getString("android.text")
|
||||
|
||||
Log.d("NotificationListener", "App: $packageName | Title: $title | Text: $text")
|
||||
BLEManager.writeToEsp32(dataString = packageName, SERVICE_UUID = DeviceUUIDs.SERVICE_NOTIFICATION_UUID, CHARACTERISTIC_UUID = DeviceUUIDs.CHAR_NOTIF_APP_UUID)
|
||||
BLEManager.writeToEsp32(dataString = title, SERVICE_UUID = DeviceUUIDs.SERVICE_NOTIFICATION_UUID, CHARACTERISTIC_UUID = DeviceUUIDs.CHAR_NOTIF_TITLE_UUID)
|
||||
BLEManager.writeToEsp32(dataString = text, SERVICE_UUID = DeviceUUIDs.SERVICE_NOTIFICATION_UUID, CHARACTERISTIC_UUID = DeviceUUIDs.CHAR_NOTIF_TEXT_UUID)
|
||||
|
||||
|
||||
}
|
||||
override fun onNotificationRemoved(sbn: StatusBarNotification?) {
|
||||
super.onNotificationRemoved(sbn)
|
||||
|
||||
@@ -24,7 +24,7 @@ import androidx.compose.ui.unit.dp
|
||||
import com.example.smart_watch_android.BLE.BLEManager
|
||||
import com.example.smart_watch_android.BLE.DeviceUUIDs
|
||||
import com.example.smart_watch_android.MainViewModel
|
||||
|
||||
@androidx.annotation.RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
|
||||
@Composable
|
||||
fun ControlPanelScreen(
|
||||
viewModel: MainViewModel,
|
||||
@@ -55,7 +55,6 @@ fun ControlPanelScreen(
|
||||
onClick = {
|
||||
if (textInput.isNotEmpty()) {
|
||||
BLEManager.writeToEsp32(
|
||||
context = context,
|
||||
dataString = textInput,
|
||||
SERVICE_UUID = DeviceUUIDs.SERVICE_UUID,
|
||||
CHARACTERISTIC_UUID = DeviceUUIDs.CHAR_MESSAGE_UUID,
|
||||
|
||||
Reference in new issue
Block a user