bluetooth_low_energy
bluetooth_low_energy copied to clipboard
connect fail with TypeError
MyPeripheral is not found in bluetooth_low_energy_platform_interface,but found in bluetooth_low_energy_android or bluetooth_low_energy_windows,So I can not get MyPeripheral. Using Code:
void connect(BleDevice device) {
LogUtils.i(_sTag, "connect device:${device.deviceId}");
var peripheralUUID = device.peripheralUUID;
if (peripheralUUID == null) {
LogUtils.e(_sTag, "connect error:ble device not found");
callback.error(CodeConstants.CONNECT_ERROR_BLE_DEVICE_NOT_FOUND,
"ble device not found");
return;
}
mPeripheral = Peripheral(uuid: UUID.fromString(peripheralUUID));
_timeoutHelper.runWithTimeout(
action: () {
_centralManager.connect(mPeripheral);
return Future.value(true);
},
timeout: Duration(milliseconds: _timeout),
onTimeout: () {
if (!_taskFinishFlag) {
LogUtils.e(_sTag, "connect error:connect timeout");
callback.error(CodeConstants.CONNECT_TIME_OUT, "connect timeout");
}
return Future.value(true);
});
}
Crash Info
[main]: main error:Instance of 'TypeError',stackTrace:#0 MyCentralManager.connect (package:bluetooth_low_energy_windows/src/my_central_manager.dart:95:7)
#1 TranGattClient.connect (package:tran_connection/bluetooth/tran_gatt_client.dart:60:21)
Relative Code:
Future<void> connect(Peripheral peripheral) async {
if (peripheral is! MyPeripheral) {
throw TypeError();
}
final addressArgs = peripheral.addressArgs;
logger.info('connect: $addressArgs');
await _api.connect(addressArgs);
}