@iotize/device-com-ble.cordova 3.6.2-alpha.2 → 3.6.3
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 -174
- package/bundles/iotize-device-com-ble.cordova.umd.js.map +1 -1
- package/bundles/iotize-device-com-ble.cordova.umd.min.js.map +1 -1
- package/esm2015/lib/ble-com-protocol.js.map +1 -1
- package/esm2015/lib/cordova-ble-error.js.map +1 -1
- package/esm2015/lib/cordova-interface.js.map +1 -1
- package/esm2015/lib/cordova-service-adapter.js.map +1 -1
- package/esm2015/lib/definitions.js.map +1 -1
- package/esm2015/lib/iotize-ble-cordova-plugin.js.map +1 -1
- package/esm2015/lib/logger.js.map +1 -1
- package/esm2015/lib/scanner.js.map +1 -1
- package/esm2015/lib/utility.js.map +1 -1
- package/esm2015/public_api.js.map +1 -1
- package/fesm2015/iotize-device-com-ble.cordova.js.map +1 -1
- package/package.json +1 -1
- package/plugin.xml +68 -68
- 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 +27 -27
- package/src/android/src/ble/BLECom.java +5 -3
- package/src/android/src/ble/BLEComError.java +119 -119
- package/src/android/src/ble/BLEProtocol.java +874 -874
- package/src/android/src/ble/BLEScanner.java +283 -283
- package/src/android/src/ble/JSONBuilder.java +83 -83
- package/src/android/src/ble/PluginResponse.java +129 -129
- package/src/android/src/ble/TapUtility.java +26 -26
- package/src/android/src/ble/characteristics/AbstractLwM2MRequestCharacteristic.java +48 -48
- package/src/android/src/ble/characteristics/CharacteristicAdapter.java +221 -221
- package/src/android/src/ble/characteristics/CharacteristicAdapterCallbacks.java +21 -21
- package/src/android/src/ble/characteristics/LwM2M20BytesRequestCharacteristicAdapter.java +161 -161
- package/src/android/src/ble/characteristics/LwM2MMTURequestCharacteristicAdapter.java +134 -134
- package/src/android/src/ble/commands/BLECommand.java +18 -18
- package/src/android/src/ble/commands/CharacteristicOperationCompletedNotifier.java +37 -37
- package/src/android/src/ble/commands/CommandCompletedNotifier.java +10 -10
- package/src/android/src/ble/commands/DescriptorOperationCompletedNotifier.java +36 -36
- package/src/android/src/ble/commands/GattOperationCompletedNotifier.java +28 -28
- package/src/ios/BLECom.swift +744 -732
- package/src/ios/BLEManager.swift +41 -23
- package/src/ios/BLETapPeripheral.swift +5 -10
- package/src/ios/CBPeripheralConverter.swift +164 -139
- package/src/ios/Queue.swift +114 -108
- package/src/windows/IoTizeBLE.pdb +0 -0
- package/src/windows/IoTizeBLE.pri +0 -0
- package/src/windows/iotize-ble-com.js +159 -159
- package/www/plugin.js +1 -1
- 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/{4.8.1 → 6.8.2}/fileChanges/last-build.bin +0 -0
|
@@ -1,221 +1,221 @@
|
|
|
1
|
-
package com.iotize.plugin.cordova.ble.characteristics;
|
|
2
|
-
|
|
3
|
-
import android.bluetooth.BluetoothGatt;
|
|
4
|
-
import android.bluetooth.BluetoothGattCharacteristic;
|
|
5
|
-
import android.bluetooth.BluetoothGattDescriptor;
|
|
6
|
-
import android.os.Build;
|
|
7
|
-
import android.util.Log;
|
|
8
|
-
|
|
9
|
-
import androidx.annotation.NonNull;
|
|
10
|
-
import androidx.annotation.Nullable;
|
|
11
|
-
import androidx.annotation.RequiresApi;
|
|
12
|
-
|
|
13
|
-
import com.iotize.android.communication.protocol.ble.BLECommandRunnable;
|
|
14
|
-
import com.iotize.android.communication.protocol.ble.exception.CannotSetCharacteristicNotification;
|
|
15
|
-
import com.iotize.android.communication.protocol.ble.exception.CannotWriteCharacteristicException;
|
|
16
|
-
import com.iotize.android.communication.protocol.ble.exception.CharacteristicNotificationNotSupported;
|
|
17
|
-
import com.iotize.android.communication.protocol.ble.exception.DescriptorNotAvailableException;
|
|
18
|
-
import com.iotize.android.communication.protocol.ble.exception.WritePacketIsTooBigException;
|
|
19
|
-
import com.iotize.android.core.util.Helper;
|
|
20
|
-
import com.iotize.plugin.cordova.ble.BLEProtocol;
|
|
21
|
-
import com.iotize.plugin.cordova.ble.commands.BLECommand;
|
|
22
|
-
import com.iotize.plugin.cordova.ble.commands.CharacteristicOperationCompletedNotifier;
|
|
23
|
-
import com.iotize.plugin.cordova.ble.commands.DescriptorOperationCompletedNotifier;
|
|
24
|
-
|
|
25
|
-
import java.util.UUID;
|
|
26
|
-
|
|
27
|
-
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
28
|
-
public class CharacteristicAdapter implements CharacteristicAdapterCallbacks {
|
|
29
|
-
|
|
30
|
-
static final UUID NOTIFICATION_DESCRIPTOR_UUID =
|
|
31
|
-
UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
|
|
32
|
-
|
|
33
|
-
private static final String TAG = "AbstractCharacteristicA";
|
|
34
|
-
@NonNull
|
|
35
|
-
public final BluetoothGattCharacteristic characteristic;
|
|
36
|
-
|
|
37
|
-
@NonNull
|
|
38
|
-
protected final BLEProtocol protocol;
|
|
39
|
-
|
|
40
|
-
@Nullable
|
|
41
|
-
private OnCharacteristicValueChangedCallback onCharacteristicValueChangedCallback;
|
|
42
|
-
|
|
43
|
-
@Nullable
|
|
44
|
-
protected CharacteristicAdapterCallbacks callbacks;
|
|
45
|
-
|
|
46
|
-
@Nullable
|
|
47
|
-
private OnCharacteristicOperationResult cmdCallback;
|
|
48
|
-
|
|
49
|
-
public CharacteristicAdapter(
|
|
50
|
-
@NonNull BLEProtocol protocol,
|
|
51
|
-
@NonNull BluetoothGattCharacteristic characteristic,
|
|
52
|
-
@Nullable CharacteristicAdapterCallbacks callbacks) {
|
|
53
|
-
this.protocol = protocol;
|
|
54
|
-
this.characteristic = characteristic;
|
|
55
|
-
this.callbacks = callbacks;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public void setCallbacks(CharacteristicAdapterCallbacks callbacks) {
|
|
59
|
-
this.callbacks = callbacks;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public boolean isWritable() {
|
|
63
|
-
int properties = this.characteristic.getProperties();
|
|
64
|
-
return (properties & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) != 0;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public void startNotification(BluetoothGatt gatt, OnCharacteristicOperationResult callback) {
|
|
68
|
-
this.startOrStopValueChangedSubscription(gatt, true, callback);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public void stopNotification(BluetoothGatt gatt, OnCharacteristicOperationResult callback) {
|
|
72
|
-
this.startOrStopValueChangedSubscription(gatt, false, callback);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
private void startOrStopValueChangedSubscription(BluetoothGatt gatt, boolean start, OnCharacteristicOperationResult callback) {
|
|
76
|
-
queueCommand(callback, () -> {
|
|
77
|
-
if (gatt.setCharacteristicNotification(characteristic, start)) {
|
|
78
|
-
|
|
79
|
-
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(NOTIFICATION_DESCRIPTOR_UUID);
|
|
80
|
-
|
|
81
|
-
if (descriptor == null) {
|
|
82
|
-
throw new DescriptorNotAvailableException(characteristic, NOTIFICATION_DESCRIPTOR_UUID);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (start) {
|
|
86
|
-
int properties = characteristic.getProperties();
|
|
87
|
-
if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
|
|
88
|
-
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
|
|
89
|
-
} else if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
|
|
90
|
-
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
|
|
91
|
-
} else {
|
|
92
|
-
throw new CharacteristicNotificationNotSupported(this.characteristic);
|
|
93
|
-
}
|
|
94
|
-
} else {
|
|
95
|
-
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (!gatt.writeDescriptor(descriptor)) {
|
|
99
|
-
throw new CannotSetCharacteristicNotification(this.characteristic);
|
|
100
|
-
}
|
|
101
|
-
} else {
|
|
102
|
-
throw new CannotSetCharacteristicNotification(this.characteristic);
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
void queueCommand(@Nullable OnCharacteristicOperationResult cb, @NonNull BLECommandRunnable runnable) {
|
|
108
|
-
this.protocol.queueCommand(new BLECommand(
|
|
109
|
-
runnable,
|
|
110
|
-
new CharacteristicOperationCompletedNotifier(cb, this.characteristic)
|
|
111
|
-
));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
void queueCommand(@Nullable OnDescriptorOperationResult cb, @NonNull BLECommandRunnable runnable, BluetoothGattDescriptor descriptor) {
|
|
115
|
-
this.protocol.queueCommand(new BLECommand(
|
|
116
|
-
runnable,
|
|
117
|
-
new DescriptorOperationCompletedNotifier(cb, descriptor)
|
|
118
|
-
)
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
public byte[] getValue() {
|
|
123
|
-
return this.characteristic.getValue();
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public void read(@Nullable OnCharacteristicOperationResult callback) {
|
|
127
|
-
queueCommand(callback, () -> {
|
|
128
|
-
this.protocol.getGatt().readCharacteristic(this.characteristic);
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
public void readDescriptor(@NonNull UUID descriptorUUID, @Nullable OnDescriptorOperationResult callback) {
|
|
133
|
-
BluetoothGattDescriptor descriptor = this.characteristic.getDescriptor(descriptorUUID);
|
|
134
|
-
if (descriptor == null && callback != null) {
|
|
135
|
-
callback.onError(null, new DescriptorNotAvailableException(this.characteristic, descriptorUUID));
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
queueCommand(callback, () -> {
|
|
139
|
-
this.protocol.getGatt().readDescriptor(descriptor);
|
|
140
|
-
}, descriptor);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public void onValueChanged(OnCharacteristicValueChangedCallback onCharacteristicValueChangedCallback) {
|
|
144
|
-
this.onCharacteristicValueChangedCallback = onCharacteristicValueChangedCallback;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
public void write(BluetoothGatt gatt, byte[] packet, int writeType, @Nullable OnCharacteristicOperationResult callback) throws CannotWriteCharacteristicException, WritePacketIsTooBigException {
|
|
148
|
-
queueCommand(callback, () -> {
|
|
149
|
-
if (packet.length > protocol.getMTU()) {
|
|
150
|
-
// throw new WritePacketIsTooBigException(packet);
|
|
151
|
-
Log.w(TAG, "current mtu is " + protocol.getMTU() + " bytes but writing " + packet.length + " bytes");
|
|
152
|
-
}
|
|
153
|
-
Log.d(TAG, "_writeData ... packet = 0x" + Helper.ByteArrayToHexString(packet) + " (size = " + packet.length + ")");
|
|
154
|
-
this.characteristic.setValue(packet);
|
|
155
|
-
this.characteristic.setWriteType(writeType);
|
|
156
|
-
boolean writeSuccess = gatt.writeCharacteristic(this.characteristic);
|
|
157
|
-
if (!writeSuccess) {
|
|
158
|
-
Log.w(TAG, "Cannot write on characteristic on first try. WRITE ENABLED: " + this.isWritable() + "/ Write type: " + this.characteristic.getWriteType());
|
|
159
|
-
throw new CannotWriteCharacteristicException(gatt, this.characteristic);
|
|
160
|
-
}
|
|
161
|
-
Log.d(TAG, "_writeData OK ! packet = 0x" + Helper.ByteArrayToHexString(packet) + " (size = " + packet.length + ")");
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
@Override
|
|
166
|
-
public void onCharacteristicValueChanged(BluetoothGatt gatt) {
|
|
167
|
-
if (this.onCharacteristicValueChangedCallback != null) {
|
|
168
|
-
this.onCharacteristicValueChangedCallback.onCharacteristicValueChanged(this.characteristic.getValue());
|
|
169
|
-
}
|
|
170
|
-
if (this.callbacks != null) {
|
|
171
|
-
this.callbacks.onCharacteristicValueChanged(gatt);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
@Override
|
|
176
|
-
public void onCharacteristicWrite(BluetoothGatt gatt, int status) throws WritePacketIsTooBigException, CannotWriteCharacteristicException {
|
|
177
|
-
if (this.callbacks != null) {
|
|
178
|
-
this.callbacks.onCharacteristicWrite(gatt, status);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
@Override
|
|
183
|
-
public void onCharacteristicRead(BluetoothGatt gatt, int status) {
|
|
184
|
-
if (this.callbacks != null) {
|
|
185
|
-
this.callbacks.onCharacteristicRead(gatt, status);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
@Override
|
|
190
|
-
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
|
|
191
|
-
if (descriptor.getUuid().equals(NOTIFICATION_DESCRIPTOR_UUID)) {
|
|
192
|
-
if (this.cmdCallback != null) {
|
|
193
|
-
this.cmdCallback.onResult(this.characteristic, status);
|
|
194
|
-
this.cmdCallback = null;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
if (this.callbacks != null) {
|
|
198
|
-
this.callbacks.onDescriptorWrite(gatt, descriptor, status);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
public interface OnCharacteristicOperationResult {
|
|
203
|
-
|
|
204
|
-
void onResult(@NonNull BluetoothGattCharacteristic characteristic, int statusCode);
|
|
205
|
-
|
|
206
|
-
void onError(@NonNull BluetoothGattCharacteristic characteristic, @NonNull Exception ex);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
public interface OnDescriptorOperationResult {
|
|
210
|
-
|
|
211
|
-
void onResult(@NonNull BluetoothGattDescriptor descriptor, int statusCode);
|
|
212
|
-
|
|
213
|
-
void onError(@Nullable BluetoothGattDescriptor descriptor, @NonNull Exception ex);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
public interface OnCharacteristicValueChangedCallback {
|
|
217
|
-
|
|
218
|
-
void onCharacteristicValueChanged(byte[] value);
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
}
|
|
1
|
+
package com.iotize.plugin.cordova.ble.characteristics;
|
|
2
|
+
|
|
3
|
+
import android.bluetooth.BluetoothGatt;
|
|
4
|
+
import android.bluetooth.BluetoothGattCharacteristic;
|
|
5
|
+
import android.bluetooth.BluetoothGattDescriptor;
|
|
6
|
+
import android.os.Build;
|
|
7
|
+
import android.util.Log;
|
|
8
|
+
|
|
9
|
+
import androidx.annotation.NonNull;
|
|
10
|
+
import androidx.annotation.Nullable;
|
|
11
|
+
import androidx.annotation.RequiresApi;
|
|
12
|
+
|
|
13
|
+
import com.iotize.android.communication.protocol.ble.BLECommandRunnable;
|
|
14
|
+
import com.iotize.android.communication.protocol.ble.exception.CannotSetCharacteristicNotification;
|
|
15
|
+
import com.iotize.android.communication.protocol.ble.exception.CannotWriteCharacteristicException;
|
|
16
|
+
import com.iotize.android.communication.protocol.ble.exception.CharacteristicNotificationNotSupported;
|
|
17
|
+
import com.iotize.android.communication.protocol.ble.exception.DescriptorNotAvailableException;
|
|
18
|
+
import com.iotize.android.communication.protocol.ble.exception.WritePacketIsTooBigException;
|
|
19
|
+
import com.iotize.android.core.util.Helper;
|
|
20
|
+
import com.iotize.plugin.cordova.ble.BLEProtocol;
|
|
21
|
+
import com.iotize.plugin.cordova.ble.commands.BLECommand;
|
|
22
|
+
import com.iotize.plugin.cordova.ble.commands.CharacteristicOperationCompletedNotifier;
|
|
23
|
+
import com.iotize.plugin.cordova.ble.commands.DescriptorOperationCompletedNotifier;
|
|
24
|
+
|
|
25
|
+
import java.util.UUID;
|
|
26
|
+
|
|
27
|
+
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
28
|
+
public class CharacteristicAdapter implements CharacteristicAdapterCallbacks {
|
|
29
|
+
|
|
30
|
+
static final UUID NOTIFICATION_DESCRIPTOR_UUID =
|
|
31
|
+
UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
|
|
32
|
+
|
|
33
|
+
private static final String TAG = "AbstractCharacteristicA";
|
|
34
|
+
@NonNull
|
|
35
|
+
public final BluetoothGattCharacteristic characteristic;
|
|
36
|
+
|
|
37
|
+
@NonNull
|
|
38
|
+
protected final BLEProtocol protocol;
|
|
39
|
+
|
|
40
|
+
@Nullable
|
|
41
|
+
private OnCharacteristicValueChangedCallback onCharacteristicValueChangedCallback;
|
|
42
|
+
|
|
43
|
+
@Nullable
|
|
44
|
+
protected CharacteristicAdapterCallbacks callbacks;
|
|
45
|
+
|
|
46
|
+
@Nullable
|
|
47
|
+
private OnCharacteristicOperationResult cmdCallback;
|
|
48
|
+
|
|
49
|
+
public CharacteristicAdapter(
|
|
50
|
+
@NonNull BLEProtocol protocol,
|
|
51
|
+
@NonNull BluetoothGattCharacteristic characteristic,
|
|
52
|
+
@Nullable CharacteristicAdapterCallbacks callbacks) {
|
|
53
|
+
this.protocol = protocol;
|
|
54
|
+
this.characteristic = characteristic;
|
|
55
|
+
this.callbacks = callbacks;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public void setCallbacks(CharacteristicAdapterCallbacks callbacks) {
|
|
59
|
+
this.callbacks = callbacks;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public boolean isWritable() {
|
|
63
|
+
int properties = this.characteristic.getProperties();
|
|
64
|
+
return (properties & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) != 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public void startNotification(BluetoothGatt gatt, OnCharacteristicOperationResult callback) {
|
|
68
|
+
this.startOrStopValueChangedSubscription(gatt, true, callback);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public void stopNotification(BluetoothGatt gatt, OnCharacteristicOperationResult callback) {
|
|
72
|
+
this.startOrStopValueChangedSubscription(gatt, false, callback);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private void startOrStopValueChangedSubscription(BluetoothGatt gatt, boolean start, OnCharacteristicOperationResult callback) {
|
|
76
|
+
queueCommand(callback, () -> {
|
|
77
|
+
if (gatt.setCharacteristicNotification(characteristic, start)) {
|
|
78
|
+
|
|
79
|
+
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(NOTIFICATION_DESCRIPTOR_UUID);
|
|
80
|
+
|
|
81
|
+
if (descriptor == null) {
|
|
82
|
+
throw new DescriptorNotAvailableException(characteristic, NOTIFICATION_DESCRIPTOR_UUID);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (start) {
|
|
86
|
+
int properties = characteristic.getProperties();
|
|
87
|
+
if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
|
|
88
|
+
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
|
|
89
|
+
} else if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
|
|
90
|
+
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
|
|
91
|
+
} else {
|
|
92
|
+
throw new CharacteristicNotificationNotSupported(this.characteristic);
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!gatt.writeDescriptor(descriptor)) {
|
|
99
|
+
throw new CannotSetCharacteristicNotification(this.characteristic);
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
throw new CannotSetCharacteristicNotification(this.characteristic);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
void queueCommand(@Nullable OnCharacteristicOperationResult cb, @NonNull BLECommandRunnable runnable) {
|
|
108
|
+
this.protocol.queueCommand(new BLECommand(
|
|
109
|
+
runnable,
|
|
110
|
+
new CharacteristicOperationCompletedNotifier(cb, this.characteristic)
|
|
111
|
+
));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
void queueCommand(@Nullable OnDescriptorOperationResult cb, @NonNull BLECommandRunnable runnable, BluetoothGattDescriptor descriptor) {
|
|
115
|
+
this.protocol.queueCommand(new BLECommand(
|
|
116
|
+
runnable,
|
|
117
|
+
new DescriptorOperationCompletedNotifier(cb, descriptor)
|
|
118
|
+
)
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public byte[] getValue() {
|
|
123
|
+
return this.characteristic.getValue();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public void read(@Nullable OnCharacteristicOperationResult callback) {
|
|
127
|
+
queueCommand(callback, () -> {
|
|
128
|
+
this.protocol.getGatt().readCharacteristic(this.characteristic);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public void readDescriptor(@NonNull UUID descriptorUUID, @Nullable OnDescriptorOperationResult callback) {
|
|
133
|
+
BluetoothGattDescriptor descriptor = this.characteristic.getDescriptor(descriptorUUID);
|
|
134
|
+
if (descriptor == null && callback != null) {
|
|
135
|
+
callback.onError(null, new DescriptorNotAvailableException(this.characteristic, descriptorUUID));
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
queueCommand(callback, () -> {
|
|
139
|
+
this.protocol.getGatt().readDescriptor(descriptor);
|
|
140
|
+
}, descriptor);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public void onValueChanged(OnCharacteristicValueChangedCallback onCharacteristicValueChangedCallback) {
|
|
144
|
+
this.onCharacteristicValueChangedCallback = onCharacteristicValueChangedCallback;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
public void write(BluetoothGatt gatt, byte[] packet, int writeType, @Nullable OnCharacteristicOperationResult callback) throws CannotWriteCharacteristicException, WritePacketIsTooBigException {
|
|
148
|
+
queueCommand(callback, () -> {
|
|
149
|
+
if (packet.length > protocol.getMTU()) {
|
|
150
|
+
// throw new WritePacketIsTooBigException(packet);
|
|
151
|
+
Log.w(TAG, "current mtu is " + protocol.getMTU() + " bytes but writing " + packet.length + " bytes");
|
|
152
|
+
}
|
|
153
|
+
Log.d(TAG, "_writeData ... packet = 0x" + Helper.ByteArrayToHexString(packet) + " (size = " + packet.length + ")");
|
|
154
|
+
this.characteristic.setValue(packet);
|
|
155
|
+
this.characteristic.setWriteType(writeType);
|
|
156
|
+
boolean writeSuccess = gatt.writeCharacteristic(this.characteristic);
|
|
157
|
+
if (!writeSuccess) {
|
|
158
|
+
Log.w(TAG, "Cannot write on characteristic on first try. WRITE ENABLED: " + this.isWritable() + "/ Write type: " + this.characteristic.getWriteType());
|
|
159
|
+
throw new CannotWriteCharacteristicException(gatt, this.characteristic);
|
|
160
|
+
}
|
|
161
|
+
Log.d(TAG, "_writeData OK ! packet = 0x" + Helper.ByteArrayToHexString(packet) + " (size = " + packet.length + ")");
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
@Override
|
|
166
|
+
public void onCharacteristicValueChanged(BluetoothGatt gatt) {
|
|
167
|
+
if (this.onCharacteristicValueChangedCallback != null) {
|
|
168
|
+
this.onCharacteristicValueChangedCallback.onCharacteristicValueChanged(this.characteristic.getValue());
|
|
169
|
+
}
|
|
170
|
+
if (this.callbacks != null) {
|
|
171
|
+
this.callbacks.onCharacteristicValueChanged(gatt);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
@Override
|
|
176
|
+
public void onCharacteristicWrite(BluetoothGatt gatt, int status) throws WritePacketIsTooBigException, CannotWriteCharacteristicException {
|
|
177
|
+
if (this.callbacks != null) {
|
|
178
|
+
this.callbacks.onCharacteristicWrite(gatt, status);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
@Override
|
|
183
|
+
public void onCharacteristicRead(BluetoothGatt gatt, int status) {
|
|
184
|
+
if (this.callbacks != null) {
|
|
185
|
+
this.callbacks.onCharacteristicRead(gatt, status);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
@Override
|
|
190
|
+
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
|
|
191
|
+
if (descriptor.getUuid().equals(NOTIFICATION_DESCRIPTOR_UUID)) {
|
|
192
|
+
if (this.cmdCallback != null) {
|
|
193
|
+
this.cmdCallback.onResult(this.characteristic, status);
|
|
194
|
+
this.cmdCallback = null;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (this.callbacks != null) {
|
|
198
|
+
this.callbacks.onDescriptorWrite(gatt, descriptor, status);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public interface OnCharacteristicOperationResult {
|
|
203
|
+
|
|
204
|
+
void onResult(@NonNull BluetoothGattCharacteristic characteristic, int statusCode);
|
|
205
|
+
|
|
206
|
+
void onError(@NonNull BluetoothGattCharacteristic characteristic, @NonNull Exception ex);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public interface OnDescriptorOperationResult {
|
|
210
|
+
|
|
211
|
+
void onResult(@NonNull BluetoothGattDescriptor descriptor, int statusCode);
|
|
212
|
+
|
|
213
|
+
void onError(@Nullable BluetoothGattDescriptor descriptor, @NonNull Exception ex);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
public interface OnCharacteristicValueChangedCallback {
|
|
217
|
+
|
|
218
|
+
void onCharacteristicValueChanged(byte[] value);
|
|
219
|
+
|
|
220
|
+
}
|
|
221
|
+
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
package com.iotize.plugin.cordova.ble.characteristics;
|
|
2
|
-
|
|
3
|
-
import android.bluetooth.BluetoothGatt;
|
|
4
|
-
import android.bluetooth.BluetoothGattDescriptor;
|
|
5
|
-
|
|
6
|
-
import com.iotize.android.communication.protocol.ble.exception.CannotWriteCharacteristicException;
|
|
7
|
-
import com.iotize.android.communication.protocol.ble.exception.WritePacketIsTooBigException;
|
|
8
|
-
|
|
9
|
-
import java.util.UUID;
|
|
10
|
-
|
|
11
|
-
public interface CharacteristicAdapterCallbacks {
|
|
12
|
-
|
|
13
|
-
void onCharacteristicValueChanged(BluetoothGatt gatt);
|
|
14
|
-
|
|
15
|
-
void onCharacteristicWrite(BluetoothGatt gatt, int status) throws WritePacketIsTooBigException, CannotWriteCharacteristicException;
|
|
16
|
-
|
|
17
|
-
void onCharacteristicRead(BluetoothGatt gatt, int status);
|
|
18
|
-
|
|
19
|
-
void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status);
|
|
20
|
-
|
|
21
|
-
}
|
|
1
|
+
package com.iotize.plugin.cordova.ble.characteristics;
|
|
2
|
+
|
|
3
|
+
import android.bluetooth.BluetoothGatt;
|
|
4
|
+
import android.bluetooth.BluetoothGattDescriptor;
|
|
5
|
+
|
|
6
|
+
import com.iotize.android.communication.protocol.ble.exception.CannotWriteCharacteristicException;
|
|
7
|
+
import com.iotize.android.communication.protocol.ble.exception.WritePacketIsTooBigException;
|
|
8
|
+
|
|
9
|
+
import java.util.UUID;
|
|
10
|
+
|
|
11
|
+
public interface CharacteristicAdapterCallbacks {
|
|
12
|
+
|
|
13
|
+
void onCharacteristicValueChanged(BluetoothGatt gatt);
|
|
14
|
+
|
|
15
|
+
void onCharacteristicWrite(BluetoothGatt gatt, int status) throws WritePacketIsTooBigException, CannotWriteCharacteristicException;
|
|
16
|
+
|
|
17
|
+
void onCharacteristicRead(BluetoothGatt gatt, int status);
|
|
18
|
+
|
|
19
|
+
void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status);
|
|
20
|
+
|
|
21
|
+
}
|