Implement pairing on Apple (#59)

This commit is contained in:
Foti Dim
2024-08-09 19:17:31 +02:00
committed by Foti Dim
parent 95c0b88181
commit 7606e8fd29
18 changed files with 371 additions and 139 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ class Capabilities {
static bool supportsConnectedDevicesApi = !Platform.isWeb;
static bool supportsPairingApi = !Platform.isWeb && !Platform.isCupertino;
static bool supportsPairingApi = !Platform.isWeb;
static bool supportsRequestMtuApi = !Platform.isWeb;
}
+6 -6
View File
@@ -27,19 +27,19 @@ class MockUniversalBle extends UniversalBlePlatform {
ScanFilter? scanFilter,
PlatformConfig? platformConfig,
}) async =>
onScanResult?.call(_mockBleDevice);
updateScanResult(_mockBleDevice);
@override
Future<void> stopScan() async {}
@override
Future<void> connect(String deviceId, {Duration? connectionTimeout}) async {
onConnectionChange?.call(deviceId, true);
updateConnection(deviceId, true);
}
@override
Future<void> disconnect(String deviceId) async {
onConnectionChange?.call(deviceId, false);
updateConnection(deviceId, false);
}
@override
@@ -99,12 +99,12 @@ class MockUniversalBle extends UniversalBlePlatform {
@override
Future<void> pair(String deviceId) async {
onPairingStateChange?.call(deviceId, true, null);
updatePairingState(deviceId, true, null);
}
@override
Future<void> unPair(String deviceId) async {
onPairingStateChange?.call(deviceId, false, null);
Future<void> unpair(String deviceId) async {
updatePairingState(deviceId, false, null);
}
@override
+1 -1
View File
@@ -192,7 +192,7 @@ class _MyAppState extends State<MyApp> {
},
),
PlatformButton(
text: 'Queue: ${_queueType.name.toUpperCase()}',
text: 'Queue: ${_queueType.name}',
onPressed: () {
setState(() {
_queueType = switch (_queueType) {
@@ -84,11 +84,11 @@ class _PeripheralDetailPageState extends State<PeripheralDetailPage> {
void _handlePairingStateChange(
String deviceId, bool isPaired, String? error) {
print('OnPairStateChange $deviceId, $isPaired');
print('isPaired $deviceId, $isPaired');
if (error != null && error.isNotEmpty) {
_addLog("PairStateChangeError", "(Paired: $isPaired): $error ");
} else {
_addLog("PairStateChange", isPaired);
_addLog("PairStateChange - isPaired", isPaired);
}
}
@@ -254,7 +254,10 @@ class _PeripheralDetailPageState extends State<PeripheralDetailPage> {
enabled: !isConnected,
onPressed: () async {
try {
await UniversalBle.connect(widget.deviceId);
bool connected = await UniversalBle.connect(
widget.deviceId,
);
_addLog("ConnectionResult", connected);
} catch (e) {
_addLog('ConnectError', e);
}
@@ -405,29 +408,41 @@ class _PeripheralDetailPageState extends State<PeripheralDetailPage> {
BleInputProperty.disabled),
text: 'Unsubscribe',
),
if (Capabilities.supportsPairingApi)
PlatformButton(
onPressed: () async {
await UniversalBle.pair(widget.deviceId);
},
text: 'Pair',
),
if (Capabilities.supportsPairingApi)
PlatformButton(
onPressed: () async {
bool? isPaired = await UniversalBle.isPaired(
widget.deviceId);
_addLog('IsPaired', isPaired);
},
text: 'IsPaired',
),
if (Capabilities.supportsPairingApi)
PlatformButton(
onPressed: () async {
await UniversalBle.unPair(widget.deviceId);
},
text: 'UnPair',
),
PlatformButton(
onPressed: () async {
await UniversalBle.pair(
widget.deviceId,
// pairingCommand: BleCommand(
// service:
// "",
// characteristic:
// "",
// ),
);
},
text: 'Pair',
),
PlatformButton(
onPressed: () async {
bool? isPaired = await UniversalBle.isPaired(
widget.deviceId,
// pairingCheckCommand: BleCommand(
// service:
// "",
// characteristic:
// "",
// ),
);
_addLog('IsPaired', isPaired);
},
text: 'IsPaired',
),
PlatformButton(
onPressed: () async {
await UniversalBle.unpair(widget.deviceId);
},
text: 'Unpair',
),
],
),
),