We are developing with flutter.
I am creating an application using bluetooth, but the implementation using flutter_blue is not working at all.
This is my first time using bluetooth, and if there is anything I don't know or lack of knowledge, please let me know.
Smartphone: Android 9
flutter_blue:^0.7.2
1, Scan Does Not Work
I'm thinking of using the Bluetooth shutter I bought from Daiso to make it work when I press the button.
Even if you startscan, most probability is that Daiso shutter device will not be retrieved.
For some reason, if you scan immediately after OFF→ON, you may be able to get it well.
2, Service is not getting well
If the scan works and the connect works (for some reason, the connection is already done when the scan works...), I'm trying to get a service and make it work specifically when the shutter button is pressed by Notify.
The list of services always returns length 0 (nothing has been retrieved).
Is it possible that Daiso's Bluetooth doesn't have a service...?
No matter how hard I tried the above two points, it didn't work, so I ended up asking a question.
Also, the Bluetooth of the smartphone itself works well every time to connect to the Daiso shutter.However, most apps are not included in the scan.
Also, when connecting the Daiso shutter to your smartphone with Bluetooth, it is almost impossible to include it in the scan.It's connected!
Below is the code that I have struggled with so far.
If you have any questions, I would appreciate it if you could comment.
Thank you for your reply!!
(We are also asking questions on other question sites!)
// Start scanning
try{
flutterBlue.isOn.then(bool isOn){
if(isOn){
flutterBlue.isScanning.first.then(isScanning)async{
if(!isScanning){
wait startScanning();
}
});
} else{
Fluttertoast.showToast(msg:'Turn Bluetooth ON');
}
});
} on Exception catch(e){
print('start scan error:$e');
}
return SafeArea(
child —Column(
children:<Widget>[
// Sometimes the Daiso shutter doesn't get stuck in the scanResults here...
StreamBuilder <List<ScanResult>>(
stream —flutterBlue.scanResults,
builder: (context, snapshot) {
// narrow down to connectable
final scanList=snapshot.data
.where(scanResult)=>
scanResult.advertisementData.connectable)
.toList();
return ListView.builder(
shrinkWrap:true,
itemCount —scanList.length,
itemBuilder: (context, index) {
Return Card(
// Retrieve what is already connected
child —StreamBuilder<List<BluetoothDevice>>(
stream —flutterBlue.connectedDevices.asStream(),
initialData: [ ],
builder: (c, snapshot) {
// True if connected
varisConnect=false;
for (final device in snapshot.data) {
if(device.id.toString()==
scanList[index].device.id.toString()){
isConnect = true;
// Services length is 0 here...
device.services.listen(
(services) {
services.forEach(
(service) {
service.characteristics.forEach(
(characteristics) {
characteristics.setNotifyValue(true);
characteristics.value.listen(
(val) {
print(val);
},
);
},
);
},
);
},
);
}
}
US>return ListTile(
// Connection in progressing
leading —isConnect
? Icon(
Icons.bluetooth_connected,
color:Colors.blue,
)
: const SizedBox.shrink(),
onTap:() async {
if(!isConnect){
wait showBluetoothConnect(
scanList [index].device, showVM);
} else{
waitFluttertoast.showToast(msg:' Already Connected');
}
},
);
},
),
);
},
);
},
),
],
),
);
}
// scan stop
Future<void>stopScanning()async {
waitflutterBlue.stopScan();
}
// scan start
Future<void>startScanning()async {
waitflutterBlue.startScan(timeout:const Duration(seconds:4));
}
// CONNECTION
Future<void>showBluetoothConnect (BluetoothDevice device) async {
try{
final state = wait device.state.first;
if(state==BluetoothDeviceState.connected){
waitFluttertoast.showToast(msg:' Already Connected');
return;
}
wait device.disconnect();
bool isConnect;
wait device.connect().timeout(const Duration(seconds:5),
onTimeout:() async {
debugPrint ('timeout occurred');
isConnect=false;
waitFluttertoast.showToast(msg: 'Unable to connect to Bluetooth device');
wait device.disconnect();
}).then(data){
if(isConnect==null){
Fluttertoast.showToast(msg:'Connected to Bluetooth device!');
}
});
} on Exception catch(e){
waitFluttertoast.showToast(msg: 'Unable to connect to Bluetooth device');
print('Bluetooth connect error:$e');
}
}
Without using flutter_blue, I was able to read it in one of two patterns, using the volume button being manipulated and recognized as a keyboard!!
·Use RawKeyboardListener
Note: https://stackoverflow.com/questions/54200938/external-keyboard-in-flutter-support
https://api.flutter.dev/flutter/widgets/RawKeyboardListener-class.html
·Use hardware_buttons
Note: https://pub.dev/packages/hardware_buttons
However, if you press the button, the volume will be operated, so you have to verify it.
Currently, we do not know how to intercept volume operations when searching.
However, you may be able to use the same name method used in the following JavaScript article for "But if you press the button, you will also need to verify the volume."
JavaScript Articles
What's the difference between event.stopProduction and event.preventDefault?
Q:
They seeem to be doing the same thing...
Is one modern and one old? Are they supported by different browsers?
What is the difference between event.stopPropagation and event.preventDefault?
They seem to be doing the same thing...
Is one modern and the other old or supported by a different browser?
A:
stopPropagation
stop the event from bubbling up the event chain.
Prevents events from bubbling the event chain.
preventDefault
prevents the default action the browser makes on that event.
Prevents the default actions that the browser takes for the event.
MDN Description
Event.preventDefault()
The eventDefault() method on the Event interface tells the user agent that the default action should not be taken normally if the event is not explicitly processed.
stopPropagation() is an Event interface method that suppresses further propagation during the current event capture or bubbling process.
JavaScript preventDefault() is the only article that says it can't be stopped.
event.preventDefault() on keydown isn't working [duplicate]
Q:
Trying to capture the key presses on a Bootstrap Tab Panel menus, but just bubbles up ignore the preventDefault() placed on the tabs' keydown handler.
I try to capture the keypress from the menu on the Bootstrap tab panel, but I ignore the preventDefault() in the key-down handler on the tab and pop up.
document.onkeydown=function(e){
console.log(" document caught the keydown event";
};
$('body>div>ul>li>a').on("keydown", function(e){
console.log("handled by the child-stop bubbling please";
e.preventDefault();
});
A:
Try.stopPropagation()
e.stopPropagation()prevents the event from bubbling up the DOM tree,preventing any parent handlers from being notified of the event.
Try e.stopPropagation().
e.stopPropagation() prevents events from popping up in the DOM tree and notification of events to the parent handler.
$('body>div>ul>li>a').on("keydown", function(e){
console.log("handled by the child-stop bubbling please";
e.preventDefault();
e.stopPropagation();
});
Flutter Documents
Flutter>dart: html>KeyEvent class
KeyEvent class
Methods
preventDefault()→void
Inherited
stopPropagation()→void
Inherited
preventDefault method
stopPropagation method
Is there any way to disable the volume key operation on Flutter?Question
There is no answer and it has not been resolved yet.
Flutter:How can I preempt default behavior on key press?
I'm trying to intercept when a user presses the volume buttons to perform a specific action and prevent the default behavior (volume changes).
You are trying to prevent the default behavior (change volume) when the user presses the volume button to perform a specific behavior.
RawKeyboard.instance.addListener(_keyboardListener);
void_keyboardListener(RawKeyEvent) {
if(e.runtimeType==RawKeyUpEvent){
RawKeyEventDataAndroidA=e.data;
if(eA.keyCode==24) {//volume upkey
_goNextPage();
}
if(eA.keyCode==25) {//volume downkey
_goPrevPage();
}
}
}
How would I go about presenting the volume from changing (and stopping the volume slider from applying at the top)?
A Javascript analogous would be calling event.preventDefault()
on the key event.
This sees to be a parent trial matter, but I haven't been able to find any answers in the docs.
How do I prevent the volume from changing (and the volume slider from being displayed at the top)?
A similarity in Javascript is to invoke event.preventDefault()
in key events.
This seems to be quite trivial, but I couldn't find the answer in the document.
© 2024 OneMinuteCode. All rights reserved.