From 7621a1672f37556c38e38ee7dc82bc18bcfa4918 Mon Sep 17 00:00:00 2001 From: Naveen Sakthivel Date: Wed, 2 Jun 2021 22:36:36 +0530 Subject: [PATCH] added git action --- .github/workflows/publish-package.yml | 23 +++++++++++++++ android/build.gradle | 5 +--- android/gradle.properties | 23 ++------------- .../BleAdvertiserModule.kt | 11 +++++++- .../java/dev/dotworld/ble/AppPreferences.kt | 28 +++++++++++++++++++ .../dotworld/ble/bluetooth/gatt/GattServer.kt | 21 ++++++++++++-- .../dotworld/ble/protocol/v2/BlueTraceV2.kt | 13 ++++++--- .../ble/protocol/v2/V2ReadRequestPayload.kt | 6 ++++ package.json | 9 ++++-- 9 files changed, 104 insertions(+), 35 deletions(-) create mode 100644 .github/workflows/publish-package.yml create mode 100644 android/src/main/java/dev/dotworld/ble/AppPreferences.kt diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml new file mode 100644 index 0000000..f041cf3 --- /dev/null +++ b/.github/workflows/publish-package.yml @@ -0,0 +1,23 @@ +name: Node.js Package +on: + release: + types: [created] +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v2 + # Setup .npmrc file to publish to GitHub Packages + - uses: actions/setup-node@v2 + with: + node-version: '12.x' + registry-url: 'https://npm.pkg.github.com' + # Defaults to the user or organization that owns the workflow file + scope: '@teamdotworld' + - run: npm install + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/android/build.gradle b/android/build.gradle index 7e909f7..347362e 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -43,9 +43,6 @@ android { buildConfigField "int", "PUSH_NOTIFICATION_ID", PUSH_NOTIFICATION_ID buildConfigField "String", "PUSH_NOTIFICATION_CHANNEL_NAME", PUSH_NOTIFICATION_CHANNEL_NAME - buildConfigField "long", "SCAN_DURATION", SCAN_DURATION - buildConfigField "long", "MIN_SCAN_INTERVAL", MIN_SCAN_INTERVAL - buildConfigField "long", "MAX_SCAN_INTERVAL", MAX_SCAN_INTERVAL buildConfigField "long", "HEALTH_CHECK_INTERVAL", HEALTH_CHECK_INTERVAL buildConfigField "long", "MAX_QUEUE_TIME", MAX_QUEUE_TIME buildConfigField "long", "BM_CHECK_INTERVAL", BM_CHECK_INTERVAL @@ -61,7 +58,7 @@ android { buildConfigField "String", "V2_CHARACTERISTIC_ID", V2_CHARACTERISTIC_ID - buildConfigField "String", "LOCATION", LOCATION + buildConfigField "String", "PUBLIC_KEY", PUBLIC_KEY } diff --git a/android/gradle.properties b/android/gradle.properties index 2e8b705..4f429d6 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -2,7 +2,6 @@ BleAdvertiser_kotlinVersion=1.5.0 BleAdvertiser_compileSdkVersion=29 BleAdvertiser_buildToolsVersion=29.0.2 BleAdvertiser_targetSdkVersion=29 - # Project-wide Gradle settings. # IDE (e.g. Android Studio) users: # Gradle settings configured through the IDE *will override* @@ -24,42 +23,26 @@ android.useAndroidX=true android.enableJetifier=true # Kotlin code style for this project: "official" or "obsolete": kotlin.code.style=official - - STAGING_SERVICE_UUID="fd05a570-274a-4b1f-a5a3-eb52e5e02b8b" PROD_SERVICE_UUID="" - - SERVICE_FOREGROUND_NOTIFICATION_ID=771579 SERVICE_FOREGROUND_CHANNEL_ID="DW Trace Updates" SERVICE_FOREGROUND_CHANNEL_NAME="DW Trace Foreground Service" - PUSH_NOTIFICATION_ID=771578 PUSH_NOTIFICATION_CHANNEL_NAME="OpenTrace Notifications" - #service configurations -SCAN_DURATION=3000 -MIN_SCAN_INTERVAL=4000 -MAX_SCAN_INTERVAL=5000 - HEALTH_CHECK_INTERVAL=900000 ADVERTISING_DURATION=180000 ADVERTISING_INTERVAL=2000 - MAX_QUEUE_TIME=5000 BM_CHECK_INTERVAL=540000 PURGE_INTERVAL=86400000 PURGE_TTL=1814400000 - CONNECTION_TIMEOUT=6000 BLACKLIST_DURATION=120000 - -V2_CHARACTERISTIC_ID = "53447ca9-1e5b-448e-ab7b-bd9438c048af" - -NOTIFY_SERVICE_ID = "afecf75a-0f7e-450f-85e0-5683c92fe321" - +V2_CHARACTERISTIC_ID="53447ca9-1e5b-448e-ab7b-bd9438c048af" +NOTIFY_SERVICE_ID="afecf75a-0f7e-450f-85e0-5683c92fe321" MANUFACTURER_ID=1023 -LOCATION="Naveen Home" - +PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAooDxEsQ0VGa80Yp5fwczAIEq7fuQF6cQqaD+czcBRK0UhH0FXjxTFKfA9OlFMHF/GH4n1vvne/rPFEl2sVhxJucbV9g7dwv70WyZNXODqIdX4dqyn3Y7Uz6h3gX9lUYtjW5KFBtEysJl3Vtq4gQyGfKZbjxnreI/BU+Xbjg9QqhQ0T93UFNluZKPf0o9ki+BFSD3gc1NnyUhfa7ncCFaSRkd8/fyNeR/zaKUN/W4+MMiZ9huCh2+ZIrQ2fiIZIX8gqWjwZ2tH39Cquy3pzkOtFOavVOw1di9fdqicq5L4YUdoakNItuAirOgcHOfa8s2L6piOxNiPqnZ9++nqn9WQQIDAQAB" CAN_ADVERTISE=false CAN_SCAN=true diff --git a/android/src/main/java/com/reactnativebleadvertiser/BleAdvertiserModule.kt b/android/src/main/java/com/reactnativebleadvertiser/BleAdvertiserModule.kt index 9397668..7a42ed8 100644 --- a/android/src/main/java/com/reactnativebleadvertiser/BleAdvertiserModule.kt +++ b/android/src/main/java/com/reactnativebleadvertiser/BleAdvertiserModule.kt @@ -4,6 +4,7 @@ import android.util.Log import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod +import dev.dotworld.ble.AppPreferences import dev.dotworld.ble.Utils class BleAdvertiserModule(reactContext: ReactApplicationContext) : @@ -13,6 +14,10 @@ class BleAdvertiserModule(reactContext: ReactApplicationContext) : private const val TAG = "BleAdvertiserModule" } + init { + AppPreferences.init(reactContext.applicationContext) + } + override fun getName(): String { return "BleAdvertiser" } @@ -20,17 +25,21 @@ class BleAdvertiserModule(reactContext: ReactApplicationContext) : @ReactMethod fun setUserId(userId: String) { Log.i(TAG, "setUserId: $userId") + AppPreferences.userId = userId } @ReactMethod fun resetUserId() { Log.i(TAG, "resetUserId") + AppPreferences.userId = null } @ReactMethod fun startService() { Log.i(TAG, "Start Service") - Utils.startBluetoothMonitoringService(reactApplicationContext) + if (AppPreferences.userId != null) { + Utils.startBluetoothMonitoringService(reactApplicationContext) + } } } diff --git a/android/src/main/java/dev/dotworld/ble/AppPreferences.kt b/android/src/main/java/dev/dotworld/ble/AppPreferences.kt new file mode 100644 index 0000000..a94d3fe --- /dev/null +++ b/android/src/main/java/dev/dotworld/ble/AppPreferences.kt @@ -0,0 +1,28 @@ +package dev.dotworld.ble + +import android.content.Context +import android.content.SharedPreferences + +object AppPreferences { + private const val NAME = "beambox" + private const val MODE = Context.MODE_PRIVATE + private lateinit var preferences: SharedPreferences + + private val USER_ID = Pair("user_id", null) + + fun init(context: Context) { + preferences = context.getSharedPreferences(NAME, MODE) + } + + private inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) { + val editor = edit() + operation(editor) + editor.apply() + } + + var userId: String? + get() = preferences.getString(USER_ID.first, USER_ID.second) + set(value) = preferences.edit { + it.putString(USER_ID.first, value) + } +} diff --git a/android/src/main/java/dev/dotworld/ble/bluetooth/gatt/GattServer.kt b/android/src/main/java/dev/dotworld/ble/bluetooth/gatt/GattServer.kt index 94b0cb6..9b641c9 100644 --- a/android/src/main/java/dev/dotworld/ble/bluetooth/gatt/GattServer.kt +++ b/android/src/main/java/dev/dotworld/ble/bluetooth/gatt/GattServer.kt @@ -58,16 +58,27 @@ class GattServer constructor(val context: Context, serviceUUIDString: String) { } device?.let { + Log.i(TAG, "onCharacteristicReadRequest from ${device.address}") + if (BlueTrace.supportsCharUUID(characteristic?.uuid)) { characteristic?.uuid?.let { charUUID -> val bluetraceImplementation = BlueTrace.getImplementation(charUUID) - val base = readPayloadMap.getOrPut(device.address) { + val base = readPayloadMap.getOrPut(device.address, { bluetraceImplementation.peripheral.prepareReadRequestData( bluetraceImplementation.versionInt ) - } + }) val value = base.copyOfRange(offset, base.size) + Log.i( + TAG, + "onCharacteristicReadRequest from ${device.address} - $requestId- $offset - ${ + String( + value, + Charsets.UTF_8 + ) + }" + ) bluetoothGattServer?.sendResponse( device, requestId, @@ -76,6 +87,7 @@ class GattServer constructor(val context: Context, serviceUUIDString: String) { value ) } + } else { Log.i(TAG, "unsupported characteristic UUID from ${device.address}") bluetoothGattServer?.sendResponse( @@ -84,6 +96,7 @@ class GattServer constructor(val context: Context, serviceUUIDString: String) { ) } } + } override fun onCharacteristicWriteRequest( @@ -103,7 +116,7 @@ class GattServer constructor(val context: Context, serviceUUIDString: String) { responseNeeded, offset, value - ) + ); Log.i(TAG, "onCharacteristicWriteRequest: ") value?.let { Log.i(TAG, "onCharacteristicWriteRequest: ${String(it)}") @@ -122,7 +135,9 @@ class GattServer constructor(val context: Context, serviceUUIDString: String) { } fun startServer(): Boolean { + bluetoothGattServer = bluetoothManager.openGattServer(context, gattServerCallback) + bluetoothGattServer?.let { it.clearServices() return true diff --git a/android/src/main/java/dev/dotworld/ble/protocol/v2/BlueTraceV2.kt b/android/src/main/java/dev/dotworld/ble/protocol/v2/BlueTraceV2.kt index 22d5f5d..dc2a131 100644 --- a/android/src/main/java/dev/dotworld/ble/protocol/v2/BlueTraceV2.kt +++ b/android/src/main/java/dev/dotworld/ble/protocol/v2/BlueTraceV2.kt @@ -1,14 +1,17 @@ package dev.dotworld.ble.protocol.v2 -import android.content.res.Resources import android.os.Build import android.util.Log -import com.reactnativebleadvertiser.R +import com.google.gson.Gson +import com.reactnativebleadvertiser.BuildConfig +import dev.dotworld.ble.AppPreferences +import dev.dotworld.ble.Utils import dev.dotworld.ble.protocol.BlueTraceProtocol import dev.dotworld.ble.protocol.PeripheralInterface import dev.dotworld.ble.streetpass.CentralDevice import dev.dotworld.ble.streetpass.ConnectionRecord import dev.dotworld.ble.streetpass.PeripheralDevice +import java.util.* class BlueTraceV2 : BlueTraceProtocol( @@ -23,10 +26,12 @@ class V2Peripheral : PeripheralInterface { } override fun prepareReadRequestData(protocolVersion: Int): ByteArray { - val publicKey = Resources.getSystem().getString(R.string.public_key) return V2ReadRequestPayload( v = protocolVersion, - id = "test",//Utils.encryptByPublicKey("test-${Date()}", publicKey), + id = Utils.encryptByPublicKey( + Gson().toJson(UserData(AppPreferences.userId ?: "", Date())), + BuildConfig.PUBLIC_KEY + ), o = "DW", peripheral = PeripheralDevice(Build.MODEL, "SELF") ).getPayload() diff --git a/android/src/main/java/dev/dotworld/ble/protocol/v2/V2ReadRequestPayload.kt b/android/src/main/java/dev/dotworld/ble/protocol/v2/V2ReadRequestPayload.kt index 2ae0642..28858a0 100644 --- a/android/src/main/java/dev/dotworld/ble/protocol/v2/V2ReadRequestPayload.kt +++ b/android/src/main/java/dev/dotworld/ble/protocol/v2/V2ReadRequestPayload.kt @@ -1,8 +1,11 @@ package dev.dotworld.ble.protocol.v2 +import android.os.Parcelable import com.google.gson.Gson import com.google.gson.GsonBuilder import dev.dotworld.ble.streetpass.PeripheralDevice +import kotlinx.parcelize.Parcelize +import java.util.* //acting as peripheral class V2ReadRequestPayload( @@ -27,3 +30,6 @@ class V2ReadRequestPayload( } } } + +@Parcelize +data class UserData(val id: String, val date: Date = Date()) : Parcelable diff --git a/package.json b/package.json index f8f0065..e51036d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "react-native-ble-advertiser", - "version": "0.1.0", + "name": "@teamdotworld/rn-ble-advertiser", + "version": "1.0.0", "description": "Advertise given message using BLE", "main": "lib/commonjs/index", "module": "lib/module/index", @@ -36,7 +36,10 @@ "ios", "android" ], - "repository": "https://github.com/TeamDotworld/react-native-ble-advertiser", + "repository": { + "type": "git", + "url": "https://github.com/TeamDotworld/react-native-ble-advertiser.git" + }, "author": "Naveen Sakthivel (https://dotworld.in)", "license": "MIT", "bugs": {