simplejsble 0.0.4 → 0.0.6

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.
Files changed (273) hide show
  1. package/VERSION +1 -0
  2. package/android/CMakeLists.txt +62 -20
  3. package/cmake/epilogue.cmake +81 -0
  4. package/cmake/find/FindDBus1.cmake +20 -0
  5. package/cmake/find/Findfmt.cmake +41 -0
  6. package/cmake/find/Findgoogletest.cmake +18 -0
  7. package/cmake/parse_version.cmake +45 -0
  8. package/cmake/prelude.cmake +8 -0
  9. package/dependencies/README +4 -0
  10. package/dependencies/external/kvn/kvn_bytearray.h +297 -0
  11. package/dependencies/external/kvn/kvn_safe_callback.hpp +66 -0
  12. package/dependencies/external/kvn/logfwd.hpp +35 -0
  13. package/dependencies/internal/include/fmt/LICENSE +27 -0
  14. package/dependencies/internal/include/fmt/args.h +220 -0
  15. package/dependencies/internal/include/fmt/base.h +2962 -0
  16. package/dependencies/internal/include/fmt/chrono.h +2338 -0
  17. package/dependencies/internal/include/fmt/color.h +610 -0
  18. package/dependencies/internal/include/fmt/compile.h +539 -0
  19. package/dependencies/internal/include/fmt/core.h +5 -0
  20. package/dependencies/internal/include/fmt/format-inl.h +1949 -0
  21. package/dependencies/internal/include/fmt/format.h +4244 -0
  22. package/dependencies/internal/include/fmt/os.h +427 -0
  23. package/dependencies/internal/include/fmt/ostream.h +166 -0
  24. package/dependencies/internal/include/fmt/printf.h +633 -0
  25. package/dependencies/internal/include/fmt/ranges.h +850 -0
  26. package/dependencies/internal/include/fmt/std.h +726 -0
  27. package/dependencies/internal/include/fmt/xchar.h +373 -0
  28. package/dependencies/internal/include/nanopb/pb.h +948 -0
  29. package/dependencies/internal/include/nanopb/pb_common.h +49 -0
  30. package/dependencies/internal/include/nanopb/pb_decode.h +204 -0
  31. package/dependencies/internal/include/nanopb/pb_encode.h +195 -0
  32. package/dependencies/internal/include/simplejni/Common.hpp +598 -0
  33. package/dependencies/internal/include/simplejni/References.hpp +151 -0
  34. package/dependencies/internal/include/simplejni/Registry.hpp +249 -0
  35. package/dependencies/internal/include/simplejni/VM.hpp +93 -0
  36. package/dependencies/internal/src/nanopb/pb_common.c +388 -0
  37. package/dependencies/internal/src/nanopb/pb_decode.c +1763 -0
  38. package/dependencies/internal/src/nanopb/pb_encode.c +1009 -0
  39. package/dependencies/internal/src/simplejni/Common.cpp +4 -0
  40. package/package.json +5 -1
  41. package/simpleble/CMakeLists.txt +519 -0
  42. package/simpleble/cmake/simpleble.pc.in +11 -0
  43. package/simpleble/include/simpleble/Adapter.h +102 -0
  44. package/simpleble/include/simpleble/AdapterSafe.h +58 -0
  45. package/simpleble/include/simpleble/Advanced.h +50 -0
  46. package/simpleble/include/simpleble/Characteristic.h +39 -0
  47. package/simpleble/include/simpleble/Config.h +64 -0
  48. package/simpleble/include/simpleble/Descriptor.h +30 -0
  49. package/simpleble/include/simpleble/Exceptions.h +72 -0
  50. package/simpleble/include/simpleble/Logging.h +73 -0
  51. package/simpleble/include/simpleble/Peripheral.h +82 -0
  52. package/simpleble/include/simpleble/PeripheralSafe.h +64 -0
  53. package/simpleble/include/simpleble/Service.h +34 -0
  54. package/simpleble/include/simpleble/SimpleBLE.h +8 -0
  55. package/simpleble/include/simpleble/Types.h +49 -0
  56. package/simpleble/include/simpleble/Utils.h +13 -0
  57. package/simpleble/include/simpleble_c/DEPRECATED +2 -0
  58. package/simpleble/include/simpleble_c/adapter.h +188 -0
  59. package/simpleble/include/simpleble_c/logging.h +37 -0
  60. package/simpleble/include/simpleble_c/peripheral.h +304 -0
  61. package/simpleble/include/simpleble_c/simpleble.h +21 -0
  62. package/simpleble/include/simpleble_c/types.h +73 -0
  63. package/simpleble/include/simpleble_c/utils.h +27 -0
  64. package/simpleble/src/CommonUtils.h +63 -0
  65. package/simpleble/src/Config.cpp +25 -0
  66. package/simpleble/src/Exceptions.cpp +31 -0
  67. package/simpleble/src/Logging.cpp +136 -0
  68. package/simpleble/src/LoggingInternal.h +85 -0
  69. package/simpleble/src/Utils.cpp +24 -0
  70. package/simpleble/src/backends/android/AdapterAndroid.cpp +101 -0
  71. package/simpleble/src/backends/android/AdapterAndroid.h +67 -0
  72. package/simpleble/src/backends/android/BackendAndroid.cpp +41 -0
  73. package/simpleble/src/backends/android/BackendAndroid.h +26 -0
  74. package/simpleble/src/backends/android/PeripheralAndroid.cpp +365 -0
  75. package/simpleble/src/backends/android/PeripheralAndroid.h +90 -0
  76. package/simpleble/src/backends/android/bridge/BluetoothGattCallback.cpp +432 -0
  77. package/simpleble/src/backends/android/bridge/BluetoothGattCallback.h +102 -0
  78. package/simpleble/src/backends/android/bridge/ScanCallback.cpp +142 -0
  79. package/simpleble/src/backends/android/bridge/ScanCallback.h +55 -0
  80. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothAdapter.cpp +107 -0
  81. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothAdapter.h +55 -0
  82. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothDevice.cpp +68 -0
  83. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothDevice.h +54 -0
  84. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGatt.cpp +115 -0
  85. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGatt.h +75 -0
  86. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattCharacteristic.cpp +142 -0
  87. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattCharacteristic.h +66 -0
  88. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattDescriptor.cpp +67 -0
  89. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattDescriptor.h +46 -0
  90. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattService.cpp +106 -0
  91. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattService.h +47 -0
  92. package/simpleble/src/backends/android/types/android/bluetooth/le/BluetoothScanner.cpp +47 -0
  93. package/simpleble/src/backends/android/types/android/bluetooth/le/BluetoothScanner.h +37 -0
  94. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanRecord.cpp +69 -0
  95. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanRecord.h +41 -0
  96. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanResult.cpp +63 -0
  97. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanResult.h +42 -0
  98. package/simpleble/src/backends/android/types/android/os/ParcelUUID.cpp +32 -0
  99. package/simpleble/src/backends/android/types/android/os/ParcelUUID.h +30 -0
  100. package/simpleble/src/backends/android/types/android/util/SparseArray.cpp +54 -0
  101. package/simpleble/src/backends/android/types/android/util/SparseArray.h +37 -0
  102. package/simpleble/src/backends/android/types/java/util/Iterator.cpp +36 -0
  103. package/simpleble/src/backends/android/types/java/util/Iterator.h +28 -0
  104. package/simpleble/src/backends/android/types/java/util/List.cpp +29 -0
  105. package/simpleble/src/backends/android/types/java/util/List.h +27 -0
  106. package/simpleble/src/backends/android/types/java/util/Set.cpp +33 -0
  107. package/simpleble/src/backends/android/types/java/util/Set.h +28 -0
  108. package/simpleble/src/backends/android/types/java/util/UUID.cpp +26 -0
  109. package/simpleble/src/backends/android/types/java/util/UUID.h +29 -0
  110. package/simpleble/src/backends/common/AdapterBase.cpp +53 -0
  111. package/simpleble/src/backends/common/AdapterBase.h +81 -0
  112. package/simpleble/src/backends/common/AdapterBaseTypes.h +22 -0
  113. package/simpleble/src/backends/common/BackendBase.h +20 -0
  114. package/simpleble/src/backends/common/BackendUtils.h +33 -0
  115. package/simpleble/src/backends/common/CharacteristicBase.cpp +28 -0
  116. package/simpleble/src/backends/common/CharacteristicBase.h +38 -0
  117. package/simpleble/src/backends/common/DescriptorBase.cpp +7 -0
  118. package/simpleble/src/backends/common/DescriptorBase.h +19 -0
  119. package/simpleble/src/backends/common/PeripheralBase.h +82 -0
  120. package/simpleble/src/backends/common/ServiceBase.cpp +18 -0
  121. package/simpleble/src/backends/common/ServiceBase.h +28 -0
  122. package/simpleble/src/backends/dongl/AdapterBaseTypes.h +22 -0
  123. package/simpleble/src/backends/dongl/AdapterDongl.cpp +369 -0
  124. package/simpleble/src/backends/dongl/AdapterDongl.h +60 -0
  125. package/simpleble/src/backends/dongl/BackendDongl.cpp +35 -0
  126. package/simpleble/src/backends/dongl/PeripheralDongl.cpp +562 -0
  127. package/simpleble/src/backends/dongl/PeripheralDongl.h +136 -0
  128. package/simpleble/src/backends/dongl/protocol/basic.pb.c +33 -0
  129. package/simpleble/src/backends/dongl/protocol/basic.pb.h +172 -0
  130. package/simpleble/src/backends/dongl/protocol/d2h.pb.c +18 -0
  131. package/simpleble/src/backends/dongl/protocol/d2h.pb.h +118 -0
  132. package/simpleble/src/backends/dongl/protocol/h2d.pb.c +12 -0
  133. package/simpleble/src/backends/dongl/protocol/h2d.pb.h +69 -0
  134. package/simpleble/src/backends/dongl/protocol/simpleble.pb.c +123 -0
  135. package/simpleble/src/backends/dongl/protocol/simpleble.pb.h +908 -0
  136. package/simpleble/src/backends/dongl/protocol/softdevice.pb.c +18 -0
  137. package/simpleble/src/backends/dongl/protocol/softdevice.pb.h +815 -0
  138. package/simpleble/src/backends/dongl/protocol/softdevice_gap.pb.c +339 -0
  139. package/simpleble/src/backends/dongl/protocol/softdevice_gap.pb.h +2086 -0
  140. package/simpleble/src/backends/dongl/protocol/softdevice_gattc.pb.c +114 -0
  141. package/simpleble/src/backends/dongl/protocol/softdevice_gattc.pb.h +772 -0
  142. package/simpleble/src/backends/dongl/protocol/softdevice_gatts.pb.c +117 -0
  143. package/simpleble/src/backends/dongl/protocol/softdevice_gatts.pb.h +766 -0
  144. package/simpleble/src/backends/dongl/protocol/softdevice_types.pb.c +207 -0
  145. package/simpleble/src/backends/dongl/protocol/softdevice_types.pb.h +1686 -0
  146. package/simpleble/src/backends/dongl/serial/Protocol.cpp +887 -0
  147. package/simpleble/src/backends/dongl/serial/Protocol.h +117 -0
  148. package/simpleble/src/backends/dongl/serial/ProtocolBase.cpp +95 -0
  149. package/simpleble/src/backends/dongl/serial/ProtocolBase.h +54 -0
  150. package/simpleble/src/backends/dongl/serial/Wire.cpp +133 -0
  151. package/simpleble/src/backends/dongl/serial/Wire.h +116 -0
  152. package/simpleble/src/backends/dongl/usb/UsbHelper.cpp +53 -0
  153. package/simpleble/src/backends/dongl/usb/UsbHelper.h +32 -0
  154. package/simpleble/src/backends/dongl/usb/UsbHelperApple.cpp +266 -0
  155. package/simpleble/src/backends/dongl/usb/UsbHelperApple.h +34 -0
  156. package/simpleble/src/backends/dongl/usb/UsbHelperImpl.h +29 -0
  157. package/simpleble/src/backends/dongl/usb/UsbHelperLinux.cpp +21 -0
  158. package/simpleble/src/backends/dongl/usb/UsbHelperLinux.h +22 -0
  159. package/simpleble/src/backends/dongl/usb/UsbHelperNull.cpp +21 -0
  160. package/simpleble/src/backends/dongl/usb/UsbHelperNull.h +22 -0
  161. package/simpleble/src/backends/dongl/usb/UsbHelperWindows.cpp +21 -0
  162. package/simpleble/src/backends/dongl/usb/UsbHelperWindows.h +22 -0
  163. package/simpleble/src/backends/linux/AdapterLinux.cpp +102 -0
  164. package/simpleble/src/backends/linux/AdapterLinux.h +55 -0
  165. package/simpleble/src/backends/linux/BackendBluez.cpp +90 -0
  166. package/simpleble/src/backends/linux/PeripheralLinux.cpp +394 -0
  167. package/simpleble/src/backends/linux/PeripheralLinux.h +90 -0
  168. package/simpleble/src/backends/linux_legacy/AdapterLinuxLegacy.cpp +102 -0
  169. package/simpleble/src/backends/linux_legacy/AdapterLinuxLegacy.h +55 -0
  170. package/simpleble/src/backends/linux_legacy/BackendBluezLegacy.cpp +87 -0
  171. package/simpleble/src/backends/linux_legacy/PeripheralLinuxLegacy.cpp +376 -0
  172. package/simpleble/src/backends/linux_legacy/PeripheralLinuxLegacy.h +90 -0
  173. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Adapter.h +46 -0
  174. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Agent.h +52 -0
  175. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Bluez.h +37 -0
  176. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezOrg.h +22 -0
  177. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezOrgBluez.h +26 -0
  178. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezRoot.h +32 -0
  179. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Characteristic.h +47 -0
  180. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Descriptor.h +33 -0
  181. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Device.h +64 -0
  182. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Exceptions.h +43 -0
  183. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Service.h +27 -0
  184. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Types.h +9 -0
  185. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Adapter1.h +49 -0
  186. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Agent1.h +100 -0
  187. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/AgentManager1.h +24 -0
  188. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Battery1.h +32 -0
  189. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Device1.h +62 -0
  190. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattCharacteristic1.h +48 -0
  191. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattDescriptor1.h +41 -0
  192. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattService1.h +28 -0
  193. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/Interface.h +67 -0
  194. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/InterfaceRegistry.h +62 -0
  195. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/Proxy.h +117 -0
  196. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Connection.h +50 -0
  197. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Exceptions.h +56 -0
  198. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Holder.h +147 -0
  199. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Logging.h +57 -0
  200. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Message.h +89 -0
  201. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Path.h +24 -0
  202. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/interfaces/ObjectManager.h +26 -0
  203. package/simpleble/src/backends/linux_legacy/src/Adapter.cpp +78 -0
  204. package/simpleble/src/backends/linux_legacy/src/Agent.cpp +72 -0
  205. package/simpleble/src/backends/linux_legacy/src/Bluez.cpp +45 -0
  206. package/simpleble/src/backends/linux_legacy/src/BluezOrg.cpp +20 -0
  207. package/simpleble/src/backends/linux_legacy/src/BluezOrgBluez.cpp +23 -0
  208. package/simpleble/src/backends/linux_legacy/src/BluezRoot.cpp +44 -0
  209. package/simpleble/src/backends/linux_legacy/src/Characteristic.cpp +64 -0
  210. package/simpleble/src/backends/linux_legacy/src/Descriptor.cpp +27 -0
  211. package/simpleble/src/backends/linux_legacy/src/Device.cpp +104 -0
  212. package/simpleble/src/backends/linux_legacy/src/Exceptions.cpp +29 -0
  213. package/simpleble/src/backends/linux_legacy/src/Service.cpp +33 -0
  214. package/simpleble/src/backends/linux_legacy/src/advanced/Interface.cpp +155 -0
  215. package/simpleble/src/backends/linux_legacy/src/advanced/Proxy.cpp +374 -0
  216. package/simpleble/src/backends/linux_legacy/src/base/Connection.cpp +219 -0
  217. package/simpleble/src/backends/linux_legacy/src/base/Exceptions.cpp +39 -0
  218. package/simpleble/src/backends/linux_legacy/src/base/Holder.cpp +739 -0
  219. package/simpleble/src/backends/linux_legacy/src/base/Message.cpp +622 -0
  220. package/simpleble/src/backends/linux_legacy/src/base/Path.cpp +129 -0
  221. package/simpleble/src/backends/linux_legacy/src/interfaces/Adapter1.cpp +123 -0
  222. package/simpleble/src/backends/linux_legacy/src/interfaces/Agent1.cpp +143 -0
  223. package/simpleble/src/backends/linux_legacy/src/interfaces/AgentManager1.cpp +34 -0
  224. package/simpleble/src/backends/linux_legacy/src/interfaces/Battery1.cpp +30 -0
  225. package/simpleble/src/backends/linux_legacy/src/interfaces/Device1.cpp +170 -0
  226. package/simpleble/src/backends/linux_legacy/src/interfaces/GattCharacteristic1.cpp +118 -0
  227. package/simpleble/src/backends/linux_legacy/src/interfaces/GattDescriptor1.cpp +78 -0
  228. package/simpleble/src/backends/linux_legacy/src/interfaces/GattService1.cpp +28 -0
  229. package/simpleble/src/backends/linux_legacy/src/interfaces/ObjectManager.cpp +58 -0
  230. package/simpleble/src/backends/macos/AdapterBaseMacOS.h +29 -0
  231. package/simpleble/src/backends/macos/AdapterBaseMacOS.mm +235 -0
  232. package/simpleble/src/backends/macos/AdapterMac.h +78 -0
  233. package/simpleble/src/backends/macos/AdapterMac.mm +156 -0
  234. package/simpleble/src/backends/macos/BackendCoreBluetooth.mm +45 -0
  235. package/simpleble/src/backends/macos/PeripheralBaseMacOS.h +49 -0
  236. package/simpleble/src/backends/macos/PeripheralBaseMacOS.mm +714 -0
  237. package/simpleble/src/backends/macos/PeripheralMac.h +81 -0
  238. package/simpleble/src/backends/macos/PeripheralMac.mm +229 -0
  239. package/simpleble/src/backends/macos/Utils.h +9 -0
  240. package/simpleble/src/backends/macos/Utils.mm +23 -0
  241. package/simpleble/src/backends/plain/AdapterPlain.cpp +65 -0
  242. package/simpleble/src/backends/plain/AdapterPlain.h +49 -0
  243. package/simpleble/src/backends/plain/BackendPlain.cpp +30 -0
  244. package/simpleble/src/backends/plain/PeripheralPlain.cpp +159 -0
  245. package/simpleble/src/backends/plain/PeripheralPlain.h +72 -0
  246. package/simpleble/src/backends/windows/AdapterWindows.cpp +338 -0
  247. package/simpleble/src/backends/windows/AdapterWindows.h +89 -0
  248. package/simpleble/src/backends/windows/BackendWinRT.cpp +67 -0
  249. package/simpleble/src/backends/windows/BackendWinRT.h +18 -0
  250. package/simpleble/src/backends/windows/MtaManager.cpp +49 -0
  251. package/simpleble/src/backends/windows/MtaManager.h +90 -0
  252. package/simpleble/src/backends/windows/PeripheralWindows.cpp +487 -0
  253. package/simpleble/src/backends/windows/PeripheralWindows.h +129 -0
  254. package/simpleble/src/backends/windows/Utils.cpp +146 -0
  255. package/simpleble/src/backends/windows/Utils.h +47 -0
  256. package/simpleble/src/builders/BuildVec.h +32 -0
  257. package/simpleble/src/builders/BuilderBase.h +87 -0
  258. package/simpleble/src/external/TaskRunner.hpp +99 -0
  259. package/simpleble/src/external/ThreadRunner.h +52 -0
  260. package/simpleble/src/external/kvn_safe_callback.hpp +66 -0
  261. package/simpleble/src/external/kvn_safe_map.hpp +94 -0
  262. package/simpleble/src/external/kvn_threadrunner.hpp +70 -0
  263. package/simpleble/src/external/logfwd.hpp +35 -0
  264. package/simpleble/src/frontends/base/Adapter.cpp +111 -0
  265. package/simpleble/src/frontends/base/Advanced.cpp +34 -0
  266. package/simpleble/src/frontends/base/Backend.cpp +95 -0
  267. package/simpleble/src/frontends/base/Backend.h +76 -0
  268. package/simpleble/src/frontends/base/Characteristic.cpp +56 -0
  269. package/simpleble/src/frontends/base/Descriptor.cpp +21 -0
  270. package/simpleble/src/frontends/base/Peripheral.cpp +113 -0
  271. package/simpleble/src/frontends/base/Service.cpp +26 -0
  272. package/simpleble/src/frontends/safe/AdapterSafe.cpp +158 -0
  273. package/simpleble/src/frontends/safe/PeripheralSafe.cpp +219 -0
@@ -0,0 +1,151 @@
1
+ #pragma once
2
+
3
+ #include <jni.h>
4
+
5
+ #include "VM.hpp"
6
+
7
+ namespace SimpleJNI {
8
+
9
+ template <typename T>
10
+ class GlobalRef {
11
+ public:
12
+ GlobalRef() = default;
13
+
14
+ explicit GlobalRef(T obj) : _obj(obj ? static_cast<T>(VM::env()->NewGlobalRef(obj)) : nullptr) {}
15
+
16
+ ~GlobalRef() {
17
+ if (_obj && VM::is_jvm_alive()) VM::env()->DeleteGlobalRef(_obj);
18
+ }
19
+
20
+ // Copy constructor
21
+ GlobalRef(const GlobalRef& other)
22
+ : _obj(other._obj ? static_cast<T>(VM::env()->NewGlobalRef(other._obj)) : nullptr) {}
23
+
24
+ // Copy assignment operator
25
+ GlobalRef& operator=(const GlobalRef& other) {
26
+ if (this != &other) {
27
+ if (_obj) VM::env()->DeleteGlobalRef(_obj);
28
+ _obj = other._obj ? static_cast<T>(VM::env()->NewGlobalRef(other._obj)) : nullptr;
29
+ }
30
+ return *this;
31
+ }
32
+
33
+ // Move constructor
34
+ GlobalRef(GlobalRef&& other) noexcept : _obj(other._obj) { other._obj = nullptr; }
35
+
36
+ // Move assignment operator
37
+ GlobalRef& operator=(GlobalRef&& other) noexcept {
38
+ if (this != &other) {
39
+ if (_obj) VM::env()->DeleteGlobalRef(_obj);
40
+ _obj = other._obj;
41
+ other._obj = nullptr;
42
+ }
43
+ return *this;
44
+ }
45
+
46
+ T get() const { return _obj; }
47
+
48
+ protected:
49
+ T _obj = nullptr;
50
+ };
51
+
52
+ template <typename T>
53
+ class LocalRef {
54
+ public:
55
+ LocalRef() = default;
56
+
57
+ explicit LocalRef(T obj) : _obj(obj ? static_cast<T>(VM::env()->NewLocalRef(obj)) : nullptr) {}
58
+
59
+ ~LocalRef() {
60
+ if (_obj) VM::env()->DeleteLocalRef(_obj);
61
+ }
62
+
63
+ // Copy constructor
64
+ LocalRef(const LocalRef& other) : _obj(other._obj ? static_cast<T>(VM::env()->NewLocalRef(other._obj)) : nullptr) {}
65
+
66
+ // Copy assignment operator
67
+ LocalRef& operator=(const LocalRef& other) {
68
+ if (this != &other) {
69
+ if (_obj) VM::env()->DeleteLocalRef(_obj);
70
+ _obj = other._obj ? static_cast<T>(VM::env()->NewLocalRef(other._obj)) : nullptr;
71
+ }
72
+ return *this;
73
+ }
74
+
75
+ // Move constructor
76
+ LocalRef(LocalRef&& other) noexcept : _obj(other._obj) { other._obj = nullptr; }
77
+
78
+ // Move assignment operator
79
+ LocalRef& operator=(LocalRef&& other) noexcept {
80
+ if (this != &other) {
81
+ if (_obj) VM::env()->DeleteLocalRef(_obj);
82
+ _obj = other._obj;
83
+ other._obj = nullptr;
84
+ }
85
+ return *this;
86
+ }
87
+
88
+ T get() const { return _obj; }
89
+
90
+ protected:
91
+ T _obj = nullptr;
92
+ };
93
+
94
+ // A type for references intended to be returned to JNI
95
+ template <typename T>
96
+ class ReleasableLocalRef : public LocalRef<T> {
97
+ public:
98
+ using LocalRef<T>::LocalRef;
99
+ T release() noexcept {
100
+ T obj = this->_obj;
101
+ this->_obj = nullptr;
102
+ return obj;
103
+ }
104
+ };
105
+
106
+ template <typename T>
107
+ class WeakRef {
108
+ public:
109
+ WeakRef() = default;
110
+
111
+ explicit WeakRef(T obj) : _obj(obj ? static_cast<T>(VM::env()->NewWeakGlobalRef(obj)) : nullptr) {}
112
+
113
+ ~WeakRef() {
114
+ if (_obj && VM::is_jvm_alive()) VM::env()->DeleteWeakGlobalRef(_obj);
115
+ }
116
+
117
+ // Copy constructor
118
+ WeakRef(const WeakRef& other)
119
+ : _obj(other._obj ? static_cast<T>(VM::env()->NewWeakGlobalRef(other._obj)) : nullptr) {}
120
+
121
+ // Copy assignment operator
122
+ WeakRef& operator=(const WeakRef& other) {
123
+ if (this != &other) {
124
+ if (_obj) VM::env()->DeleteWeakGlobalRef(_obj);
125
+ _obj = other._obj ? static_cast<T>(VM::env()->NewWeakGlobalRef(other._obj)) : nullptr;
126
+ }
127
+ return *this;
128
+ }
129
+
130
+ // Move constructor
131
+ WeakRef(WeakRef&& other) noexcept : _obj(other._obj) { other._obj = nullptr; }
132
+
133
+ // Move assignment operator
134
+ WeakRef& operator=(WeakRef&& other) noexcept {
135
+ if (this != &other) {
136
+ if (_obj) VM::env()->DeleteWeakGlobalRef(_obj);
137
+ _obj = other._obj;
138
+ other._obj = nullptr;
139
+ }
140
+ return *this;
141
+ }
142
+
143
+ T get() const { return _obj; }
144
+
145
+ bool is_valid() const { return _obj && !VM::env()->IsSameObject(_obj, nullptr); }
146
+
147
+ protected:
148
+ T _obj = nullptr;
149
+ };
150
+
151
+ } // namespace SimpleJNI
@@ -0,0 +1,249 @@
1
+ #pragma once
2
+
3
+ #include "References.hpp"
4
+
5
+ #include <string>
6
+ #include <vector>
7
+ #include <unordered_map>
8
+
9
+
10
+ namespace SimpleJNI {
11
+
12
+ /**
13
+ * @brief Descriptor for a single Java method to be preloaded.
14
+ *
15
+ * This struct holds the metadata for a method that a Java class needs to expose to C++.
16
+ * It links a method name and signature to a jmethodID pointer where the resolved ID will
17
+ * be stored during JNI_OnLoad.
18
+ */
19
+ struct MethodDescriptor {
20
+ std::string name; ///< Name of the Java method (e.g., "startScan").
21
+ std::string signature; ///< JNI signature of the method (e.g., "(Landroid/bluetooth/le/ScanCallback;)V").
22
+ jmethodID* target = nullptr; ///< Pointer to where the resolved jmethodID will be stored.
23
+
24
+ MethodDescriptor(const std::string& n, const std::string& sig, jmethodID* tgt)
25
+ : name(n), signature(sig), target(tgt) {}
26
+ };
27
+
28
+ /**
29
+ * @brief Descriptor for a Java class and its methods to be preloaded.
30
+ *
31
+ * This struct defines a Java class by its fully qualified name and a list of methods
32
+ * that need JNI access. It ties the class and its methods to storage locations
33
+ * (GlobalRef<jclass> and jmethodID pointers) that will be populated during preloading.
34
+ */
35
+ struct JNIDescriptor {
36
+ std::string class_name; ///< Fully qualified Java class name (e.g., "android/bluetooth/le/BluetoothLeScanner").
37
+ GlobalRef<jclass>* class_target; ///< Pointer to where the resolved jclass will be stored.
38
+ std::vector<MethodDescriptor> methods; ///< List of methods to preload for this class.
39
+
40
+ JNIDescriptor(const std::string& name, SimpleJNI::GlobalRef<jclass>* cls_target,
41
+ std::initializer_list<MethodDescriptor> meths)
42
+ : class_name(name), class_target(cls_target), methods(meths) {}
43
+ };
44
+
45
+ /**
46
+ * @brief Descriptor for a Java class and its static methods to be preloaded.
47
+ *
48
+ * This struct defines a Java class by its fully qualified name and a list of static methods
49
+ * that need JNI access. It ties the class and its static methods to storage locations
50
+ * (GlobalRef<jclass> and jmethodID pointers) that will be populated during preloading.
51
+ */
52
+ struct StaticJNIDescriptor {
53
+ std::string class_name; ///< Fully qualified Java class name (e.g., "android/bluetooth/le/BluetoothLeScanner").
54
+ GlobalRef<jclass>* class_target; ///< Pointer to where the resolved jclass will be stored.
55
+ std::vector<MethodDescriptor> static_methods; ///< List of static methods to preload for this class.
56
+
57
+ StaticJNIDescriptor(const std::string& name, SimpleJNI::GlobalRef<jclass>* cls_target,
58
+ std::initializer_list<MethodDescriptor> meths)
59
+ : class_name(name), class_target(cls_target), static_methods(meths) {}
60
+ };
61
+
62
+ /**
63
+ * @brief Singleton registrar for collecting and preloading JNI class descriptors.
64
+ *
65
+ * The Registrar collects JNIDescriptor and StaticJNIDescriptor instances at compile-time
66
+ * (via static initialization) and preloads their JNI resources (jclass and jmethodID)
67
+ * during JNI_OnLoad. This ensures all necessary JNI metadata is resolved early on the
68
+ * correct thread, avoiding runtime ClassLoader or thread-context issues.
69
+ */
70
+ class Registrar {
71
+ public:
72
+ /// @brief Get the singleton instance of the Registrar.
73
+ static Registrar& get() {
74
+ static Registrar instance;
75
+
76
+ // Register common classes that are used in many places.
77
+ static const JNIDescriptor desc{
78
+ "java/lang/Object", // Java class name
79
+ nullptr, // Where to store the jclass
80
+ { // Instance methods to preload
81
+ {"hashCode", "()I", nullptr},
82
+ }
83
+ };
84
+
85
+ instance.register_descriptor(&desc);
86
+
87
+ return instance;
88
+ }
89
+
90
+ /**
91
+ * @brief Register a JNIDescriptor for preloading.
92
+ *
93
+ * Called automatically by AutoRegister during static initialization. Adds the descriptor
94
+ * to an internal list that will be processed in preload().
95
+ * @param descriptor Pointer to the JNIDescriptor to register.
96
+ */
97
+ void register_descriptor(const JNIDescriptor* descriptor) { descriptors.push_back(descriptor); }
98
+
99
+ /**
100
+ * @brief Register a StaticJNIDescriptor for preloading.
101
+ *
102
+ * Called automatically by AutoRegister during static initialization. Adds the descriptor
103
+ * to an internal list that will be processed in preload().
104
+ * @param descriptor Pointer to the StaticJNIDescriptor to register.
105
+ */
106
+ void register_static_descriptor(const StaticJNIDescriptor* descriptor) { static_descriptors.push_back(descriptor); }
107
+
108
+ /**
109
+ * @brief Preload all registered JNI resources.
110
+ *
111
+ * Called from JNI_OnLoad to resolve jclass and jmethodID for all registered descriptors.
112
+ * Uses the provided JNIEnv to perform JNI lookups on the thread that loads the library,
113
+ * ensuring access to the correct ClassLoader context (e.g., system classes on Android).
114
+ * @param env JNIEnv pointer from JNI_OnLoad.
115
+ * @throws std::runtime_error if a class or method cannot be resolved.
116
+ */
117
+ void preload(JNIEnv* env) {
118
+ // Preload instance method descriptors
119
+ for (const JNIDescriptor* desc : descriptors) {
120
+ // Load the Java class
121
+ jclass local_cls = env->FindClass(desc->class_name.c_str());
122
+ if (!local_cls) {
123
+ throw std::runtime_error("Failed to load class: " + desc->class_name);
124
+ }
125
+
126
+ GlobalRef<jclass> class_target(local_cls);
127
+ env->DeleteLocalRef(local_cls); // Clean up the local reference
128
+
129
+ // If *desc->class_target is null, we don't set it.
130
+ if (desc->class_target != nullptr) {
131
+ *desc->class_target = class_target;
132
+ }
133
+ class_targets[desc->class_name] = class_target;
134
+
135
+ // Load each method
136
+ for (const MethodDescriptor& method : desc->methods) {
137
+ jmethodID method_id = env->GetMethodID(class_target.get(), method.name.c_str(), method.signature.c_str());
138
+ if (!method_id) {
139
+ throw std::runtime_error("Failed to get method: " + desc->class_name + "." + method.name);
140
+ }
141
+
142
+ // If *method.target is null, we don't set it.
143
+ if (method.target != nullptr) {
144
+ *method.target = method_id;
145
+ }
146
+ method_targets[desc->class_name][method.name] = method_id;
147
+ }
148
+ }
149
+
150
+ // Clear descriptors once they are preloaded
151
+ descriptors.clear();
152
+
153
+ // Preload static method descriptors
154
+ for (const StaticJNIDescriptor* desc : static_descriptors) {
155
+ // Load the Java class
156
+ jclass local_cls = env->FindClass(desc->class_name.c_str());
157
+ if (!local_cls) {
158
+ throw std::runtime_error("Failed to load class: " + desc->class_name);
159
+ }
160
+
161
+ GlobalRef<jclass> class_target(local_cls);
162
+ env->DeleteLocalRef(local_cls); // Clean up the local reference
163
+
164
+ // If *desc->class_target is null, we don't set it.
165
+ if (desc->class_target != nullptr) {
166
+ *desc->class_target = class_target;
167
+ }
168
+ static_class_targets[desc->class_name] = class_target;
169
+
170
+ // Load each static method
171
+ for (const MethodDescriptor& method : desc->static_methods) {
172
+ jmethodID method_id = env->GetStaticMethodID(class_target.get(), method.name.c_str(), method.signature.c_str());
173
+ if (!method_id) {
174
+ throw std::runtime_error("Failed to get static method: " + desc->class_name + "." + method.name);
175
+ }
176
+
177
+ // If *method.target is null, we don't set it.
178
+ if (method.target != nullptr) {
179
+ *method.target = method_id;
180
+ }
181
+ static_method_targets[desc->class_name][method.name] = method_id;
182
+ }
183
+ }
184
+
185
+ // Clear static descriptors once they are preloaded
186
+ static_descriptors.clear();
187
+ }
188
+
189
+ GlobalRef<jclass> get_class(const std::string& name) {
190
+ return class_targets[name];
191
+ }
192
+
193
+ jmethodID get_method(const std::string& class_name, const std::string& method_name) {
194
+ return method_targets[class_name][method_name];
195
+ }
196
+
197
+ GlobalRef<jclass> get_static_class(const std::string& name) {
198
+ return static_class_targets[name];
199
+ }
200
+
201
+ jmethodID get_static_method(const std::string& class_name, const std::string& method_name) {
202
+ return static_method_targets[class_name][method_name];
203
+ }
204
+
205
+ private:
206
+ Registrar() = default;
207
+ ~Registrar() = default;
208
+ Registrar(const Registrar&) = delete;
209
+ Registrar& operator=(const Registrar&) = delete;
210
+
211
+ std::unordered_map<std::string, GlobalRef<jclass>> class_targets;
212
+ std::unordered_map<std::string, std::unordered_map<std::string, jmethodID>> method_targets;
213
+
214
+ std::unordered_map<std::string, GlobalRef<jclass>> static_class_targets;
215
+ std::unordered_map<std::string, std::unordered_map<std::string, jmethodID>> static_method_targets;
216
+
217
+ std::vector<const JNIDescriptor*> descriptors; ///< List of registered descriptors.
218
+ std::vector<const StaticJNIDescriptor*> static_descriptors; ///< List of registered static descriptors.
219
+ };
220
+
221
+ /**
222
+ * @brief Helper to automatically register JNI descriptors at compile time.
223
+ *
224
+ * This template class uses static initialization to register descriptors with the Registrar
225
+ * when the program loads. The constructor runs before main() or JNI_OnLoad, ensuring all
226
+ * classes are registered without manual intervention. It supports registering non-static methods,
227
+ * static methods, or both.
228
+ * @tparam T The class type (e.g., BluetoothScanner) that owns the descriptor.
229
+ */
230
+ template <typename T>
231
+ struct AutoRegister {
232
+ // Constructor for non-static methods only
233
+ AutoRegister(const JNIDescriptor* desc) {
234
+ Registrar::get().register_descriptor(desc);
235
+ }
236
+
237
+ // Constructor for static methods only
238
+ AutoRegister(const StaticJNIDescriptor* static_desc) {
239
+ Registrar::get().register_static_descriptor(static_desc);
240
+ }
241
+
242
+ // Constructor for both non-static and static methods
243
+ AutoRegister(const JNIDescriptor* desc, const StaticJNIDescriptor* static_desc) {
244
+ Registrar::get().register_descriptor(desc);
245
+ Registrar::get().register_static_descriptor(static_desc);
246
+ }
247
+ };
248
+
249
+ } // namespace SimpleJNI
@@ -0,0 +1,93 @@
1
+ #pragma once
2
+
3
+ #include <jni.h>
4
+
5
+ #include <mutex>
6
+
7
+ namespace SimpleJNI {
8
+
9
+ // Generic class to handle the Java Virtual Machine (JVM)
10
+ class VM {
11
+ public:
12
+ // TODO: Make the VM class transparent to the JavaVM pointer.
13
+ static JavaVM* jvm(JavaVM* jvm_override = nullptr) {
14
+ static std::mutex mutex; // Function-local for tied lifecycle
15
+ static VM instance; // Function-local for lazy init and auto-cleanup
16
+
17
+ std::scoped_lock lock(mutex); // Lock entire access for safety
18
+ if (jvm_override != nullptr) {
19
+ if (instance._jvm != nullptr && instance._jvm != jvm_override) {
20
+ throw std::runtime_error("JavaVM pointer already set");
21
+ }
22
+ instance._jvm = jvm_override;
23
+ } else if (instance._jvm == nullptr) {
24
+ jsize count;
25
+ if (JNI_GetCreatedJavaVMs(&instance._jvm, 1, &count) != JNI_OK || count == 0) {
26
+ throw std::runtime_error("Failed to retrieve the Java Virtual Machine");
27
+ }
28
+ }
29
+ return instance._jvm;
30
+ }
31
+
32
+ static bool is_jvm_alive() {
33
+ JavaVM* jvm = VM::jvm();
34
+ if (!jvm) return false;
35
+ JNIEnv* env = nullptr;
36
+ return jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) == JNI_OK;
37
+ }
38
+
39
+ static JNIEnv* env() {
40
+ JNIEnv* env = nullptr;
41
+ JavaVM* jvm = VM::jvm();
42
+
43
+ auto getEnvResult = jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
44
+ switch (getEnvResult) {
45
+ case JNI_OK:
46
+ break;
47
+ case JNI_EDETACHED: {
48
+ #ifdef __ANDROID__
49
+ auto result = jvm->AttachCurrentThread(&env, nullptr);
50
+ #else
51
+ auto result = jvm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr);
52
+ #endif
53
+ if (result != JNI_OK) {
54
+ throw std::runtime_error("Failed to attach the current thread to the JVM");
55
+ }
56
+ break;
57
+ }
58
+ case JNI_EVERSION:
59
+ throw std::runtime_error("Failed to get the JNIEnv, JNI version not supported");
60
+ default:
61
+ throw std::runtime_error("Failed to get the JNIEnv, unknown error");
62
+ }
63
+ return env;
64
+ }
65
+
66
+ static void attach() {
67
+ JNIEnv* env = nullptr;
68
+ JavaVM* jvm = VM::jvm();
69
+ #ifdef __ANDROID__
70
+ auto result = jvm->AttachCurrentThread(&env, nullptr);
71
+ #else
72
+ auto result = jvm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr);
73
+ #endif
74
+ if (result != JNI_OK) {
75
+ throw std::runtime_error("Failed to attach the current thread to the JVM");
76
+ }
77
+ }
78
+
79
+ static void detach() {
80
+ JavaVM* jvm = VM::jvm();
81
+ jvm->DetachCurrentThread();
82
+ }
83
+
84
+ private:
85
+ VM() = default;
86
+ virtual ~VM() = default;
87
+ VM(VM& other) = delete; // Remove the copy constructor
88
+ void operator=(const VM&) = delete; // Remove the copy assignment
89
+
90
+ JavaVM* _jvm = nullptr;
91
+ };
92
+
93
+ } // namespace SimpleJNI