@iotize/device-com-ble.cordova 3.0.0 → 3.3.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.
- package/README.md +174 -177
- package/bundles/iotize-device-com-ble.cordova.umd.js +1113 -129
- package/bundles/iotize-device-com-ble.cordova.umd.js.map +1 -1
- package/bundles/iotize-device-com-ble.cordova.umd.min.js +15 -1
- package/bundles/iotize-device-com-ble.cordova.umd.min.js.map +1 -1
- package/esm2015/iotize-device-com-ble.cordova.js +5 -1
- package/esm2015/iotize-device-com-ble.cordova.js.map +1 -0
- package/esm2015/iotize-device-com-ble.cordova.metadata.json +1 -0
- package/esm2015/iotize-device-com-ble.cordova.ngsummary.json +1 -1
- package/esm2015/lib/ble-com-protocol.js +68 -0
- package/esm2015/lib/ble-com-protocol.js.map +1 -0
- package/esm2015/lib/ble-com-protocol.metadata.json +1 -0
- package/esm2015/lib/ble-com-protocol.ngsummary.json +1 -0
- package/esm2015/lib/cordova-ble-error.js +36 -0
- package/esm2015/lib/cordova-ble-error.js.map +1 -0
- package/esm2015/lib/cordova-ble-error.metadata.json +1 -0
- package/esm2015/lib/cordova-ble-error.ngsummary.json +1 -0
- package/esm2015/lib/cordova-interface.js +2 -0
- package/esm2015/lib/cordova-interface.js.map +1 -0
- package/esm2015/lib/cordova-interface.metadata.json +1 -0
- package/esm2015/lib/cordova-interface.ngsummary.json +1 -0
- package/esm2015/lib/cordova-service-adapter.js +214 -0
- package/esm2015/lib/cordova-service-adapter.js.map +1 -0
- package/esm2015/lib/cordova-service-adapter.metadata.json +1 -0
- package/esm2015/lib/cordova-service-adapter.ngsummary.json +1 -0
- package/esm2015/lib/definitions.js +2 -0
- package/esm2015/lib/definitions.js.map +1 -0
- package/esm2015/lib/definitions.metadata.json +1 -0
- package/esm2015/lib/definitions.ngsummary.json +1 -0
- package/esm2015/lib/iotize-ble-cordova-plugin.js +170 -0
- package/esm2015/lib/iotize-ble-cordova-plugin.js.map +1 -0
- package/esm2015/lib/iotize-ble-cordova-plugin.metadata.json +1 -0
- package/esm2015/lib/iotize-ble-cordova-plugin.ngsummary.json +1 -0
- package/esm2015/lib/logger.js +3 -0
- package/esm2015/lib/logger.js.map +1 -0
- package/esm2015/lib/logger.metadata.json +1 -0
- package/esm2015/{www → lib}/logger.ngsummary.json +1 -1
- package/esm2015/lib/scanner.js +151 -0
- package/esm2015/lib/scanner.js.map +1 -0
- package/esm2015/lib/scanner.metadata.json +1 -0
- package/esm2015/lib/scanner.ngsummary.json +1 -0
- package/esm2015/lib/utility.js +8 -0
- package/esm2015/lib/utility.js.map +1 -0
- package/esm2015/lib/utility.metadata.json +1 -0
- package/esm2015/lib/utility.ngsummary.json +1 -0
- package/esm2015/public_api.js +5 -2
- package/esm2015/public_api.js.map +1 -0
- package/esm2015/public_api.metadata.json +1 -0
- package/esm2015/public_api.ngsummary.json +1 -1
- package/fesm2015/iotize-device-com-ble.cordova.js +543 -75
- package/fesm2015/iotize-device-com-ble.cordova.js.map +1 -1
- package/iotize-device-com-ble.cordova.d.ts +5 -0
- package/iotize-device-com-ble.cordova.metadata.json +1 -1
- package/lib/ble-com-protocol.d.ts +20 -0
- package/lib/cordova-ble-error.d.ts +29 -0
- package/lib/cordova-interface.d.ts +5 -0
- package/lib/cordova-service-adapter.d.ts +58 -0
- package/lib/definitions.d.ts +39 -0
- package/lib/iotize-ble-cordova-plugin.d.ts +27 -0
- package/lib/logger.d.ts +1 -0
- package/lib/scanner.d.ts +55 -0
- package/lib/utility.d.ts +2 -0
- package/package.json +10 -11
- package/plugin.xml +9 -11
- package/public_api.d.ts +5 -1
- package/src/android/.gradle/4.8.1/fileChanges/last-build.bin +0 -0
- package/src/android/.gradle/4.8.1/fileHashes/fileHashes.bin +0 -0
- package/src/android/.gradle/4.8.1/fileHashes/fileHashes.lock +0 -0
- package/src/android/.gradle/6.8.2/fileChanges/last-build.bin +0 -0
- package/src/android/.gradle/6.8.2/fileHashes/fileHashes.lock +0 -0
- package/src/android/.gradle/6.8.2/gc.properties +0 -0
- package/src/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/src/android/.gradle/buildOutputCleanup/cache.properties +2 -0
- package/src/android/.gradle/checksums/checksums.lock +0 -0
- package/src/android/.gradle/configuration-cache/gc.properties +0 -0
- package/src/android/.gradle/vcs-1/gc.properties +0 -0
- package/src/android/.gradle/vcsWorkingDirs/gc.properties +0 -0
- package/src/android/.idea/android.iml +9 -0
- package/src/android/.idea/gradle.xml +16 -0
- package/src/android/.idea/modules.xml +8 -0
- package/src/android/.idea/vcs.xml +6 -0
- package/src/android/build.gradle +5 -4
- package/src/android/gradle/wrapper/gradle-wrapper.properties +1 -2
- package/src/android/src/ble/ArgsHelper.java +73 -0
- package/src/android/src/ble/BLECom.java +559 -0
- package/src/android/src/{BLEComError.java → ble/BLEComError.java} +22 -10
- package/src/android/src/{JSONBuilder.java → ble/JSONBuilder.java} +20 -5
- package/src/android/src/{PluginResponse.java → ble/PluginResponse.java} +51 -2
- package/src/android/src/ble/UUIDHelper.java +33 -0
- package/src/android/src/ble/models/CharacteristicDescription.java +39 -0
- package/src/android/src/ble/models/CharacteristicProperties.java +41 -0
- package/src/android/src/ble/models/DescriptorDescription.java +21 -0
- package/src/android/src/ble/models/RequestDeviceOptions.java +45 -0
- package/src/android/src/ble/models/ServiceDescription.java +51 -0
- package/src/ios/BLECom.swift +562 -124
- package/src/ios/BLEGenericPeripheral.swift +479 -0
- package/src/ios/BLEManager.swift +142 -47
- package/src/ios/{BLEPeripheral.swift → BLETapPeripheral.swift} +22 -85
- package/src/ios/CBPeripheralConverter.swift +86 -1
- package/src/ios/Queue.swift +71 -0
- package/src/windows/iotize-ble-com.js +159 -179
- package/www/plugin.js +2 -0
- package/www/plugin.js.LICENSE.txt +14 -0
- package/CHANGELOG.md +0 -42
- package/esm2015/www/ble-com-protocol.js +0 -75
- package/esm2015/www/ble-com-protocol.ngsummary.json +0 -1
- package/esm2015/www/cordova-interface.js +0 -1
- package/esm2015/www/cordova-interface.ngsummary.json +0 -1
- package/esm2015/www/definitions.js +0 -1
- package/esm2015/www/definitions.ngsummary.json +0 -1
- package/esm2015/www/index.js +0 -3
- package/esm2015/www/index.ngsummary.json +0 -1
- package/esm2015/www/logger.js +0 -3
- package/esm2015/www/scanner.js +0 -89
- package/esm2015/www/scanner.ngsummary.json +0 -1
- package/esm5/iotize-device-com-ble.cordova.js +0 -2
- package/esm5/public_api.js +0 -2
- package/esm5/www/ble-com-protocol.js +0 -121
- package/esm5/www/cordova-interface.js +0 -1
- package/esm5/www/definitions.js +0 -1
- package/esm5/www/index.js +0 -3
- package/esm5/www/logger.js +0 -3
- package/esm5/www/scanner.js +0 -111
- package/fesm5/iotize-device-com-ble.cordova.js +0 -233
- package/fesm5/iotize-device-com-ble.cordova.js.map +0 -1
- package/src/android/.project +0 -17
- package/src/android/.settings/org.eclipse.buildship.core.prefs +0 -13
- package/src/android/src/ArgsHelper.java +0 -30
- package/src/android/src/BLECom.java +0 -375
- package/src/public_api.ts +0 -1
- package/src/www/ble-com-protocol.spec.ts +0 -9
- package/src/www/ble-com-protocol.ts +0 -114
- package/src/www/cordova-interface.ts +0 -29
- package/src/www/definitions.ts +0 -11
- package/src/www/index.ts +0 -4
- package/src/www/logger.ts +0 -3
- package/src/www/scanner.spec.ts +0 -98
- package/src/www/scanner.ts +0 -132
- package/www/ble-com-protocol.d.ts +0 -16
- package/www/cordova-interface.d.ts +0 -11
- package/www/definitions.d.ts +0 -6
- package/www/index.d.ts +0 -4
- package/www/iotize-ble.js +0 -55
- package/www/logger.d.ts +0 -2
- package/www/scanner.d.ts +0 -18
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"iotize-device-com-ble.cordova.js","sources":["ng://@iotize/device-com-ble.cordova/www/logger.ts","ng://@iotize/device-com-ble.cordova/www/ble-com-protocol.ts","ng://@iotize/device-com-ble.cordova/www/scanner.ts"],"sourcesContent":["import { createDebugger } from '@iotize/common/debug';\r\n\r\nexport const debug = createDebugger(`@iotize/device-com-ble.cordova`);","//\r\n// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license. \r\n//\r\n// ble-com-protocol.ts\r\n// device-com-ble.cordova BLE Cordova Plugin\r\n//\r\nimport { bufferToHexString, hexStringToBuffer } from '@iotize/common/byte-converter';\r\nimport { safeEnumValue } from '@iotize/common/utility';\r\nimport {\r\n ComProtocolConnectOptions,\r\n ComProtocolDisconnectOptions,\r\n ComProtocolOptions,\r\n ComProtocolSendOptions,\r\n ConnectionState,\r\n} from '@iotize/tap/protocol/api';\r\nimport { QueueComProtocol } from '@iotize/tap/protocol/core';\r\nimport { from, Observable, Subject, Subscription } from 'rxjs';\r\nimport { filter, first } from 'rxjs/operators';\r\n\r\nimport { CordovaInterface } from './cordova-interface';\r\nimport { debug } from './logger';\r\n\r\ndeclare var iotizeBLE: CordovaInterface;\r\n\r\nexport class BLEComProtocol extends QueueComProtocol {\r\n private deviceId = \"\";\r\n private _connectionStateSubject?: Subject<ConnectionState>;\r\n private get connectionStateSubject() {\r\n if (!this._connectionStateSubject) {\r\n this._connectionStateSubject = new Subject();\r\n }\r\n return this._connectionStateSubject;\r\n }\r\n\r\n private _connectionStateSubscription?: Subscription;\r\n\r\n constructor(deviceId: string, comProtocolOptions?: ComProtocolOptions) {\r\n super();\r\n this.deviceId = deviceId;\r\n if (comProtocolOptions) {\r\n this.options = comProtocolOptions;\r\n } else {\r\n this.options.connect.timeout = 60000;\r\n }\r\n }\r\n\r\n _connect(options?: ComProtocolConnectOptions): Observable<any> {\r\n debug('_connect', options);\r\n const connectionStateSubject = this.connectionStateSubject;\r\n this._connectionStateSubscription = connectionStateSubject.subscribe((val) => this.setConnectionState(val));\r\n\r\n const onConnectionStateChange = (val: string) => {\r\n debug('_connect observable: onConnect \"', val, '\"');\r\n connectionStateSubject.next(safeEnumValue(ConnectionState, val));\r\n };\r\n const onError = (error: any) => {\r\n debug('_connect observable: onError', error);\r\n connectionStateSubject.error(error);\r\n };\r\n iotizeBLE.connect(this.deviceId, onConnectionStateChange, onError);\r\n\r\n return connectionStateSubject.pipe(\r\n filter(state => state === ConnectionState.CONNECTED),\r\n first()\r\n );\r\n }\r\n\r\n _disconnect(options?: ComProtocolDisconnectOptions): Observable<any> {\r\n debug('_disconnect', options);\r\n return from(this._cordovaCallToPromise(\r\n iotizeBLE.disConnect,\r\n this.deviceId\r\n ));\r\n }\r\n\r\n write(data: Uint8Array): Promise<any> {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n read(): Promise<Uint8Array> {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n send(data: Uint8Array, options?: ComProtocolSendOptions): Observable<any> {\r\n const promise = this._cordovaCallToPromise<string>(\r\n iotizeBLE.send,\r\n this.deviceId,\r\n bufferToHexString(data)\r\n )\r\n .then((hexString: string) => hexStringToBuffer(hexString));\r\n return from(promise);\r\n }\r\n\r\n protected _cordovaCallToPromise<T>(cordovaFct: (...args: any[]) => any, ...args: any[]): Promise<T> {\r\n if (!cordovaFct) {\r\n return Promise.reject(new Error(`INTERNAL ERROR UNKOWN CORDOVA FUNCTION`)); // TODO proper error\r\n }\r\n debug('Call to ', cordovaFct.name, ...args);\r\n return new Promise<T>((resolve: any, reject: any) => {\r\n args.push((result: any) => {\r\n debug('success handler ', result);\r\n resolve(result);\r\n });\r\n args.push((err: any) => {\r\n debug('error handler ', err);\r\n reject(err);\r\n });\r\n\r\n cordovaFct.apply(iotizeBLE, args);\r\n });\r\n }\r\n\r\n};\r\n\r\n","//\r\n// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license. \r\n//\r\n// scanner.ts\r\n// device-com-ble.cordova BLE Cordova Plugin\r\n//\r\n\r\nimport { Observable, BehaviorSubject } from 'rxjs';\r\nimport { DeviceScanner, DeviceScannerOptions } from '@iotize/tap/scanner/api';\r\nimport { CordovaInterface } from './cordova-interface';\r\nimport { debug } from './logger';\r\nimport { CordovaBLEScanResult } from './definitions';\r\n\r\ndeclare var iotizeBLE: CordovaInterface;\r\n\r\n/**\r\n * \r\n */\r\nexport class BLEScanner implements DeviceScanner<CordovaBLEScanResult> {\r\n\r\n private _results = new BehaviorSubject<CordovaBLEScanResult[]>([]);\r\n private _scanning$ = new BehaviorSubject<boolean>(false);\r\n \r\n constructor(private cordovaInterface: CordovaInterface = iotizeBLE) {\r\n }\r\n\r\n get scanning(): Observable<boolean> {\r\n return this._scanning$.asObservable();\r\n }\r\n\r\n get isScanning(): boolean {\r\n return this._scanning$.value;\r\n }\r\n\r\n /**\r\n * Gets the observable on the devices$ Subject\r\n * @return\r\n */\r\n get results(): Observable<CordovaBLEScanResult[]> {\r\n return this._results.asObservable();\r\n }\r\n\r\n\r\n /**\r\n * Launches the scan for BLE devices\r\n * Throws if BLE is not available\r\n */\r\n start(options?: DeviceScannerOptions): Promise<void> {\r\n return this.checkAvailable().then(\r\n isAvailable => {\r\n if (!isAvailable) {\r\n return Promise.reject(\"BLE is not available\");\r\n }\r\n debug(\"Start Scanning ...\");\r\n this._scanning$.next(true);\r\n return new Promise<void>((resolve, reject) => {\r\n this.cordovaInterface.startScan((result) => {\r\n debug('iotizeBLE.startScan', result);\r\n if (result == 'Ok') {\r\n resolve();\r\n return;\r\n }\r\n this.addOrRefreshDevice(result);\r\n }, (error) => {\r\n this.cordovaInterface\r\n .getLastError((lasterror) => {\r\n debug(\"let BLE error \" + lasterror);\r\n }, (err) => {\r\n debug('cannot get last BLE error: ', err);\r\n });\r\n reject(error);\r\n this._scanning$.next(false);\r\n });\r\n }\r\n )\r\n });\r\n }\r\n\r\n /**\r\n * \r\n */\r\n stop(): Promise<void> {\r\n debug(\"Stop Scanning ...\");\r\n return new Promise<void>((resolve, reject) => {\r\n this.cordovaInterface\r\n .stopScan((result) => {\r\n this._scanning$.next(false);\r\n resolve();\r\n },\r\n (error) => {\r\n this._scanning$.next(false);\r\n reject(error);\r\n });\r\n });\r\n }\r\n\r\n /**\r\n * Returns true if this scanner is available\r\n */\r\n checkAvailable(): Promise<boolean> {\r\n return new Promise<boolean>((resolve, reject) => {\r\n this.cordovaInterface.checkAvailable((result) => {\r\n debug('checkAvailable result', result);\r\n resolve(result);\r\n }, (error) => {\r\n reject(error);\r\n });\r\n })\r\n }\r\n\r\n private get devices() {\r\n return this._results.value;\r\n }\r\n\r\n private addOrRefreshDevice(newDevice: CordovaBLEScanResult) {\r\n let storedDeviceIndex = this.devices.findIndex((entry) => entry.address == newDevice.address);\r\n if (storedDeviceIndex >= 0) {\r\n let storedDevice = this.devices[storedDeviceIndex];\r\n if (storedDevice.name != newDevice.name || storedDevice.rssi != newDevice.rssi) {\r\n debug(`Updating device at index ${storedDeviceIndex}, name=${storedDevice.name} with rssi=${storedDevice.rssi}`);\r\n this.devices[storedDeviceIndex] = newDevice;\r\n // this.devices = [...this.devices];\r\n this._results.next(this.devices);\r\n }\r\n }\r\n else {\r\n debug(`Adding new device name=${newDevice.name} with rssi=${newDevice.rssi}`);\r\n this.devices.push(newDevice);\r\n this._results.next(this.devices);\r\n }\r\n }\r\n}\r\n"],"names":[],"mappings":";;;;;;;;AAEO,IAAM,KAAK,GAAG,cAAc,CAAC,gCAAgC,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACIrE;IAkBoC,kCAAgB;IAYhD,wBAAY,QAAgB,EAAE,kBAAuC;QAArE,YACI,iBAAO,SAOV;QAnBO,cAAQ,GAAG,EAAE,CAAC;QAalB,KAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,kBAAkB,EAAE;YACpB,KAAI,CAAC,OAAO,GAAG,kBAAkB,CAAC;SACrC;aAAM;YACH,KAAI,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,GAAG,KAAK,CAAC;SACxC;;KACJ;IAjBD,sBAAY,kDAAsB;aAAlC;YACI,IAAI,CAAC,IAAI,CAAC,uBAAuB,EAAE;gBAC/B,IAAI,CAAC,uBAAuB,GAAG,IAAI,OAAO,EAAE,CAAC;aAChD;YACD,OAAO,IAAI,CA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IAAI,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;YACnD,IAAI,YAAY,CAAC,IAAI,IAAI,SAAS,CAAC,IAAI,IAAI,YAAY,CAAC,IAAI,IAAI,SAAS,CAAC,IAAI,EAAE;gBAC5E,KAAK,CAAC,8BAA4B,iBAAiB,eAAU,YAAY,CAAC,IAAI,mBAAc,YAAY,CAAC,IAAM,CAAC,CAAC;gBACjH,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAC,GAAG,SAAS,CAAC;gBAE5C,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;aACpC;SACJ;aACI;YACD,KAAK,CAAC,4BAA0B,SAAS,CAAC,IAAI,mBAAc,SAAS,CAAC,IAAM,CAAC,CAAC;YAC9E,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YAC7B,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;SACpC;KACJ;IACL,iBAAC;AAAD,CAAC;;;;"}
|
package/src/android/.project
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<projectDescription>
|
|
3
|
-
<name>android</name>
|
|
4
|
-
<comment>Project android created by Buildship.</comment>
|
|
5
|
-
<projects>
|
|
6
|
-
</projects>
|
|
7
|
-
<buildSpec>
|
|
8
|
-
<buildCommand>
|
|
9
|
-
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
|
|
10
|
-
<arguments>
|
|
11
|
-
</arguments>
|
|
12
|
-
</buildCommand>
|
|
13
|
-
</buildSpec>
|
|
14
|
-
<natures>
|
|
15
|
-
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
|
16
|
-
</natures>
|
|
17
|
-
</projectDescription>
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
arguments=
|
|
2
|
-
auto.sync=false
|
|
3
|
-
build.scans.enabled=false
|
|
4
|
-
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
|
|
5
|
-
connection.project.dir=
|
|
6
|
-
eclipse.preferences.version=1
|
|
7
|
-
gradle.user.home=
|
|
8
|
-
java.home=C\:/Program Files/Java/jdk1.8.0_172
|
|
9
|
-
jvm.arguments=
|
|
10
|
-
offline.mode=false
|
|
11
|
-
override.workspace.settings=true
|
|
12
|
-
show.console.view=true
|
|
13
|
-
show.executions.view=true
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license.
|
|
3
|
-
//
|
|
4
|
-
// ArgsHelper.java
|
|
5
|
-
// device-com-ble.cordova BLE Cordova Plugin
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
package com.iotize.plugin;
|
|
9
|
-
|
|
10
|
-
import org.json.JSONArray;
|
|
11
|
-
import org.json.JSONException;
|
|
12
|
-
|
|
13
|
-
class ArgsHelper {
|
|
14
|
-
private final JSONArray args;
|
|
15
|
-
|
|
16
|
-
public ArgsHelper(JSONArray args) {
|
|
17
|
-
this.args = args;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
public String getString(int i) throws BLEComError {
|
|
21
|
-
if (this.args == null || i >= this.args.length()){
|
|
22
|
-
throw new BLEComError(BLEComError.Code.ILLEGAL_ARGUMENT, "Missing argument n°" + (i+1));
|
|
23
|
-
}
|
|
24
|
-
try {
|
|
25
|
-
return this.args.getString(i);
|
|
26
|
-
} catch (JSONException e) {
|
|
27
|
-
throw new Error("INTERNAL ERROR", e);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
@@ -1,375 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license.
|
|
3
|
-
//
|
|
4
|
-
// BLECom.java
|
|
5
|
-
// device-com-ble.cordova BLE Cordova Plugin
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
package com.iotize.plugin;
|
|
9
|
-
|
|
10
|
-
import android.annotation.SuppressLint;
|
|
11
|
-
import android.app.Activity;
|
|
12
|
-
import android.bluetooth.BluetoothAdapter;
|
|
13
|
-
import android.bluetooth.BluetoothDevice;
|
|
14
|
-
import android.bluetooth.BluetoothManager;
|
|
15
|
-
import android.content.Context;
|
|
16
|
-
import android.content.Intent;
|
|
17
|
-
import android.content.pm.PackageManager;
|
|
18
|
-
import android.provider.Settings;
|
|
19
|
-
import android.util.Log;
|
|
20
|
-
|
|
21
|
-
import com.iotize.android.communication.protocol.ble.BLEProtocol;
|
|
22
|
-
import com.iotize.android.communication.protocol.ble.DeviceManager;
|
|
23
|
-
import com.iotize.android.communication.protocol.ble.scanner.BLEScanner;
|
|
24
|
-
import com.iotize.android.core.buffer.ByteArrayHelper;
|
|
25
|
-
import com.iotize.android.core.util.Helper;
|
|
26
|
-
import com.iotize.android.device.api.device.scanner.IOnDeviceDiscovered;
|
|
27
|
-
|
|
28
|
-
import org.apache.cordova.CallbackContext;
|
|
29
|
-
import org.apache.cordova.CordovaInterface;
|
|
30
|
-
import org.apache.cordova.CordovaPlugin;
|
|
31
|
-
import org.apache.cordova.CordovaWebView;
|
|
32
|
-
import org.apache.cordova.LOG;
|
|
33
|
-
import org.apache.cordova.PermissionHelper;
|
|
34
|
-
import org.json.JSONArray;
|
|
35
|
-
|
|
36
|
-
import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
|
|
37
|
-
|
|
38
|
-
public class BLECom extends CordovaPlugin {
|
|
39
|
-
// actions
|
|
40
|
-
private static final String CONNECT = "connect";
|
|
41
|
-
private static final String DISCONNECT = "disConnect";
|
|
42
|
-
private static final String SEND_REQUEST = "sendRequest";
|
|
43
|
-
private static final String GET_LAST_ERROR = "getLastError";
|
|
44
|
-
private static final String START_SCAN = "startScan";
|
|
45
|
-
private static final String STOP_SCAN = "stopScan";
|
|
46
|
-
private static final String IS_ENABLED = "isEnabled";
|
|
47
|
-
private static final String CHECK_AVAILABLE = "checkAvailable";
|
|
48
|
-
private static final String IS_CONNECTED = "isConnected";
|
|
49
|
-
private static final String ENABLE = "enable";
|
|
50
|
-
|
|
51
|
-
private static final int REQUEST_ENABLE_BLUETOOTH = 1;
|
|
52
|
-
private static final int REQUEST_SCAN_PERMISSIONS = 2;
|
|
53
|
-
|
|
54
|
-
private static final String TAG = "BLECom";
|
|
55
|
-
private DeviceManager<BLEProtocol> peripherals;
|
|
56
|
-
private BluetoothAdapter bluetoothAdapter;
|
|
57
|
-
private PluginResponse enableBluetoothCallback;
|
|
58
|
-
private PluginResponse pluginResponseDiscoverDevice;
|
|
59
|
-
private PluginResponse permissionCallback;
|
|
60
|
-
private BLEScanner scanner;
|
|
61
|
-
private IOnDeviceDiscovered<BLEScanner.BLEScanData> onDeviceDiscoveredCallback;
|
|
62
|
-
|
|
63
|
-
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
|
64
|
-
super.initialize(cordova, webView);
|
|
65
|
-
|
|
66
|
-
this.peripherals = new DeviceManager<>();
|
|
67
|
-
Log.d(TAG, "Initializing IoTizeBLE Plugin");
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
@Override
|
|
71
|
-
public void onStart() {
|
|
72
|
-
super.onStart();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
@Override
|
|
76
|
-
public void onStop() {
|
|
77
|
-
super.onStop();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
@SuppressLint("MissingPermission")
|
|
81
|
-
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) {
|
|
82
|
-
|
|
83
|
-
PluginResponse pluginResponse = new PluginResponse(
|
|
84
|
-
action,
|
|
85
|
-
args,
|
|
86
|
-
callbackContext
|
|
87
|
-
);
|
|
88
|
-
ArgsHelper argsHelper = new ArgsHelper(args);
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
this.setupBluetoothAdapter();
|
|
92
|
-
|
|
93
|
-
switch (action) {
|
|
94
|
-
case STOP_SCAN:
|
|
95
|
-
Log.d(TAG, STOP_SCAN);
|
|
96
|
-
this.stopScan(pluginResponse);
|
|
97
|
-
break;
|
|
98
|
-
case START_SCAN:
|
|
99
|
-
Log.d(TAG, START_SCAN);
|
|
100
|
-
this.startScan(pluginResponse);
|
|
101
|
-
break;
|
|
102
|
-
case CONNECT: {
|
|
103
|
-
Log.d(TAG, CONNECT);
|
|
104
|
-
String macAddress = argsHelper.getString(0);
|
|
105
|
-
this.connect(pluginResponse, macAddress);
|
|
106
|
-
break;
|
|
107
|
-
}
|
|
108
|
-
case DISCONNECT: {
|
|
109
|
-
Log.d(TAG, DISCONNECT);
|
|
110
|
-
String macAddress = argsHelper.getString(0);
|
|
111
|
-
this.disconnect(pluginResponse, macAddress);
|
|
112
|
-
break;
|
|
113
|
-
}
|
|
114
|
-
case GET_LAST_ERROR:
|
|
115
|
-
Log.d(TAG, GET_LAST_ERROR);
|
|
116
|
-
this.getLastError(pluginResponse);
|
|
117
|
-
break;
|
|
118
|
-
case SEND_REQUEST:
|
|
119
|
-
String deviceId = argsHelper.getString(0);
|
|
120
|
-
String hexString = argsHelper.getString(1);
|
|
121
|
-
this.sendRequest(pluginResponse, deviceId, hexString);
|
|
122
|
-
break;
|
|
123
|
-
case CHECK_AVAILABLE:
|
|
124
|
-
case IS_ENABLED:
|
|
125
|
-
pluginResponse.success(bluetoothAdapter.isEnabled());
|
|
126
|
-
|
|
127
|
-
break;
|
|
128
|
-
case IS_CONNECTED: {
|
|
129
|
-
String macAddress = argsHelper.getString(0);
|
|
130
|
-
boolean isConnected = peripherals.get(macAddress).isConnected();
|
|
131
|
-
pluginResponse.success(isConnected);
|
|
132
|
-
break;
|
|
133
|
-
}
|
|
134
|
-
case ENABLE:
|
|
135
|
-
if (enableBluetoothCallback != null) {
|
|
136
|
-
Log.w(TAG, "There is already an enable request pending...");
|
|
137
|
-
}
|
|
138
|
-
enableBluetoothCallback = pluginResponse;
|
|
139
|
-
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
140
|
-
cordova.startActivityForResult(this, intent, REQUEST_ENABLE_BLUETOOTH);
|
|
141
|
-
break;
|
|
142
|
-
default:
|
|
143
|
-
throw BLEComError.illegalAction("Illegal action " + action);
|
|
144
|
-
}
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
catch (BLEComError err) {
|
|
148
|
-
pluginResponse.error(err);
|
|
149
|
-
return false;
|
|
150
|
-
}
|
|
151
|
-
catch (Throwable err) {
|
|
152
|
-
pluginResponse.error(BLEComError.unknownError(err));
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
private void setupBluetoothAdapter() throws BLEComError {
|
|
158
|
-
if (bluetoothAdapter == null) {
|
|
159
|
-
Activity activity = cordova.getActivity();
|
|
160
|
-
boolean hardwareSupportsBLE = activity.getApplicationContext()
|
|
161
|
-
.getPackageManager()
|
|
162
|
-
.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
|
|
163
|
-
if (!hardwareSupportsBLE) {
|
|
164
|
-
LOG.w(TAG, "This hardware does not support Bluetooth Low Energy.");
|
|
165
|
-
throw BLEComError.bleNotAvailable();
|
|
166
|
-
}
|
|
167
|
-
BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
|
|
168
|
-
if (bluetoothManager == null){
|
|
169
|
-
throw BLEComError.bleNotAvailable();
|
|
170
|
-
}
|
|
171
|
-
bluetoothAdapter = bluetoothManager.getAdapter();
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
private void startScan(PluginResponse pluginResponse) {
|
|
177
|
-
|
|
178
|
-
if (pluginResponseDiscoverDevice != null && pluginResponseDiscoverDevice != pluginResponse){
|
|
179
|
-
Log.w(TAG, "Scan is already started...");
|
|
180
|
-
}
|
|
181
|
-
pluginResponseDiscoverDevice = pluginResponse;
|
|
182
|
-
if(!PermissionHelper.hasPermission(this, ACCESS_COARSE_LOCATION)) {
|
|
183
|
-
permissionCallback = pluginResponse;
|
|
184
|
-
PermissionHelper.requestPermission(this, REQUEST_SCAN_PERMISSIONS, ACCESS_COARSE_LOCATION);
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
if (scanner == null){
|
|
189
|
-
this.initBLEScanner();
|
|
190
|
-
}
|
|
191
|
-
scanner.start();
|
|
192
|
-
pluginResponse.newResult("Ok");
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
private void initBLEScanner() {
|
|
197
|
-
Log.d(TAG, "initBLEScanner()");
|
|
198
|
-
scanner = new BLEScanner(cordova.getContext());
|
|
199
|
-
if (onDeviceDiscoveredCallback == null){
|
|
200
|
-
this.onDeviceDiscoveredCallback = new IOnDeviceDiscovered<BLEScanner.BLEScanData>() {
|
|
201
|
-
@Override
|
|
202
|
-
public void onDeviceDiscovered(BLEScanner.BLEScanData device) {
|
|
203
|
-
Log.d(TAG, "Device discovered: " + device);
|
|
204
|
-
if (pluginResponseDiscoverDevice != null) {
|
|
205
|
-
try {
|
|
206
|
-
pluginResponseDiscoverDevice.newResult(
|
|
207
|
-
JSONBuilder.toJSONObject(device)
|
|
208
|
-
);
|
|
209
|
-
} catch (Throwable e) {
|
|
210
|
-
pluginResponseDiscoverDevice.newResult(BLEComError.internalError(e));
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
else{
|
|
214
|
-
Log.w(TAG, "Discovered device but no listener has been setup");
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
@Override
|
|
219
|
-
public void onDeviceLost(BLEScanner.BLEScanData bleScanData) {
|
|
220
|
-
Log.w(TAG, "Device lost" + bleScanData);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
@Override
|
|
224
|
-
public void onScanFailed(Throwable throwable) {
|
|
225
|
-
Log.w(TAG, "Scan failed", throwable);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
scanner.setOnDeviceDiscoveredCallback(this.onDeviceDiscoveredCallback);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
@SuppressLint("NewApi")
|
|
234
|
-
private void stopScan(PluginResponse pluginResponse) {
|
|
235
|
-
pluginResponseDiscoverDevice = null;
|
|
236
|
-
if (scanner == null || !scanner.isEnabled()){
|
|
237
|
-
Log.w(TAG, "Scanner is not running");
|
|
238
|
-
pluginResponse.success();
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
scanner.stop();
|
|
242
|
-
pluginResponse.success();
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
private void getLastError(PluginResponse pluginResponse) {
|
|
246
|
-
throw new Error("Not implemented yet");
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
private void disconnect(final PluginResponse pluginResponse, String macAddress) {
|
|
250
|
-
BLEProtocol peripheral = peripherals.get(macAddress);
|
|
251
|
-
|
|
252
|
-
executeAsync(() -> {
|
|
253
|
-
try {
|
|
254
|
-
peripheral.disconnect();
|
|
255
|
-
pluginResponse.success();
|
|
256
|
-
} catch (Exception e) {
|
|
257
|
-
pluginResponse.error(new BLEComError(BLEComError.Code.DISCONNECTION_ERROR, e));
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
private void sendRequest(PluginResponse pluginResponse, String deviceId, String hexString) throws BLEComError {
|
|
263
|
-
if (deviceId == null) {
|
|
264
|
-
throw new IllegalArgumentException("Device id must not be null");
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
byte[] data = hexStringToByteArray(hexString);
|
|
268
|
-
Log.d(TAG, SEND_REQUEST + " " + deviceId + " " + hexString);
|
|
269
|
-
|
|
270
|
-
BLEProtocol peripheral = peripherals.get(deviceId);
|
|
271
|
-
executeAsync(() -> {
|
|
272
|
-
try {
|
|
273
|
-
byte[] response = peripheral.send(data);
|
|
274
|
-
pluginResponse.success(response);
|
|
275
|
-
} catch (Exception e) {
|
|
276
|
-
pluginResponse.error(BLEComError.requestError(e, deviceId, data));
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
private void executeAsync(Runnable runnable) {
|
|
282
|
-
cordova.getThreadPool().execute(runnable);
|
|
283
|
-
//runnable.run();
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
private byte[] hexStringToByteArray(String hexString) throws BLEComError {
|
|
287
|
-
try{
|
|
288
|
-
return Helper.HexStringToByteArray(hexString);
|
|
289
|
-
}
|
|
290
|
-
catch (Throwable ex){
|
|
291
|
-
throw BLEComError.illegalArgument("Should be a valid hexadecimal string");
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
private void connect(PluginResponse pluginResponse, String macAddress) throws BLEComError {
|
|
296
|
-
BLEProtocol peripheral = peripherals.getIfExists(macAddress);
|
|
297
|
-
if (peripheral == null){
|
|
298
|
-
if (!BluetoothAdapter.checkBluetoothAddress(macAddress)) {
|
|
299
|
-
throw BLEComError.invalidMacAddress(macAddress);
|
|
300
|
-
}
|
|
301
|
-
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress);
|
|
302
|
-
peripheral = new BLEProtocol(cordova.getActivity(), device);
|
|
303
|
-
peripherals.put(macAddress, peripheral);
|
|
304
|
-
}
|
|
305
|
-
BLEProtocol finalPeripheral = peripheral;
|
|
306
|
-
setConnectionStateCallback(pluginResponse, macAddress);
|
|
307
|
-
if (finalPeripheral.isConnected()){
|
|
308
|
-
pluginResponse.newResult("CONNECTED");
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
executeAsync(() -> {
|
|
312
|
-
try {
|
|
313
|
-
finalPeripheral.connect();
|
|
314
|
-
pluginResponse.newResult("CONNECTED");
|
|
315
|
-
} catch(Exception e) {
|
|
316
|
-
pluginResponse.error(BLEComError.connectionError(e));
|
|
317
|
-
}
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/* @Override */
|
|
322
|
-
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
323
|
-
for(int result:grantResults) {
|
|
324
|
-
if(result == PackageManager.PERMISSION_DENIED) {
|
|
325
|
-
LOG.d(TAG, "User *rejected* Coarse Location Access");
|
|
326
|
-
if (permissionCallback != null){
|
|
327
|
-
this.permissionCallback.error("Location permission not granted.");
|
|
328
|
-
}
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
switch(requestCode) {
|
|
334
|
-
case REQUEST_SCAN_PERMISSIONS:
|
|
335
|
-
LOG.d(TAG, "User granted scan permissions");
|
|
336
|
-
this.startScan(pluginResponseDiscoverDevice);
|
|
337
|
-
this.permissionCallback = null;
|
|
338
|
-
break;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
@Override
|
|
343
|
-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
344
|
-
|
|
345
|
-
if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
|
|
346
|
-
|
|
347
|
-
if (resultCode == Activity.RESULT_OK) {
|
|
348
|
-
LOG.d(TAG, "User enabled Bluetooth");
|
|
349
|
-
if (enableBluetoothCallback != null) {
|
|
350
|
-
enableBluetoothCallback.success();
|
|
351
|
-
}
|
|
352
|
-
} else {
|
|
353
|
-
LOG.d(TAG, "User did *NOT* enable Bluetooth");
|
|
354
|
-
if (enableBluetoothCallback != null) {
|
|
355
|
-
enableBluetoothCallback.error("User did not enable Bluetooth");
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
enableBluetoothCallback = null;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
private void setConnectionStateCallback(PluginResponse pluginResponse, String macAddress) {
|
|
364
|
-
BLEProtocol peripheral = peripherals.getIfExists(macAddress);
|
|
365
|
-
if (peripheral == null) {
|
|
366
|
-
pluginResponse.error(BLEComError.illegalAction( "unregistered peripheral"));
|
|
367
|
-
return;
|
|
368
|
-
}
|
|
369
|
-
peripheral.addOnConnectionStatusChangeListener(
|
|
370
|
-
(newConnectionState, oldConnectionState) -> {
|
|
371
|
-
LOG.d(TAG, "Old State: " + oldConnectionState + ", New State: " + newConnectionState);
|
|
372
|
-
pluginResponse.newResult(newConnectionState.toString());
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
}
|
package/src/public_api.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './www/index';
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license.
|
|
3
|
-
//
|
|
4
|
-
// ble-com-protocol.ts
|
|
5
|
-
// device-com-ble.cordova BLE Cordova Plugin
|
|
6
|
-
//
|
|
7
|
-
import { bufferToHexString, hexStringToBuffer } from '@iotize/common/byte-converter';
|
|
8
|
-
import { safeEnumValue } from '@iotize/common/utility';
|
|
9
|
-
import {
|
|
10
|
-
ComProtocolConnectOptions,
|
|
11
|
-
ComProtocolDisconnectOptions,
|
|
12
|
-
ComProtocolOptions,
|
|
13
|
-
ComProtocolSendOptions,
|
|
14
|
-
ConnectionState,
|
|
15
|
-
} from '@iotize/tap/protocol/api';
|
|
16
|
-
import { QueueComProtocol } from '@iotize/tap/protocol/core';
|
|
17
|
-
import { from, Observable, Subject, Subscription } from 'rxjs';
|
|
18
|
-
import { filter, first } from 'rxjs/operators';
|
|
19
|
-
|
|
20
|
-
import { CordovaInterface } from './cordova-interface';
|
|
21
|
-
import { debug } from './logger';
|
|
22
|
-
|
|
23
|
-
declare var iotizeBLE: CordovaInterface;
|
|
24
|
-
|
|
25
|
-
export class BLEComProtocol extends QueueComProtocol {
|
|
26
|
-
private deviceId = "";
|
|
27
|
-
private _connectionStateSubject?: Subject<ConnectionState>;
|
|
28
|
-
private get connectionStateSubject() {
|
|
29
|
-
if (!this._connectionStateSubject) {
|
|
30
|
-
this._connectionStateSubject = new Subject();
|
|
31
|
-
}
|
|
32
|
-
return this._connectionStateSubject;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
private _connectionStateSubscription?: Subscription;
|
|
36
|
-
|
|
37
|
-
constructor(deviceId: string, comProtocolOptions?: ComProtocolOptions) {
|
|
38
|
-
super();
|
|
39
|
-
this.deviceId = deviceId;
|
|
40
|
-
if (comProtocolOptions) {
|
|
41
|
-
this.options = comProtocolOptions;
|
|
42
|
-
} else {
|
|
43
|
-
this.options.connect.timeout = 60000;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
_connect(options?: ComProtocolConnectOptions): Observable<any> {
|
|
48
|
-
debug('_connect', options);
|
|
49
|
-
const connectionStateSubject = this.connectionStateSubject;
|
|
50
|
-
this._connectionStateSubscription = connectionStateSubject.subscribe((val) => this.setConnectionState(val));
|
|
51
|
-
|
|
52
|
-
const onConnectionStateChange = (val: string) => {
|
|
53
|
-
debug('_connect observable: onConnect "', val, '"');
|
|
54
|
-
connectionStateSubject.next(safeEnumValue(ConnectionState, val));
|
|
55
|
-
};
|
|
56
|
-
const onError = (error: any) => {
|
|
57
|
-
debug('_connect observable: onError', error);
|
|
58
|
-
connectionStateSubject.error(error);
|
|
59
|
-
};
|
|
60
|
-
iotizeBLE.connect(this.deviceId, onConnectionStateChange, onError);
|
|
61
|
-
|
|
62
|
-
return connectionStateSubject.pipe(
|
|
63
|
-
filter(state => state === ConnectionState.CONNECTED),
|
|
64
|
-
first()
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
_disconnect(options?: ComProtocolDisconnectOptions): Observable<any> {
|
|
69
|
-
debug('_disconnect', options);
|
|
70
|
-
return from(this._cordovaCallToPromise(
|
|
71
|
-
iotizeBLE.disConnect,
|
|
72
|
-
this.deviceId
|
|
73
|
-
));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
write(data: Uint8Array): Promise<any> {
|
|
77
|
-
throw new Error("Method not implemented.");
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
read(): Promise<Uint8Array> {
|
|
81
|
-
throw new Error("Method not implemented.");
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
send(data: Uint8Array, options?: ComProtocolSendOptions): Observable<any> {
|
|
85
|
-
const promise = this._cordovaCallToPromise<string>(
|
|
86
|
-
iotizeBLE.send,
|
|
87
|
-
this.deviceId,
|
|
88
|
-
bufferToHexString(data)
|
|
89
|
-
)
|
|
90
|
-
.then((hexString: string) => hexStringToBuffer(hexString));
|
|
91
|
-
return from(promise);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
protected _cordovaCallToPromise<T>(cordovaFct: (...args: any[]) => any, ...args: any[]): Promise<T> {
|
|
95
|
-
if (!cordovaFct) {
|
|
96
|
-
return Promise.reject(new Error(`INTERNAL ERROR UNKOWN CORDOVA FUNCTION`)); // TODO proper error
|
|
97
|
-
}
|
|
98
|
-
debug('Call to ', cordovaFct.name, ...args);
|
|
99
|
-
return new Promise<T>((resolve: any, reject: any) => {
|
|
100
|
-
args.push((result: any) => {
|
|
101
|
-
debug('success handler ', result);
|
|
102
|
-
resolve(result);
|
|
103
|
-
});
|
|
104
|
-
args.push((err: any) => {
|
|
105
|
-
debug('error handler ', err);
|
|
106
|
-
reject(err);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
cordovaFct.apply(iotizeBLE, args);
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
};
|
|
114
|
-
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license.
|
|
3
|
-
//
|
|
4
|
-
// cordova-interface.ts
|
|
5
|
-
// device-com-ble.cordova BLE Cordova Plugin
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
import { CordovaBLEScanResult } from "./definitions";
|
|
9
|
-
|
|
10
|
-
// import { exec } from "cordova";
|
|
11
|
-
export interface CordovaInterface {
|
|
12
|
-
|
|
13
|
-
checkAvailable (success: (data: any) => any, failure: (err: any) => any): void;
|
|
14
|
-
|
|
15
|
-
startScan(success: (data: CordovaBLEScanResult | 'Ok') => any, failure: (err: any) => any): void;
|
|
16
|
-
|
|
17
|
-
stopScan(success: (data: any) => any, failure: (err: any) => any): void;
|
|
18
|
-
|
|
19
|
-
connect(device_id: string, success: (data: any) => any, failure: (err: any) => any): void;
|
|
20
|
-
|
|
21
|
-
disConnect(device_id: string, success: (data: any) => any, failure: (err: any) => any): void;
|
|
22
|
-
|
|
23
|
-
isConnected(device_id: string, success: (data: any) => any, failure: (err: any) => any): void;
|
|
24
|
-
|
|
25
|
-
send(device_id: string, data: any, success: (data: any) => any, failure: (err: any) => any): void;
|
|
26
|
-
|
|
27
|
-
getLastError(success: (data: any) => any, failure: (err: any) => any): void;
|
|
28
|
-
|
|
29
|
-
}
|
package/src/www/definitions.ts
DELETED
package/src/www/index.ts
DELETED
package/src/www/logger.ts
DELETED