Я использую Unity 2022.3.62f2 и BluetoothlehardwareNterface. />
Код: Выделить всё
15:42:31.461 >> Connecting joystick DeviceName [MAC Address]...
15:42:31.705 >> device disconnected: [MAC Address]
15:43:01.465 >> connection to DeviceName [MAC Address] timed out!
< /code>
Отключение происходит менее чем через 250 мс после попытки подключения, а затем процесс пройдет после 30 -х годов (TIMEOUT_DEFAULTМетод соединения
Код: Выделить всё
public async UniTask Connect(Peripheral _peripheral, CancellationToken _cancellationToken = default)
{
if (!string.IsNullOrEmpty(DeviceAddress))
{
Debug.LogWarning($">> Already connected to {DeviceName}. Disconnecting first...");
Disconnect();
await UniTask.Delay(RECONNECTION_GUARD_DELAY, cancellationToken: _cancellationToken);
}
IsConnected = false;
DeviceName = null;
DeviceAddress = null;
var connectionPromise = new UniTaskCompletionSource();
await using var registration = _cancellationToken.Register(() => connectionPromise.TrySetCanceled());
print($">> Connecting joystick {_peripheral.ToString()}...");
BluetoothLEHardwareInterface.ConnectToPeripheral(
_peripheral.Address,
OnDeviceConnected,
null, // ServiceCallback, unused
OnCharacteristicDiscovered,
OnDeviceDisconnected);
bool success = await connectionPromise.Task.Timeout(TimeSpan.FromMilliseconds(TIMEOUT_DEFAULT));
if (success) print($">> Successfully paired joystick {_peripheral.ToString()}!");
else Debug.LogError($">> connection to {_peripheral.ToString()} timed out!");
if (IsConnected) OnConnected?.Invoke();
return success;
void OnDeviceConnected(string _name)
{
DeviceAddress = _peripheral.Address;
DeviceName = _name;
IsConnected = true;
connectionPromise.TrySetResult(true);
}
}
< /code>
BluetoothLEHardwareInterface.ConnectToPeripheral()Отсоедините обратный вызов < /h4>
private void OnDeviceDisconnected(string _disconnectedDeviceAddress)
{
print(">> device disconnected: " + _disconnectedDeviceAddress);
if (string.IsNullOrEmpty(DeviceAddress) || DeviceAddress != _disconnectedDeviceAddress)
return;
DeviceName = null;
DeviceAddress = null;
IsConnected = false;
OnDisconnected?.Invoke();
}
< /code>
I only have 2 devices, but the order in which I connect to them doesn't seem to matter, the first one always immediately disconnects.
Any ideas why? Is this a known Android Bluetooth LE stack issue? Is there something wrong about my implementation related to UniTask/async?
Подробнее здесь: https://stackoverflow.com/questions/797 ... st-attempt
Мобильная версия