Skip to content

Commit b19b1e2

Browse files
committed
updated logic for v1.4.39
1 parent 66fca94 commit b19b1e2

File tree

4 files changed

+147
-12
lines changed

4 files changed

+147
-12
lines changed

README.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
# react-native-capture 1.4.32
1+
# react-native-capture 1.4.39
22

33
This react native module allows a React Native application to use and control Socket Mobile wireless barcode scanners, NFC Reader/Writer, and Camera (iOS only) to capture and deliver data capture to such application.
44

55
# Devices compatibility and CaptureSDK versions
66

7-
| Devices | <= 1.2 | 1.3 | 1.4 |
8-
| :--------------------------------------------: | :----: | :-: | :-: |
9-
| **SocketCam C860** ||||
10-
| **SocketCam C820** ||||
11-
| **S720/D720/S820** ||||
12-
| **D600, S550, and all other barcode scanners** ||||
13-
| **S370** ||||
7+
| Devices | <= 1.2 | 1.3 | 1.4 |
8+
| :--------------------------------------------: | :----: | :-: | :-----: |
9+
| **SocketCam C860** |||[^1] |
10+
| **SocketCam C820** ||||
11+
| **S720/D720/S820** ||||
12+
| **D600, S550, and all other barcode scanners** ||||
13+
| **S370** ||||
14+
15+
[^1]: _SocketCam C860 is currently supported on the latest version of Android only. More on SocketCam can be found [here](https://www.socketmobile.com/readers-accessories/product-families/socketcam)._
1416

1517
## Getting started
1618

@@ -217,6 +219,40 @@ Next, in their `app/gradle.build` file, they will need to add the below code.
217219
}
218220
```
219221

222+
## Enable Start Capture Service on Android
223+
224+
In order enable Start Capture Service on Android (so that the developer does't need to actively run/open Companion on the same device), the developer will need to add some security configurations to allow this service to run in the background of their app.
225+
226+
First, in `your-app/android/app/src/main/res`, there needs to be an `xml` directory if there isn't one already. In that directory, you will need to add the file `network_security_config.xml`. That file should contain the below information.
227+
228+
```
229+
<?xml version="1.0" encoding="utf-8"?>
230+
<network-security-config>
231+
<base-config cleartextTrafficPermitted="false" />
232+
<domain-config cleartextTrafficPermitted="true">
233+
<domain includeSubdomains="false">localhost</domain>
234+
<domain includeSubdomains="false">127.0.0.1</domain>
235+
</domain-config>
236+
</network-security-config>
237+
238+
```
239+
240+
Then, in their app's `AndroidManifest.xml` file, the developer will need to add the below property into the `<application>` tag.
241+
242+
```
243+
android:networkSecurityConfig="@xml/network_security_config"
244+
```
245+
246+
Finally, add the below line into just before the `AndroidManifest.xml` file's closing `</manifest>` tag.
247+
248+
```
249+
<queries>
250+
<package android:name="com.socketmobile.companion"/>
251+
</queries>
252+
```
253+
254+
For more on the network security configuration for Android, please check out the cleartext section in [the Android docs](https://docs.socketmobile.com/capture/java/en/latest/android/getting-started.html#enable-cleartext-traffic).
255+
220256
## Upgrading to iOS SDK Version 1.8.34
221257

222258
I needed to deintegrate pods and then re-install them using `pod install --repo-update`. Doing a regular `pod install` gave me the error the below error.

android/src/main/java/com/socketmobile/CaptureModule.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,17 @@ public void startSocketCamExtension(int clientHandle){
7676

7777
@ReactMethod
7878
public void startCaptureService(Promise promise) {
79-
Context context = getReactApplicationContext();
80-
context.sendBroadcast(getStartIntent());
81-
promise.resolve(null);
79+
try {
80+
Context context = getReactApplicationContext();
81+
String serviceStarted = CaptureService.start(context);
82+
if (serviceStarted.indexOf("success") != -1){
83+
promise.resolve(serviceStarted);
84+
} else {
85+
promise.resolve("Could not start capture service.");
86+
}
87+
} catch (Exception e) {
88+
promise.reject("CAPTURE_SERVICE_ERROR", "Error starting capture service", e);
89+
}
8290
}
8391

8492
public void createExtensionEventAndTrigger(String message, int status){
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.socketmobile;
2+
3+
import android.annotation.SuppressLint;
4+
import android.content.ComponentName;
5+
import android.content.Context;
6+
import android.content.Intent;
7+
import android.content.pm.PackageManager;
8+
import android.net.Uri;
9+
import android.os.Build;
10+
import android.util.Log;
11+
12+
import static android.content.Intent.FLAG_RECEIVER_FOREGROUND;
13+
14+
import com.facebook.react.bridge.ReactContext;
15+
16+
import java.util.logging.Logger;
17+
18+
final class CaptureService {
19+
private static final String BASE_PACKAGE = "com.socketmobile.capture";
20+
private static final String SERVICE_APP_ID = "com.socketmobile.companion";
21+
private static final String BROADCAST_RECEIVER = BASE_PACKAGE + ".StartService";
22+
private static final String ACTION = BASE_PACKAGE + ".START_SERVICE";
23+
private static final String SERVICE_NAME = BASE_PACKAGE + ".AndroidService";
24+
25+
private static final Logger log = Logger.getLogger(CaptureService.class.getName());
26+
27+
private CaptureService() {
28+
throw new UnsupportedOperationException(
29+
this.getClass().getName() + " is a utility class. Do not instantiate.");
30+
}
31+
32+
static Intent getInstallIntent(Context context) {
33+
Uri marketUri = Uri.parse("market://details?id=" + SERVICE_APP_ID);
34+
Uri httpUri = Uri.parse("https://play.google.com/store/apps/details?id=" + SERVICE_APP_ID);
35+
36+
Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
37+
if (systemCanHandleIntent(context, marketIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
38+
return marketIntent;
39+
} else {
40+
return new Intent(Intent.ACTION_VIEW, httpUri);
41+
}
42+
43+
}
44+
45+
@SuppressLint("InlinedApi") static Intent getStartIntent() {
46+
return new Intent(ACTION)
47+
.setFlags(FLAG_RECEIVER_FOREGROUND)
48+
.setComponent(new ComponentName(SERVICE_APP_ID, BROADCAST_RECEIVER));
49+
}
50+
51+
static boolean isInstalled(Context context) {
52+
return systemCanHandleBroadcast(context, getStartIntent(), 0);
53+
}
54+
55+
static void install(Context context) {
56+
context.startActivity(getInstallIntent(context));
57+
}
58+
59+
public static String start(Context context) {
60+
try {
61+
Intent serviceIntent = getExplicitStartIntent(context);
62+
if (serviceIntent != null) {
63+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
64+
context.startForegroundService(serviceIntent);
65+
return "successfully started with startForegroundService";
66+
}
67+
context.sendBroadcast(getStartIntent());
68+
}
69+
} catch (Exception e) {
70+
Log.d("CaptureService", "Could not start with startForeground Service: " + e.getMessage());
71+
e.printStackTrace();
72+
context.sendBroadcast(getStartIntent());
73+
return "successfully started with sendBroadcast";
74+
}
75+
return "Error starting service: Unknown error";
76+
}
77+
78+
private static Intent getExplicitStartIntent(Context context) {
79+
return new Intent()
80+
.setComponent(new ComponentName(SERVICE_APP_ID, SERVICE_NAME));
81+
}
82+
83+
private static boolean systemCanHandleIntent(Context ctx, Intent i, int flags) {
84+
return ctx.getPackageManager().queryIntentActivities(i, flags).size() > 0;
85+
}
86+
87+
private static boolean systemCanHandleBroadcast(Context ctx, Intent i, int flags) {
88+
return ctx.getPackageManager().queryBroadcastReceivers(i, flags).size() > 0;
89+
}
90+
}
91+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-native-capture",
33
"title": "React Native Capture",
4-
"version": "1.4.32",
4+
"version": "1.4.39",
55
"description": "Socket Mobile CaptureSDK for React Native",
66
"main": "index.js",
77
"files": [

0 commit comments

Comments
 (0)