@rhinosw/react-native-usb-serial 0.0.2 → 0.1.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.
@@ -7,17 +7,48 @@ import com.facebook.react.bridge.Arguments
7
7
  import com.facebook.react.bridge.WritableArray
8
8
  import com.facebook.react.bridge.WritableMap
9
9
  import com.facebook.react.bridge.ReadableMap
10
- import com.rezaul.usbserial.UsbManager
10
+ import com.rezaul.usbserial.UsbManager as CustomUsbManager
11
11
  import com.facebook.react.modules.core.DeviceEventManagerModule
12
12
  import com.rezaul.usbserial.RawReadConfig
13
13
  import com.rezaul.usbserial.SoilSensorConfig
14
14
  import com.facebook.react.bridge.Promise
15
15
 
16
+ import android.content.BroadcastReceiver
17
+ import android.content.Context
18
+ import android.content.Intent
19
+ import android.content.IntentFilter
20
+ import android.hardware.usb.UsbManager
21
+
16
22
  @ReactModule(name = UsbSerialModule.NAME)
23
+
24
+
17
25
  class UsbSerialModule(reactContext: ReactApplicationContext) :
18
26
  NativeUsbSerialSpec(reactContext) {
19
27
 
20
- private val usbManager = UsbManager(reactContext)
28
+ private val usbReceiver = object : BroadcastReceiver() {
29
+ override fun onReceive(context: Context?, intent: Intent?) {
30
+ val device = intent?.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
31
+ val deviceMap = device?.let { usbDeviceToMap(it) }
32
+ when (intent?.action) {
33
+ UsbManager.ACTION_USB_DEVICE_ATTACHED -> {
34
+ sendEvent("USB_DEVICE_ATTACHED", deviceMap)
35
+ }
36
+ UsbManager.ACTION_USB_DEVICE_DETACHED -> {
37
+ sendEvent("USB_DEVICE_DETACHED", deviceMap)
38
+ }
39
+ }
40
+ }
41
+ }
42
+
43
+ init {
44
+ val filter = IntentFilter().apply {
45
+ addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
46
+ addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
47
+ }
48
+ reactApplicationContext.registerReceiver(usbReceiver, filter)
49
+ }
50
+
51
+ private val usbManager = CustomUsbManager(reactContext)
21
52
 
22
53
  override fun multiply(a: Double, b: Double): Double {
23
54
  return a * b
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhinosw/react-native-usb-serial",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "description": "Production-ready React Native USB serial communication module for Android with Modbus RTU soil sensor support",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",