A plugin for handling Vibration API on iOS, Android, and web. API docs.
benjamindean / flutter_vibration Goto Github PK
View Code? Open in Web Editor NEWHandle vibration on iOS and Android in Flutter apps
Handle vibration on iOS and Android in Flutter apps
A plugin for handling Vibration API on iOS, Android, and web. API docs.
Hey.
I love the latest plugin update that added the CoreHaptics support. Would it be possible for you to create a method (can be named supportCustomVibrations
or whatever) which would return :
true
on Android.true
on CoreHaptics iOS devices (and false
on older devices).This way, we could have a better control on which devices we can play custom vibrations, and on which devices it's not possible to do so.
Thanks !
Tested on iOS: Having an odd number of elements in the pattern list will cause the app to completely crash
Add vibration: ^1.2.4
to pubspec.yaml
.
Add permission
on the AndroidManifest.xml
(main)
Add an example like below:
import 'package:flutter/material.dart';
import 'package:vibration/vibration.dart';
void main() => runApp(VibratingApp());
class VibratingApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Vibration Plugin example app'),
),
body: Builder(builder: (BuildContext context) {
return Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text('Vibrate for default 500ms'),
onPressed: () {
Vibration.vibrate();
},
),
RaisedButton(
child: Text('Vibrate for 1000ms'),
onPressed: () {
Vibration.vibrate(duration: 1000);
},
),
RaisedButton(
child: Text('Vibrate with pattern'),
onPressed: () {
final snackBar = SnackBar(
content: Text(
'Pattern: wait 0.5s, vibrate 1s, wait 0.5s, vibrate 2s, wait 0.5s, vibrate 3s, wait 0.5s, vibrate 0.5s'),
);
Scaffold.of(context).showSnackBar(snackBar);
Vibration.vibrate(
pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500]);
},
)
],
),
);
})),
);
}
}
vibration
not working on my device
)[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.15.2 19C57,
locale en-US)
• Flutter version 1.12.13+hotfix.8 at
/Users/rrifafauzikomara/Documents/flutter
• Framework revision 0b8abb4724 (3 months ago), 2020-02-11 11:44:36 -0800
• Engine revision e1e6ced81d
• Dart version 2.7.0
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at /Users/rrifafauzikomara/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling
support)
• Platform android-29, build-tools 29.0.3
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build
1.8.0_212-release-1586-b4-5784211)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 11.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.4.1, Build version 11E503a
• CocoaPods version 1.8.4
[✓] Android Studio (version 3.6)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 45.1.1
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build
1.8.0_212-release-1586-b4-5784211)
[✓] VS Code (version 1.39.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.5.1
[✓] Connected device (1 available)
• SM A600G • 5200e3e7f01e45f3 • android-arm • Android 9 (API 28)
• No issues found!
what is the name for MethodChannel ? I used following package name in widget tests with no success:
MethodChannel channel =
const MethodChannel('com.benjaminabel.vibration');
channel.setMockMethodCallHandler((MethodCall methodCall) async {
print("=============>>>>> $methodCall <<<<<===================");
});
Hi,
I have :
await Vibration.hasCustomVibrationsSupport()) --> returns false --> i can't use the haptic feature.
2 questions:
On which device have you tested ?
are you supporting ios 14 ?
thx.
In debug mode, My android phone is vibrated by flutter/dart codes. Suddenly I realized that I didn't edit xml for permission at all. Is this normal behavior? 👀
I didn't touch debug/main/profile AndroidManifest.xml at all.
i get an error when i try this:
if (Vibration.hasVibrator()) {
Vibration.vibrate(duration: 500);
return true;
}
saying
Conditions must have a static type of 'bool'.
Try changing the condition.
Even if hasAmplitudeControl()
returns true
in iOS, calling vibrate()
with a custom amplitude parameter doesn't variate the amplitude of the generated vibration. On Android it works correctly
Flutter 2.2.1
Vibration 1.7.3
iPhone 6S with iOS 14.5.1, the only one that I have
I have vibration turned on with a pattern but in some cases I want the vibration to stop earlier, so I use Vibration.cancel().
On android it works great but on iOS it seems to continue to vibrate until the pattern is done.
It seems that the last publication of this plugin misses the README.md
I mean v1.6.1
https://pub.dev/packages/vibration/
It's been long. Please update the package
hello, i'm getting a warning about swift 5 conversion on ios, anything i can do to get rid of that message?
i'm using v 1.2.2.
Thanks.
On Vibration.vibrate() it throws a message null check operator called on null value
Flutter Version 2.8.1
Dart Version 2.15.1
I tried to add this plugin to my app, and tested another one. Both had the same issue on iOS and web.
I basically made it vibrate when clicking a button. On few first clicks, nothing happens, but then suddenly the vibration works.
What could be the issue? I'm just calling Vibration.vibrate()
, not even bothering with the custom duration overload, and not waiting for anything, it's just that function
Can it show 3D Touch vibration
请问是否可以实现3d touch这种,短促的震动?
My visual code is causing an error when I put this method (has) in my code.
Can you give me a hand?
import 'package:flutter/material.dart';
import 'package:vibration/vibration.dart';
void main() => runApp(Home());
class Home extends StatelessWidget {
const Home({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Flutter Flutter",
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text("Flutter vibrate"),
),
body: Body(),
),
);
}
}
class Body extends StatefulWidget {
@override
_BodyState createState() => _BodyState();
}
class _BodyState extends State<Body> {
void vibrate() {
if(Vibration.hasVibrator()){
Vibration.vibrate();
}
}
@override
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: vibrate,
child: Text("Click to vibrate"),
)
);
}
}`
my project has pod a lot of oc pod frameworks,swift need use_frameworks!, but there will make my project player's c/c++ lib conflicts.
Hi,
Does this plugin have to depend on flutter_test ? Flutter_test depends on an old version of the image plugin, which I now can't upgrade because this vibration plugin depends on flutter_test.
Because no versions of vibration match >1.2.3 <2.0.0 and vibration 1.2.3 depends on flutter_test any from sdk, vibration ^1.2.3 requires flutter_test any from sdk.
And because every version of flutter_test from sdk depends on image 2.1.4, vibration ^1.2.3 requires image 2.1.4.
I tried to set amplitude but the intensity doesn't seem to change. on iOS 14.
I am getting errors about SwiftVibrationPlugin on a device using iOS 12. Does this only work on 13 and up? Thanks!
Here is my code:
void _submmit() async {
if (await Vibration.hasVibrator()) {
print("111");
Vibration.vibrate(duration: 500);
if (await Vibration.hasAmplitudeControl()) {
Vibration.vibrate(amplitude: 128);
}
} else {
print("222");
}
}
The log printed "111",but the phone didn't vibrate.
My phone is :iPhone 7
IOS: 13.3.1
.podspec 配置文件中添加
s.swift_version = '4.0'
i want just to vibrate for a few seconds and all this time that it intensifies friom 1 to 255, or rather, reversely, goes from 255 to 1
from you readme i thought that to perform that i'm supposed to do just that
Vibration.vibrate(
duration: duration,
intensities: reverse ? const [255, 1] : const [1, 255],
);
but it doesn't seem to work and that's confusing
upd: testing on iphone x
Vibration.hasVibrator()
in iOS always promis false.
I've checked swift code and you get the value of hasVibrator
from TARGET_OS_SIMULATOR == 1
and then this code always returns false in physical iOS device.
What's wrong?
while try to make a iOS build it crash due to this error-->
Unhandled exception:
FileSystemException(uri=org-dartlang-untranslatable-uri:package%3Avibration%2Fvibration.dart; message=StandardFileSystem only supports file:* and data:* URIs)
#0 StandardFileSystem.entityForUri (package:front_end/src/api_prototype/standard_file_system.dart:34:7)
#1 asFileUri (package:vm/kernel_front_end.dart:623:37)
#2 writeDepfile (package:vm/kernel_front_end.dart:763:21)
#3 FrontendCompiler.compile (package:frontend_server/frontend_server.dart:586:9)
#4 starter (package:flutter_frontend_server/server.dart:85:12)
#5 main (file:///opt/s/w/ir/cache/builder/src/flutter/flutter_frontend_server/bin/starter.dart:13:24)
Failed to package /Users/appledev/Desktop/flutter/flutterClient.
Vibration is not working for Android 10
I have already added below the line in my AndroidManifest.xml:
<uses-permission android:name="android.permission.VIBRATE"/>
Code for Vibration:
if (Vibration.hasVibrator() != null) {
if (Vibration.hasAmplitudeControl() != null) {
Vibration.vibrate(duration: 500,amplitude: 128,);
} else {
Vibration.vibrate();
}
}
In case of Android 10 hasAmplitudeControl condition is satisfied and also there is no error but still, the device is not vibrating.
I am getting this warning when I run flutter:
Note: /usr/local/flutter/.pub-cache/hosted/pub.dartlang.org/vibration-1.2.1/android/src/main/java/com/benjaminabel/vibration/VibrationPlugin.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
I tried using it, and if my screen is off, it doesn't vibrate, until the screen is switched on
Does anyone have any idea about the same? Any fixes or clues?
How can i set the interval of vibration for more than hrs,
eg:
RaisedButton(
child: Text('Vibrate for 1000ms'),
onPressed: () {
Vibration.vibrate(duration: 100000, repeat: 90000000);
},
),
how can i set it for hrs?
or vibrate untill we tap Vibration.cancel();
When calling Vibration.vibrate
in the background (from a callback from firebase_messaging
), nothing seems to happen.
during development of my app i pass qa on these devices and then on relese just bumped the version from 1.7.3
to 1.7.4-nullsafety.0
after app released i received a feedback that vibration is not working on these devices:
is it possible that there were any changes that might have broken the plugin for these?
Hi there! THanks for your cool vibration plugin 👍
My app won't load on iPhone SE simulator since 1.3.0 update.
I have an EXC_BAD_ACCESS
error. Working on a PR. :-)
In the README there is the following example code:
Vibration.vibrate(pattern: [500, 1000, 500, 2000], intensities: [1, 255]);
It doesn't seem to be working on Android:
E/MethodChannel#vibration(10881): Failed to handle method call
E/MethodChannel#vibration(10881): java.lang.IllegalArgumentException: timing and amplitude arrays must be of equal length (timings.length=4, amplitudes.length=2)
E/MethodChannel#vibration(10881): at android.os.VibrationEffect$Waveform.validate(VibrationEffect.java:635)
E/MethodChannel#vibration(10881): at android.os.VibrationEffect.createWaveform(VibrationEffect.java:244)
E/MethodChannel#vibration(10881): at com.benjaminabel.vibration.Vibration.vibrate(Vibration.java:74)
E/MethodChannel#vibration(10881): at com.benjaminabel.vibration.VibrationMethodChannelHandler.onMethodCall(VibrationMethodChannelHandler.java:48)
E/MethodChannel#vibration(10881): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#vibration(10881): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#vibration(10881): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:738)
E/MethodChannel#vibration(10881): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#vibration(10881): at android.os.MessageQueue.next(MessageQueue.java:335)
E/MethodChannel#vibration(10881): at android.os.Looper.loop(Looper.java:183)
E/MethodChannel#vibration(10881): at android.app.ActivityThread.main(ActivityThread.java:7660)
E/MethodChannel#vibration(10881): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#vibration(10881): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/MethodChannel#vibration(10881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
It also doesn't make much sense to set intensity for the time between vibrations.
When I run my application for iOS it fails with this error.
flutter/.pub-cache/hosted/pub.dartlang.org/vibration-1.4.0/ios/Classes/VibrationPlugin.m:2:9: fatal error: 'vibration/vibration-Swift.h' file not found #import <vibration/vibration-Swift.h>
Anyone who can help me to resolve this error?
I am checking whether this plugin works in ios.
so I wrote really simple flutter like following:
import 'package:vibration/vibration.dart';
//...
void _vibrate() {
Vibration.vibrate();
}
// ...
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'vibration test',
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: ()=> _vibrate(),
tooltip: 'vibrate',
child: Icon(Icons.vibration),
),
);
}
when I debug it in Xcode, I got following result in VibrationPlugin.m:
Thread 1: EXC_BAD_ACCESS (code=2, address=0x16b627fe0)
and several [VibrationPlugin registerWithRegistrar:registrar] object on the left window.
Does somebody know what is the reason of it?
After spending long time of research I guess it could be stack overflow problem.
but I don't know why it occurs.
if (Vibration.hasVibrator()) {
}
error: Conditions must have a static type of 'bool'. (non_bool_condition at ....)
if i set [500, 1000, 500, 2000, 500, 3000, 500, 100] this pattern and vibration run continue when user not stop them what i do?
Also how i speed the vibration?
Non-fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: PlatformException(error, timing and amplitude arrays must be of equal length (timings.length=4, amplitudes.length=2), null, java.lang.IllegalArgumentException: timing and amplitude arrays must be of equal length (timings.length=4, amplitudes.length=2)
at android.os.VibrationEffect.createWaveform(VibrationEffect.java:256)
at d.d.a.a.d(Unknown Source:74)
at d.d.a.b.onMethodCall(Unknown Source:147)
at f.a.c.a.j$a.a(Unknown Source:17)
at io.flutter.embedding.engine.f.e.k(Unknown Source:18)
at io.flutter.embedding.engine.f.e.l(Unknown Source:20)
at io.flutter.embedding.engine.f.e.m(Unknown Source:0)
at io.flutter.embedding.engine.f.c.run(Unknown Source:12)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
). Error thrown null.
at StandardMethodCodec.decodeEnvelope(message_codecs.dart:607)
at MethodChannel._invokeMethod(platform_channel.dart:167)
How do I vibrate forever? Do I need my own loop or can I do something with the repeat
param in Vibrator.vibrate
?
Hi! how to vibrate without user interaction? i'm making flutter app that read qr codes, and i want vibrate as feedback once qr is readed.
The vibrate function is usefull when i tap a button or i have interaction with the UI, but if i call from a stream or subscription the vibration is not triggered.
Regards
#import <vibration/vibration-Swift.h> in VibrationPlugin.m throws 'vibration/vibration-Swift.h' file not found when I try to build.
What am I missing?
I'm not sure If this should be reported. But I cannot figure out how this happened. All the codes seem to be right under Build.VERSION.SDK_INT
. Is there anyway to omit the warning or change the code to suppress It? I used SuppressLint
, but It seems to be no relation on this issue.
.pub-cache\hosted\pub.dartlang.org\vibration-1.0.2\android\src\main\java\com\benjaminabel\vibration\VibrationPlugin.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Edit:
.pub-cache\hosted\pub.dartlang.org\vibration-1.0.2\android\src\main\java\com\benjaminabel\vibration\VibrationPlugin.java:37: warning: [deprecation] vibrate(long) in Vibrator has been deprecated
vibrator.vibrate(duration);
^
.pub-cache\hosted\pub.dartlang.org\vibration-1.0.2\android\src\main\java\com\benjaminabel\vibration\VibrationPlugin.java:54: warning: [deprecation] vibrate(long[],int) in Vibrator has been deprecated
vibrator.vibrate(patternLong, repeat);
^
2 warnings
This is the details extracted, but this seems to be correct in Logic...um....
It would be really cool to check, whether the vibration is enabled by the user, so that one can respect the user settings and vibrate only if the vibration is enabled.
Downloading dependencies
-> Installing Flutter (1.0.0)
-> Installing vibrate (0.0.1)
- Running pre install hooks
[!] Unable to determine Swift version for the following pods:
- vibrate
does not specify a Swift version and none of the targets (Runner
) integrating it have the SWIFT_VERSION
attribute set. Please contact the author or set the SWIFT_VERSION
attribute in at least one of the targets that integrate this pod.
/Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.0/lib/cocoapods/installer/xcode/target_validator.rb:115:in verify_swift_pods_swift_version' /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.0/lib/cocoapods/installer/xcode/target_validator.rb:37:in
validate!'
/Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.0/lib/cocoapods/installer.rb:459:in validate_targets' /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.0/lib/cocoapods/installer.rb:138:in
install!'
/Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.0/lib/cocoapods/command/install.rb:48:in run' /Library/Ruby/Gems/2.3.0/gems/claide-1.0.2/lib/claide/command.rb:334:in
run'
/Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.0/lib/cocoapods/command.rb:52:in run' /Library/Ruby/Gems/2.3.0/gems/cocoapods-1.6.0/bin/pod:55:in
<top (required)>'
/usr/local/bin/pod:22:in load' /usr/local/bin/pod:22:in
I have no problem on android but tried on 2 devices in ios it didn't work
He's working on a sample project on iOS. Doesn't work on my project I have repeatedly checked if everything is the same.
I would be glad if you could help about the problem.
thanks in advance
Vibration.hasVibrator() seems to return true
on iPads, event though they don't support this.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.