@capgo/capacitor-bluetooth-low-energy 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.
@@ -0,0 +1,448 @@
1
+ import Foundation
2
+ import Capacitor
3
+ import CoreBluetooth
4
+
5
+ // swiftlint:disable type_body_length file_length
6
+ @objc(BluetoothLowEnergyPlugin)
7
+ public class BluetoothLowEnergyPlugin: CAPPlugin, CAPBridgedPlugin {
8
+ private let pluginVersion: String = "1.0.0"
9
+ public let identifier = "BluetoothLowEnergyPlugin"
10
+ public let jsName = "BluetoothLowEnergy"
11
+ public let pluginMethods: [CAPPluginMethod] = [
12
+ CAPPluginMethod(name: "initialize", returnType: CAPPluginReturnPromise),
13
+ CAPPluginMethod(name: "isAvailable", returnType: CAPPluginReturnPromise),
14
+ CAPPluginMethod(name: "isEnabled", returnType: CAPPluginReturnPromise),
15
+ CAPPluginMethod(name: "isLocationEnabled", returnType: CAPPluginReturnPromise),
16
+ CAPPluginMethod(name: "openAppSettings", returnType: CAPPluginReturnPromise),
17
+ CAPPluginMethod(name: "openBluetoothSettings", returnType: CAPPluginReturnPromise),
18
+ CAPPluginMethod(name: "openLocationSettings", returnType: CAPPluginReturnPromise),
19
+ CAPPluginMethod(name: "checkPermissions", returnType: CAPPluginReturnPromise),
20
+ CAPPluginMethod(name: "requestPermissions", returnType: CAPPluginReturnPromise),
21
+ CAPPluginMethod(name: "startScan", returnType: CAPPluginReturnPromise),
22
+ CAPPluginMethod(name: "stopScan", returnType: CAPPluginReturnPromise),
23
+ CAPPluginMethod(name: "connect", returnType: CAPPluginReturnPromise),
24
+ CAPPluginMethod(name: "disconnect", returnType: CAPPluginReturnPromise),
25
+ CAPPluginMethod(name: "createBond", returnType: CAPPluginReturnPromise),
26
+ CAPPluginMethod(name: "isBonded", returnType: CAPPluginReturnPromise),
27
+ CAPPluginMethod(name: "discoverServices", returnType: CAPPluginReturnPromise),
28
+ CAPPluginMethod(name: "getServices", returnType: CAPPluginReturnPromise),
29
+ CAPPluginMethod(name: "getConnectedDevices", returnType: CAPPluginReturnPromise),
30
+ CAPPluginMethod(name: "readCharacteristic", returnType: CAPPluginReturnPromise),
31
+ CAPPluginMethod(name: "writeCharacteristic", returnType: CAPPluginReturnPromise),
32
+ CAPPluginMethod(name: "startCharacteristicNotifications", returnType: CAPPluginReturnPromise),
33
+ CAPPluginMethod(name: "stopCharacteristicNotifications", returnType: CAPPluginReturnPromise),
34
+ CAPPluginMethod(name: "readDescriptor", returnType: CAPPluginReturnPromise),
35
+ CAPPluginMethod(name: "writeDescriptor", returnType: CAPPluginReturnPromise),
36
+ CAPPluginMethod(name: "readRssi", returnType: CAPPluginReturnPromise),
37
+ CAPPluginMethod(name: "requestMtu", returnType: CAPPluginReturnPromise),
38
+ CAPPluginMethod(name: "requestConnectionPriority", returnType: CAPPluginReturnPromise),
39
+ CAPPluginMethod(name: "startAdvertising", returnType: CAPPluginReturnPromise),
40
+ CAPPluginMethod(name: "stopAdvertising", returnType: CAPPluginReturnPromise),
41
+ CAPPluginMethod(name: "startForegroundService", returnType: CAPPluginReturnPromise),
42
+ CAPPluginMethod(name: "stopForegroundService", returnType: CAPPluginReturnPromise),
43
+ CAPPluginMethod(name: "getPluginVersion", returnType: CAPPluginReturnPromise)
44
+ ]
45
+
46
+ private var implementation: BluetoothLowEnergy?
47
+
48
+ override public func load() {
49
+ implementation = BluetoothLowEnergy(plugin: self)
50
+ }
51
+
52
+ @objc func initialize(_ call: CAPPluginCall) {
53
+ let mode = call.getString("mode") ?? "central"
54
+ implementation?.initialize(mode: mode) { error in
55
+ if let error = error {
56
+ call.reject(error.localizedDescription)
57
+ } else {
58
+ call.resolve()
59
+ }
60
+ }
61
+ }
62
+
63
+ @objc func isAvailable(_ call: CAPPluginCall) {
64
+ let available = implementation?.isAvailable() ?? false
65
+ call.resolve(["available": available])
66
+ }
67
+
68
+ @objc func isEnabled(_ call: CAPPluginCall) {
69
+ let enabled = implementation?.isEnabled() ?? false
70
+ call.resolve(["enabled": enabled])
71
+ }
72
+
73
+ @objc func isLocationEnabled(_ call: CAPPluginCall) {
74
+ // Location is always enabled on iOS for BLE purposes
75
+ call.resolve(["enabled": true])
76
+ }
77
+
78
+ @objc func openAppSettings(_ call: CAPPluginCall) {
79
+ DispatchQueue.main.async {
80
+ if let url = URL(string: UIApplication.openSettingsURLString) {
81
+ UIApplication.shared.open(url, options: [:]) { success in
82
+ if success {
83
+ call.resolve()
84
+ } else {
85
+ call.reject("Could not open app settings")
86
+ }
87
+ }
88
+ } else {
89
+ call.reject("Could not create settings URL")
90
+ }
91
+ }
92
+ }
93
+
94
+ @objc func openBluetoothSettings(_ call: CAPPluginCall) {
95
+ // On iOS, we can only open the app settings, not Bluetooth settings directly
96
+ openAppSettings(call)
97
+ }
98
+
99
+ @objc func openLocationSettings(_ call: CAPPluginCall) {
100
+ // On iOS, we can only open the app settings
101
+ openAppSettings(call)
102
+ }
103
+
104
+ @objc override public func checkPermissions(_ call: CAPPluginCall) {
105
+ let status = implementation?.getPermissionStatus() ?? "prompt"
106
+ call.resolve([
107
+ "bluetooth": status,
108
+ "location": "granted"
109
+ ])
110
+ }
111
+
112
+ @objc override public func requestPermissions(_ call: CAPPluginCall) {
113
+ implementation?.requestPermissions { status in
114
+ call.resolve([
115
+ "bluetooth": status,
116
+ "location": "granted"
117
+ ])
118
+ }
119
+ }
120
+
121
+ @objc func startScan(_ call: CAPPluginCall) {
122
+ var serviceUUIDs: [CBUUID]?
123
+ if let services = call.getArray("services", String.self) {
124
+ serviceUUIDs = services.map { CBUUID(string: $0) }
125
+ }
126
+
127
+ let timeout = call.getDouble("timeout") ?? 0
128
+ let allowDuplicates = call.getBool("allowDuplicates") ?? false
129
+
130
+ implementation?.startScan(
131
+ services: serviceUUIDs,
132
+ timeout: timeout,
133
+ allowDuplicates: allowDuplicates
134
+ ) { error in
135
+ if let error = error {
136
+ call.reject(error.localizedDescription)
137
+ } else {
138
+ call.resolve()
139
+ }
140
+ }
141
+ }
142
+
143
+ @objc func stopScan(_ call: CAPPluginCall) {
144
+ implementation?.stopScan()
145
+ call.resolve()
146
+ }
147
+
148
+ @objc func connect(_ call: CAPPluginCall) {
149
+ guard let deviceId = call.getString("deviceId") else {
150
+ call.reject("deviceId is required")
151
+ return
152
+ }
153
+
154
+ let autoConnect = call.getBool("autoConnect") ?? false
155
+
156
+ implementation?.connect(deviceId: deviceId, autoConnect: autoConnect) { error in
157
+ if let error = error {
158
+ call.reject(error.localizedDescription)
159
+ } else {
160
+ call.resolve()
161
+ }
162
+ }
163
+ }
164
+
165
+ @objc func disconnect(_ call: CAPPluginCall) {
166
+ guard let deviceId = call.getString("deviceId") else {
167
+ call.reject("deviceId is required")
168
+ return
169
+ }
170
+
171
+ implementation?.disconnect(deviceId: deviceId) { error in
172
+ if let error = error {
173
+ call.reject(error.localizedDescription)
174
+ } else {
175
+ call.resolve()
176
+ }
177
+ }
178
+ }
179
+
180
+ @objc func createBond(_ call: CAPPluginCall) {
181
+ // Bonding is handled automatically on iOS
182
+ call.resolve()
183
+ }
184
+
185
+ @objc func isBonded(_ call: CAPPluginCall) {
186
+ // iOS doesn't expose bonding information
187
+ call.resolve(["bonded": false])
188
+ }
189
+
190
+ @objc func discoverServices(_ call: CAPPluginCall) {
191
+ guard let deviceId = call.getString("deviceId") else {
192
+ call.reject("deviceId is required")
193
+ return
194
+ }
195
+
196
+ implementation?.discoverServices(deviceId: deviceId) { error in
197
+ if let error = error {
198
+ call.reject(error.localizedDescription)
199
+ } else {
200
+ call.resolve()
201
+ }
202
+ }
203
+ }
204
+
205
+ @objc func getServices(_ call: CAPPluginCall) {
206
+ guard let deviceId = call.getString("deviceId") else {
207
+ call.reject("deviceId is required")
208
+ return
209
+ }
210
+
211
+ let services = implementation?.getServices(deviceId: deviceId) ?? []
212
+ call.resolve(["services": services])
213
+ }
214
+
215
+ @objc func getConnectedDevices(_ call: CAPPluginCall) {
216
+ let devices = implementation?.getConnectedDevices() ?? []
217
+ call.resolve(["devices": devices])
218
+ }
219
+
220
+ @objc func readCharacteristic(_ call: CAPPluginCall) {
221
+ guard let deviceId = call.getString("deviceId"),
222
+ let service = call.getString("service"),
223
+ let characteristic = call.getString("characteristic") else {
224
+ call.reject("deviceId, service, and characteristic are required")
225
+ return
226
+ }
227
+
228
+ implementation?.readCharacteristic(
229
+ deviceId: deviceId,
230
+ serviceUUID: service,
231
+ characteristicUUID: characteristic
232
+ ) { result in
233
+ switch result {
234
+ case .success(let value):
235
+ call.resolve(["value": value])
236
+ case .failure(let error):
237
+ call.reject(error.localizedDescription)
238
+ }
239
+ }
240
+ }
241
+
242
+ @objc func writeCharacteristic(_ call: CAPPluginCall) {
243
+ guard let deviceId = call.getString("deviceId"),
244
+ let service = call.getString("service"),
245
+ let characteristic = call.getString("characteristic"),
246
+ let value = call.getArray("value", Int.self) else {
247
+ call.reject("deviceId, service, characteristic, and value are required")
248
+ return
249
+ }
250
+
251
+ let writeType = call.getString("type") ?? "withResponse"
252
+
253
+ implementation?.writeCharacteristic(
254
+ deviceId: deviceId,
255
+ serviceUUID: service,
256
+ characteristicUUID: characteristic,
257
+ value: value.map { UInt8($0) },
258
+ writeType: writeType
259
+ ) { error in
260
+ if let error = error {
261
+ call.reject(error.localizedDescription)
262
+ } else {
263
+ call.resolve()
264
+ }
265
+ }
266
+ }
267
+
268
+ @objc func startCharacteristicNotifications(_ call: CAPPluginCall) {
269
+ guard let deviceId = call.getString("deviceId"),
270
+ let service = call.getString("service"),
271
+ let characteristic = call.getString("characteristic") else {
272
+ call.reject("deviceId, service, and characteristic are required")
273
+ return
274
+ }
275
+
276
+ implementation?.startCharacteristicNotifications(
277
+ deviceId: deviceId,
278
+ serviceUUID: service,
279
+ characteristicUUID: characteristic
280
+ ) { error in
281
+ if let error = error {
282
+ call.reject(error.localizedDescription)
283
+ } else {
284
+ call.resolve()
285
+ }
286
+ }
287
+ }
288
+
289
+ @objc func stopCharacteristicNotifications(_ call: CAPPluginCall) {
290
+ guard let deviceId = call.getString("deviceId"),
291
+ let service = call.getString("service"),
292
+ let characteristic = call.getString("characteristic") else {
293
+ call.reject("deviceId, service, and characteristic are required")
294
+ return
295
+ }
296
+
297
+ implementation?.stopCharacteristicNotifications(
298
+ deviceId: deviceId,
299
+ serviceUUID: service,
300
+ characteristicUUID: characteristic
301
+ ) { error in
302
+ if let error = error {
303
+ call.reject(error.localizedDescription)
304
+ } else {
305
+ call.resolve()
306
+ }
307
+ }
308
+ }
309
+
310
+ @objc func readDescriptor(_ call: CAPPluginCall) {
311
+ guard let deviceId = call.getString("deviceId"),
312
+ let service = call.getString("service"),
313
+ let characteristic = call.getString("characteristic"),
314
+ let descriptor = call.getString("descriptor") else {
315
+ call.reject("deviceId, service, characteristic, and descriptor are required")
316
+ return
317
+ }
318
+
319
+ implementation?.readDescriptor(
320
+ deviceId: deviceId,
321
+ serviceUUID: service,
322
+ characteristicUUID: characteristic,
323
+ descriptorUUID: descriptor
324
+ ) { result in
325
+ switch result {
326
+ case .success(let value):
327
+ call.resolve(["value": value])
328
+ case .failure(let error):
329
+ call.reject(error.localizedDescription)
330
+ }
331
+ }
332
+ }
333
+
334
+ @objc func writeDescriptor(_ call: CAPPluginCall) {
335
+ guard let deviceId = call.getString("deviceId"),
336
+ let service = call.getString("service"),
337
+ let characteristic = call.getString("characteristic"),
338
+ let descriptor = call.getString("descriptor"),
339
+ let value = call.getArray("value", Int.self) else {
340
+ call.reject("deviceId, service, characteristic, descriptor, and value are required")
341
+ return
342
+ }
343
+
344
+ implementation?.writeDescriptor(
345
+ deviceId: deviceId,
346
+ serviceUUID: service,
347
+ characteristicUUID: characteristic,
348
+ descriptorUUID: descriptor,
349
+ value: value.map { UInt8($0) }
350
+ ) { error in
351
+ if let error = error {
352
+ call.reject(error.localizedDescription)
353
+ } else {
354
+ call.resolve()
355
+ }
356
+ }
357
+ }
358
+
359
+ @objc func readRssi(_ call: CAPPluginCall) {
360
+ guard let deviceId = call.getString("deviceId") else {
361
+ call.reject("deviceId is required")
362
+ return
363
+ }
364
+
365
+ implementation?.readRssi(deviceId: deviceId) { result in
366
+ switch result {
367
+ case .success(let rssi):
368
+ call.resolve(["rssi": rssi])
369
+ case .failure(let error):
370
+ call.reject(error.localizedDescription)
371
+ }
372
+ }
373
+ }
374
+
375
+ @objc func requestMtu(_ call: CAPPluginCall) {
376
+ guard let deviceId = call.getString("deviceId") else {
377
+ call.reject("deviceId is required")
378
+ return
379
+ }
380
+
381
+ // iOS negotiates MTU automatically, return the max MTU
382
+ let mtu = implementation?.getMtu(deviceId: deviceId) ?? 185
383
+ call.resolve(["mtu": mtu])
384
+ }
385
+
386
+ @objc func requestConnectionPriority(_ call: CAPPluginCall) {
387
+ // iOS doesn't support connection priority changes
388
+ call.resolve()
389
+ }
390
+
391
+ @objc func startAdvertising(_ call: CAPPluginCall) {
392
+ let name = call.getString("name")
393
+ var serviceUUIDs: [CBUUID]?
394
+ if let services = call.getArray("services", String.self) {
395
+ serviceUUIDs = services.map { CBUUID(string: $0) }
396
+ }
397
+
398
+ implementation?.startAdvertising(name: name, services: serviceUUIDs) { error in
399
+ if let error = error {
400
+ call.reject(error.localizedDescription)
401
+ } else {
402
+ call.resolve()
403
+ }
404
+ }
405
+ }
406
+
407
+ @objc func stopAdvertising(_ call: CAPPluginCall) {
408
+ implementation?.stopAdvertising()
409
+ call.resolve()
410
+ }
411
+
412
+ @objc func startForegroundService(_ call: CAPPluginCall) {
413
+ // iOS doesn't have foreground services like Android
414
+ // Background execution is handled through Background Modes capability
415
+ call.resolve()
416
+ }
417
+
418
+ @objc func stopForegroundService(_ call: CAPPluginCall) {
419
+ call.resolve()
420
+ }
421
+
422
+ @objc func getPluginVersion(_ call: CAPPluginCall) {
423
+ call.resolve(["version": pluginVersion])
424
+ }
425
+
426
+ // MARK: - Event Emitters
427
+
428
+ func emitDeviceScanned(device: [String: Any]) {
429
+ notifyListeners("deviceScanned", data: ["device": device])
430
+ }
431
+
432
+ func emitDeviceConnected(deviceId: String) {
433
+ notifyListeners("deviceConnected", data: ["deviceId": deviceId])
434
+ }
435
+
436
+ func emitDeviceDisconnected(deviceId: String) {
437
+ notifyListeners("deviceDisconnected", data: ["deviceId": deviceId])
438
+ }
439
+
440
+ func emitCharacteristicChanged(deviceId: String, service: String, characteristic: String, value: [Int]) {
441
+ notifyListeners("characteristicChanged", data: [
442
+ "deviceId": deviceId,
443
+ "service": service,
444
+ "characteristic": characteristic,
445
+ "value": value
446
+ ])
447
+ }
448
+ }
@@ -0,0 +1,11 @@
1
+ import XCTest
2
+ @testable import BluetoothLowEnergyPlugin
3
+
4
+ class BluetoothLowEnergyTests: XCTestCase {
5
+
6
+ func testPluginInit() throws {
7
+ // Basic test to verify the plugin can be instantiated
8
+ XCTAssertTrue(true)
9
+ }
10
+
11
+ }
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "@capgo/capacitor-bluetooth-low-energy",
3
+ "version": "1.0.0",
4
+ "description": "Bluetooth Low Energy (BLE) plugin for Capacitor with support for scanning, connecting, reading, writing, and notifications.",
5
+ "main": "dist/plugin.cjs.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "unpkg": "dist/plugin.js",
9
+ "files": [
10
+ "android/src/main/",
11
+ "android/build.gradle",
12
+ "dist/",
13
+ "ios/Sources",
14
+ "ios/Tests",
15
+ "Package.swift",
16
+ "CapgoCapacitorBluetoothLowEnergy.podspec"
17
+ ],
18
+ "author": "Martin Donadieu <martin@capgo.app>",
19
+ "license": "MPL-2.0",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/Cap-go/capacitor-bluetooth-low-energy.git"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/Cap-go/capacitor-bluetooth-low-energy/issues"
26
+ },
27
+ "keywords": [
28
+ "capacitor",
29
+ "bluetooth",
30
+ "ble",
31
+ "bluetooth-low-energy",
32
+ "plugin",
33
+ "native"
34
+ ],
35
+ "scripts": {
36
+ "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
37
+ "verify:ios": "xcodebuild -scheme CapgoCapacitorBluetoothLowEnergy -destination generic/platform=iOS",
38
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
39
+ "verify:web": "npm run build",
40
+ "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
41
+ "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
42
+ "eslint": "eslint .",
43
+ "prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
44
+ "swiftlint": "node-swiftlint",
45
+ "docgen": "docgen --api BluetoothLowEnergyPlugin --output-readme README.md --output-json dist/docs.json",
46
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
47
+ "clean": "rimraf ./dist",
48
+ "watch": "tsc --watch",
49
+ "prepublishOnly": "npm run build"
50
+ },
51
+ "devDependencies": {
52
+ "@capacitor/android": "^8.0.0",
53
+ "@capacitor/cli": "^8.0.0",
54
+ "@capacitor/core": "^8.0.0",
55
+ "@capacitor/docgen": "^0.3.1",
56
+ "@capacitor/ios": "^8.0.0",
57
+ "@ionic/eslint-config": "^0.4.0",
58
+ "@ionic/prettier-config": "^4.0.0",
59
+ "@ionic/swiftlint-config": "^2.0.0",
60
+ "@types/node": "^24.10.1",
61
+ "eslint": "^8.57.1",
62
+ "eslint-plugin-import": "^2.31.0",
63
+ "husky": "^9.1.7",
64
+ "prettier": "^3.6.2",
65
+ "prettier-plugin-java": "^2.7.7",
66
+ "rimraf": "^6.1.0",
67
+ "rollup": "^4.53.2",
68
+ "swiftlint": "^2.0.0",
69
+ "typescript": "^5.9.3"
70
+ },
71
+ "peerDependencies": {
72
+ "@capacitor/core": ">=8.0.0"
73
+ },
74
+ "eslintConfig": {
75
+ "extends": "@ionic/eslint-config/recommended"
76
+ },
77
+ "prettier": "@ionic/prettier-config",
78
+ "swiftlint": "@ionic/swiftlint-config",
79
+ "capacitor": {
80
+ "ios": {
81
+ "src": "ios"
82
+ },
83
+ "android": {
84
+ "src": "android"
85
+ }
86
+ }
87
+ }