Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/scavanger/libusb_android_helper

Helper plugin for libusb_android
https://github.com/scavanger/libusb_android_helper

android flutter libusb

Last synced: about 2 months ago
JSON representation

Helper plugin for libusb_android

Awesome Lists containing this project

README

        

![Push Validation](https://github.com/scavanger/libusb_android_helper/actions/workflows/dart.yml/badge.svg?event=push)
# Libusb Android Helper

libusb_android has the restriction on (unrooted) Android that no USB devices can be listed and found.
See [libusb android readme](https://github.com/libusb/libusb/blob/master/android/README)

This helper plugin closes this gap by using the native Java API to find and open the devices and retrieve the native file descriptor to continue working with it in libusb_android.

Flutter plugins cannot contain native C (ffi) and Java code at the same time, hence the split into two plugins.

## Getting Started

Add a dependency to your pubspec.yaml

```dart
dependencies:
libusb_android_helper: ^1.0.0
```

include the libusb_android_helper package at the top of your dart file.

```dart
import 'package:libusb_android_helper/libusb_android_helper.dart';
```

### Optional

Add
```xml


...


...






```
to your AndroidManifest.xml

and place device_filter.xml
```xml

```
in the res/xml directory. This will notify your app when one of the specified devices
is connected or disconnected.

### Usage

List all devices already connected:

```dart
Future> getUsbDevices() async {
List? devices;
try {
devices = await LibusbAndroidHelper.listDevices();
} on PlatformException catch (e) {
return List.empty();
}
if (devices != null) {
return devices;
} else {
return List.empty();
}
}
```

Get notified when a USB device is connected or disconnected:
```dart
LibusbAndroidHelper.usbEventStream?.listen((event) {
try {
if (event.action == UsbAction.usbDeviceAttached) {
_device = event.device;
} else if (event.action == UsbAction.usbDeviceDetached) {
_device = null;
}
} on PlatformException catch (e) {
// error
}
});
```

Request authorization to access the USB device from the user:
```dart
if (!(await device.hasPermission())){
await device.requestPermission();
}
```

Open the device and pass the native handle to libusb_android
```dart
const String _libName = 'libusb_android';
final DynamicLibrary _dynamicLibrary = () {
if (Platform.isAndroid) {
return DynamicLibrary.open('lib$_libName.so');
}
throw UnsupportedError('Unsupported platform: ${Platform.operatingSystem}');
}();
final LibusbAndroidBindings _bindings = LibusbAndroidBindings(_dynamicLibrary);
// ...
if (await device?.open()) {
int retValue = 0;
Pointer> context = calloc>();
Pointer> deviceHandle = calloc>();

retValue = _bindings.libusb_set_option(context.value, libusb_option.LIBUSB_OPTION_NO_DEVICE_DISCOVERY);
if (retValue < 0) {
return;
}

retValue =_bindings.libusb_init(context);
if (retValue < 0) {
return;
}

retValue = _bindings.libusb_wrap_sys_device(context.value, device!.handle, deviceHandle);
if (retValue < 0) {
return;
}

Pointer device = _bindings.libusb_get_device(deviceHandle.value);

// Work with device
}
// ...
// Don't forget
calloc.free(context);
calloc.free(dev_handle);
```