react-native-bluebreeze 0.0.11 → 1.0.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.
Files changed (96) hide show
  1. package/README.md +97 -13
  2. package/android/build.gradle +2 -3
  3. package/android/generated/jni/CMakeLists.txt +1 -9
  4. package/android/generated/jni/RNBlueBreezeSpec-generated.cpp +1 -1
  5. package/android/generated/jni/react/renderer/components/RNBlueBreezeSpec/RNBlueBreezeSpecJSI.h +234 -279
  6. package/android/src/main/java/com/bluebreeze/BlueBreezeModule.kt +140 -120
  7. package/ios/BlueBreezeImpl.swift +30 -24
  8. package/ios/Bluebreeze.h +1 -1
  9. package/ios/Bluebreeze.mm +2 -7
  10. package/ios/generated/{RNBlueBreezeSpecJSI.h → ReactCodegen/RNBlueBreezeSpecJSI.h} +234 -279
  11. package/lib/commonjs/implementation/bluebreeze_characteristic.js +42 -11
  12. package/lib/commonjs/implementation/bluebreeze_characteristic.js.map +1 -1
  13. package/lib/commonjs/implementation/bluebreeze_connection_status.js +2 -0
  14. package/lib/commonjs/implementation/bluebreeze_connection_status.js.map +1 -0
  15. package/lib/commonjs/implementation/bluebreeze_device.js +53 -13
  16. package/lib/commonjs/implementation/bluebreeze_device.js.map +1 -1
  17. package/lib/commonjs/implementation/bluebreeze_scan_result.js +19 -0
  18. package/lib/commonjs/implementation/bluebreeze_scan_result.js.map +1 -1
  19. package/lib/commonjs/implementation/bluebreeze_service.js +9 -0
  20. package/lib/commonjs/implementation/bluebreeze_service.js.map +1 -1
  21. package/lib/commonjs/implementation/native_bridge.js +108 -0
  22. package/lib/commonjs/implementation/native_bridge.js.map +1 -0
  23. package/lib/commonjs/index.js +29 -100
  24. package/lib/commonjs/index.js.map +1 -1
  25. package/lib/commonjs/package.json +1 -0
  26. package/lib/module/implementation/bluebreeze_characteristic.js +43 -10
  27. package/lib/module/implementation/bluebreeze_characteristic.js.map +1 -1
  28. package/lib/module/implementation/bluebreeze_connection_status.js +2 -0
  29. package/lib/module/implementation/bluebreeze_connection_status.js.map +1 -0
  30. package/lib/module/implementation/bluebreeze_device.js +54 -12
  31. package/lib/module/implementation/bluebreeze_device.js.map +1 -1
  32. package/lib/module/implementation/bluebreeze_scan_result.js +19 -0
  33. package/lib/module/implementation/bluebreeze_scan_result.js.map +1 -1
  34. package/lib/module/implementation/bluebreeze_service.js +9 -0
  35. package/lib/module/implementation/bluebreeze_service.js.map +1 -1
  36. package/lib/module/implementation/native_bridge.js +99 -0
  37. package/lib/module/implementation/native_bridge.js.map +1 -0
  38. package/lib/module/index.js +29 -100
  39. package/lib/module/index.js.map +1 -1
  40. package/lib/module/package.json +1 -0
  41. package/lib/typescript/commonjs/src/NativeBlueBreeze.d.ts +1 -1
  42. package/lib/typescript/commonjs/src/NativeBlueBreeze.d.ts.map +1 -1
  43. package/lib/typescript/commonjs/src/implementation/bluebreeze_authorization.d.ts +10 -1
  44. package/lib/typescript/commonjs/src/implementation/bluebreeze_authorization.d.ts.map +1 -1
  45. package/lib/typescript/commonjs/src/implementation/bluebreeze_characteristic.d.ts +29 -0
  46. package/lib/typescript/commonjs/src/implementation/bluebreeze_characteristic.d.ts.map +1 -1
  47. package/lib/typescript/commonjs/src/implementation/bluebreeze_connection_status.d.ts +7 -0
  48. package/lib/typescript/commonjs/src/implementation/bluebreeze_connection_status.d.ts.map +1 -0
  49. package/lib/typescript/commonjs/src/implementation/bluebreeze_device.d.ts +42 -1
  50. package/lib/typescript/commonjs/src/implementation/bluebreeze_device.d.ts.map +1 -1
  51. package/lib/typescript/commonjs/src/implementation/bluebreeze_scan_result.d.ts +11 -0
  52. package/lib/typescript/commonjs/src/implementation/bluebreeze_scan_result.d.ts.map +1 -1
  53. package/lib/typescript/commonjs/src/implementation/bluebreeze_service.d.ts +6 -0
  54. package/lib/typescript/commonjs/src/implementation/bluebreeze_service.d.ts.map +1 -1
  55. package/lib/typescript/commonjs/src/implementation/bluebreeze_state.d.ts +16 -1
  56. package/lib/typescript/commonjs/src/implementation/bluebreeze_state.d.ts.map +1 -1
  57. package/lib/typescript/commonjs/src/implementation/native_bridge.d.ts +17 -0
  58. package/lib/typescript/commonjs/src/implementation/native_bridge.d.ts.map +1 -0
  59. package/lib/typescript/commonjs/src/index.d.ts +0 -13
  60. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  61. package/lib/typescript/module/src/NativeBlueBreeze.d.ts +1 -1
  62. package/lib/typescript/module/src/NativeBlueBreeze.d.ts.map +1 -1
  63. package/lib/typescript/module/src/implementation/bluebreeze_authorization.d.ts +10 -1
  64. package/lib/typescript/module/src/implementation/bluebreeze_authorization.d.ts.map +1 -1
  65. package/lib/typescript/module/src/implementation/bluebreeze_characteristic.d.ts +29 -0
  66. package/lib/typescript/module/src/implementation/bluebreeze_characteristic.d.ts.map +1 -1
  67. package/lib/typescript/module/src/implementation/bluebreeze_connection_status.d.ts +7 -0
  68. package/lib/typescript/module/src/implementation/bluebreeze_connection_status.d.ts.map +1 -0
  69. package/lib/typescript/module/src/implementation/bluebreeze_device.d.ts +43 -2
  70. package/lib/typescript/module/src/implementation/bluebreeze_device.d.ts.map +1 -1
  71. package/lib/typescript/module/src/implementation/bluebreeze_scan_result.d.ts +12 -1
  72. package/lib/typescript/module/src/implementation/bluebreeze_scan_result.d.ts.map +1 -1
  73. package/lib/typescript/module/src/implementation/bluebreeze_service.d.ts +7 -1
  74. package/lib/typescript/module/src/implementation/bluebreeze_service.d.ts.map +1 -1
  75. package/lib/typescript/module/src/implementation/bluebreeze_state.d.ts +16 -1
  76. package/lib/typescript/module/src/implementation/bluebreeze_state.d.ts.map +1 -1
  77. package/lib/typescript/module/src/implementation/native_bridge.d.ts +17 -0
  78. package/lib/typescript/module/src/implementation/native_bridge.d.ts.map +1 -0
  79. package/lib/typescript/module/src/index.d.ts +6 -19
  80. package/lib/typescript/module/src/index.d.ts.map +1 -1
  81. package/package.json +13 -23
  82. package/react-native-bluebreeze.podspec +1 -1
  83. package/src/NativeBlueBreeze.ts +1 -1
  84. package/src/implementation/bluebreeze_authorization.ts +10 -1
  85. package/src/implementation/bluebreeze_characteristic.ts +48 -10
  86. package/src/implementation/bluebreeze_connection_status.ts +6 -0
  87. package/src/implementation/bluebreeze_device.ts +62 -13
  88. package/src/implementation/bluebreeze_scan_result.ts +18 -0
  89. package/src/implementation/bluebreeze_service.ts +8 -0
  90. package/src/implementation/bluebreeze_state.ts +16 -1
  91. package/src/implementation/native_bridge.ts +196 -0
  92. package/src/index.tsx +24 -202
  93. package/android/generated/jni/react/renderer/components/RNBlueBreezeSpec/RNBlueBreezeSpecJSI-generated.cpp +0 -179
  94. package/ios/generated/RNBlueBreezeSpecJSI-generated.cpp +0 -179
  95. /package/ios/generated/{RNBlueBreezeSpec → ReactCodegen/RNBlueBreezeSpec}/RNBlueBreezeSpec-generated.mm +0 -0
  96. /package/ios/generated/{RNBlueBreezeSpec → ReactCodegen/RNBlueBreezeSpec}/RNBlueBreezeSpec.h +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-bluebreeze",
3
- "version": "0.0.11",
3
+ "version": "1.0.0",
4
4
  "description": "BlueBreeze bindings for React Native. A modern Bluetooth LE library.",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/commonjs/index.js",
@@ -42,8 +42,7 @@
42
42
  "typecheck": "tsc",
43
43
  "lint": "eslint \"**/*.{js,ts,tsx}\"",
44
44
  "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
45
- "prepare": "bob build",
46
- "release": "release-it --no-increment"
45
+ "prepare": "bob build"
47
46
  },
48
47
  "keywords": [
49
48
  "react-native",
@@ -64,13 +63,15 @@
64
63
  "registry": "https://registry.npmjs.org/"
65
64
  },
66
65
  "dependencies": {
67
- "react-native-value-emitter": "^1.0.0"
66
+ "react-native-value-emitter": "^1.0.1"
68
67
  },
69
68
  "devDependencies": {
70
69
  "@eslint/js": "^9.21.0",
71
70
  "@evilmartians/lefthook": "^1.10.10",
72
71
  "@react-native-community/cli": "15.1.3",
72
+ "@react-native/babel-preset": "0.87.1",
73
73
  "@react-native/eslint-config": "^0.77.1",
74
+ "@react-native/jest-preset": "0.87.1",
74
75
  "@types/jest": "^29.5.14",
75
76
  "@types/react": "^19.0.8",
76
77
  "del-cli": "^6.0.0",
@@ -82,9 +83,8 @@
82
83
  "jest": "^29.7.0",
83
84
  "prettier": "^3.5.1",
84
85
  "react": "18.3.1",
85
- "react-native": "0.77.1",
86
- "react-native-builder-bob": "^0.37.0",
87
- "release-it": "^18.1.2",
86
+ "react-native": "0.87.1",
87
+ "react-native-builder-bob": "^0.43.1",
88
88
  "turbo": "^2.4.2",
89
89
  "typescript": "^5.7.3",
90
90
  "typescript-eslint": "^8.24.1"
@@ -101,29 +101,20 @@
101
101
  ],
102
102
  "packageManager": "yarn@3.6.1",
103
103
  "jest": {
104
- "preset": "react-native",
104
+ "preset": "@react-native/jest-preset",
105
105
  "modulePathIgnorePatterns": [
106
106
  "<rootDir>/example/node_modules",
107
107
  "<rootDir>/lib/"
108
- ]
108
+ ],
109
+ "moduleNameMapper": {
110
+ "^.*/NativeBlueBreeze$": "<rootDir>/src/NativeBlueBreeze.ts"
111
+ }
109
112
  },
110
113
  "commitlint": {
111
114
  "extends": [
112
115
  "@commitlint/config-conventional"
113
116
  ]
114
117
  },
115
- "release-it": {
116
- "git": {
117
- "commitMessage": "chore: release ${version}",
118
- "tagName": "v${version}"
119
- },
120
- "npm": {
121
- "publish": true
122
- },
123
- "github": {
124
- "release": true
125
- }
126
- },
127
118
  "eslintConfig": {
128
119
  "root": true,
129
120
  "extends": [
@@ -175,8 +166,7 @@
175
166
  [
176
167
  "typescript",
177
168
  {
178
- "project": "tsconfig.build.json",
179
- "esm": true
169
+ "project": "tsconfig.build.json"
180
170
  }
181
171
  ]
182
172
  ]
@@ -17,7 +17,7 @@ Pod::Spec.new do |s|
17
17
  s.source_files = "ios/**/*.{h,m,mm,cpp,swift}"
18
18
  s.private_header_files = "ios/**/*.h"
19
19
 
20
- s.dependency "BlueBreeze", "~> 0.0.16"
20
+ s.dependency "BlueBreeze", "1.0.1"
21
21
 
22
22
  # Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
23
23
  # See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
@@ -1,6 +1,6 @@
1
1
  import type { TurboModule } from 'react-native'
2
2
  import { TurboModuleRegistry } from 'react-native'
3
- import type { EventEmitter } from 'react-native/Libraries/Types/CodegenTypes'
3
+ import type { EventEmitter } from 'react-native/Libraries/Types/CodegenTypesNamespace'
4
4
 
5
5
  /// Events
6
6
 
@@ -1 +1,10 @@
1
- export type BBAuthorization = 'unknown' | 'showRationale' | 'restricted' | 'denied' | 'authorized'
1
+ /** Whether this app is currently allowed to use Bluetooth. */
2
+ export type BBAuthorization =
3
+ /** Not yet determined. */
4
+ | 'unknown'
5
+ /** Permission has been denied once but not permanently. Android only. */
6
+ | 'showRationale'
7
+ /** Permission has been permanently denied. */
8
+ | 'denied'
9
+ /** Permission has been granted. */
10
+ | 'authorized'
@@ -1,6 +1,19 @@
1
- import BlueBreeze from "react-native-bluebreeze"
2
1
  import type { StateValueEmitter } from "react-native-value-emitter"
2
+ import {
3
+ deviceCharacteristicData,
4
+ deviceCharacteristicNotifyEnabled,
5
+ deviceCharacteristicRead,
6
+ deviceCharacteristicSubscribe,
7
+ deviceCharacteristicUnsubscribe,
8
+ deviceCharacteristicWrite,
9
+ } from "./native_bridge"
3
10
 
11
+ /**
12
+ * A GATT characteristic belonging to a {@link BBService}.
13
+ *
14
+ * Rediscovered (and replaced with a new instance) every time services are discovered.
15
+ * Do not hold onto a `BBCharacteristic` reference across a disconnect/reconnect cycle.
16
+ */
4
17
  export class BBCharacteristic {
5
18
  constructor(
6
19
  deviceId: string,
@@ -13,28 +26,53 @@ export class BBCharacteristic {
13
26
  this.name = name
14
27
  this.properties = properties
15
28
 
16
- this.data = BlueBreeze.deviceCharacteristicData(deviceId, serviceId, id)
17
- this.notifyEnabled = BlueBreeze.deviceCharacteristicNotifyEnabled(deviceId, serviceId, id)
29
+ this.data = deviceCharacteristicData(deviceId, serviceId, id)
30
+ this.notifyEnabled = deviceCharacteristicNotifyEnabled(deviceId, serviceId, id)
18
31
 
19
- this.read = () => BlueBreeze.deviceCharacteristicRead(deviceId, serviceId, id)
20
- this.write = (data: number[], withResponse: boolean) => BlueBreeze.deviceCharacteristicWrite(deviceId, serviceId, id, data, withResponse)
21
- this.subscribe = () => BlueBreeze.deviceCharacteristicSubscribe(deviceId, serviceId, id)
22
- this.unsubscribe = () => BlueBreeze.deviceCharacteristicUnsubscribe(deviceId, serviceId, id)
32
+ this.read = () => deviceCharacteristicRead(deviceId, serviceId, id)
33
+ this.write = (data: number[], withResponse: boolean) => deviceCharacteristicWrite(deviceId, serviceId, id, data, withResponse)
34
+ this.subscribe = () => deviceCharacteristicSubscribe(deviceId, serviceId, id)
35
+ this.unsubscribe = () => deviceCharacteristicUnsubscribe(deviceId, serviceId, id)
23
36
  }
24
37
 
38
+ /** The characteristic's UUID. */
25
39
  id: string
40
+
41
+ /** A human-readable name for well-known (Bluetooth SIG assigned-number) characteristics, if recognized. */
26
42
  name?: string
43
+
44
+ /** The GATT properties this characteristic declares. */
27
45
  properties: string[]
28
46
 
29
- // Data
47
+ /** The last value read or notified for this characteristic. Empty until first populated. */
30
48
  data: StateValueEmitter<number[]>
31
49
 
32
- // Notifying
50
+ /** Whether this app is currently subscribed to notifications for this characteristic. */
33
51
  notifyEnabled: StateValueEmitter<boolean>
34
52
 
35
- // Operations
53
+ /**
54
+ * Reads this characteristic's current value, also updating {@link data}.
55
+ * @throws if the read fails or times out.
56
+ */
36
57
  read: () => Promise<number[]>
58
+
59
+ /**
60
+ * Writes a value to this characteristic.
61
+ * @param data The bytes to write.
62
+ * @param withResponse Whether to wait for the peripheral's write acknowledgment.
63
+ * @throws if the write fails or times out.
64
+ */
37
65
  write: (data: number[], withResponse: boolean) => Promise<void>
66
+
67
+ /**
68
+ * Subscribes to notifications.
69
+ * @throws if the subscribe request fails or times out.
70
+ */
38
71
  subscribe: () => Promise<void>
72
+
73
+ /**
74
+ * Unsubscribes from notifications.
75
+ * @throws if the unsubscribe request fails or times out.
76
+ */
39
77
  unsubscribe: () => Promise<void>
40
78
  }
@@ -0,0 +1,6 @@
1
+ /** Connection status of a {@link BBDevice}. */
2
+ export type BBDeviceConnectionStatus =
3
+ /** Not connected. */
4
+ | 'disconnected'
5
+ /** Connected and ready for operations. */
6
+ | 'connected'
@@ -1,7 +1,27 @@
1
- import BlueBreeze from "react-native-bluebreeze"
2
1
  import type { StateValueEmitter } from "react-native-value-emitter"
2
+ import type { BBDeviceConnectionStatus } from "./bluebreeze_connection_status"
3
3
  import type { BBService } from "./bluebreeze_service"
4
+ import {
5
+ deviceConnect,
6
+ deviceConnectionStatus,
7
+ deviceDisconnect,
8
+ deviceDiscoverServices,
9
+ deviceMTU,
10
+ deviceRequestMTU,
11
+ deviceServices,
12
+ } from "./native_bridge"
4
13
 
14
+ /**
15
+ * A Bluetooth Low Energy peripheral, either seen while scanning or already connected from a
16
+ * prior session. Obtained via `BlueBreeze.devices` -- never constructed directly.
17
+ *
18
+ * A `BBDevice` stays valid for the app's lifetime once created: it isn't recreated across a
19
+ * connect/disconnect cycle, so its {@link connectionStatus}, {@link mtu}, and object identity
20
+ * all stay stable. Its {@link services} list, however, is rebuilt from scratch on every
21
+ * {@link discoverServices} call -- including one after a reconnect -- so a {@link BBService} or
22
+ * its {@link BBCharacteristic}s should not be held onto across a disconnect/reconnect; re-read
23
+ * `services` instead.
24
+ */
5
25
  export class BBDevice {
6
26
  constructor(
7
27
  id: string,
@@ -10,31 +30,60 @@ export class BBDevice {
10
30
  this.id = id
11
31
  this.name = name
12
32
 
13
- this.services = BlueBreeze.deviceServices(id)
14
- this.connectionStatus = BlueBreeze.deviceConnectionStatus(id)
15
- this.mtu = BlueBreeze.deviceMTU(id)
33
+ this.services = deviceServices(id)
34
+ this.connectionStatus = deviceConnectionStatus(id)
35
+ this.mtu = deviceMTU(id)
16
36
 
17
- this.connect = () => BlueBreeze.deviceConnect(id)
18
- this.disconnect = () => BlueBreeze.deviceDisconnect(id)
19
- this.discoverServices = () => BlueBreeze.deviceDiscoverServices(id)
20
- this.requestMTU = (mtu: number) => BlueBreeze.deviceRequestMTU(id, mtu)
37
+ this.connect = () => deviceConnect(id)
38
+ this.disconnect = () => deviceDisconnect(id)
39
+ this.discoverServices = () => deviceDiscoverServices(id)
40
+ this.requestMTU = (mtu: number) => deviceRequestMTU(id, mtu)
21
41
  }
22
42
 
43
+ /** The device's unique identifier. */
23
44
  id: string
45
+
46
+ /** The device's advertised name, if any. */
24
47
  name?: string
25
48
 
26
- // Services
49
+ /** The device's services discovered so far. Empty until {@link discoverServices} has been called and completed. */
27
50
  services: StateValueEmitter<BBService[]>
28
51
 
29
- // Connection status
30
- connectionStatus: StateValueEmitter<string>
52
+ /** Current connection status of the device. */
53
+ connectionStatus: StateValueEmitter<BBDeviceConnectionStatus>
31
54
 
32
- // MTU
55
+ /**
56
+ * The negotiated ATT MTU in bytes. Stays at the platform default until {@link requestMTU}
57
+ * has been called and resolved.
58
+ */
33
59
  mtu: StateValueEmitter<number>
34
60
 
35
- // Operations
61
+ /**
62
+ * Connects the device.
63
+ * @throws if the connection attempt fails or times out.
64
+ */
36
65
  connect: () => Promise<void>
66
+
67
+ /**
68
+ * Disconnects the device.
69
+ * @throws if the disconnect attempt fails or times out.
70
+ */
37
71
  disconnect: () => Promise<void>
72
+
73
+ /**
74
+ * Discovers all of the device's services and their characteristics, populating
75
+ * {@link services}. Requires an active connection.
76
+ * @throws if discovery fails or times out.
77
+ */
38
78
  discoverServices: () => Promise<void>
79
+
80
+ /**
81
+ * Requests a larger MTU, updating {@link mtu} with whatever size the device actually
82
+ * negotiates. Requires an active connection.
83
+ *
84
+ * @param mtu The desired MTU size in bytes.
85
+ * @returns The negotiated MTU size.
86
+ * @throws if the request fails or times out.
87
+ */
39
88
  requestMTU: (mtu: number) => Promise<number>
40
89
  }
@@ -1,5 +1,8 @@
1
1
  import type { BBDevice } from "./bluebreeze_device"
2
2
 
3
+ /**
4
+ * A single BLE advertisement seen while scanning.
5
+ */
3
6
  export class BBScanResult {
4
7
  constructor(
5
8
  device: BBDevice,
@@ -21,12 +24,27 @@ export class BBScanResult {
21
24
  this.manufacturerData = manufacturerData
22
25
  }
23
26
 
27
+ /** The device that sent this advertisement. */
24
28
  device: BBDevice
29
+
30
+ /** The advertised local name, if any. Falls back to {@link device}'s name. */
25
31
  name?: string
32
+
33
+ /** Received signal strength, in dBm. */
26
34
  rssi: number
35
+
36
+ /** Whether this advertisement indicates the device currently accepts connections. */
27
37
  connectable: boolean
38
+
39
+ /** Service UUIDs included in this advertisement. */
28
40
  advertisedServices: string[]
41
+
42
+ /** The Bluetooth SIG-assigned company identifier for manufacturer-specific data, if present. */
29
43
  manufacturerId?: number
44
+
45
+ /** A human-readable name for {@link manufacturerId}, if recognized. */
30
46
  manufacturerName?: string
47
+
48
+ /** Raw manufacturer-specific advertisement data, if present. */
31
49
  manufacturerData?: number[]
32
50
  }
@@ -1,5 +1,8 @@
1
1
  import type { BBCharacteristic } from "./bluebreeze_characteristic"
2
2
 
3
+ /**
4
+ * A GATT service belonging to a {@link BBDevice}.
5
+ */
3
6
  export class BBService {
4
7
  constructor(
5
8
  id: string,
@@ -11,7 +14,12 @@ export class BBService {
11
14
  this.characteristics = characteristics
12
15
  }
13
16
 
17
+ /** The service's UUID. */
14
18
  id: string
19
+
20
+ /** A human-readable name for well-known (Bluetooth SIG assigned-number) services, if recognized. */
15
21
  name?: string
22
+
23
+ /** This service's characteristics. */
16
24
  characteristics: BBCharacteristic[]
17
25
  }
@@ -1 +1,16 @@
1
- export type BBState = 'unknown' | 'resetting' | 'unsupported' | 'unauthorized' | 'poweredOff' | 'poweredOn'
1
+ /**
2
+ * The power/availability state of the device's Bluetooth adapter.
3
+ */
4
+ export type BBState =
5
+ /** Not yet determined. */
6
+ | 'unknown'
7
+ /** Bluetooth is turned off. */
8
+ | 'poweredOff'
9
+ /** Bluetooth is turned on and available for scanning and connecting. */
10
+ | 'poweredOn'
11
+ /** iOS only: Bluetooth access is restricted at the OS level (e.g. by an MDM profile). */
12
+ | 'unauthorized'
13
+ /** iOS only: this device's hardware doesn't support Bluetooth Low Energy. */
14
+ | 'unsupported'
15
+ /** iOS only: the Bluetooth stack is resetting; treat like a temporary `unknown`. */
16
+ | 'resetting'
@@ -0,0 +1,196 @@
1
+ import { StateValueEmitter } from 'react-native-value-emitter'
2
+ import NativeBlueBreeze, { type SpecDeviceCharacteristic, type SpecDeviceService } from '../NativeBlueBreeze'
3
+ import { BBCharacteristic } from './bluebreeze_characteristic'
4
+ import type { BBDeviceConnectionStatus } from './bluebreeze_connection_status'
5
+ import { BBService } from './bluebreeze_service'
6
+
7
+ /**
8
+ * Per-device/per-characteristic wiring between the native module and {@link BBDevice}/
9
+ * {@link BBCharacteristic}. Not part of the package's public API: everything here is consumed
10
+ * directly by those two classes' constructors and is intentionally not re-exported from the
11
+ * package entry point (`src/index.tsx`) -- callers use `BBDevice`/`BBCharacteristic` methods
12
+ * instead of these raw, ID-keyed functions.
13
+ */
14
+
15
+ function mapGetOrSet<T>(map: Map<string, T>, key: string, defaultValue: () => T): T {
16
+ if (map.get(key) == undefined) {
17
+ map.set(key, defaultValue())
18
+ }
19
+
20
+ return map.get(key)!
21
+ }
22
+
23
+ // Device services
24
+
25
+ const _deviceServices = new Map<string, StateValueEmitter<BBService[]>>()
26
+
27
+ export const deviceServices = (id: string): StateValueEmitter<BBService[]> => {
28
+ return mapGetOrSet(
29
+ _deviceServices,
30
+ id,
31
+ () => new StateValueEmitter<BBService[]>(
32
+ NativeBlueBreeze.deviceServices(id).map(
33
+ (s) => convertDeviceService(id, s)
34
+ )
35
+ )
36
+ )
37
+ }
38
+
39
+ NativeBlueBreeze.deviceServicesEmitter((value) => {
40
+ deviceServices(value.id).add(
41
+ value.value.map((s) => convertDeviceService(value.id, s))
42
+ )
43
+ })
44
+
45
+ // Device connection status
46
+
47
+ const _deviceConnectionStatus = new Map<string, StateValueEmitter<BBDeviceConnectionStatus>>()
48
+
49
+ export const deviceConnectionStatus = (id: string): StateValueEmitter<BBDeviceConnectionStatus> => {
50
+ return mapGetOrSet(
51
+ _deviceConnectionStatus,
52
+ id,
53
+ () => new StateValueEmitter<BBDeviceConnectionStatus>(
54
+ NativeBlueBreeze.deviceConnectionStatus(id) as BBDeviceConnectionStatus
55
+ )
56
+ )
57
+ }
58
+
59
+ NativeBlueBreeze.deviceConnectionStatusEmitter((value) => {
60
+ deviceConnectionStatus(value.id).add(value.value as BBDeviceConnectionStatus)
61
+ })
62
+
63
+ // Device MTU
64
+
65
+ const _deviceMTU = new Map<string, StateValueEmitter<number>>()
66
+
67
+ export const deviceMTU = (id: string): StateValueEmitter<number> => {
68
+ return mapGetOrSet(
69
+ _deviceMTU,
70
+ id,
71
+ () => new StateValueEmitter<number>(
72
+ NativeBlueBreeze.deviceMTU(id)
73
+ )
74
+ )
75
+ }
76
+
77
+ NativeBlueBreeze.deviceMTUEmitter((value) => {
78
+ deviceMTU(value.id).add(value.value)
79
+ })
80
+
81
+ // Device operations
82
+
83
+ export const deviceConnect = NativeBlueBreeze.deviceConnect
84
+
85
+ export const deviceDisconnect = NativeBlueBreeze.deviceDisconnect
86
+
87
+ export const deviceDiscoverServices = NativeBlueBreeze.deviceDiscoverServices
88
+
89
+ export const deviceRequestMTU = NativeBlueBreeze.deviceRequestMTU
90
+
91
+ // Device characteristic notify enabled
92
+
93
+ const _deviceCharacteristicNotifyEnabled = new Map<string, Map<string, Map<string, StateValueEmitter<boolean>>>>()
94
+
95
+ export const deviceCharacteristicNotifyEnabled = (
96
+ id: string,
97
+ serviceId: string,
98
+ characteristicId: string
99
+ ): StateValueEmitter<boolean> => {
100
+ const deviceMap = mapGetOrSet(
101
+ _deviceCharacteristicNotifyEnabled,
102
+ id,
103
+ () => new Map<string, Map<string, StateValueEmitter<boolean>>>()
104
+ )
105
+
106
+ const serviceMap = mapGetOrSet(
107
+ deviceMap,
108
+ serviceId,
109
+ () => new Map<string, StateValueEmitter<boolean>>()
110
+ )
111
+
112
+ return mapGetOrSet(
113
+ serviceMap,
114
+ characteristicId,
115
+ () => new StateValueEmitter<boolean>(
116
+ NativeBlueBreeze.deviceCharacteristicNotifyEnabled(id, serviceId, characteristicId)
117
+ )
118
+ )
119
+ }
120
+
121
+ NativeBlueBreeze.deviceCharacteristicNotifyEnabledEmitter((value) => {
122
+ deviceCharacteristicNotifyEnabled(value.id, value.serviceId, value.characteristicId).add(value.value)
123
+ })
124
+
125
+ // Device characteristic data
126
+
127
+ const _deviceCharacteristicData = new Map<string, Map<string, Map<string, StateValueEmitter<number[]>>>>()
128
+
129
+ export const deviceCharacteristicData = (
130
+ id: string,
131
+ serviceId: string,
132
+ characteristicId: string
133
+ ): StateValueEmitter<number[]> => {
134
+ const deviceMap = mapGetOrSet(
135
+ _deviceCharacteristicData,
136
+ id,
137
+ () => new Map<string, Map<string, StateValueEmitter<number[]>>>()
138
+ )
139
+
140
+ const serviceMap = mapGetOrSet(
141
+ deviceMap,
142
+ serviceId,
143
+ () => new Map<string, StateValueEmitter<number[]>>()
144
+ )
145
+
146
+ return mapGetOrSet(
147
+ serviceMap,
148
+ characteristicId,
149
+ () => new StateValueEmitter<number[]>(
150
+ NativeBlueBreeze.deviceCharacteristicData(id, serviceId, characteristicId)
151
+ )
152
+ )
153
+ }
154
+
155
+ NativeBlueBreeze.deviceCharacteristicDataEmitter((value) => {
156
+ deviceCharacteristicData(value.id, value.serviceId, value.characteristicId).add(value.value)
157
+ })
158
+
159
+ // Device characteristic operations
160
+
161
+ export const deviceCharacteristicRead = NativeBlueBreeze.deviceCharacteristicRead
162
+
163
+ export const deviceCharacteristicWrite = NativeBlueBreeze.deviceCharacteristicWrite
164
+
165
+ export const deviceCharacteristicSubscribe = NativeBlueBreeze.deviceCharacteristicSubscribe
166
+
167
+ export const deviceCharacteristicUnsubscribe = NativeBlueBreeze.deviceCharacteristicUnsubscribe
168
+
169
+ // Object decoders
170
+
171
+ const convertDeviceService = (
172
+ deviceId: string,
173
+ service: SpecDeviceService
174
+ ): BBService => {
175
+ return new BBService(
176
+ service.id,
177
+ service.name,
178
+ service.characteristics.map(
179
+ (c) => convertDeviceCharacteristic(deviceId, service.id, c)
180
+ )
181
+ )
182
+ }
183
+
184
+ const convertDeviceCharacteristic = (
185
+ deviceId: string,
186
+ serviceId: string,
187
+ characteristic: SpecDeviceCharacteristic
188
+ ): BBCharacteristic => {
189
+ return new BBCharacteristic(
190
+ deviceId,
191
+ serviceId,
192
+ characteristic.id,
193
+ characteristic.name,
194
+ characteristic.properties,
195
+ )
196
+ }