Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions GALAXY_WATCH_INTEGRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# Pyrrha Mobile-Watch Integration Guide

## Samsung Accessory Protocol Integration

This document describes the Samsung Accessory Protocol integration between the Pyrrha Mobile App (Provider) and Pyrrha Watch App (Consumer) for real-time sensor data transmission.

## Architecture Overview

```
Prometeo Device (BLE) → Mobile App (Provider) → Galaxy Watch (Consumer)
```

### Mobile App (Provider)
- **Service**: `ProviderService.java` extends `SAAgent`
- **Role**: Provider (sends sensor data)
- **App Name**: `PyrrhaMobileProvider`
- **Channel ID**: 104
- **Service Profile**: `/org/pyrrha-platform/readings`

### Watch App (Consumer)
- **Service**: JavaScript consumer in `connect.js`
- **Role**: Consumer (receives sensor data)
- **Expected Provider**: `PyrrhaMobileProvider`
- **Channel ID**: 104

## Data Flow

1. **BLE Reception**: Mobile app receives sensor data from Prometeo device via Bluetooth LE
2. **Data Parsing**: `DeviceDashboard.displayData()` parses space-separated sensor values:
- `parts[2]` = Temperature (°C)
- `parts[4]` = Humidity (%)
- `parts[6]` = Carbon Monoxide (ppm)
- `parts[8]` = Nitrogen Dioxide (ppm)
3. **Data Validation**: Invalid readings (CO > 1000 or < 0, NO2 > 10 or < 0) are set to 0
4. **JSON Formatting**: Data is formatted as JSON with message type and timestamp
5. **Samsung Accessory Protocol**: Data transmitted via Samsung Accessory Protocol to watch
6. **Watch Display**: Watch receives and displays real-time sensor readings

## JSON Message Format

```json
{
"messageType": "sensor_data",
"temperature": 25.4,
"humidity": 65.2,
"co": 15.3,
"no2": 0.8,
"timestamp": 1672531200000,
"deviceId": "Prometeo:00:00:00:00:00:01",
"status": "normal"
}
```

### Status Values
- `"normal"`: All readings within safe thresholds
- `"warning"`: One or more readings at 80% of alert threshold
- `"alert"`: One or more readings exceed safety thresholds

### Alert Thresholds
- **Temperature**: 32°C
- **Humidity**: 80%
- **Carbon Monoxide**: 420 ppm
- **Nitrogen Dioxide**: 8 ppm

## Service Configuration

### Mobile App Configuration

**AndroidManifest.xml**:
```xml
<service android:name="org.pyrrha_platform.galaxy.ProviderService" />
```

**accessoryservices.xml**:
```xml
<application name="PyrrhaMobileProvider">
<serviceProfile
id="/org/pyrrha-platform/readings"
name="PyrrhaSensorProvider"
role="provider"
serviceImpl="org.pyrrha_platform.galaxy.ProviderService"
version="2.0"
serviceLimit="ANY"
serviceTimeout="10000">
<supportedTransports>
<transport type="TRANSPORT_BT" />
<transport type="TRANSPORT_WIFI" />
</supportedTransports>
<serviceChannel
id="104"
dataRate="HIGH"
priority="HIGH"
reliability="ENABLE" />
<supportedFeatures>
<feature type="message" />
</supportedFeatures>
</serviceProfile>
</application>
```

### Watch App Configuration

**config.xml**:
```xml
<tizen:application id="Jqb25DY60P.pyrrha" package="Jqb25DY60P" required_version="5.5"/>
<tizen:setting background-support="enable" encryption="disable" hwkey-event="enable"/>
<tizen:privilege name="http://tizen.org/privilege/healthinfo"/>
<tizen:privilege name="http://tizen.org/privilege/alarm.set"/>
```

## Testing Integration

### Prerequisites
1. Samsung Galaxy A51 with Android 14 (API 34)
2. Samsung Galaxy Watch 3 with Tizen 5.5
3. Both devices paired via Samsung Galaxy Watch app
4. Pyrrha Mobile App installed on phone
5. Pyrrha Watch App installed on watch

### Test Procedure

1. **Start Mobile App**: Launch Pyrrha app and connect to Prometeo device
2. **Check Provider Service**: Verify ProviderService starts and searches for watches
3. **Start Watch App**: Launch Pyrrha app on Galaxy Watch
4. **Connection**: Watch should automatically discover and connect to mobile provider
5. **Data Flow**: Sensor readings should appear on watch within 3 seconds of mobile reception

### Debugging

**Mobile App Logs**:
```bash
adb logcat -s PyrrhaMobileProvider
```

**Watch App Logs**:
Access via Tizen Studio or Samsung Internet debugger on watch

### Common Issues

1. **Connection Failed**: Ensure both devices are on same Samsung account and paired
2. **Service Not Found**: Verify both apps are running and have correct service profiles
3. **No Data**: Check that Prometeo device is connected to mobile app via BLE
4. **Invalid Data**: Sensor validation may filter out invalid readings (CO > 1000, NO2 > 10)

## Implementation Details

### ProviderService Features
- **Automatic Discovery**: Searches for Galaxy Watches on service start
- **Connection Management**: Handles multiple watch connections
- **Error Handling**: Comprehensive error codes and reconnection logic
- **Heartbeat Support**: Responds to watch heartbeat requests
- **Data Broadcasting**: Sends sensor data every 3 seconds to connected watches

### Watch Consumer Features
- **Auto-Connect**: Automatically connects to PyrrhaMobileProvider
- **Message Parsing**: Handles JSON sensor data and control messages
- **UI Updates**: Real-time sensor display with circular Galaxy Watch 3 optimization
- **Alert System**: Vibration alerts when readings exceed thresholds
- **Reconnection**: Automatic reconnection on connection loss

## Security Considerations

- Samsung Accessory Protocol uses built-in Samsung authentication
- Data transmission encrypted via Samsung framework
- No additional authentication required for paired devices
- Service limited to Samsung Galaxy ecosystem

## Performance Notes

- **Update Frequency**: 3-second intervals for optimal battery life
- **Data Size**: JSON messages ~200 bytes each
- **Battery Impact**: Minimal - uses Samsung's optimized protocol
- **Range**: Standard Bluetooth range (10 meters typical)

## Development Notes

- Provider service automatically starts with DeviceDashboard activity
- Watch consumer runs continuously while app is active
- Service connections managed in activity lifecycle
- Error handling includes graceful degradation without watch connectivity

## Future Enhancements

- Historical data synchronization
- Multiple device support
- Custom alert thresholds
- Watch-initiated sensor requests
- Offline data buffering
115 changes: 75 additions & 40 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
namespace 'org.pyrrha_platform'
compileSdk 34 // Updated to Android 14 (latest stable)
buildToolsVersion "34.0.0"

defaultConfig {
applicationId "org.pyrrha.platform"
minSdkVersion 19
targetSdkVersion 30
minSdkVersion 26 // Updated to Android 8.0 (covers Samsung Galaxy A51 and 95%+ devices)
targetSdkVersion 34 // Updated to Android 14 (IBM App ID temporarily removed)
multiDexEnabled true
versionCode 1
versionName "1.0"
versionCode 2 // Incremented for modernization
versionName "2.0.0" // Version bump for major modernization

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
manifestPlaceholders = ['appIdRedirectScheme': android.defaultConfig.applicationId]
// manifestPlaceholders = ['appIdRedirectScheme': android.defaultConfig.applicationId] // TEMPORARILY REMOVED
}

buildTypes {
Expand All @@ -22,50 +23,84 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

// Enable modern Android features
buildFeatures {
viewBinding true
dataBinding true
buildConfig true
}

lint {
baseline = file("lint-baseline.xml")
abortOnError false
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.annotation:annotation:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'

// Core AndroidX libraries - updated to latest stable versions
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.annotation:annotation:1.7.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.core:core-ktx:1.12.0' // Added for modern Android

// Lifecycle - updated to use ViewModel and LiveData instead of deprecated extensions
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.7.0'
implementation 'androidx.lifecycle:lifecycle-livedata:2.7.0'
implementation 'androidx.lifecycle:lifecycle-runtime:2.7.0'

// Navigation - updated to latest
implementation 'androidx.navigation:navigation-fragment:2.7.6'
implementation 'androidx.navigation:navigation-ui:2.7.6'

// Samsung SDK files (keep existing)
implementation files('libs/accessory-v2.6.4.jar')
implementation files('libs/sdk-v1.0.0.jar')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.1'

// Testing - updated versions
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

// JSON processing - updated Gson
implementation 'com.google.code.gson:gson:2.10.1'

// MQTT - updated to latest stable versions
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
implementation('org.eclipse.paho:org.eclipse.paho.android.service:1.1.1') {
exclude module: 'support-v4'
}
implementation 'com.github.ibm-cloud-security:appid-clientsdk-android:6.+'

// We need this dependency for the api res call
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'

def room_version = "2.3.0"


// IBM App ID SDK for authentication - TEMPORARILY REMOVED
// implementation 'com.github.ibm-cloud-security:appid-clientsdk-android:6.+'

// Networking - updated Retrofit and OkHttp to latest stable
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'

// Room database - updated to latest stable
def room_version = "2.6.1"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"

// optional - Guava support for Room, including Optional and ListenableFuture

// Room optional features - updated
implementation "androidx.room:room-rxjava3:$room_version" // Updated from RxJava2 to RxJava3
implementation "androidx.room:room-guava:$room_version"

// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"


// Security and modern Android features
implementation 'androidx.security:security-crypto:1.1.0-alpha06' // For secure data storage
implementation 'androidx.work:work-runtime:2.9.0' // For background tasks

}

android{
Expand Down Expand Up @@ -94,9 +129,9 @@ if (propFile.canRead()) {
}
}
} else {
throw new InvalidUserDataException('pyrrha.properties found, but some entries are missing')
throw new RuntimeException('pyrrha.properties found, but some entries are missing')
}
} else {
// The properties file was not found
throw new MissingResourceException('pyrrha.properties not found')
throw new RuntimeException('pyrrha.properties not found')
}
Loading
Loading