@react-native-ohos/react-native-ble-plx 3.5.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.
Files changed (54) hide show
  1. package/LICENSE +202 -0
  2. package/README.OpenSource +11 -0
  3. package/README.md +9 -0
  4. package/app.plugin.js +1 -0
  5. package/harmony/rn_bleplx/BuildProfile.ets +9 -0
  6. package/harmony/rn_bleplx/build-profile.json5 +8 -0
  7. package/harmony/rn_bleplx/hvigorfile.ts +6 -0
  8. package/harmony/rn_bleplx/index.ets +5 -0
  9. package/harmony/rn_bleplx/obfuscation-rules.txt +18 -0
  10. package/harmony/rn_bleplx/oh-package.json5 +13 -0
  11. package/harmony/rn_bleplx/src/main/cpp/CMakeLists.txt +9 -0
  12. package/harmony/rn_bleplx/src/main/cpp/generated/BlePlx.cpp +64 -0
  13. package/harmony/rn_bleplx/src/main/cpp/generated/BlePlx.h +21 -0
  14. package/harmony/rn_bleplx/src/main/cpp/generated/BlePlxRNOHGeneratedPackage.h +76 -0
  15. package/harmony/rn_bleplx/src/main/ets/BleDevice.ts +79 -0
  16. package/harmony/rn_bleplx/src/main/ets/BleModule.ts +1208 -0
  17. package/harmony/rn_bleplx/src/main/ets/BlePlxInterface.ts +7 -0
  18. package/harmony/rn_bleplx/src/main/ets/BlePlxModule.ts +226 -0
  19. package/harmony/rn_bleplx/src/main/ets/BlePlxPackage.ts +26 -0
  20. package/harmony/rn_bleplx/src/main/ets/Characteristic.ts +159 -0
  21. package/harmony/rn_bleplx/src/main/ets/CommonConstants.ts +10 -0
  22. package/harmony/rn_bleplx/src/main/ets/Descriptor.ts +123 -0
  23. package/harmony/rn_bleplx/src/main/ets/Service.ts +65 -0
  24. package/harmony/rn_bleplx/src/main/ets/common/BleError.ts +70 -0
  25. package/harmony/rn_bleplx/src/main/ets/common/BleErrorToJsObjectConverter.ts +43 -0
  26. package/harmony/rn_bleplx/src/main/ets/common/BleEvent.ts +13 -0
  27. package/harmony/rn_bleplx/src/main/ets/common/BleUtils.ts +66 -0
  28. package/harmony/rn_bleplx/src/main/ets/common/IdGenerator.ts +29 -0
  29. package/harmony/rn_bleplx/src/main/ets/common/IdGeneratorKey.ts +50 -0
  30. package/harmony/rn_bleplx/src/main/ets/common/InstanceIdGenerator.ts +17 -0
  31. package/harmony/rn_bleplx/src/main/ets/common/Logger.ts +64 -0
  32. package/harmony/rn_bleplx/src/main/ets/common/PermissionHandler.ts +78 -0
  33. package/harmony/rn_bleplx/src/main/ets/common/ServiceFactory.ts +17 -0
  34. package/harmony/rn_bleplx/src/main/ets/generated/components/ts.ts +8 -0
  35. package/harmony/rn_bleplx/src/main/ets/generated/index.ets +8 -0
  36. package/harmony/rn_bleplx/src/main/ets/generated/ts.ts +9 -0
  37. package/harmony/rn_bleplx/src/main/ets/generated/turboModules/BlePlx.ts +105 -0
  38. package/harmony/rn_bleplx/src/main/ets/generated/turboModules/ts.ts +8 -0
  39. package/harmony/rn_bleplx/src/main/module.json5 +7 -0
  40. package/harmony/rn_bleplx/ts.ts +6 -0
  41. package/harmony/rn_bleplx.har +0 -0
  42. package/package.json +181 -0
  43. package/src/BleError.js +555 -0
  44. package/src/BleManager.js +1324 -0
  45. package/src/BleModule.js +857 -0
  46. package/src/Characteristic.js +180 -0
  47. package/src/Descriptor.js +83 -0
  48. package/src/Device.js +378 -0
  49. package/src/NativeBlePlx.ts +101 -0
  50. package/src/Service.js +204 -0
  51. package/src/TypeDefinition.js +365 -0
  52. package/src/Utils.js +29 -0
  53. package/src/index.d.ts +2126 -0
  54. package/src/index.js +20 -0
@@ -0,0 +1,7 @@
1
+ // Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
2
+ // Use of this source code is governed by a Apache-2.0 license that can be
3
+ // found in the LICENSE file.
4
+
5
+ export interface BlePlxInterface {
6
+ dispatchEvent(name: string, value: any);
7
+ }
@@ -0,0 +1,226 @@
1
+ // Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
2
+ // Use of this source code is governed by a Apache-2.0 license that can be
3
+ // found in the LICENSE file.
4
+
5
+ import { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
6
+ import { TM } from './generated/ts';
7
+ import { BleClientManager } from './BleModule'
8
+ import { BlePlxInterface } from './BlePlxInterface'
9
+ import { ValueType } from '@kit.ArkData';
10
+ import { PermissionHandler } from './common/PermissionHandler'
11
+
12
+ export class BlePlxModule extends TurboModule implements TM.BlePlx.Spec, BlePlxInterface {
13
+ static NAME = 'BlePlx';
14
+
15
+ private manager: BleClientManager;
16
+
17
+ constructor(ctx: TurboModuleContext) {
18
+ super(ctx);
19
+
20
+ let permissionHandler = new PermissionHandler();
21
+ permissionHandler.checkPermission('ohos.permission.ACCESS_BLUETOOTH', ctx.uiAbilityContext);
22
+ }
23
+
24
+ public dispatchEvent(name: string, value: any) {
25
+ this.ctx.rnInstance.emitDeviceEvent(name, value);
26
+ }
27
+
28
+ addListener(eventName: string): void {
29
+ // Keep: Required for RN built in Event Emitter Calls.
30
+ }
31
+
32
+ removeListeners(count: number): void {
33
+ // Keep: Required for RN built in Event Emitter Calls.
34
+ }
35
+
36
+ createClient(restoreStateIdentifier?: string): void {
37
+ this.manager = new BleClientManager(restoreStateIdentifier);
38
+ this.manager.delegate = this;
39
+ }
40
+
41
+ destroyClient(): Promise<void> {
42
+ this.manager.invalidate();
43
+ this.manager = null;
44
+ return Promise.resolve();
45
+ }
46
+
47
+ cancelTransaction(transactionId: string): Promise<void> {
48
+ return this.manager.cancelTransaction(transactionId);
49
+ }
50
+
51
+ setLogLevel(logLevel: string): Promise<Object> {
52
+ return this.manager.setLogLevel(logLevel);
53
+ }
54
+
55
+ logLevel(): Promise<Object> {
56
+ return this.manager.getLogLevel();
57
+ }
58
+
59
+ enable(transactionId: string): Promise<void> {
60
+ return this.manager.enable(transactionId);
61
+ }
62
+
63
+ disable(transactionId: string): Promise<void> {
64
+ return this.manager.disable(transactionId);
65
+ }
66
+
67
+ state(): Promise<Object> {
68
+ return this.manager.state();
69
+ }
70
+
71
+ startDeviceScan(filteredUUIDs: string[], options: Object): Promise<void> {
72
+ return this.manager.startDeviceScan(filteredUUIDs, options);
73
+ }
74
+
75
+ stopDeviceScan(): Promise<void> {
76
+ return this.manager.stopDeviceScan();
77
+ }
78
+
79
+ requestConnectionPriorityForDevice(deviceId: string, connectionPriority: number,
80
+ transactionId: string): Promise<Object> {
81
+ return this.manager.requestConnectionPriorityForDevice(deviceId, connectionPriority, transactionId);
82
+ }
83
+
84
+ readRSSIForDevice(deviceId: string, transactionId: string): Promise<Object> {
85
+ return this.manager.readRSSIForDevice(deviceId, transactionId);
86
+ }
87
+
88
+ requestMTUForDevice(deviceId: string, mtu: number, transactionId: string): Promise<Object> {
89
+ return this.manager.requestMTUForDevice(deviceId, mtu, transactionId);
90
+ }
91
+
92
+ devices(deviceIdentifiers: string[]): Promise<Object[]> {
93
+ return this.manager.devices(deviceIdentifiers);
94
+ }
95
+
96
+ connectedDevices(serviceUUIDs: string[]): Promise<Object[]> {
97
+ return this.manager.getConnectedDevices(serviceUUIDs);
98
+ }
99
+
100
+ connectToDevice(deviceId: string, options?: Object): Promise<Object> {
101
+ return this.manager.connectToDevice(deviceId, options as Map<string, ValueType>);
102
+ }
103
+
104
+ cancelDeviceConnection(deviceId: string): Promise<Object> {
105
+ return this.manager.cancelDeviceConnection(deviceId);
106
+ }
107
+
108
+ isDeviceConnected(deviceId: string): Promise<boolean> {
109
+ return this.manager.isDeviceConnected(deviceId);
110
+ }
111
+
112
+ discoverAllServicesAndCharacteristicsForDevice(deviceId: string, transactionId: string): Promise<Object> {
113
+ return this.manager.discoverAllServicesAndCharacteristicsForDevice(deviceId, transactionId);
114
+ }
115
+
116
+ servicesForDevice(deviceId: string): Promise<Object[]> {
117
+ return this.manager.servicesForDevice(deviceId);
118
+ }
119
+
120
+ characteristicsForDevice(deviceId: string, serviceUUID: string): Promise<Object[]> {
121
+ return this.manager.characteristicsForDevice(deviceId, serviceUUID);
122
+ }
123
+
124
+ characteristicsForService(serviceIdentifier: number): Promise<Object[]> {
125
+ return this.manager.characteristicsForService(serviceIdentifier);
126
+ }
127
+
128
+ descriptorsForDevice(deviceId: string, serviceUUID: string, characteristicUUID: string): Promise<Object[]> {
129
+ return this.manager.descriptorsForDevice(deviceId, serviceUUID, characteristicUUID);
130
+ }
131
+
132
+ descriptorsForService(serviceIdentifier: number, characteristicUUID: string): Promise<Object[]> {
133
+ return this.manager.descriptorsForService(serviceIdentifier, characteristicUUID);
134
+ }
135
+
136
+ descriptorsForCharacteristic(characteristicIdentifier: number): Promise<Object[]> {
137
+ return this.manager.descriptorsForCharacteristic(characteristicIdentifier);
138
+ }
139
+
140
+ readCharacteristicForDevice(deviceId: string, serviceUUID: string, characteristicUUID: string,
141
+ transactionId: string): Promise<Object> {
142
+ return this.manager.readCharacteristicForDevice(deviceId, serviceUUID, characteristicUUID, transactionId);
143
+ }
144
+
145
+ readCharacteristicForService(serviceIdentifier: number, characteristicUUID: string,
146
+ transactionId: string): Promise<Object> {
147
+ return this.manager.readCharacteristicForService(serviceIdentifier, characteristicUUID, transactionId);
148
+ }
149
+
150
+ readCharacteristic(characteristicIdentifier: number, transactionId: string): Promise<Object> {
151
+ return this.manager.readCharacteristic(characteristicIdentifier, transactionId);
152
+ }
153
+
154
+ writeCharacteristicForDevice(deviceId: string, serviceUUID: string, characteristicUUID: string, valueBase64: string,
155
+ response: boolean, transactionId: string): Promise<Object> {
156
+ return this.manager.writeCharacteristicForDevice(deviceId, serviceUUID, characteristicUUID, valueBase64, response,
157
+ transactionId);
158
+ }
159
+
160
+ writeCharacteristicForService(serviceIdentifier: number, characteristicUUID: string, valueBase64: string,
161
+ response: boolean, transactionId: string): Promise<Object> {
162
+ return this.manager.writeCharacteristicForService(serviceIdentifier, characteristicUUID, valueBase64, response,
163
+ transactionId);
164
+ }
165
+
166
+ writeCharacteristic(characteristicIdentifier: number, valueBase64: string, response: boolean,
167
+ transactionId: string): Promise<Object> {
168
+ return this.manager.writeCharacteristic(characteristicIdentifier, valueBase64, response, transactionId);
169
+ }
170
+
171
+ monitorCharacteristicForDevice(deviceId: string, serviceUUID: string, characteristicUUID: string,
172
+ transactionId: string, subscriptionType?: string): Promise<void> {
173
+ return this.manager.monitorCharacteristicForDevice(deviceId, serviceUUID, characteristicUUID, transactionId, subscriptionType);
174
+ }
175
+
176
+ monitorCharacteristicForService(serviceIdentifier: number, characteristicUUID: string,
177
+ transactionId: string, subscriptionType?: string): Promise<void> {
178
+ return this.manager.monitorCharacteristicForService(serviceIdentifier, characteristicUUID, transactionId, subscriptionType);
179
+ }
180
+
181
+ monitorCharacteristic(characteristicIdentifier: number, transactionId: string, subscriptionType?: string): Promise<void> {
182
+ return this.manager.monitorCharacteristic(characteristicIdentifier, transactionId, subscriptionType);
183
+ }
184
+
185
+ readDescriptorForDevice(deviceId: string, serviceUUID: string, characteristicUUID: string, descriptorUUID: string,
186
+ transactionId: string): Promise<Object> {
187
+ return this.manager.readDescriptorForDevice(deviceId, serviceUUID, characteristicUUID, descriptorUUID,
188
+ transactionId);
189
+ }
190
+
191
+ readDescriptorForService(serviceIdentifier: number, characteristicUUID: string, descriptorUUID: string,
192
+ transactionId: string): Promise<Object> {
193
+ return this.manager.readDescriptorForService(serviceIdentifier, characteristicUUID, descriptorUUID, transactionId);
194
+ }
195
+
196
+ readDescriptorForCharacteristic(characteristicIdentifier: number, descriptorUUID: string,
197
+ transactionId: string): Promise<Object> {
198
+ return this.manager.readDescriptorForCharacteristic(characteristicIdentifier, descriptorUUID, transactionId);
199
+ }
200
+
201
+ readDescriptor(descriptorIdentifier: number, transactionId: string): Promise<Object> {
202
+ return this.manager.readDescriptor(descriptorIdentifier, transactionId);
203
+ }
204
+
205
+ writeDescriptorForDevice(deviceId: string, serviceUUID: string, characteristicUUID: string, descriptorUUID: string,
206
+ valueBase64: string, transactionId: string): Promise<Object> {
207
+ return this.manager.writeDescriptorForDevice(deviceId, serviceUUID, characteristicUUID, descriptorUUID, valueBase64,
208
+ transactionId);
209
+ }
210
+
211
+ writeDescriptorForService(serviceIdentifier: number, characteristicUUID: string, descriptorUUID: string,
212
+ valueBase64: string, transactionId: string): Promise<Object> {
213
+ return this.manager.writeDescriptorForService(serviceIdentifier, characteristicUUID, descriptorUUID, valueBase64,
214
+ transactionId);
215
+ }
216
+
217
+ writeDescriptorForCharacteristic(characteristicIdentifier: number, descriptorUUID: string, valueBase64: string,
218
+ transactionId: string): Promise<Object> {
219
+ return this.manager.writeDescriptorForCharacteristic(characteristicIdentifier, descriptorUUID, valueBase64,
220
+ transactionId);
221
+ }
222
+
223
+ writeDescriptor(descriptorIdentifier: number, valueBase64: string, transactionId: string): Promise<Object> {
224
+ return this.manager.writeDescriptor(descriptorIdentifier, valueBase64, transactionId);
225
+ }
226
+ }
@@ -0,0 +1,26 @@
1
+ // Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
2
+ // Use of this source code is governed by a Apache-2.0 license that can be
3
+ // found in the LICENSE file.
4
+
5
+ import { RNPackage, TurboModulesFactory } from '@rnoh/react-native-openharmony/ts';
6
+ import type { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
7
+ import { BlePlxModule } from './BlePlxModule';
8
+
9
+ class BlePlxModulesFactory extends TurboModulesFactory {
10
+ createTurboModule(name: string): TurboModule | null {
11
+ if (name == 'BlePlx') {
12
+ return new BlePlxModule(this.ctx);
13
+ }
14
+ return null;
15
+ }
16
+
17
+ hasTurboModule(name: string): boolean {
18
+ return name == 'BlePlx';
19
+ }
20
+ }
21
+
22
+ export class BlePlxPackage extends RNPackage {
23
+ createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory {
24
+ return new BlePlxModulesFactory(ctx);
25
+ }
26
+ }
@@ -0,0 +1,159 @@
1
+ // Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
2
+ // Use of this source code is governed by a Apache-2.0 license that can be
3
+ // found in the LICENSE file.
4
+
5
+ import ble from '@ohos.bluetooth.ble';
6
+ import Logger from './common/Logger';
7
+ import { Service } from './Service';
8
+ import { arrayBufferToBase64 } from './common/BleUtils'
9
+ import { Descriptor } from './Descriptor'
10
+ import { IdGenerator } from './common/IdGenerator';
11
+ import { IdGeneratorKey } from './common/IdGeneratorKey';
12
+ import { ValuesBucket } from '@kit.ArkData';
13
+ import { InstanceIdGenerator } from './common/InstanceIdGenerator';
14
+
15
+ export class Characteristic {
16
+ private id: number;
17
+ private serviceID: number;
18
+ private serviceUUID: string;
19
+ private deviceID: string;
20
+ private value: ArrayBuffer;
21
+ public gattCharacteristic: ble.BLECharacteristic;
22
+
23
+ public writeType: ble.GattWriteType;
24
+
25
+ public setValue(value: ArrayBuffer) {
26
+ this.value = value;
27
+ }
28
+
29
+ constructor(id: number, deviceID: string, serviceID: number, serviceUUID: string,
30
+ gattCharacteristic: ble.BLECharacteristic) {
31
+ this.id = id;
32
+ this.deviceID = deviceID;
33
+ this.serviceUUID = serviceUUID;
34
+ this.serviceID = serviceID;
35
+ this.gattCharacteristic = gattCharacteristic;
36
+ }
37
+
38
+ static constructorWithId(id: number, service: Service, gattCharacteristic: ble.BLECharacteristic): Characteristic {
39
+ return new Characteristic(id, service.getDeviceID(), service.getId(), service.getUuid(), gattCharacteristic);
40
+ }
41
+
42
+ static constructorWithNative(service: Service, gattCharacteristic: ble.BLECharacteristic): Characteristic {
43
+ let id: number =
44
+ IdGenerator.getIdForKey(new IdGeneratorKey(service.getDeviceID(), gattCharacteristic.characteristicUuid,
45
+ InstanceIdGenerator.generateInstanceId(gattCharacteristic.characteristicUuid)))
46
+ return new Characteristic(id, service.getDeviceID(), service.getId(), service.getUuid(), gattCharacteristic);
47
+ }
48
+
49
+ static constructorWithOther(other: Characteristic): Characteristic {
50
+ return new Characteristic(other.id, other.deviceID, other.serviceID, other.serviceUUID, other.gattCharacteristic);
51
+ }
52
+
53
+ public getId(): number {
54
+ return this.id;
55
+ }
56
+
57
+ public getUuid(): string {
58
+ return this.gattCharacteristic.characteristicUuid;
59
+ }
60
+
61
+ public getServiceID(): number {
62
+ return this.serviceID;
63
+ }
64
+
65
+ public getServiceUUID(): string {
66
+ return this.serviceUUID;
67
+ }
68
+
69
+ public getDeviceId(): string {
70
+ return this.deviceID;
71
+ }
72
+
73
+ public getInstanceId(): number {
74
+ return InstanceIdGenerator.generateInstanceId(this.gattCharacteristic.characteristicUuid);
75
+ }
76
+
77
+ public getGattDescriptor(uuid: string): ble.BLEDescriptor | null {
78
+ let descriptor: ble.BLEDescriptor = null;
79
+ this.gattCharacteristic.descriptors.forEach(value => {
80
+ if (value.descriptorUuid == uuid) {
81
+ descriptor = value;
82
+ }
83
+ })
84
+
85
+ return descriptor;
86
+ }
87
+
88
+ public setWriteType(writeType: number) {
89
+ this.writeType = writeType;
90
+ }
91
+
92
+ public isReadable(): boolean {
93
+ return this.gattCharacteristic.properties.read ?? false;
94
+ }
95
+
96
+ public isWritableWithResponse(): boolean {
97
+ return this.gattCharacteristic.properties.write ?? false;
98
+ }
99
+
100
+ public isWritableWithoutResponse(): boolean {
101
+ return this.gattCharacteristic.properties.writeNoResponse ?? false;
102
+ }
103
+
104
+ public isNotifiable(): boolean {
105
+ return this.gattCharacteristic.properties.notify ?? false;
106
+ }
107
+
108
+ public getDescriptors(): Descriptor[] {
109
+ var descriptors: Descriptor[] = []
110
+ this.gattCharacteristic.descriptors.forEach(value => {
111
+ let obj = Descriptor.constructorWithNative(this, value);
112
+ descriptors.push(obj);
113
+ });
114
+ return descriptors;
115
+ }
116
+
117
+ public isNotifying(): boolean {
118
+ return false;
119
+ }
120
+
121
+ public isIndicatable(): boolean {
122
+ return this.gattCharacteristic.properties.indicate;
123
+ }
124
+
125
+ public getValue(): ArrayBuffer {
126
+ return this.value;
127
+ }
128
+
129
+ public getDescriptorByUUID(uuid: string): Descriptor {
130
+ let descriptor: Descriptor = null;
131
+ this.gattCharacteristic.descriptors.forEach(value => {
132
+ if (value.descriptorUuid == uuid) {
133
+ descriptor = Descriptor.constructorWithNative(this, value);
134
+ }
135
+ })
136
+ return descriptor;
137
+ }
138
+
139
+ public logValue(message: string, value: ArrayBuffer) {
140
+ Logger.debug(message);
141
+ }
142
+
143
+ public asJSObject(): ValuesBucket {
144
+ return {
145
+ "id": this.getId(),
146
+ "uuid": this.getUuid(),
147
+ "deviceID": this.getDeviceId(),
148
+ "serviceID": this.getServiceID(),
149
+ "serviceUUID": this.getServiceUUID(),
150
+ "isReadable": this.isReadable(),
151
+ "isWritableWithResponse": this.isWritableWithResponse(),
152
+ "isWritableWithoutResponse": this.isWritableWithoutResponse(),
153
+ "isNotifiable": this.isNotifiable(),
154
+ "isNotifying": this.isNotifying(),
155
+ "isIndicatable": this.isIndicatable(),
156
+ "value": arrayBufferToBase64(this.value),
157
+ };
158
+ }
159
+ }
@@ -0,0 +1,10 @@
1
+ // Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
2
+ // Use of this source code is governed by a Apache-2.0 license that can be
3
+ // found in the LICENSE file.
4
+
5
+ export default class CommonConstants {
6
+ /**
7
+ * Log tag.
8
+ */
9
+ static readonly TAG = '[RNOH] BlePlx';
10
+ }
@@ -0,0 +1,123 @@
1
+ // Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
2
+ // Use of this source code is governed by a Apache-2.0 license that can be
3
+ // found in the LICENSE file.
4
+
5
+ import ble from '@ohos.bluetooth.ble';
6
+ import Logger from './common/Logger'
7
+ import { Characteristic } from './Characteristic';
8
+ import { IdGenerator } from './common/IdGenerator';
9
+ import { IdGeneratorKey } from './common/IdGeneratorKey';
10
+ import { arrayBufferToBase64 } from './common/BleUtils';
11
+ import { ValuesBucket } from '@kit.ArkData';
12
+
13
+ export class Descriptor {
14
+ private characteristicId: number;
15
+ private serviceId: number;
16
+ private characteristicUuid: string;
17
+ private serviceUuid: string;
18
+ private deviceId: string;
19
+ private descriptor: ble.BLEDescriptor;
20
+ private id: number;
21
+ private uuid: string;
22
+ private value: ArrayBuffer;
23
+
24
+ constructor(id: number,
25
+ uuid: string,
26
+ value: ArrayBuffer,
27
+ deviceId: string,
28
+ serviceId: number,
29
+ serviceUuid: string,
30
+ characteristicId: number,
31
+ characteristicUuid: string,
32
+ descriptor: ble.BLEDescriptor) {
33
+ this.id = id;
34
+ this.uuid = uuid;
35
+ this.value = value;
36
+ this.deviceId = deviceId;
37
+ this.serviceId = serviceId;
38
+ this.serviceUuid = serviceUuid;
39
+ this.characteristicId = characteristicId;
40
+ this.characteristicUuid = characteristicUuid;
41
+ this.descriptor = descriptor;
42
+ }
43
+
44
+ static constructorWithNative(characteristic: Characteristic, gattDescriptor: ble.BLEDescriptor) {
45
+ let id = IdGenerator.getIdForKey(new IdGeneratorKey(characteristic.getDeviceId(), gattDescriptor.descriptorUuid,
46
+ characteristic.getId()));
47
+ return new Descriptor(id, gattDescriptor.descriptorUuid, gattDescriptor.descriptorValue,
48
+ characteristic.getDeviceId(), characteristic.getServiceID(), characteristic.getServiceUUID(),
49
+ characteristic.getId(), characteristic.getUuid(), gattDescriptor)
50
+ }
51
+
52
+ static constructorWithId(characteristicId: number, serviceId: number, characteristicUuid: string, serviceUuid: string,
53
+ deviceId: string, descriptor: ble.BLEDescriptor, id: number, uuid: string) {
54
+ return new Descriptor(id, uuid, descriptor.descriptorValue, deviceId, serviceId, serviceUuid, characteristicId,
55
+ characteristicUuid, descriptor)
56
+ }
57
+
58
+ static constructorWithOther(other: Descriptor) {
59
+ return new Descriptor(other.id, other.uuid, other.value, other.deviceId, other.serviceId, other.serviceUuid,
60
+ other.characteristicId, other.characteristicUuid, other.descriptor)
61
+ }
62
+
63
+ public getId(): number {
64
+ return this.id;
65
+ }
66
+
67
+ public getDeviceId(): string {
68
+ return this.deviceId;
69
+ }
70
+
71
+ public getCharacteristicId(): number {
72
+ return this.characteristicId;
73
+ }
74
+
75
+ public getServiceId(): number {
76
+ return this.serviceId;
77
+ }
78
+
79
+ public getCharacteristicUuid(): string {
80
+ return this.characteristicUuid;
81
+ }
82
+
83
+ public getServiceUuid(): string {
84
+ return this.serviceUuid;
85
+ }
86
+
87
+ public getUuid(): string {
88
+ return this.uuid;
89
+ }
90
+
91
+ public getValue(): ArrayBuffer {
92
+ return this.value;
93
+ }
94
+
95
+ public setValue(value: ArrayBuffer) {
96
+ this.value = value;
97
+ }
98
+
99
+ public setValueFromCache() {
100
+ this.value = this.descriptor.descriptorValue;
101
+ }
102
+
103
+ public getNativeDescriptor(): ble.BLEDescriptor {
104
+ return this.descriptor;
105
+ }
106
+
107
+ public logValue(message: string, value: ArrayBuffer) {
108
+ Logger.debug(message);
109
+ }
110
+
111
+ public asJSObject(): ValuesBucket {
112
+ return {
113
+ "id": this.getId(),
114
+ "uuid": this.getUuid(),
115
+ "deviceID": this.getDeviceId(),
116
+ "serviceID": this.getServiceId(),
117
+ "serviceUUID": this.getServiceUuid(),
118
+ "characteristicUUID": this.getCharacteristicUuid(),
119
+ "characteristicID": this.getCharacteristicId(),
120
+ "value": arrayBufferToBase64(this.value),
121
+ };
122
+ }
123
+ }
@@ -0,0 +1,65 @@
1
+ // Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
2
+ // Use of this source code is governed by a Apache-2.0 license that can be
3
+ // found in the LICENSE file.
4
+
5
+ import ble from '@ohos.bluetooth.ble';
6
+ import Logger from './common/Logger';
7
+ import { Characteristic } from './Characteristic';
8
+ import { ValuesBucket } from '@kit.ArkData';
9
+
10
+ export class Service {
11
+ private id: number;
12
+ private deviceID: string;
13
+ private btGattService: ble.GattService;
14
+
15
+ constructor(id: number, deviceID: string, btGattService: ble.GattService) {
16
+ this.id = id;
17
+ this.deviceID = deviceID;
18
+ this.btGattService = btGattService;
19
+ }
20
+
21
+ public getId(): number {
22
+ return this.id;
23
+ }
24
+
25
+ public getUuid(): string {
26
+ return this.btGattService.serviceUuid;
27
+ }
28
+
29
+ public getDeviceID(): string {
30
+ return this.deviceID;
31
+ }
32
+
33
+ public isPrimary(): boolean {
34
+ return this.btGattService.isPrimary;
35
+ }
36
+
37
+ public getCharacteristicByUUID(uuid: string): Characteristic | null {
38
+ var characteristic: Characteristic = null;
39
+ this.btGattService.characteristics.forEach(value => {
40
+ if (value.characteristicUuid == uuid) {
41
+ characteristic = Characteristic.constructorWithNative(this, value);
42
+ }
43
+ })
44
+
45
+ return characteristic;
46
+ }
47
+
48
+ public getCharacteristics(): Characteristic[] {
49
+ var characteristics: Characteristic[] = []
50
+ this.btGattService.characteristics.forEach(value => {
51
+ let obj = Characteristic.constructorWithNative(this, value);
52
+ characteristics.push(obj);
53
+ });
54
+ return characteristics;
55
+ }
56
+
57
+ public asJSObject(): ValuesBucket {
58
+ return {
59
+ "id": this.getId(),
60
+ "uuid": this.getUuid(),
61
+ "deviceID": this.getDeviceID(),
62
+ "isPrimary": this.isPrimary(),
63
+ };
64
+ }
65
+ }
@@ -0,0 +1,70 @@
1
+ // Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
2
+ // Use of this source code is governed by a Apache-2.0 license that can be
3
+ // found in the LICENSE file.
4
+
5
+ export const BleErrorCode = {
6
+ UnknownError: 0,
7
+ BluetoothManagerDestroyed: 1,
8
+ OperationCancelled: 2,
9
+ OperationTimedOut: 3,
10
+ OperationStartFailed: 4,
11
+ InvalidIdentifiers: 5,
12
+ BluetoothUnsupported: 100,
13
+ BluetoothUnauthorized: 101,
14
+ BluetoothPoweredOff: 102,
15
+ BluetoothInUnknownState: 103,
16
+ BluetoothResetting: 104,
17
+ BluetoothStateChangeFailed: 105,
18
+ DeviceConnectionFailed: 200,
19
+ DeviceDisconnected: 201,
20
+ DeviceRSSIReadFailed: 202,
21
+ DeviceAlreadyConnected: 203,
22
+ DeviceNotFound: 204,
23
+ DeviceNotConnected: 205,
24
+ DeviceMTUChangeFailed: 206,
25
+ ServicesDiscoveryFailed: 300,
26
+ IncludedServicesDiscoveryFailed: 301,
27
+ ServiceNotFound: 302,
28
+ ServicesNotDiscovered: 303,
29
+ CharacteristicsDiscoveryFailed: 400,
30
+ CharacteristicWriteFailed: 401,
31
+ CharacteristicReadFailed: 402,
32
+ CharacteristicNotifyChangeFailed: 403,
33
+ CharacteristicNotFound: 404,
34
+ CharacteristicsNotDiscovered: 405,
35
+ CharacteristicInvalidDataFormat: 406,
36
+ DescriptorsDiscoveryFailed: 500,
37
+ DescriptorWriteFailed: 501,
38
+ DescriptorReadFailed: 502,
39
+ DescriptorNotFound: 503,
40
+ DescriptorsNotDiscovered: 504,
41
+ DescriptorInvalidDataFormat: 505,
42
+ DescriptorWriteNotAllowed: 506,
43
+ ScanStartFailed: 600,
44
+ LocationServicesDisabled: 601,
45
+ };
46
+
47
+ export class BleError extends Error {
48
+ public errorCode: number;
49
+ public harmonyCode?: number;
50
+ public reason: string;
51
+ public deviceID?: string;
52
+ public serviceUUID?: string;
53
+ public characteristicUUID?: string;
54
+ public descriptorUUID?: string;
55
+ public internalMessage?: string;
56
+
57
+ constructor(errorCode: number, reason: string, harmonyCode?: number) {
58
+ super(`Error code: ${errorCode}, harmony code: ${harmonyCode}, reason: ${reason}`);
59
+ this.errorCode = errorCode;
60
+ this.reason = reason;
61
+ this.harmonyCode = harmonyCode;
62
+ }
63
+
64
+ getMessage(): string {
65
+ return `Error code: ${this.errorCode}, harmony code: ${this.harmonyCode}, reason: ${this.reason}, ` +
66
+ `deviceId: ${this.deviceID}, serviceUuid: ${this.serviceUUID}, ` +
67
+ `characteristicUuid: ${this.characteristicUUID}, descriptorUuid: ${this.descriptorUUID}, ` +
68
+ `internalMessage: ${this.internalMessage}`;
69
+ }
70
+ }