Power Off Controller серии Xbox с C# через BluetoothC#

Место общения программистов C#
Ответить
Anonymous
 Power Off Controller серии Xbox с C# через Bluetooth

Сообщение Anonymous »

Я пишу небольшое приложение для лотка Winforms, нацеленное на 4.7.2 на ПК Win10, который позволит мне включить контроллер на выходе из игры. Я пытался сделать это в течение некоторого времени, но все еще не могу достичь отключения контроллера. Я предполагаю, что он как -то подключен к фирменному API Xbox. Прямо сейчас, в моей реализации приложения я просто полностью отключаю Bluetooth, чтобы выключить контроллер. могу отправить эту команду контроллеру, не выключив Bluetooth на моем ПК. < /p>
Это то, что я придумал: < /p>
using Windows.Devices.Bluetooth;
using Windows.Devices.Enumeration;

private async Task TurnOffXboxController()
{
string deviceSelector = BluetoothLEDevice.GetDeviceSelector();
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(deviceSelector);

var xboxControllerInfo = devices.FirstOrDefault(btDevice =>
btDevice.Name == "Xbox Wireless Controller");

var xboxController = await BluetoothLEDevice.FromIdAsync(xboxControllerInfo.Id);

// cleaning out services and disposing the device supposed to disconnect the device but the controller keeps connection
// the code below is a combo of different approaches I found around stack and git but none of them seem to work
var services = await xboxController.GetGattServicesAsync();
foreach (var service in services.Services)
{
service?.Session?.Dispose();
service?.Dispose();
}

xboxController.Dispose();
xboxController = null;

GC.Collect();
GC.WaitForPendingFinalizers();
}
< /code>
Вот как я отключаю Bluetooth: < /p>
using Windows.Devices.Radios;

private async Task RequestBluetoothDevice()
{
var access = await Radio.RequestAccessAsync();
if (access != RadioAccessStatus.Allowed)
{
Console.WriteLine("Permission denied to control radios");
return null;
}

var radios = await Radio.GetRadiosAsync();
var bluetoothRadio = radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth);
return bluetoothRadio;
}

public async Task TurnOffBluetoothAsync()
{
try
{
var bluetoothRadio = await RequestBluetoothDevice();

if (bluetoothRadio == null)
{
Console.WriteLine("Bluetooth radio not found");
return false;
}

if (bluetoothRadio.State != RadioState.Off)
{
await bluetoothRadio.SetStateAsync(RadioState.Off);
Console.WriteLine("Bluetooth turned off successfully");
return true;
}

Console.WriteLine("Bluetooth is already off");
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
return false;
}
}


Подробнее здесь: https://stackoverflow.com/questions/796 ... -bluetooth
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»