react-native-web-serial-api 0.0.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +198 -104
- package/TESTING.md +542 -0
- package/android/build.gradle +16 -2
- package/android/src/main/java/dev/webserialapi/NativeUsbSerialModule.java +74 -11
- package/android/src/main/java/dev/webserialapi/PortPickerActivity.java +61 -59
- package/bin/expose-serial.js +205 -0
- package/lib/commonjs/UsbSerial.js +58 -26
- package/lib/commonjs/UsbSerial.js.map +1 -1
- package/lib/commonjs/WebSerial.js +273 -77
- package/lib/commonjs/WebSerial.js.map +1 -1
- package/lib/commonjs/index.js +15 -3
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/lib/dom-exception.js +176 -0
- package/lib/commonjs/lib/dom-exception.js.map +1 -0
- package/lib/commonjs/lib/event-target.js +140 -0
- package/lib/commonjs/lib/event-target.js.map +1 -0
- package/lib/commonjs/lib/promise.js +23 -0
- package/lib/commonjs/lib/promise.js.map +1 -0
- package/lib/commonjs/lib/web-streams.js +42 -0
- package/lib/commonjs/lib/web-streams.js.map +1 -0
- package/lib/commonjs/testing/device-fixture.js +70 -0
- package/lib/commonjs/testing/device-fixture.js.map +1 -0
- package/lib/commonjs/testing/expose.js +91 -0
- package/lib/commonjs/testing/expose.js.map +1 -0
- package/lib/commonjs/testing/harness.js +98 -0
- package/lib/commonjs/testing/harness.js.map +1 -0
- package/lib/commonjs/testing/in-memory-serial-transport.js +653 -0
- package/lib/commonjs/testing/in-memory-serial-transport.js.map +1 -0
- package/lib/commonjs/testing/index.js +153 -0
- package/lib/commonjs/testing/index.js.map +1 -0
- package/lib/commonjs/testing/install-in-memory-serial-transport.js +54 -0
- package/lib/commonjs/testing/install-in-memory-serial-transport.js.map +1 -0
- package/lib/commonjs/testing/serial-client.js +277 -0
- package/lib/commonjs/testing/serial-client.js.map +1 -0
- package/lib/commonjs/testing/simulated-device.js +164 -0
- package/lib/commonjs/testing/simulated-device.js.map +1 -0
- package/lib/commonjs/testing/test-suite.js +142 -0
- package/lib/commonjs/testing/test-suite.js.map +1 -0
- package/lib/commonjs/transport.js +61 -0
- package/lib/commonjs/transport.js.map +1 -0
- package/lib/commonjs/websocket/WebSocketSerialTransport.js +659 -0
- package/lib/commonjs/websocket/WebSocketSerialTransport.js.map +1 -0
- package/lib/commonjs/websocket/bridge.js +234 -0
- package/lib/commonjs/websocket/bridge.js.map +1 -0
- package/lib/commonjs/websocket/index.js +33 -0
- package/lib/commonjs/websocket/index.js.map +1 -0
- package/lib/commonjs/websocket/protocol.js +55 -0
- package/lib/commonjs/websocket/protocol.js.map +1 -0
- package/lib/commonjs/websocket/serial-device-bridge.js +130 -0
- package/lib/commonjs/websocket/serial-device-bridge.js.map +1 -0
- package/lib/typescript/src/UsbSerial.d.ts +24 -67
- package/lib/typescript/src/UsbSerial.d.ts.map +1 -1
- package/lib/typescript/src/WebSerial.d.ts +16 -7
- package/lib/typescript/src/WebSerial.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +3 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/lib/dom-exception.d.ts +100 -0
- package/lib/typescript/src/lib/dom-exception.d.ts.map +1 -0
- package/lib/typescript/src/lib/event-target.d.ts +55 -0
- package/lib/typescript/src/lib/event-target.d.ts.map +1 -0
- package/lib/typescript/src/lib/promise.d.ts +11 -0
- package/lib/typescript/src/lib/promise.d.ts.map +1 -0
- package/lib/typescript/src/lib/web-streams.d.ts +9 -0
- package/lib/typescript/src/lib/web-streams.d.ts.map +1 -0
- package/lib/typescript/src/testing/device-fixture.d.ts +70 -0
- package/lib/typescript/src/testing/device-fixture.d.ts.map +1 -0
- package/lib/typescript/src/testing/expose.d.ts +71 -0
- package/lib/typescript/src/testing/expose.d.ts.map +1 -0
- package/lib/typescript/src/testing/harness.d.ts +34 -0
- package/lib/typescript/src/testing/harness.d.ts.map +1 -0
- package/lib/typescript/src/testing/in-memory-serial-transport.d.ts +216 -0
- package/lib/typescript/src/testing/in-memory-serial-transport.d.ts.map +1 -0
- package/lib/typescript/src/testing/index.d.ts +33 -0
- package/lib/typescript/src/testing/index.d.ts.map +1 -0
- package/lib/typescript/src/testing/install-in-memory-serial-transport.d.ts +25 -0
- package/lib/typescript/src/testing/install-in-memory-serial-transport.d.ts.map +1 -0
- package/lib/typescript/src/testing/serial-client.d.ts +62 -0
- package/lib/typescript/src/testing/serial-client.d.ts.map +1 -0
- package/lib/typescript/src/testing/simulated-device.d.ts +127 -0
- package/lib/typescript/src/testing/simulated-device.d.ts.map +1 -0
- package/lib/typescript/src/testing/test-suite.d.ts +75 -0
- package/lib/typescript/src/testing/test-suite.d.ts.map +1 -0
- package/lib/typescript/src/transport.d.ts +131 -0
- package/lib/typescript/src/transport.d.ts.map +1 -0
- package/lib/typescript/src/websocket/WebSocketSerialTransport.d.ts +111 -0
- package/lib/typescript/src/websocket/WebSocketSerialTransport.d.ts.map +1 -0
- package/lib/typescript/src/websocket/bridge.d.ts +66 -0
- package/lib/typescript/src/websocket/bridge.d.ts.map +1 -0
- package/lib/typescript/src/websocket/index.d.ts +19 -0
- package/lib/typescript/src/websocket/index.d.ts.map +1 -0
- package/lib/typescript/src/websocket/protocol.d.ts +64 -0
- package/lib/typescript/src/websocket/protocol.d.ts.map +1 -0
- package/lib/typescript/src/websocket/serial-device-bridge.d.ts +32 -0
- package/lib/typescript/src/websocket/serial-device-bridge.d.ts.map +1 -0
- package/package.json +57 -3
- package/src/UsbSerial.ts +65 -90
- package/src/WebSerial.ts +351 -113
- package/src/index.ts +6 -8
- package/src/lib/dom-exception.ts +129 -60
- package/src/lib/event-target.ts +58 -21
- package/src/lib/promise.ts +7 -7
- package/src/lib/web-streams.ts +43 -0
- package/src/testing/device-fixture.ts +150 -0
- package/src/testing/expose.ts +147 -0
- package/src/testing/harness.ts +124 -0
- package/src/testing/in-memory-serial-transport.ts +840 -0
- package/src/testing/index.ts +90 -0
- package/src/testing/install-in-memory-serial-transport.ts +65 -0
- package/src/testing/serial-client.ts +313 -0
- package/src/testing/simulated-device.ts +193 -0
- package/src/testing/test-suite.ts +186 -0
- package/src/transport.ts +200 -0
- package/src/websocket/WebSocketSerialTransport.ts +796 -0
- package/src/websocket/bridge.ts +299 -0
- package/src/websocket/index.ts +38 -0
- package/src/websocket/protocol.ts +101 -0
- package/src/websocket/serial-device-bridge.ts +160 -0
- package/babel.config.js +0 -3
- package/biome.json +0 -35
- package/example/.watchmanconfig +0 -1
- package/example/App.tsx +0 -71
- package/example/__tests__/App.test.tsx +0 -16
- package/example/__tests__/connectEvents.test.tsx +0 -81
- package/example/__tests__/getPorts.test.tsx +0 -140
- package/example/android/app/build.gradle +0 -120
- package/example/android/app/debug.keystore +0 -0
- package/example/android/app/proguard-rules.pro +0 -10
- package/example/android/app/src/debug/AndroidManifest.xml +0 -9
- package/example/android/app/src/main/AndroidManifest.xml +0 -38
- package/example/android/app/src/main/java/dev/uzlopak/MainActivity.kt +0 -22
- package/example/android/app/src/main/java/dev/uzlopak/MainApplication.kt +0 -41
- package/example/android/app/src/main/res/drawable/rn_edit_text_material.xml +0 -37
- package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png +0 -0
- package/example/android/app/src/main/res/values/strings.xml +0 -3
- package/example/android/app/src/main/res/values/styles.xml +0 -9
- package/example/android/build.gradle +0 -22
- package/example/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/example/android/gradle/wrapper/gradle-wrapper.properties +0 -7
- package/example/android/gradle.properties +0 -47
- package/example/android/gradlew +0 -252
- package/example/android/gradlew.bat +0 -94
- package/example/android/settings.gradle +0 -6
- package/example/app.json +0 -4
- package/example/babel.config.js +0 -21
- package/example/biome.json +0 -47
- package/example/deploy.sh +0 -11
- package/example/index.html +0 -26
- package/example/index.js +0 -9
- package/example/index.web.js +0 -8
- package/example/jest.config.js +0 -12
- package/example/metro.config.js +0 -58
- package/example/package-lock.json +0 -14510
- package/example/package.json +0 -48
- package/example/react-native.config.js +0 -17
- package/example/src/components/AppBar.tsx +0 -73
- package/example/src/components/Menu.tsx +0 -90
- package/example/src/components/SingleChoiceDialog.tsx +0 -120
- package/example/src/screens/ConnectScreen.tsx +0 -195
- package/example/src/screens/DevicesScreen.tsx +0 -252
- package/example/src/screens/TerminalScreen.tsx +0 -572
- package/example/src/settings.ts +0 -43
- package/example/src/theme.ts +0 -19
- package/example/src/util/TextUtil.ts +0 -129
- package/example/tsconfig.json +0 -10
- package/example/vite.config.mjs +0 -55
- package/scripts/deploy-release.sh +0 -127
- package/tsconfig.build.json +0 -7
- package/tsconfig.json +0 -20
package/example/App.tsx
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import {StatusBar, StyleSheet, View} from 'react-native';
|
|
3
|
-
import type {SerialPort} from 'react-native-web-serial-api';
|
|
4
|
-
import {ConnectScreen} from './src/screens/ConnectScreen';
|
|
5
|
-
import {DevicesScreen} from './src/screens/DevicesScreen';
|
|
6
|
-
import {TerminalScreen} from './src/screens/TerminalScreen';
|
|
7
|
-
import {type ConnectionSettings, DEFAULT_SETTINGS} from './src/settings';
|
|
8
|
-
import {colors} from './src/theme';
|
|
9
|
-
|
|
10
|
-
type Screen = 'devices' | 'connect' | 'terminal';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A React Native clone of Kai Morich's SimpleUsbTerminal, built on the
|
|
14
|
-
* react-native-web-serial-api Web Serial API. Runs on Android and (Chromium) web.
|
|
15
|
-
*/
|
|
16
|
-
function App(): React.JSX.Element {
|
|
17
|
-
const [screen, setScreen] = React.useState<Screen>('devices');
|
|
18
|
-
const [port, setPort] = React.useState<SerialPort | null>(null);
|
|
19
|
-
const [settings, setSettings] =
|
|
20
|
-
React.useState<ConnectionSettings>(DEFAULT_SETTINGS);
|
|
21
|
-
|
|
22
|
-
// Devices -> Connect form
|
|
23
|
-
const selectPort = (p: SerialPort) => {
|
|
24
|
-
setPort(p);
|
|
25
|
-
setScreen('connect');
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
// Connect form -> Terminal
|
|
29
|
-
const startTerminal = (s: ConnectionSettings) => {
|
|
30
|
-
setSettings(s);
|
|
31
|
-
setScreen('terminal');
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const backToDevices = () => {
|
|
35
|
-
setScreen('devices');
|
|
36
|
-
setPort(null);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const backToConnect = () => setScreen('connect');
|
|
40
|
-
|
|
41
|
-
return (
|
|
42
|
-
<View style={styles.root}>
|
|
43
|
-
<StatusBar
|
|
44
|
-
barStyle="light-content"
|
|
45
|
-
backgroundColor={colors.primaryDark}
|
|
46
|
-
/>
|
|
47
|
-
{screen === 'terminal' && port ? (
|
|
48
|
-
<TerminalScreen
|
|
49
|
-
port={port}
|
|
50
|
-
settings={settings}
|
|
51
|
-
onBack={backToConnect}
|
|
52
|
-
/>
|
|
53
|
-
) : screen === 'connect' && port ? (
|
|
54
|
-
<ConnectScreen
|
|
55
|
-
port={port}
|
|
56
|
-
initial={settings}
|
|
57
|
-
onBack={backToDevices}
|
|
58
|
-
onConnect={startTerminal}
|
|
59
|
-
/>
|
|
60
|
-
) : (
|
|
61
|
-
<DevicesScreen onSelect={selectPort} />
|
|
62
|
-
)}
|
|
63
|
-
</View>
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const styles = StyleSheet.create({
|
|
68
|
-
root: {flex: 1, backgroundColor: colors.background},
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
export default App;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @format
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// Note: import explicitly to use the types shipped with jest.
|
|
6
|
-
import {expect, it} from '@jest/globals';
|
|
7
|
-
import {render, screen, waitFor} from '@testing-library/react-native';
|
|
8
|
-
import App from '../App';
|
|
9
|
-
|
|
10
|
-
it('renders the devices screen', async () => {
|
|
11
|
-
render(<App />);
|
|
12
|
-
expect(screen.getByText('USB Devices')).toBeTruthy();
|
|
13
|
-
// DevicesScreen kicks off an async serial.getPorts() refresh on mount; let it
|
|
14
|
-
// settle so the state update is wrapped in act() (avoids a console warning).
|
|
15
|
-
await waitFor(() => expect(screen.queryByText('USB Devices')).toBeTruthy());
|
|
16
|
-
});
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @format
|
|
3
|
-
*
|
|
4
|
-
* Verifies that subscribing to Serial "connect"/"disconnect" wires up the
|
|
5
|
-
* native USB state listeners WITHOUT first calling getPorts()/requestPort(),
|
|
6
|
-
* and that a native event (e.g. the "connect" emitted when USB permission is
|
|
7
|
-
* granted) reaches the JS listener. This is what lets the example's device
|
|
8
|
-
* list auto-refresh on attach / detach / permission-grant.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import {afterEach, beforeEach, expect, it, jest} from '@jest/globals';
|
|
12
|
-
import {NativeModules} from 'react-native';
|
|
13
|
-
|
|
14
|
-
// Capture the JS listeners registered against the native event emitter so the
|
|
15
|
-
// test can drive them like the native side would.
|
|
16
|
-
const nativeListeners: Record<string, ((event: unknown) => void)[]> = {};
|
|
17
|
-
|
|
18
|
-
beforeEach(() => {
|
|
19
|
-
for (const k of Object.keys(nativeListeners)) {
|
|
20
|
-
delete nativeListeners[k];
|
|
21
|
-
}
|
|
22
|
-
(NativeModules as any).NativeUsbSerial = {
|
|
23
|
-
findAllDrivers: jest.fn(async () => []),
|
|
24
|
-
addListener: jest.fn(),
|
|
25
|
-
removeListeners: jest.fn(),
|
|
26
|
-
};
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
afterEach(() => {
|
|
30
|
-
// RCTDeviceEventEmitter is a global singleton across the RN copy; clear it so
|
|
31
|
-
// listeners from one test don't leak into the next.
|
|
32
|
-
const {DeviceEventEmitter} = require('react-native');
|
|
33
|
-
DeviceEventEmitter.removeAllListeners('connect');
|
|
34
|
-
DeviceEventEmitter.removeAllListeners('disconnect');
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
function freshSerial() {
|
|
38
|
-
let api: any;
|
|
39
|
-
jest.isolateModules(() => {
|
|
40
|
-
api = require('react-native-web-serial-api');
|
|
41
|
-
});
|
|
42
|
-
return new api.Serial();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Emit a native device event the way the TurboModule does (via
|
|
46
|
-
// RCTDeviceEventEmitter, which NativeEventEmitter listens on).
|
|
47
|
-
function emitNative(event: 'connect' | 'disconnect', payload: unknown) {
|
|
48
|
-
const {DeviceEventEmitter} = require('react-native');
|
|
49
|
-
DeviceEventEmitter.emit(event, payload);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
it('addEventListener("connect") receives native events without calling getPorts() first', () => {
|
|
53
|
-
const serial = freshSerial();
|
|
54
|
-
const onConnect = jest.fn();
|
|
55
|
-
|
|
56
|
-
// Subscribe only — no getPorts()/requestPort() beforehand.
|
|
57
|
-
serial.addEventListener('connect', onConnect);
|
|
58
|
-
|
|
59
|
-
// Native fires "connect" (e.g. attach or permission-grant).
|
|
60
|
-
emitNative('connect', {
|
|
61
|
-
deviceId: 7,
|
|
62
|
-
usbVendorId: 0x0403,
|
|
63
|
-
usbProductId: 0x6001,
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
expect(onConnect).toHaveBeenCalledTimes(1);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('forwards native disconnect to Serial listeners', () => {
|
|
70
|
-
const serial = freshSerial();
|
|
71
|
-
const onDisconnect = jest.fn();
|
|
72
|
-
serial.addEventListener('disconnect', onDisconnect);
|
|
73
|
-
|
|
74
|
-
emitNative('disconnect', {
|
|
75
|
-
deviceId: 7,
|
|
76
|
-
usbVendorId: 0x0403,
|
|
77
|
-
usbProductId: 0x6001,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
expect(onDisconnect).toHaveBeenCalledTimes(1);
|
|
81
|
-
});
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @format
|
|
3
|
-
*
|
|
4
|
-
* Android/native permission-model contract for the Web Serial polyfill.
|
|
5
|
-
*
|
|
6
|
-
* getPorts() enumerates every probed USB-serial port the app can currently
|
|
7
|
-
* access through Android USB permission (PortId.hasPermission === true) —
|
|
8
|
-
* whether that permission came from requestPort() or was granted natively
|
|
9
|
-
* (e.g. the system "use by default for this device" attach dialog). Ports the
|
|
10
|
-
* app cannot access yet are excluded.
|
|
11
|
-
*
|
|
12
|
-
* requestPort() is unchanged: it shows the native picker and resolves with the
|
|
13
|
-
* chosen port; it does not consult getPorts()/hasPermission.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';
|
|
17
|
-
import {NativeModules} from 'react-native';
|
|
18
|
-
|
|
19
|
-
type FakePort = {
|
|
20
|
-
deviceId: number;
|
|
21
|
-
portNumber: number;
|
|
22
|
-
usbVendorId: number;
|
|
23
|
-
usbProductId: number;
|
|
24
|
-
hasPermission: boolean;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const findAllDrivers = jest.fn<() => Promise<FakePort[]>>();
|
|
28
|
-
const showPortPicker = jest.fn<() => Promise<FakePort>>();
|
|
29
|
-
|
|
30
|
-
// In jest, TurboModuleRegistry.get() falls back to NativeModules[name], so
|
|
31
|
-
// registering a fake here lets the real library (Serial -> UsbSerialModule)
|
|
32
|
-
// run against controlled data. addListener/removeListeners are required by
|
|
33
|
-
// NativeEventEmitter.
|
|
34
|
-
beforeEach(() => {
|
|
35
|
-
(NativeModules as any).NativeUsbSerial = {
|
|
36
|
-
findAllDrivers,
|
|
37
|
-
showPortPicker,
|
|
38
|
-
addListener: jest.fn(),
|
|
39
|
-
removeListeners: jest.fn(),
|
|
40
|
-
};
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
afterEach(() => {
|
|
44
|
-
findAllDrivers.mockReset();
|
|
45
|
-
showPortPicker.mockReset();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// Fresh module graph each time so the Serial singleton / known-port cache does
|
|
49
|
-
// not leak between tests.
|
|
50
|
-
function freshSerial() {
|
|
51
|
-
let api: any;
|
|
52
|
-
jest.isolateModules(() => {
|
|
53
|
-
api = require('react-native-web-serial-api');
|
|
54
|
-
});
|
|
55
|
-
return new api.Serial();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const ftdi: FakePort = {
|
|
59
|
-
deviceId: 1,
|
|
60
|
-
portNumber: 0,
|
|
61
|
-
usbVendorId: 0x0403,
|
|
62
|
-
usbProductId: 0x6001,
|
|
63
|
-
hasPermission: true,
|
|
64
|
-
};
|
|
65
|
-
const cp210xUnpermitted: FakePort = {
|
|
66
|
-
deviceId: 2,
|
|
67
|
-
portNumber: 0,
|
|
68
|
-
usbVendorId: 0x10c4,
|
|
69
|
-
usbProductId: 0xea60,
|
|
70
|
-
hasPermission: false,
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
describe('getPorts() Android permission filtering', () => {
|
|
74
|
-
it('includes a device granted natively (never via requestPort)', async () => {
|
|
75
|
-
findAllDrivers.mockResolvedValueOnce([ftdi]);
|
|
76
|
-
const ports = await freshSerial().getPorts();
|
|
77
|
-
expect(ports).toHaveLength(1);
|
|
78
|
-
expect(ports[0].getInfo()).toEqual({
|
|
79
|
-
usbVendorId: 0x0403,
|
|
80
|
-
usbProductId: 0x6001,
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('excludes a probed device the app has no permission for', async () => {
|
|
85
|
-
findAllDrivers.mockResolvedValueOnce([cp210xUnpermitted]);
|
|
86
|
-
const ports = await freshSerial().getPorts();
|
|
87
|
-
expect(ports).toHaveLength(0);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('returns only the permitted subset when mixed', async () => {
|
|
91
|
-
findAllDrivers.mockResolvedValueOnce([ftdi, cp210xUnpermitted]);
|
|
92
|
-
const ports = await freshSerial().getPorts();
|
|
93
|
-
expect(ports).toHaveLength(1);
|
|
94
|
-
expect(ports[0].getInfo().usbVendorId).toBe(0x0403);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('returns an empty list when nothing is permitted', async () => {
|
|
98
|
-
findAllDrivers.mockResolvedValueOnce([
|
|
99
|
-
cp210xUnpermitted,
|
|
100
|
-
{...cp210xUnpermitted, deviceId: 3},
|
|
101
|
-
]);
|
|
102
|
-
expect(await freshSerial().getPorts()).toHaveLength(0);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('lists each port of a multi-port permitted device', async () => {
|
|
106
|
-
findAllDrivers.mockResolvedValueOnce([
|
|
107
|
-
{...ftdi, portNumber: 0},
|
|
108
|
-
{...ftdi, portNumber: 1},
|
|
109
|
-
]);
|
|
110
|
-
const ports = await freshSerial().getPorts();
|
|
111
|
-
expect(ports).toHaveLength(2);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('returns the SAME SerialPort instance across repeated calls', async () => {
|
|
115
|
-
const serial = freshSerial();
|
|
116
|
-
findAllDrivers.mockResolvedValue([ftdi]);
|
|
117
|
-
const first = await serial.getPorts();
|
|
118
|
-
const second = await serial.getPorts();
|
|
119
|
-
expect(first[0]).toBe(second[0]); // cached, not re-created
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
describe('requestPort() is unaffected by the permission filter', () => {
|
|
124
|
-
it('resolves with the picked port even if getPorts would exclude it', async () => {
|
|
125
|
-
// getPorts sees nothing permitted...
|
|
126
|
-
findAllDrivers.mockResolvedValue([]);
|
|
127
|
-
// ...but the user picks a device through the native picker.
|
|
128
|
-
showPortPicker.mockResolvedValueOnce(ftdi);
|
|
129
|
-
|
|
130
|
-
const serial = freshSerial();
|
|
131
|
-
expect(await serial.getPorts()).toHaveLength(0);
|
|
132
|
-
|
|
133
|
-
const port = await serial.requestPort();
|
|
134
|
-
expect(port.getInfo()).toEqual({
|
|
135
|
-
usbVendorId: 0x0403,
|
|
136
|
-
usbProductId: 0x6001,
|
|
137
|
-
});
|
|
138
|
-
expect(showPortPicker).toHaveBeenCalledTimes(1);
|
|
139
|
-
});
|
|
140
|
-
});
|
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
apply plugin: "com.android.application"
|
|
2
|
-
apply plugin: "org.jetbrains.kotlin.android"
|
|
3
|
-
apply plugin: "com.facebook.react"
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* This is the configuration block to customize your React Native Android app.
|
|
7
|
-
* By default you don't need to apply any configuration, just uncomment the lines you need.
|
|
8
|
-
*/
|
|
9
|
-
react {
|
|
10
|
-
/* Folders */
|
|
11
|
-
// The root of your project, i.e. where "package.json" lives. Default is '../..'
|
|
12
|
-
// root = file("../../")
|
|
13
|
-
// The folder where the react-native NPM package is. Default is ../../node_modules/react-native
|
|
14
|
-
// reactNativeDir = file("../../node_modules/react-native")
|
|
15
|
-
// The folder where the react-native Codegen package is. Default is ../../node_modules/@react-native/codegen
|
|
16
|
-
// codegenDir = file("../../node_modules/@react-native/codegen")
|
|
17
|
-
// The cli.js file which is the React Native CLI entrypoint. Default is ../../node_modules/react-native/cli.js
|
|
18
|
-
// cliFile = file("../../node_modules/react-native/cli.js")
|
|
19
|
-
|
|
20
|
-
/* Variants */
|
|
21
|
-
// The list of variants to that are debuggable. For those we're going to
|
|
22
|
-
// skip the bundling of the JS bundle and the assets. By default is just 'debug'.
|
|
23
|
-
// If you add flavors like lite, prod, etc. you'll have to list your debuggableVariants.
|
|
24
|
-
// debuggableVariants = ["liteDebug", "prodDebug"]
|
|
25
|
-
|
|
26
|
-
/* Bundling */
|
|
27
|
-
// A list containing the node command and its flags. Default is just 'node'.
|
|
28
|
-
// nodeExecutableAndArgs = ["node"]
|
|
29
|
-
//
|
|
30
|
-
// The command to run when bundling. By default is 'bundle'
|
|
31
|
-
// bundleCommand = "ram-bundle"
|
|
32
|
-
//
|
|
33
|
-
// The path to the CLI configuration file. Default is empty.
|
|
34
|
-
// bundleConfig = file(../rn-cli.config.js)
|
|
35
|
-
//
|
|
36
|
-
// The name of the generated asset file containing your JS bundle
|
|
37
|
-
// bundleAssetName = "MyApplication.android.bundle"
|
|
38
|
-
//
|
|
39
|
-
// The entry file for bundle generation. Default is 'index.android.js' or 'index.js'
|
|
40
|
-
// entryFile = file("../js/MyApplication.android.js")
|
|
41
|
-
//
|
|
42
|
-
// A list of extra flags to pass to the 'bundle' commands.
|
|
43
|
-
// See https://github.com/react-native-community/cli/blob/main/docs/commands.md#bundle
|
|
44
|
-
// extraPackagerArgs = []
|
|
45
|
-
|
|
46
|
-
/* Hermes Commands */
|
|
47
|
-
// The hermes compiler command to run. By default it is 'hermesc'
|
|
48
|
-
// hermesCommand = "$rootDir/my-custom-hermesc/bin/hermesc"
|
|
49
|
-
//
|
|
50
|
-
// The list of flags to pass to the Hermes compiler. By default is "-O", "-output-source-map"
|
|
51
|
-
// hermesFlags = ["-O", "-output-source-map"]
|
|
52
|
-
|
|
53
|
-
/* Autolinking */
|
|
54
|
-
autolinkLibrariesWithApp()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Set this to true to Run Proguard on Release builds to minify the Java bytecode.
|
|
59
|
-
*/
|
|
60
|
-
def enableProguardInReleaseBuilds = false
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* The preferred build flavor of JavaScriptCore (JSC)
|
|
64
|
-
*
|
|
65
|
-
* For example, to use the international variant, you can use:
|
|
66
|
-
* `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
|
|
67
|
-
*
|
|
68
|
-
* The international variant includes ICU i18n library and necessary data
|
|
69
|
-
* allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
|
|
70
|
-
* give correct results when using with locales other than en-US. Note that
|
|
71
|
-
* this variant is about 6MiB larger per architecture than default.
|
|
72
|
-
*/
|
|
73
|
-
def jscFlavor = 'org.webkit:android-jsc:+'
|
|
74
|
-
|
|
75
|
-
android {
|
|
76
|
-
ndkVersion rootProject.ext.ndkVersion
|
|
77
|
-
buildToolsVersion rootProject.ext.buildToolsVersion
|
|
78
|
-
compileSdk rootProject.ext.compileSdkVersion
|
|
79
|
-
|
|
80
|
-
namespace "dev.react_native_web_serial_api.example"
|
|
81
|
-
defaultConfig {
|
|
82
|
-
applicationId "dev.react_native_web_serial_api.example"
|
|
83
|
-
minSdkVersion rootProject.ext.minSdkVersion
|
|
84
|
-
targetSdkVersion rootProject.ext.targetSdkVersion
|
|
85
|
-
versionCode 1
|
|
86
|
-
versionName "1.0"
|
|
87
|
-
}
|
|
88
|
-
signingConfigs {
|
|
89
|
-
debug {
|
|
90
|
-
storeFile file('debug.keystore')
|
|
91
|
-
storePassword 'android'
|
|
92
|
-
keyAlias 'androiddebugkey'
|
|
93
|
-
keyPassword 'android'
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
buildTypes {
|
|
97
|
-
debug {
|
|
98
|
-
signingConfig signingConfigs.debug
|
|
99
|
-
}
|
|
100
|
-
release {
|
|
101
|
-
// Caution! In production, you need to generate your own keystore file.
|
|
102
|
-
// see https://reactnative.dev/docs/signed-apk-android.
|
|
103
|
-
signingConfig signingConfigs.debug
|
|
104
|
-
minifyEnabled enableProguardInReleaseBuilds
|
|
105
|
-
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
dependencies {
|
|
111
|
-
// usb-serial-for-android is provided transitively by react-native-web-serial-api
|
|
112
|
-
// The version of react-native is set by the React Native Gradle Plugin
|
|
113
|
-
implementation("com.facebook.react:react-android")
|
|
114
|
-
|
|
115
|
-
if (hermesEnabled.toBoolean()) {
|
|
116
|
-
implementation("com.facebook.react:hermes-android")
|
|
117
|
-
} else {
|
|
118
|
-
implementation jscFlavor
|
|
119
|
-
}
|
|
120
|
-
}
|
|
Binary file
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
# Add project specific ProGuard rules here.
|
|
2
|
-
# By default, the flags in this file are appended to flags specified
|
|
3
|
-
# in /usr/local/Cellar/android-sdk/24.3.3/tools/proguard/proguard-android.txt
|
|
4
|
-
# You can edit the include path and order by changing the proguardFiles
|
|
5
|
-
# directive in build.gradle.
|
|
6
|
-
#
|
|
7
|
-
# For more details, see
|
|
8
|
-
# http://developer.android.com/guide/developing/tools/proguard.html
|
|
9
|
-
|
|
10
|
-
# Add any project specific keep options here:
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
|
3
|
-
xmlns:tools="http://schemas.android.com/tools">
|
|
4
|
-
|
|
5
|
-
<application
|
|
6
|
-
android:usesCleartextTraffic="true"
|
|
7
|
-
tools:targetApi="28"
|
|
8
|
-
tools:ignore="GoogleAppIndexingWarning"/>
|
|
9
|
-
</manifest>
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
2
|
-
|
|
3
|
-
<uses-permission android:name="android.permission.INTERNET" />
|
|
4
|
-
<uses-feature android:name="android.hardware.usb.host" />
|
|
5
|
-
|
|
6
|
-
<application
|
|
7
|
-
android:name=".MainApplication"
|
|
8
|
-
android:label="@string/app_name"
|
|
9
|
-
android:icon="@mipmap/ic_launcher"
|
|
10
|
-
android:roundIcon="@mipmap/ic_launcher_round"
|
|
11
|
-
android:allowBackup="false"
|
|
12
|
-
android:theme="@style/AppTheme"
|
|
13
|
-
android:supportsRtl="true">
|
|
14
|
-
<!-- PortPickerActivity and UsbDetachReceiver are provided by
|
|
15
|
-
react-native-web-serial-api and merged in automatically. -->
|
|
16
|
-
<activity
|
|
17
|
-
android:name=".MainActivity"
|
|
18
|
-
android:label="@string/app_name"
|
|
19
|
-
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
|
|
20
|
-
android:launchMode="singleTask"
|
|
21
|
-
android:windowSoftInputMode="adjustResize"
|
|
22
|
-
android:exported="true">
|
|
23
|
-
<intent-filter>
|
|
24
|
-
<action android:name="android.intent.action.MAIN" />
|
|
25
|
-
<category android:name="android.intent.category.LAUNCHER" />
|
|
26
|
-
</intent-filter>
|
|
27
|
-
<intent-filter>
|
|
28
|
-
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
|
|
29
|
-
</intent-filter>
|
|
30
|
-
<intent-filter>
|
|
31
|
-
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
|
|
32
|
-
</intent-filter>
|
|
33
|
-
<meta-data
|
|
34
|
-
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
|
|
35
|
-
android:resource="@xml/device_filter" />
|
|
36
|
-
</activity>
|
|
37
|
-
</application>
|
|
38
|
-
</manifest>
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
package dev.react_native_web_serial_api.example
|
|
2
|
-
|
|
3
|
-
import com.facebook.react.ReactActivity
|
|
4
|
-
import com.facebook.react.ReactActivityDelegate
|
|
5
|
-
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
|
|
6
|
-
import com.facebook.react.defaults.DefaultReactActivityDelegate
|
|
7
|
-
|
|
8
|
-
class MainActivity : ReactActivity() {
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Returns the name of the main component registered from JavaScript. This is used to schedule
|
|
12
|
-
* rendering of the component.
|
|
13
|
-
*/
|
|
14
|
-
override fun getMainComponentName(): String = "SimpleUsbTerminal"
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
|
|
18
|
-
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
|
|
19
|
-
*/
|
|
20
|
-
override fun createReactActivityDelegate(): ReactActivityDelegate =
|
|
21
|
-
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
|
|
22
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
package dev.react_native_web_serial_api.example
|
|
2
|
-
|
|
3
|
-
import android.app.Application
|
|
4
|
-
import com.facebook.react.PackageList
|
|
5
|
-
import com.facebook.react.ReactApplication
|
|
6
|
-
import com.facebook.react.ReactHost
|
|
7
|
-
import com.facebook.react.ReactNativeHost
|
|
8
|
-
import com.facebook.react.ReactPackage
|
|
9
|
-
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
|
|
10
|
-
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
|
|
11
|
-
import com.facebook.react.defaults.DefaultReactNativeHost
|
|
12
|
-
import com.facebook.react.soloader.OpenSourceMergedSoMapping
|
|
13
|
-
import com.facebook.soloader.SoLoader
|
|
14
|
-
|
|
15
|
-
class MainApplication : Application(), ReactApplication {
|
|
16
|
-
|
|
17
|
-
override val reactNativeHost: ReactNativeHost =
|
|
18
|
-
object : DefaultReactNativeHost(this) {
|
|
19
|
-
override fun getPackages(): List<ReactPackage> =
|
|
20
|
-
// react-native-web-serial-api is linked automatically via autolinking
|
|
21
|
-
PackageList(this).packages
|
|
22
|
-
|
|
23
|
-
override fun getJSMainModuleName(): String = "index"
|
|
24
|
-
|
|
25
|
-
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
|
|
26
|
-
|
|
27
|
-
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
|
|
28
|
-
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
override val reactHost: ReactHost
|
|
32
|
-
get() = getDefaultReactHost(applicationContext, reactNativeHost)
|
|
33
|
-
|
|
34
|
-
override fun onCreate() {
|
|
35
|
-
super.onCreate()
|
|
36
|
-
SoLoader.init(this, OpenSourceMergedSoMapping)
|
|
37
|
-
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
|
|
38
|
-
load()
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
-
<!-- Copyright (C) 2014 The Android Open Source Project
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
-->
|
|
16
|
-
<inset xmlns:android="http://schemas.android.com/apk/res/android"
|
|
17
|
-
android:insetLeft="@dimen/abc_edit_text_inset_horizontal_material"
|
|
18
|
-
android:insetRight="@dimen/abc_edit_text_inset_horizontal_material"
|
|
19
|
-
android:insetTop="@dimen/abc_edit_text_inset_top_material"
|
|
20
|
-
android:insetBottom="@dimen/abc_edit_text_inset_bottom_material"
|
|
21
|
-
>
|
|
22
|
-
|
|
23
|
-
<selector>
|
|
24
|
-
<!--
|
|
25
|
-
This file is a copy of abc_edit_text_material (https://bit.ly/3k8fX7I).
|
|
26
|
-
The item below with state_pressed="false" and state_focused="false" causes a NullPointerException.
|
|
27
|
-
NullPointerException:tempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable(android.content.res.Resources)'
|
|
28
|
-
|
|
29
|
-
<item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/abc_textfield_default_mtrl_alpha"/>
|
|
30
|
-
|
|
31
|
-
For more info, see https://bit.ly/3CdLStv (react-native/pull/29452) and https://bit.ly/3nxOMoR.
|
|
32
|
-
-->
|
|
33
|
-
<item android:state_enabled="false" android:drawable="@drawable/abc_textfield_default_mtrl_alpha"/>
|
|
34
|
-
<item android:drawable="@drawable/abc_textfield_activated_mtrl_alpha"/>
|
|
35
|
-
</selector>
|
|
36
|
-
|
|
37
|
-
</inset>
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
<resources>
|
|
2
|
-
|
|
3
|
-
<!-- Base application theme. -->
|
|
4
|
-
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
|
|
5
|
-
<!-- Customize your theme here. -->
|
|
6
|
-
<item name="android:editTextBackground">@drawable/rn_edit_text_material</item>
|
|
7
|
-
</style>
|
|
8
|
-
|
|
9
|
-
</resources>
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
buildscript {
|
|
2
|
-
ext {
|
|
3
|
-
buildToolsVersion = "35.0.0"
|
|
4
|
-
minSdkVersion = 24
|
|
5
|
-
compileSdkVersion = 35
|
|
6
|
-
targetSdkVersion = 34
|
|
7
|
-
ndkVersion = "27.1.12297006"
|
|
8
|
-
kotlinVersion = "2.1.0"
|
|
9
|
-
}
|
|
10
|
-
repositories {
|
|
11
|
-
maven { url "https://jitpack.io" }
|
|
12
|
-
google()
|
|
13
|
-
mavenCentral()
|
|
14
|
-
}
|
|
15
|
-
dependencies {
|
|
16
|
-
classpath("com.android.tools.build:gradle")
|
|
17
|
-
classpath("com.facebook.react:react-native-gradle-plugin")
|
|
18
|
-
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
apply plugin: "com.facebook.react.rootproject"
|
|
Binary file
|