This source code is licensed under the MIT license found in the LICENSE file in the root
5 | * directory of this source tree.
6 | */
7 | package com.example.reactnativeusbserialportforandroid;
8 |
9 | import android.content.Context;
10 | import com.facebook.flipper.android.AndroidFlipperClient;
11 | import com.facebook.flipper.android.utils.FlipperUtils;
12 | import com.facebook.flipper.core.FlipperClient;
13 | import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin;
14 | import com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin;
15 | import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin;
16 | import com.facebook.flipper.plugins.inspector.DescriptorMapping;
17 | import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin;
18 | import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor;
19 | import com.facebook.flipper.plugins.network.NetworkFlipperPlugin;
20 | import com.facebook.flipper.plugins.react.ReactFlipperPlugin;
21 | import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin;
22 | import com.facebook.react.ReactInstanceManager;
23 | import com.facebook.react.bridge.ReactContext;
24 | import com.facebook.react.modules.network.NetworkingModule;
25 | import okhttp3.OkHttpClient;
26 |
27 | public class ReactNativeFlipper {
28 | public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) {
29 | if (FlipperUtils.shouldEnableFlipper(context)) {
30 | final FlipperClient client = AndroidFlipperClient.getInstance(context);
31 | client.addPlugin(new InspectorFlipperPlugin(context, DescriptorMapping.withDefaults()));
32 | client.addPlugin(new ReactFlipperPlugin());
33 | client.addPlugin(new DatabasesFlipperPlugin(context));
34 | client.addPlugin(new SharedPreferencesFlipperPlugin(context));
35 | client.addPlugin(CrashReporterPlugin.getInstance());
36 | NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin();
37 | NetworkingModule.setCustomClientBuilder(
38 | new NetworkingModule.CustomClientBuilder() {
39 | @Override
40 | public void apply(OkHttpClient.Builder builder) {
41 | builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
42 | }
43 | });
44 | client.addPlugin(networkFlipperPlugin);
45 | client.start();
46 | // Fresco Plugin needs to ensure that ImagePipelineFactory is initialized
47 | // Hence we run if after all native modules have been initialized
48 | ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
49 | if (reactContext == null) {
50 | reactInstanceManager.addReactInstanceEventListener(
51 | new ReactInstanceManager.ReactInstanceEventListener() {
52 | @Override
53 | public void onReactContextInitialized(ReactContext reactContext) {
54 | reactInstanceManager.removeReactInstanceEventListener(this);
55 | reactContext.runOnNativeModulesQueueThread(
56 | new Runnable() {
57 | @Override
58 | public void run() {
59 | client.addPlugin(new FrescoFlipperPlugin());
60 | }
61 | });
62 | }
63 | });
64 | } else {
65 | client.addPlugin(new FrescoFlipperPlugin());
66 | }
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-native-usb-serialport-for-android",
3 | "version": "0.5.0",
4 | "description": "react-native-usb-serialport-for-android",
5 | "main": "lib/commonjs/index",
6 | "module": "lib/module/index",
7 | "types": "lib/typescript/index.d.ts",
8 | "react-native": "src/index",
9 | "source": "src/index",
10 | "files": [
11 | "src",
12 | "lib",
13 | "android",
14 | "cpp",
15 | "!lib/typescript/example",
16 | "!android/build",
17 | "!**/__tests__",
18 | "!**/__fixtures__",
19 | "!**/__mocks__"
20 | ],
21 | "scripts": {
22 | "test": "jest",
23 | "typescript": "tsc --noEmit",
24 | "lint": "eslint \"**/*.{js,ts,tsx}\"",
25 | "prepare": "bob build",
26 | "release": "release-it",
27 | "example": "yarn --cwd example",
28 | "pods": "cd example && pod-install --quiet",
29 | "bootstrap": "yarn example && yarn && yarn pods",
30 | "docs": "typedoc --out docs src/index.tsx"
31 | },
32 | "keywords": [
33 | "react-native",
34 | "android",
35 | "usb",
36 | "serialport"
37 | ],
38 | "repository": "https://github.com/bastengao/react-native-usb-serialport-for-android",
39 | "author": "bastengao Generated using TypeDoc Generated using TypeDoc Generated using TypeDoc Generated using TypeDoc React Native USB serialport module for Android platform based on mik3y/usb-serial-for-android See documentation for details. See the contributing guide to learn how to contribute to the repository and the development workflow. MIT Generated using TypeDoc Generated using TypeDoc May return error with these codes: See Codes May return error with these codes: See Codes Return true if already has permission, otherwise will request permission and return false. Generated using TypeDoc May return error with these codes: See Codes Promise Listen to data received event. EventSubscription Send data with hex string. May return error with these codes: See Codes Generated using TypeDocInterface EventData
Interface Device
Interface OpenOptions
Enumeration Parity
react-native-usb-serialport-for-android
react-native-usb-serialport-for-android
4 |
5 | Installation
9 |
10 |
12 |
13 |
14 | npm install react-native-usb-serialport-for-android --save
11 | Usage
15 |
16 |
18 | import { UsbSerialManager, Parity, Codes } from "react-native-usb-serialport-for-android";
// ...
const devices = await UsbSerialManager.list();
try {
await UsbSerialManager.tryRequestPermission(2004);
const usbSerialport = await UsbSerialManager.open(2004, { baudRate: 38400, parity: Parity.None, dataBits: 8, stopBits: 1 });
const sub = usbSerialport.onReceived((event) => {
console.log(event.deviceId, event.data);
});
// unsubscribe
// sub.remove();
await usbSerialport.send('00FF');
usbSerialport.close();
} catch(err) {
console.log(err);
if (err.code === Codes.DEVICE_NOT_FOND) {
// ...
}
}
17 | Contributing
22 |
23 | License
27 |
28 | react-native-usb-serialport-for-android
Index
Enumerations
Classes
Interfaces
Type aliases
Variables
Type aliases
Listener
Type declaration
Parameters
data: EventData
Returns void
Variables
Codes
Type declaration
DEVICE_
DEVICE_
DEVICE_
DRIVER_
NOT_
OPEN_
PERMISSION_
SEND_
Usb
Interface Manager
Hierarchy
Index
Methods
has
4 |
6 | Parameters
deviceId: number
Returns Promise<boolean>
list
Returns Promise<Device[]>
open
11 |
17 | Parameters
deviceId: number
options: OpenOptions
Returns Promise<UsbSerial>
try
Parameters
deviceId: number
Returns Promise<boolean>
Class UsbSerial
Hierarchy
Index
Constructors
Properties
Methods
Constructors
constructor
Parameters
deviceId: number
eventEmitter: EventEmitter
Returns UsbSerial
Properties
device
Private event
Private listeners
Private subscriptions
Methods
close
4 |
6 | Returns Promise<any>
on
Parameters
listener: Listener
Returns EmitterSubscription
send
15 |
18 | Parameters
hexStr: string
Returns Promise<null>
hex format
3 |