-
Notifications
You must be signed in to change notification settings - Fork 55
/
App.tsx
executable file
·178 lines (154 loc) · 5.97 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import React from 'react';
import { useState, useEffect } from 'react';
import {
View,
Text,
Button,
FlatList,
PermissionsAndroid,
Platform
} from 'react-native';
import CheckBox from '@react-native-community/checkbox';
import {
InterfaceType,
StarDeviceDiscoveryManager,
StarDeviceDiscoveryManagerFactory,
StarPrinter
} from 'react-native-star-io10';
export default function App() {
const [lanIsEnabled, setLanIsEnabled] = useState(true);
const [bluetoothIsEnabled, setBluetoothIsEnabled] = useState(true);
const [bluetoothLeIsEnabled, setBluetoothLeIsEnabled] = useState(true);
const [usbIsEnabled, setUsbIsEnabled] = useState(true);
const [printers, setPrinters] = useState<StarPrinter[]>([]);
const [manager, setManager] = useState<StarDeviceDiscoveryManager | undefined>(undefined);
async function _onPressDiscoveryButton () {
// If you are using Android 12 and targetSdkVersion is 31 or later,
// you have to request Bluetooth permission (Nearby devices permission) to use the Bluetooth printer.
// https://developer.android.com/about/versions/12/features/bluetooth-permissions
if (Platform.OS == 'android' && 31 <= Platform.Version) {
if (bluetoothIsEnabled) {
var hasPermission = await _confirmBluetoothPermission();
if (!hasPermission) {
console.log(`PERMISSION ERROR: You have to allow Nearby devices to use the Bluetooth printer`);
return;
}
}
}
try {
await manager?.stopDiscovery()
var interfaceTypes: Array<InterfaceType> = []
if(lanIsEnabled) {
interfaceTypes.push(InterfaceType.Lan);
}
if(bluetoothIsEnabled) {
interfaceTypes.push(InterfaceType.Bluetooth);
}
if(bluetoothLeIsEnabled) {
interfaceTypes.push(InterfaceType.BluetoothLE);
}
if(usbIsEnabled) {
interfaceTypes.push(InterfaceType.Usb);
}
console.log(`create manager with ${interfaceTypes}`);
setManager(await StarDeviceDiscoveryManagerFactory.create(interfaceTypes));
}
catch(error) {
console.log(`Error: ${String(error)}`);
}
}
useEffect( () => {
const _startDiscovery = async () => {
setPrinters([]);
if (manager != undefined) {
manager.discoveryTime = 10000;
manager.onPrinterFound = async (printer: StarPrinter) => {
setPrinters((printers) => [...printers, printer]);
console.log(`Found printer: ${printer.connectionSettings.interfaceType} ${printer.connectionSettings.identifier}.`);
};
manager.onDiscoveryFinished = () => {
console.log(`Discovery finished.`);
};
try {
console.log(`Discovery start.`);
await manager.startDiscovery();
}
catch(error) {
console.log(`Error: ${String(error)}`);
}
}
}
_startDiscovery();
}, [manager]);
async function _confirmBluetoothPermission(): Promise<boolean> {
var hasPermission = false;
try {
hasPermission = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT);
if (!hasPermission) {
const status = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT);
hasPermission = status == PermissionsAndroid.RESULTS.GRANTED;
}
}
catch (err) {
console.warn(err);
}
return hasPermission;
}
return (
<View style={{ margin: 50 }}>
<Text>Interface</Text>
<View style={{ flexDirection: 'row', marginTop: 20 }}>
<CheckBox
style={{ marginLeft: 20 }}
value={lanIsEnabled}
onValueChange={(newValue) => {
setLanIsEnabled(newValue);
}}
/>
<Text style={{ marginLeft: 20 }}>LAN</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<CheckBox
style={{ marginLeft: 20 }}
value={bluetoothIsEnabled}
onValueChange={(newValue) => {
setBluetoothIsEnabled(newValue);
}}
/>
<Text style={{ marginLeft: 20 }}>Bluetooth</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<CheckBox
style={{ marginLeft: 20 }}
value={bluetoothLeIsEnabled}
onValueChange={(newValue) => {
setBluetoothLeIsEnabled(newValue);
}}
/>
<Text style={{ marginLeft: 20 }}>Bluetooth LE</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<CheckBox
style={{ marginLeft: 20 }}
value={usbIsEnabled}
onValueChange={(newValue) => {
setUsbIsEnabled(newValue);
}}
/>
<Text style={{ marginLeft: 20 }}>USB</Text>
</View>
<View
style={{ width: 100, marginTop: 30 }}>
<Button
title="Discovery"
onPress={_onPressDiscoveryButton}
/>
</View>
<FlatList
style={{ marginTop: 30 }}
data={printers}
renderItem={({item}) => <Text>{item.connectionSettings.interfaceType} : {item.connectionSettings.identifier}</Text>}
keyExtractor={(item, index) => index.toString()} />
</View>
);
};