Я довольно новичок в разработке Xamarin, нашел некоторый код, который заставляет меня подключаться к USB-устройству (термопринтер в данном случае). Пытаюсь запросить разрешения, но кажется, что запрос возвращается ноль.
var matchingDevice = usbManager.DeviceList.FirstOrDefault(item => item.Value.VendorId == vendorId && item.Value.ProductId == productId);
if (usbManager.DeviceList.Count == 0)
throw new Exception("No USB device connected");
if (matchingDevice.Value == null)
throw new Exception("Device not found, try to configure it in settings.");
// DeviceList is a dictionary with the port as the key, so pull out the device you want. I save the port too
var usbPort = matchingDevice.Key;
var usbDevice = matchingDevice.Value;
// Get permission from the user to access the device (otherwise connection later will be null)
if (!usbManager.HasPermission(usbDevice))
{
try
{
//PendingIntent pi = PendingIntent.GetBroadcast(Forms.Context, 0, new Intent(ACTION_USB_PERMISSION), 0);
PendingIntent pi = PendingIntent.GetBroadcast(MainActivity.Instance, 0, new Intent(ACTION_USB_PERMISSION), PendingIntentFlags.UpdateCurrent);
usbManager.RequestPermission(usbDevice, pi);
throw new Exception("Relaunch the application");
}
catch (Exception ex)
{
throw new Exception($"{usbDevice.DeviceName} - {ex.Message}");
}
}
Когда я запускаю приложение, я получаю номер порта, номер устройства и имя устройства, но всплывающее окно для разрешений говорит: «Разрешить для доступа к нулю 'Почему бы RequestPermission попытаться подключить устройство к нулю? Любая помощь будет высоко ценится.
Большое спасибо