@react-native-ohos/react-native-ble-plx 3.2.1-rc.1
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/LICENSE +202 -0
- package/README.OpenSource +11 -0
- package/README.md +9 -0
- package/app.plugin.js +1 -0
- package/harmony/rn_bleplx/build-profile.json5 +8 -0
- package/harmony/rn_bleplx/hvigorfile.ts +2 -0
- package/harmony/rn_bleplx/index.ets +27 -0
- package/harmony/rn_bleplx/obfuscation-rules.txt +18 -0
- package/harmony/rn_bleplx/oh-package.json5 +13 -0
- package/harmony/rn_bleplx/src/main/cpp/BlePlxPackage.h +36 -0
- package/harmony/rn_bleplx/src/main/cpp/CMakeLists.txt +9 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/RNOH/generated/BaseReactNativeBlePlxPackage.h +65 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/RNOH/generated/turbo_modules/BlePlx.cpp +59 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/RNOH/generated/turbo_modules/BlePlx.h +16 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/react/renderer/components/react_native_ble_plx/ComponentDescriptors.h +22 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/react/renderer/components/react_native_ble_plx/EventEmitters.cpp +18 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/react/renderer/components/react_native_ble_plx/EventEmitters.h +19 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/react/renderer/components/react_native_ble_plx/Props.cpp +21 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/react/renderer/components/react_native_ble_plx/Props.h +20 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/react/renderer/components/react_native_ble_plx/ShadowNodes.cpp +19 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/react/renderer/components/react_native_ble_plx/ShadowNodes.h +25 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/react/renderer/components/react_native_ble_plx/States.cpp +18 -0
- package/harmony/rn_bleplx/src/main/cpp/generated/react/renderer/components/react_native_ble_plx/States.h +23 -0
- package/harmony/rn_bleplx/src/main/ets/BleDevice.ts +99 -0
- package/harmony/rn_bleplx/src/main/ets/BleModule.ts +1219 -0
- package/harmony/rn_bleplx/src/main/ets/BlePlxInterface.ts +27 -0
- package/harmony/rn_bleplx/src/main/ets/BlePlxModule.ts +246 -0
- package/harmony/rn_bleplx/src/main/ets/BlePlxPackage.ets +47 -0
- package/harmony/rn_bleplx/src/main/ets/BlePlxPackage.ts +46 -0
- package/harmony/rn_bleplx/src/main/ets/Characteristic.ts +179 -0
- package/harmony/rn_bleplx/src/main/ets/CommonConstants.ts +30 -0
- package/harmony/rn_bleplx/src/main/ets/Descriptor.ts +143 -0
- package/harmony/rn_bleplx/src/main/ets/Service.ts +85 -0
- package/harmony/rn_bleplx/src/main/ets/common/BleError.ts +90 -0
- package/harmony/rn_bleplx/src/main/ets/common/BleErrorToJsObjectConverter.ts +63 -0
- package/harmony/rn_bleplx/src/main/ets/common/BleEvent.ts +33 -0
- package/harmony/rn_bleplx/src/main/ets/common/BleUtils.ts +86 -0
- package/harmony/rn_bleplx/src/main/ets/common/IdGenerator.ts +49 -0
- package/harmony/rn_bleplx/src/main/ets/common/IdGeneratorKey.ts +70 -0
- package/harmony/rn_bleplx/src/main/ets/common/InstanceIdGenerator.ts +37 -0
- package/harmony/rn_bleplx/src/main/ets/common/Logger.ts +64 -0
- package/harmony/rn_bleplx/src/main/ets/common/PermissionHandler.ts +98 -0
- package/harmony/rn_bleplx/src/main/ets/common/ServiceFactory.ts +37 -0
- package/harmony/rn_bleplx/src/main/ets/generated/components/ts.ts +5 -0
- package/harmony/rn_bleplx/src/main/ets/generated/index.ets +5 -0
- package/harmony/rn_bleplx/src/main/ets/generated/ts.ts +6 -0
- package/harmony/rn_bleplx/src/main/ets/generated/turboModules/BlePlx.ts +100 -0
- package/harmony/rn_bleplx/src/main/ets/generated/turboModules/ts.ts +5 -0
- package/harmony/rn_bleplx/src/main/module.json5 +7 -0
- package/harmony/rn_bleplx/ts.ets +26 -0
- package/harmony/rn_bleplx/ts.ts +26 -0
- package/harmony/rn_bleplx.har +0 -0
- package/package.json +185 -0
- package/src/BleError.js +555 -0
- package/src/BleManager.js +1287 -0
- package/src/BleModule.js +847 -0
- package/src/Characteristic.js +166 -0
- package/src/Descriptor.js +82 -0
- package/src/Device.js +370 -0
- package/src/NativeBlePlx.ts +121 -0
- package/src/Service.js +188 -0
- package/src/TypeDefinition.js +360 -0
- package/src/Utils.js +26 -0
- package/src/index.d.ts +2113 -0
- package/src/index.js +20 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import ble from '@ohos.bluetooth.ble';
|
|
26
|
+
import Logger from './common/Logger'
|
|
27
|
+
import { Characteristic } from './Characteristic';
|
|
28
|
+
import { IdGenerator } from './common/IdGenerator';
|
|
29
|
+
import { IdGeneratorKey } from './common/IdGeneratorKey';
|
|
30
|
+
import { arrayBufferToBase64 } from './common/BleUtils';
|
|
31
|
+
import { ValuesBucket } from '@kit.ArkData';
|
|
32
|
+
|
|
33
|
+
export class Descriptor {
|
|
34
|
+
private characteristicId: number;
|
|
35
|
+
private serviceId: number;
|
|
36
|
+
private characteristicUuid: string;
|
|
37
|
+
private serviceUuid: string;
|
|
38
|
+
private deviceId: string;
|
|
39
|
+
private descriptor: ble.BLEDescriptor;
|
|
40
|
+
private id: number;
|
|
41
|
+
private uuid: string;
|
|
42
|
+
private value: ArrayBuffer;
|
|
43
|
+
|
|
44
|
+
constructor(id: number,
|
|
45
|
+
uuid: string,
|
|
46
|
+
value: ArrayBuffer,
|
|
47
|
+
deviceId: string,
|
|
48
|
+
serviceId: number,
|
|
49
|
+
serviceUuid: string,
|
|
50
|
+
characteristicId: number,
|
|
51
|
+
characteristicUuid: string,
|
|
52
|
+
descriptor: ble.BLEDescriptor) {
|
|
53
|
+
this.id = id;
|
|
54
|
+
this.uuid = uuid;
|
|
55
|
+
this.value = value;
|
|
56
|
+
this.deviceId = deviceId;
|
|
57
|
+
this.serviceId = serviceId;
|
|
58
|
+
this.serviceUuid = serviceUuid;
|
|
59
|
+
this.characteristicId = characteristicId;
|
|
60
|
+
this.characteristicUuid = characteristicUuid;
|
|
61
|
+
this.descriptor = descriptor;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static constructorWithNative(characteristic: Characteristic, gattDescriptor: ble.BLEDescriptor) {
|
|
65
|
+
let id = IdGenerator.getIdForKey(new IdGeneratorKey(characteristic.getDeviceId(), gattDescriptor.descriptorUuid,
|
|
66
|
+
characteristic.getId()));
|
|
67
|
+
return new Descriptor(id, gattDescriptor.descriptorUuid, gattDescriptor.descriptorValue,
|
|
68
|
+
characteristic.getDeviceId(), characteristic.getServiceID(), characteristic.getServiceUUID(),
|
|
69
|
+
characteristic.getId(), characteristic.getUuid(), gattDescriptor)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static constructorWithId(characteristicId: number, serviceId: number, characteristicUuid: string, serviceUuid: string,
|
|
73
|
+
deviceId: string, descriptor: ble.BLEDescriptor, id: number, uuid: string) {
|
|
74
|
+
return new Descriptor(id, uuid, descriptor.descriptorValue, deviceId, serviceId, serviceUuid, characteristicId,
|
|
75
|
+
characteristicUuid, descriptor)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static constructorWithOther(other: Descriptor) {
|
|
79
|
+
return new Descriptor(other.id, other.uuid, other.value, other.deviceId, other.serviceId, other.serviceUuid,
|
|
80
|
+
other.characteristicId, other.characteristicUuid, other.descriptor)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public getId(): number {
|
|
84
|
+
return this.id;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public getDeviceId(): string {
|
|
88
|
+
return this.deviceId;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public getCharacteristicId(): number {
|
|
92
|
+
return this.characteristicId;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public getServiceId(): number {
|
|
96
|
+
return this.serviceId;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public getCharacteristicUuid(): string {
|
|
100
|
+
return this.characteristicUuid;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public getServiceUuid(): string {
|
|
104
|
+
return this.serviceUuid;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public getUuid(): string {
|
|
108
|
+
return this.uuid;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public getValue(): ArrayBuffer {
|
|
112
|
+
return this.value;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public setValue(value: ArrayBuffer) {
|
|
116
|
+
this.value = value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public setValueFromCache() {
|
|
120
|
+
this.value = this.descriptor.descriptorValue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public getNativeDescriptor(): ble.BLEDescriptor {
|
|
124
|
+
return this.descriptor;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public logValue(message: string, value: ArrayBuffer) {
|
|
128
|
+
Logger.debug(message);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public asJSObject(): ValuesBucket {
|
|
132
|
+
return {
|
|
133
|
+
"id": this.getId(),
|
|
134
|
+
"uuid": this.getUuid(),
|
|
135
|
+
"deviceID": this.getDeviceId(),
|
|
136
|
+
"serviceID": this.getServiceId(),
|
|
137
|
+
"serviceUUID": this.getServiceUuid(),
|
|
138
|
+
"characteristicUUID": this.getCharacteristicUuid(),
|
|
139
|
+
"characteristicID": this.getCharacteristicId(),
|
|
140
|
+
"value": arrayBufferToBase64(this.value),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import ble from '@ohos.bluetooth.ble';
|
|
26
|
+
import Logger from './common/Logger';
|
|
27
|
+
import { Characteristic } from './Characteristic';
|
|
28
|
+
import { ValuesBucket } from '@kit.ArkData';
|
|
29
|
+
|
|
30
|
+
export class Service {
|
|
31
|
+
private id: number;
|
|
32
|
+
private deviceID: string;
|
|
33
|
+
private btGattService: ble.GattService;
|
|
34
|
+
|
|
35
|
+
constructor(id: number, deviceID: string, btGattService: ble.GattService) {
|
|
36
|
+
this.id = id;
|
|
37
|
+
this.deviceID = deviceID;
|
|
38
|
+
this.btGattService = btGattService;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public getId(): number {
|
|
42
|
+
return this.id;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public getUuid(): string {
|
|
46
|
+
return this.btGattService.serviceUuid;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public getDeviceID(): string {
|
|
50
|
+
return this.deviceID;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public isPrimary(): boolean {
|
|
54
|
+
return this.btGattService.isPrimary;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public getCharacteristicByUUID(uuid: string): Characteristic | null {
|
|
58
|
+
var characteristic: Characteristic = null;
|
|
59
|
+
this.btGattService.characteristics.forEach(value => {
|
|
60
|
+
if (value.characteristicUuid == uuid) {
|
|
61
|
+
characteristic = Characteristic.constructorWithNative(this, value);
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
return characteristic;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public getCharacteristics(): Characteristic[] {
|
|
69
|
+
var characteristics: Characteristic[] = []
|
|
70
|
+
this.btGattService.characteristics.forEach(value => {
|
|
71
|
+
let obj = Characteristic.constructorWithNative(this, value);
|
|
72
|
+
characteristics.push(obj);
|
|
73
|
+
});
|
|
74
|
+
return characteristics;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public asJSObject(): ValuesBucket {
|
|
78
|
+
return {
|
|
79
|
+
"id": this.getId(),
|
|
80
|
+
"uuid": this.getUuid(),
|
|
81
|
+
"deviceID": this.getDeviceID(),
|
|
82
|
+
"isPrimary": this.isPrimary(),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export const BleErrorCode = {
|
|
26
|
+
UnknownError: 0,
|
|
27
|
+
BluetoothManagerDestroyed: 1,
|
|
28
|
+
OperationCancelled: 2,
|
|
29
|
+
OperationTimedOut: 3,
|
|
30
|
+
OperationStartFailed: 4,
|
|
31
|
+
InvalidIdentifiers: 5,
|
|
32
|
+
BluetoothUnsupported: 100,
|
|
33
|
+
BluetoothUnauthorized: 101,
|
|
34
|
+
BluetoothPoweredOff: 102,
|
|
35
|
+
BluetoothInUnknownState: 103,
|
|
36
|
+
BluetoothResetting: 104,
|
|
37
|
+
BluetoothStateChangeFailed: 105,
|
|
38
|
+
DeviceConnectionFailed: 200,
|
|
39
|
+
DeviceDisconnected: 201,
|
|
40
|
+
DeviceRSSIReadFailed: 202,
|
|
41
|
+
DeviceAlreadyConnected: 203,
|
|
42
|
+
DeviceNotFound: 204,
|
|
43
|
+
DeviceNotConnected: 205,
|
|
44
|
+
DeviceMTUChangeFailed: 206,
|
|
45
|
+
ServicesDiscoveryFailed: 300,
|
|
46
|
+
IncludedServicesDiscoveryFailed: 301,
|
|
47
|
+
ServiceNotFound: 302,
|
|
48
|
+
ServicesNotDiscovered: 303,
|
|
49
|
+
CharacteristicsDiscoveryFailed: 400,
|
|
50
|
+
CharacteristicWriteFailed: 401,
|
|
51
|
+
CharacteristicReadFailed: 402,
|
|
52
|
+
CharacteristicNotifyChangeFailed: 403,
|
|
53
|
+
CharacteristicNotFound: 404,
|
|
54
|
+
CharacteristicsNotDiscovered: 405,
|
|
55
|
+
CharacteristicInvalidDataFormat: 406,
|
|
56
|
+
DescriptorsDiscoveryFailed: 500,
|
|
57
|
+
DescriptorWriteFailed: 501,
|
|
58
|
+
DescriptorReadFailed: 502,
|
|
59
|
+
DescriptorNotFound: 503,
|
|
60
|
+
DescriptorsNotDiscovered: 504,
|
|
61
|
+
DescriptorInvalidDataFormat: 505,
|
|
62
|
+
DescriptorWriteNotAllowed: 506,
|
|
63
|
+
ScanStartFailed: 600,
|
|
64
|
+
LocationServicesDisabled: 601,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export class BleError extends Error {
|
|
68
|
+
public errorCode: number;
|
|
69
|
+
public harmonyCode?: number;
|
|
70
|
+
public reason: string;
|
|
71
|
+
public deviceID?: string;
|
|
72
|
+
public serviceUUID?: string;
|
|
73
|
+
public characteristicUUID?: string;
|
|
74
|
+
public descriptorUUID?: string;
|
|
75
|
+
public internalMessage?: string;
|
|
76
|
+
|
|
77
|
+
constructor(errorCode: number, reason: string, harmonyCode?: number) {
|
|
78
|
+
super(`Error code: ${errorCode}, harmony code: ${harmonyCode}, reason: ${reason}`);
|
|
79
|
+
this.errorCode = errorCode;
|
|
80
|
+
this.reason = reason;
|
|
81
|
+
this.harmonyCode = harmonyCode;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
getMessage(): string {
|
|
85
|
+
return `Error code: ${this.errorCode}, harmony code: ${this.harmonyCode}, reason: ${this.reason}, ` +
|
|
86
|
+
`deviceId: ${this.deviceID}, serviceUuid: ${this.serviceUUID}, ` +
|
|
87
|
+
`characteristicUuid: ${this.characteristicUUID}, descriptorUuid: ${this.descriptorUUID}, ` +
|
|
88
|
+
`internalMessage: ${this.internalMessage}`;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { BleError } from './BleError';
|
|
26
|
+
|
|
27
|
+
export class BleErrorToJsObjectConverter {
|
|
28
|
+
public toJSCallback(error: BleError): any[] {
|
|
29
|
+
const result: any[] = [];
|
|
30
|
+
result.push(this.toJs(error));
|
|
31
|
+
result.push(null);
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public toJs(error: BleError): string {
|
|
36
|
+
const obj: any = {};
|
|
37
|
+
|
|
38
|
+
obj["errorCode"] = error.errorCode;
|
|
39
|
+
|
|
40
|
+
obj["attErrorCode"] =
|
|
41
|
+
error.harmonyCode != null && error.harmonyCode >= 0 && error.harmonyCode < 0x80 ? error.harmonyCode : null;
|
|
42
|
+
|
|
43
|
+
obj["iosErrorCode"] = null;
|
|
44
|
+
|
|
45
|
+
obj["harmonyErrorCode"] = error.harmonyCode != null && error.harmonyCode >= 0x80 ? error.harmonyCode : null;
|
|
46
|
+
|
|
47
|
+
this.appendString(obj, "reason", error.reason);
|
|
48
|
+
this.appendString(obj, "deviceID", error.deviceID);
|
|
49
|
+
this.appendString(obj, "serviceUUID", error.serviceUUID);
|
|
50
|
+
this.appendString(obj, "characteristicUUID", error.characteristicUUID);
|
|
51
|
+
this.appendString(obj, "descriptorUUID", error.descriptorUUID);
|
|
52
|
+
this.appendString(obj, "internalMessage", error.internalMessage);
|
|
53
|
+
return JSON.stringify(obj);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private appendString(obj: any, key: string, value: string | undefined): void {
|
|
57
|
+
if (value !== undefined) {
|
|
58
|
+
obj[key] = value === null ? null : value;
|
|
59
|
+
} else {
|
|
60
|
+
obj[key] = null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export const BleEvent = {
|
|
26
|
+
scanEvent: 'ScanEvent',
|
|
27
|
+
readEvent: 'ReadEvent',
|
|
28
|
+
stateChangeEvent: 'StateChangeEvent',
|
|
29
|
+
restoreStateEvent: 'RestoreStateEvent',
|
|
30
|
+
disconnectionEvent: 'DisconnectionEvent',
|
|
31
|
+
connectingEvent: 'ConnectingEvent',
|
|
32
|
+
connectedEvent: 'ConnectedEvent'
|
|
33
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { ValuesBucket } from '@kit.ArkData';
|
|
26
|
+
import { ble } from '@kit.ConnectivityKit';
|
|
27
|
+
import { buffer } from '@kit.ArkTS';
|
|
28
|
+
|
|
29
|
+
// Base64转ArrayBuffer
|
|
30
|
+
export function base64ToArrayBuffer(string: string): ArrayBuffer {
|
|
31
|
+
let dataArray = new Uint8Array(buffer.from(string, 'base64').buffer);
|
|
32
|
+
return dataArray.buffer;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ArrayBuffer转Base64
|
|
36
|
+
export function arrayBufferToBase64(arrayBuffer: ArrayBuffer): string {
|
|
37
|
+
let base64 = ''
|
|
38
|
+
let encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
39
|
+
let bytes = new Uint8Array(arrayBuffer)
|
|
40
|
+
let byteLength = bytes.byteLength
|
|
41
|
+
let byteRemainder = byteLength % 3
|
|
42
|
+
let mainLength = byteLength - byteRemainder
|
|
43
|
+
let a, b, c, d
|
|
44
|
+
let chunk
|
|
45
|
+
// Main loop deals with bytes in chunks of 3
|
|
46
|
+
for (let i = 0; i < mainLength; i = i + 3) {
|
|
47
|
+
// Combine the three bytes into a single integer
|
|
48
|
+
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
|
|
49
|
+
// Use bitmasks to extract 6-bit segments from the triplet
|
|
50
|
+
a = (chunk & 16515072) >> 18 // 16515072 = (2^6 - 1) << 18
|
|
51
|
+
b = (chunk & 258048) >> 12 // 258048 = (2^6 - 1) << 12
|
|
52
|
+
c = (chunk & 4032) >> 6 // 4032 = (2^6 - 1) << 6
|
|
53
|
+
d = chunk & 63 // 63 = 2^6 - 1
|
|
54
|
+
// Convert the raw binary segments to the appropriate ASCII encoding
|
|
55
|
+
base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
|
|
56
|
+
}
|
|
57
|
+
// Deal with the remaining bytes and padding
|
|
58
|
+
if (byteRemainder === 1) {
|
|
59
|
+
chunk = bytes[mainLength]
|
|
60
|
+
a = (chunk & 252) >> 2 // 252 = (2^6 - 1) << 2
|
|
61
|
+
// Set the 4 least significant bits to zero
|
|
62
|
+
b = (chunk & 3) << 4 // 3 = 2^2 - 1
|
|
63
|
+
base64 += encodings[a] + encodings[b] + '=='
|
|
64
|
+
} else if (byteRemainder === 2) {
|
|
65
|
+
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
|
|
66
|
+
a = (chunk & 64512) >> 10 // 64512 = (2^6 - 1) << 10
|
|
67
|
+
b = (chunk & 1008) >> 4 // 1008 = (2^6 - 1) << 4
|
|
68
|
+
// Set the 2 least significant bits to zero
|
|
69
|
+
c = (chunk & 15) << 2 // 15 = 2^4 - 1
|
|
70
|
+
base64 += encodings[a] + encodings[b] + encodings[c] + '='
|
|
71
|
+
}
|
|
72
|
+
return base64
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function scanResultToJsObjectConverter(scanResult: ble.ScanResult): ValuesBucket {
|
|
76
|
+
return {
|
|
77
|
+
id: scanResult.deviceId,
|
|
78
|
+
name: scanResult.deviceName,
|
|
79
|
+
rssi: scanResult.rssi,
|
|
80
|
+
isConnectable: scanResult.connectable,
|
|
81
|
+
manufacturerData: arrayBufferToBase64(scanResult.data),
|
|
82
|
+
localName: '',
|
|
83
|
+
rawScanRecord: '',
|
|
84
|
+
mtu: 0
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { IdGeneratorKey } from './IdGeneratorKey';
|
|
26
|
+
|
|
27
|
+
export class IdGenerator {
|
|
28
|
+
private static idMap = new Map<IdGeneratorKey, number>();
|
|
29
|
+
private static nextKey = 0;
|
|
30
|
+
|
|
31
|
+
static getIdForKey(idGeneratorKey: IdGeneratorKey): number {
|
|
32
|
+
var id: number = undefined;
|
|
33
|
+
this.idMap.forEach((value, key) => {
|
|
34
|
+
if (key.equals(idGeneratorKey)) {
|
|
35
|
+
id = value;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
if (id !== undefined) {
|
|
39
|
+
return id;
|
|
40
|
+
}
|
|
41
|
+
this.idMap.set(idGeneratorKey, ++this.nextKey);
|
|
42
|
+
return this.nextKey;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static clear(): void {
|
|
46
|
+
this.idMap.clear();
|
|
47
|
+
this.nextKey = 0;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export class IdGeneratorKey {
|
|
26
|
+
readonly deviceAddress: string;
|
|
27
|
+
readonly uuid: string;
|
|
28
|
+
readonly id: number;
|
|
29
|
+
|
|
30
|
+
constructor(deviceAddress: string, uuid: string, id: number) {
|
|
31
|
+
this.deviceAddress = deviceAddress;
|
|
32
|
+
this.uuid = uuid;
|
|
33
|
+
this.id = id;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
equals(other: any): boolean {
|
|
37
|
+
if (this === other) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
if (other === null || typeof other !== 'object' || !(other instanceof IdGeneratorKey)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const that = other as IdGeneratorKey;
|
|
45
|
+
|
|
46
|
+
if (this.id !== that.id) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
if (this.deviceAddress !== that.deviceAddress) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return this.uuid === that.uuid;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
hashCode(): number {
|
|
56
|
+
let result = this.stringHashCode(this.deviceAddress);
|
|
57
|
+
result = 31 * result + this.stringHashCode(this.uuid);
|
|
58
|
+
result = 31 * result + this.id;
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
stringHashCode(str: string): number {
|
|
63
|
+
let hash = 0;
|
|
64
|
+
for (let i = 0; i < str.length; i++) {
|
|
65
|
+
hash = ((hash << 5) - hash) + str.charCodeAt(i);
|
|
66
|
+
hash |= 0; // Convert to 32bit integer
|
|
67
|
+
}
|
|
68
|
+
return hash;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export class InstanceIdGenerator {
|
|
26
|
+
static instanceMap: Map<string, number> = new Map();
|
|
27
|
+
|
|
28
|
+
// 根据uuid生成instantId
|
|
29
|
+
static generateInstanceId(uuid: string): number {
|
|
30
|
+
let value = this.instanceMap.get(uuid);
|
|
31
|
+
if (value == null) {
|
|
32
|
+
value = new Date().getTime();
|
|
33
|
+
this.instanceMap.set(uuid, value);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
}
|