I want to know whether the used device is tv or not..I am using flutter_device_type package but it only detects the tablet and consider any other device as phone
You can use the device_info_plus package for detecting Android TV.
The AndroidDeviceInfo class contains a list of all the system's features as documented in the Android PackageManager
For detecting TV, you can use e.g:
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
bool isTV = androidInfo.systemFeatures.contains('android.software.leanback')
By default flutter detects the device based on its minimum size(screens bigger than a minimum width considered as a tablet), something like this code:
String getDeviceType() {
final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
return data.size.shortestSide < 600 ? 'phone' :'tablet';
}
In another hand with Java/Kotlin and based on this documentation you can detect if the device is android Tv with this code:
val uiModeManager = getSystemService(UI_MODE_SERVICE) as UiModeManager
if (uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION) {
Log.d(TAG, "Running on a TV Device")
} else {
Log.d(TAG, "Running on a non-TV Device")
}
So the proper option would be using a native code and connecting it to flutter using platform-channels
Related
I want to get a device id or any unique number that can not be changed when someone uninstalls the app. This device is for both android and iOS and is in the flutter framework. I want this to manage my device management.
Thanks for the help.
I tried some of the most commonly used packages in flutter like device_info_plus and flutter_udid but IDs generated from these changes every day.
Did you tried platform_device_id
This package can help you to get deviceId in android, iOS, windows, macOS and even in web and linux.
After importing the package you need to use following code
try {
deviceId = await PlatformDeviceId.getDeviceId;
} on PlatformException {
deviceId = 'Failed to get deviceId.';
}
You can use #Rohan's answer also. platform_device_id package. Read more
There is guide for device_info_plus package Read more
add package to your flutter project.
Create a method like this:
Future<String> getUniqueDeviceId() async {
String uniqueDeviceId = '';
var deviceInfo = DeviceInfoPlugin();
if (Platform.isIOS) {
var iosDeviceInfo = await deviceInfo.iosInfo;
uniqueDeviceId =
'${iosDeviceInfo.name}:${iosDeviceInfo.identifierForVendor}'; //
unique ID on iOS
} else if(Platform.isAndroid) {
var androidDeviceInfo = await deviceInfo.androidInfo;
uniqueDeviceId =
'${androidDeviceInfo.name}:${androidDeviceInfo.id}' ; // unique ID
on Android
}
return uniqueDeviceId;
}
use it like this:
String deviceId = await getUniqueDeviceId();
Note (updateded):
Don't use androidDeviceInfo.androidId. This would change when your mac address changes. Mobile devices above Android OS 10/11 will generate a randomized MAC. This feature is enabled by default unless disabled manually. This would cause the androidId to change when switiching networks. You can confirm this by yourself by changing androidDeviceInfo.id to androidDeviceInfo.androidId above.
(Got details from #Ariel)
It works fine for many android devices except samsungs.
ClipboardData? cdata = await Clipboard.getData(Clipboard.kTextPlain);
String? copiedtext = cdata?.text;
Already I have built a flutter project. Now I need to print from a pos embedded device. I am googling, but I don't get any solution.
Please help me if there is any solution.
Actually I need for Android Q2 device
I have the same device and i already built flutter application , and ran into the same probleme .
I contacted the company and they provided me with android sdk so i added a channel and called the print from the flutter code.
EDIT:
In your case, you can download the SDK provided by the company and write native code to print the receipt.
example:
in flutter
static const platform = const MethodChannel('com.example.myapplication');
Future<void> _print() async {
try {
final bool result = await platform.invokeMethod('print',{"data":Printdata});
if (result) {
// success
} else {
// failed
}
} on PlatformException catch (e) {
//failed to print
}
}
Then in the Main.java / Main.kt implement the method from the SDK documentation
example:
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("print")) {
data = call.argument("data");
// Code from SDK documentation
} else {
result.notImplemented();
}
}
ref: Example Nativcode in flutter
Add third party SDK to android
Try this library "flutter_bluetooth_serial" and connect to the printer via direct mac address like this:
BluetoothConnection connection = await BluetoothConnection.toAddress("mac");
I have this problem is that in flutter I notice there is not able to operate or use the traditional bluetooth as there is no any library supporting it. I have tested flutter_blue-master etc. So then I saw that it can behave as beacon. So I have used the codes below. For android I just set
Region(
identifier: 'com.example.myDeviceRegion',)); its able to work. So the same I set in IOS its not able to work? So what is best workaround for blueetooth in flutter? I am using this package flutter_beacon. For the beacon broadcasting I am using this package beacon_broadcast.
initScanBeacon() async {
await flutterBeacon.initializeScanning;
await checkAllRequirements();
if (!authorizationStatusOk ||
!locationServiceEnabled ||
!bluetoothEnabled) {
print('RETURNED, authorizationStatusOk=$authorizationStatusOk, '
'locationServiceEnabled=$locationServiceEnabled, '
'bluetoothEnabled=$bluetoothEnabled');
return;
}
/*final regions = <Region>[
Region(
identifier: 'com.example.myDeviceRegion',
),
];*/
final regions = <Region>[];
regions.add(Region(
identifier: 'com.example.myDeviceRegion',
minor: 100,
major: 1));
if (_streamRanging != null) {
if (_streamRanging.isPaused) {
_streamRanging.resume();
return;
}
}
_streamRanging =
flutterBeacon.monitoring(regions).listen((MonitoringResult result) {
print(result);
if (result != null && mounted) {
print("GOT RESTULT READY");
setState(() {
//_regionBeacons[result.region] = result.region;
_beacons.clear();
print("List value is json"+result.toJson.toString());
_regionBeacons.values.forEach((list) {
print("List value is");
_beacons.addAll(list);
print("after Beacon size now is "+_beacons.length.toString());
});
//_beacons.sort(_compareParameters);
print("Beacon size now is "+_beacons.length.toString());
});
}
});
}
A few things to check:
Make sure your Region definition has a proximityUUID value. I am surprised that it works even on Android without this. On iOS it certainly won't work at all -- iOS requires a beacon proximityUUID be specified up front in order to detect. The value you give for the prximityUUID must exactly match what your beacon is advertising or you won't see it.
Make sure you have gone through all the iOS setup steps here: https://pub.dev/packages/flutter_beacon
Be extra sure that you have granted location permission to your iOS app. You can go to Settings -> Your App Name to check if location permission is granted.
Make sure bluetooth is enabled in settings for the phone
Make sure location is enabled in settings for the phone
https://pub.dev/packages/flutter_beacon
There's an update on GitHub but was yet to push to pub.dev previously.
Do update to 0.5.0.
Always check pub.dev for updates. or github reported issues.
Is there any way to check if my device supports BLE through dart code? I am looking for something like this.
switch ([_manager state])
{
case CBCentralManagerStateUnsupported:
state = #"This device does not support Bluetooth Low Energy.";
break;
case CBCentralManagerStateUnauthorized:
state = #"This app is not authorized to use Bluetooth Low Energy.";
break;
case CBCentralManagerStatePoweredOff:
state = #"Bluetooth on this device is currently powered off.";
break;
case CBCentralManagerStateResetting:
state = #"The BLE Manager is resetting; a state update is pending.";
break;
case CBCentralManagerStatePoweredOn:
state = #"Bluetooth LE is turned on and ready for communication.";
break;
case CBCentralManagerStateUnknown:
state = #"The state of the BLE Manager is unknown.";
break;
default:
state = #"The state of the BLE Manager is unknown.";
}
I know this is an old thread, Thought of updating this anyway as it might help someone else.
You can try using FlutterBlue bluetooth plugin, which is a new SDK from Flutter.
FlutterBlue aims to offer the most from both platforms (iOS and Android).
FlutterBlue should work for both Android and IOS platform. The plugin should also help to scan and connect nearby devices.
Here is the sample from the documentation:
[https://github.com/pauldemarco/flutter_blue#obtain-an-instance][1]
Obtain an instance:
FlutterBlue flutterBlue = FlutterBlue.instance;
isAvailable property checks if whether the device supports Bluetooth
Future<bool> get isAvailable =>
_channel.invokeMethod('isAvailable').then<bool>((d) => d);
isOn property Checks if Bluetooth functionality is turned on :
Future<bool> get isOn => _channel.invokeMethod('isOn').then<bool>((d) => d);
Scan for devices :[https://github.com/pauldemarco/flutter_blue#scan-for-devices][2]
// Start scanning
flutterBlue.startScan(timeout: Duration(seconds: 4));
// Listen to scan results
var subscription = flutterBlue.scanResults.listen((scanResult) {
// do something with scan result
device = scanResult.device;
print('${device.name} found! rssi: ${scanResult.rssi}');
});
// Stop scanning
flutterBlue.stopScan();
Connect/disconnect to a device
https://github.com/pauldemarco/flutter_blue#connect-to-a-device
// Connect to the device
await device.connect();
// Disconnect from device
device.disconnect();
Discover services:
https://github.com/pauldemarco/flutter_blue#discover-services
List<BluetoothService> services = await device.discoverServices();
services.forEach((service) {
// do something with service
});
You can scan for low energy devices with ScanMode.lowLatency :
Stream<ScanResult> scan({
ScanMode scanMode = ScanMode.lowLatency,
List<Guid> withServices = const [],
List<Guid> withDevices = const [],
Duration timeout,
}) async* {
var settings = protos.ScanSettings.create()
..androidScanMode = scanMode.value
..serviceUuids.addAll(withServices.map((g) => g.toString()).toList());
if (_isScanning.value == true) {
throw Exception('Another scan is already in progress.');
}
....
You can also read, write the characters and discriptors or do other stuff with the help of FlutterBlue. Hope that helps.
[1]: https://github.com/pauldemarco/flutter_blue#obtain-an-instance
[2]: https://github.com/pauldemarco/flutter_blue#scan-for-devices