native_device_kit 0.0.1
native_device_kit: ^0.0.1 copied to clipboard
A production-grade Flutter plugin for unified access to Bluetooth, NFC, USB, and native hardware features.
Native Device Kit #
A production-grade Flutter plugin that provides a unified, high-performance API to access native hardware features on Android and iOS. This plugin abstracts complex platform-specific implementations (Kotlin/Swift) into simple, clean Dart interfaces.
Features #
- Bluetooth (BLE): Scan for devices, connect/disconnect, read services, and stream events.
- NFC: Read and write NDEF tags with background tag discovery.
- USB: List connected USB devices and manage communication (supports Android USB Host & iOS External Accessory).
- Hardware Bridge: A flexible bridge to invoke any third-party native SDK method dynamically.
Installation #
Add native_device_kit as a dependency in your pubspec.yaml file.
dependencies:
native_device_kit:
path: ./native_device_kit # If local, or use git/pub version
Platform Configuration #
Android #
Add the following permissions to your android/app/src/main/AndroidManifest.xml:
<manifest ...>
<!-- Bluetooth Permissions -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- NFC Permissions -->
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="false" />
<!-- USB Permissions -->
<uses-feature android:name="android.hardware.usb.host" android:required="false" />
</manifest>
iOS #
Add the following keys to your ios/Runner/Info.plist to explain why you need these permissions:
<dict>
<!-- Bluetooth Usage Description -->
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Need Bluetooth to scan for and connect to supported devices.</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Need Bluetooth to connect to supported devices.</string>
<!-- NFC Usage Description -->
<key>NFCReaderUsageDescription</key>
<string>Need NFC to read and write tags.</string>
<!-- USB / External Accessory (if applicable) -->
<key>UISupportsDocumentBrowser</key>
<true/>
</dict>
Usage #
The plugin exposes a singleton instance NativeDeviceKit.instance which provides access to all modules.
1. Bluetooth Module #
import 'package:native_device_kit/native_device_kit.dart';
// Access the Bluetooth module
final bluetooth = NativeDeviceKit.instance.bluetooth;
// 1. Listen for events (Scan results, connection status)
bluetooth.eventStream.listen((event) {
print("Bluetooth Event: $event");
});
// 2. Start Scanning
try {
await bluetooth.scan();
} catch (e) {
print("Scan Error: $e");
}
// 3. Connect to a device
await bluetooth.connect("DEVICE_ID_OR_UUID");
// 4. Disconnect
await bluetooth.disconnect("DEVICE_ID_OR_UUID");
2. NFC Module #
import 'package:native_device_kit/native_device_kit.dart';
final nfc = NativeDeviceKit.instance.nfc;
// 1. Listen for NFC tags
nfc.eventStream.listen((event) {
print("NFC Tag Discovered: $event");
});
// 2. Start Reading
await nfc.readTag();
// 3. Write to a Tag
await nfc.writeTag("Hello from Flutter!");
// 4. Stop Reading
await nfc.stopReading();
3. USB Module #
import 'package:native_device_kit/native_device_kit.dart';
final usb = NativeDeviceKit.instance.usb;
// 1. Get Connected Devices
List<Map<String, dynamic>> devices = await usb.getConnectedDevices();
for (var device in devices) {
print("Found USB Device: ${device['deviceName']} (ID: ${device['deviceId']})");
}
// 2. Open Device Connection
await usb.openDevice("DEVICE_ID");
4. Hardware Bridge (Advanced) #
Use the bridge to invoke native methods that aren't covered by the standard modules. This is useful for integrating 3rd party SDKs without modifying the plugin core.
final bridge = NativeDeviceKit.instance.bridge;
// Invoke a custom native method
try {
final result = await bridge.invoke("customNativeMethod", {"param": "value"});
print("Native Result: $result");
} catch (e) {
print("Bridge Error: $e");
}
Architecture #
This plugin follows a clean, layered architecture:
- Dart API Layer: Strong-typed, easy-to-use classes (
BluetoothModule,NfcModule, etc.) that abstract the underlying platform channels. - Platform Channel Layer: Uses
MethodChannelfor command invocation andEventChannelfor streaming events from native to Dart. - Native Implementation Layer:
- Android: Kotlin with Coroutines and standard Android APIs (BluetoothAdapter, NfcAdapter, UsbManager).
- iOS: Swift with CoreBluetooth, CoreNFC, and ExternalAccessory.
License #
This project is licensed under the MIT License - see the LICENSE file for details.