Skip to content

Commit cdc5cd8

Browse files
committed
fix(ios): manufacturerData fixes
1 parent e72f2fb commit cdc5cd8

File tree

1 file changed

+56
-53
lines changed

1 file changed

+56
-53
lines changed

src/bluetooth.ios.ts

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ export class AdvertismentData {
480480
constructor(private advData: NSDictionary<string, any>) {}
481481
get manufacturerData() {
482482
const data = this.advData.objectForKey(CBAdvertisementDataManufacturerDataKey);
483-
if (data) {
483+
if (data && data.length > 2) {
484484
return toArrayBuffer(data.subdataWithRange(NSMakeRange(2, data.length - 2)));
485485
}
486486
return undefined;
@@ -490,7 +490,7 @@ export class AdvertismentData {
490490
}
491491
get manufacturerId() {
492492
const data = this.advData.objectForKey(CBAdvertisementDataManufacturerDataKey);
493-
if (data) {
493+
if (data && data.length >=2) {
494494
const manufacturerIdBuffer = toArrayBuffer(data.subdataWithRange(NSMakeRange(0, 2)));
495495
return new DataView(manufacturerIdBuffer, 0).getUint16(0, true);
496496
}
@@ -1158,60 +1158,63 @@ export class Bluetooth extends BluetoothCommon {
11581158
})
11591159
);
11601160
}
1161-
return this._getWrapper(args, CBCharacteristicProperties.PropertyWrite).then((wrapper) => new Promise((resolve, reject) => {
1162-
CLog(CLogTypes.info, methodName, `---- peripheralUUID:${args.peripheralUUID} serviceUUID:${args.serviceUUID} characteristicUUID:${args.characteristicUUID}`);
1163-
const valueEncoded = valueToNSData(args.value, args.encoding);
1161+
return this._getWrapper(args, CBCharacteristicProperties.PropertyWrite).then(
1162+
(wrapper) =>
1163+
new Promise((resolve, reject) => {
1164+
CLog(CLogTypes.info, methodName, `---- peripheralUUID:${args.peripheralUUID} serviceUUID:${args.serviceUUID} characteristicUUID:${args.characteristicUUID}`);
1165+
const valueEncoded = valueToNSData(args.value, args.encoding);
11641166

1165-
if (valueEncoded === null) {
1166-
return reject(
1167-
new BluetoothError(BluetoothCommon.msg_invalid_value, {
1168-
method: methodName,
1169-
arguments: args,
1170-
})
1171-
);
1172-
}
1173-
const pUUID = args.peripheralUUID;
1174-
const p = wrapper.peripheral;
1175-
const subD = {
1176-
peripheralDidWriteValueForCharacteristicError: (peripheral: CBPeripheral, characteristic: CBCharacteristic, error?: NSError) => {
1177-
CLog(CLogTypes.info, methodName, '---- peripheralDidWriteValueForCharacteristicError', error);
1178-
const UUID = NSUUIDToString(peripheral.identifier);
1179-
const cUUID = CBUUIDToString(characteristic.UUID);
1180-
const sUUID = CBUUIDToString(characteristic.service.UUID);
1181-
if (UUID === pUUID && cUUID === args.characteristicUUID && sUUID === args.serviceUUID) {
1182-
if (error) {
1183-
reject(
1184-
new BluetoothError(error.localizedDescription, {
1185-
method: methodName,
1186-
status: error.code,
1187-
})
1188-
);
1189-
} else {
1190-
resolve();
1191-
}
1167+
if (valueEncoded === null) {
1168+
return reject(
1169+
new BluetoothError(BluetoothCommon.msg_invalid_value, {
1170+
method: methodName,
1171+
arguments: args,
1172+
})
1173+
);
1174+
}
1175+
const pUUID = args.peripheralUUID;
1176+
const p = wrapper.peripheral;
1177+
const subD = {
1178+
peripheralDidWriteValueForCharacteristicError: (peripheral: CBPeripheral, characteristic: CBCharacteristic, error?: NSError) => {
1179+
CLog(CLogTypes.info, methodName, '---- peripheralDidWriteValueForCharacteristicError', error);
1180+
const UUID = NSUUIDToString(peripheral.identifier);
1181+
const cUUID = CBUUIDToString(characteristic.UUID);
1182+
const sUUID = CBUUIDToString(characteristic.service.UUID);
1183+
if (UUID === pUUID && cUUID === args.characteristicUUID && sUUID === args.serviceUUID) {
1184+
if (error) {
1185+
reject(
1186+
new BluetoothError(error.localizedDescription, {
1187+
method: methodName,
1188+
status: error.code,
1189+
})
1190+
);
1191+
} else {
1192+
resolve();
1193+
}
1194+
p.delegate.removeSubDelegate(subD);
1195+
}
1196+
},
1197+
};
1198+
p.delegate.addSubDelegate(subD);
1199+
try {
1200+
p.writeValueForCharacteristicType(valueEncoded, wrapper.characteristic, CBCharacteristicWriteType.WithResponse);
1201+
} catch (ex) {
1202+
CLog(CLogTypes.error, methodName, '---- error:', ex);
11921203
p.delegate.removeSubDelegate(subD);
1204+
return reject(
1205+
new BluetoothError(ex.message, {
1206+
stack: ex.stack,
1207+
nativeException: ex.nativeException,
1208+
method: methodName,
1209+
arguments: args,
1210+
})
1211+
);
11931212
}
1194-
},
1195-
};
1196-
p.delegate.addSubDelegate(subD);
1197-
try {
1198-
p.writeValueForCharacteristicType(valueEncoded, wrapper.characteristic, CBCharacteristicWriteType.WithResponse);
1199-
} catch (ex) {
1200-
CLog(CLogTypes.error, methodName, '---- error:', ex);
1201-
p.delegate.removeSubDelegate(subD);
1202-
return reject(
1203-
new BluetoothError(ex.message, {
1204-
stack: ex.stack,
1205-
nativeException: ex.nativeException,
1206-
method: methodName,
1207-
arguments: args,
1208-
})
1209-
);
1210-
}
1211-
if (BluetoothUtil.debug) {
1212-
CLog(CLogTypes.info, methodName, JSON.stringify(valueToString(valueEncoded)));
1213-
}
1214-
}));
1213+
if (BluetoothUtil.debug) {
1214+
CLog(CLogTypes.info, methodName, JSON.stringify(valueToString(valueEncoded)));
1215+
}
1216+
})
1217+
);
12151218
}
12161219

12171220
@prepareArgs

0 commit comments

Comments
 (0)