How can I set my device name for bluetooth in Flutter?
In Java, there is one plugin BluetoothAdapter which could be used to set the bluetootg device name, but in Flutter, I did not find a way to do this.
thanks
Try FlutterBlue which is a bluetooth plugin for Flutter. You should able to do that via that plugin. Here is an example of reading/writing a characteristic from that page just in case if link goes poof:
// Reads all characteristics
var characteristics = service.characteristics;
for(BluetoothCharacteristic c in characteristics) {
List<int> value = await c.read();
print(value);
}
// Writes to a characteristic
await c.write([0x12, 0x34])
Related
I am using the device_info_plus plugin like this:
Future<void> getHardwareID() async {
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
final AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
hwid = androidInfo.androidId;
} else if (Platform.isIOS) {
final IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
hwid = iosInfo.identifierForVendor;
}
}
The issue is that when I use the same phone with different versions of the app - so if I upload a new bundle for testing for instance - then androidInfo.androidId will be something different. I suppose this is what they mean with the description
The Android hardware device ID that is unique between the device +
user and app signing.
Eventhough I think with app signing they mean the keystore file and properties which do not change so I am not sure if this is the reason
Apple seems to work fine since iosInfo.identifierForVendor just returns the
Unique UUID value identifying the current device
This is a problem since I would like to keep track of users in a database without them having to register with an account. (How) Is this possible?
There is an id property described like this:
Either a changelist number, or a label like M4-rc20
But that does not sound like what I am looking for
How can I get an ID from an android device that does not change if the app version changes?
I have developed a React Native app for Android device to connect with a HW board and one of the functionality is to communicate with HW board with Serial I/F Adapter from Mobile .
I have tried out multiple npm packages and none of them I could get to work.
Here is my sample code
import SerialPortAPI from 'react-native-serial-port-api';
const path = await SerialPortAPI.devicePaths(paths => {
console.log("List paths", paths)
})
const connectDevice = async (cfg) => {
const { baudRate, serialPortName, stopBits } = cfg
serialPort = await SerialPortAPI.open(serialPortName, { baudRate, stopBits});
const sub = serialPort.onReceived(buff => {
const str = buff.toString('hex').toUpperCase()
console.log(str);
})
await serialPort.send('A7B7');
}
It is NOT listing the device List connected and also not able to open/write/read.
Other packages I tried are:
react-native-usbserial
react-native-serialport
react-native-usb-serialport
react-native-serial-port-api
I any pointers and working sample will be of great help.
Regards
Raghu VT
Are you connecting the phone to your pc or it should work as host? I think that is key point to understand.
My phone is Host and I started with Android code.
Would recommend to use Android to test if possible.
You want to pay attention to the port type used.
If you use a USB AB connector you need to use an otg cable or adapter.
if case of type C , this will be detected automatically.
Hope could provide some hints
When I try to use the flutter blue library to enable notifications I get not the full string returned.
The code I use:
await characteristic.setNotifyValue(true);
characteristic.value.listen((value) {
print(utf8.decode(value));
});
It returns me
{"PumpType":"Demopri
While I should get
{"PumpType":"Demopring","Power":1,"TempWater":28.5,"TempAmbient":22.0,"TempSet":29.0,"ErrorMessages":""}
It seems to cut of my message but when I use a serial monitoring application or a native Android application I get the full string, so there is no problem with the peripheral.
The device I ran all my tests on is an Android 10 device and it worked with native Android app.
I solved it by changing the MTU size in flutter blue.
final mtu = await device.mtu.first;
await device.requestMtu(512);
I found I had to request the change of MTU size and then wait for it to be fully negotiated otherwise the stream attached to characteristic didn't trigger for the notify events.
await connectedDevice.requestMtu(characteristicMTULength);
int mtu = await device.mtu.first;
while (mtu != characteristicMTULength) {
print("Waiting for requested MTU");
await Future.delayed(Duration(seconds: 1));
mtu = await device.mtu.first;
}
I'm trying to receive and send data from and to an arduino uno. I've tried looking into flutter blue plugin and flutter bluetooth serial plugin ,flutter serial plugin
seems to be incomplete and flutter blue lacks examples or documentation, and the official github example is way too complicated and is irrelevant to what i want to do. I want a very simple method of sending or retrieving data from an arduino using a HC-05 module.
If you are working with a HC-05 module (No Low Energy Bluetooth). Use ´flutter_bluetooth_serial´ package. It is not a great package, but it should work.
This example may not work.
Scan for devices:
//Here the scan results will be saved
List<BluetoothDiscoveryResult> results = List<BluetoothDiscoveryResult>();
void startDiscovery() {
streamSubscription = FlutterBluetoothSerial.instance.startDiscovery().listen((r) {
results.add(r);
});
streamSubscription.onDone(() {
//Do something when the discovery process ends
});
}
Connect to device:
Use this function when you select a device from the result list.
BluetoothConnection connection;
connect(String address) async {
try {
connection = await BluetoothConnection.toAddress(address);
print('Connected to the device');
connection.input.listen((Uint8List data) {
//Data entry point
print(ascii.decode(data));
})
} catch (exception) {
print('Cannot connect, exception occured');
}
}
Send data:
Future send(Uint8List data) async {
connection.output.add(data);
await _connection.output.allSent;
}
Try the example app from the ´flutter_bluetooth_serial´-package now. They even included reading data from an HC-05-module! If you want something simpler, try extracting the essential code from the example and copycoding it into another app.
I follows Google/chrome samples for Web Bluetooth. I have two writeValue operations. One is within the requestDevice promise and it works perfectly. Second one, I save the characteristic reference and writeValue when the action trigger. The message is sent but connection broke automatically. I am using Mac OSX 10.13.3 and chrome64 and modify Android BLE Peripheral Simulator (From google github)
The code segment ---
var bluetoothDevice;
var myCharacteristic;
navigator.bluetooth.requestDevice({
...
})
.then(device => {
bluetoothDevice = device;
....
})
....
.then(characteristic =>{
myCharacteristic = characteristic;
let encoder = new TextEncoder('utf-8');
let sendMsg = encoder.encode("hello");
/*
It works...
*/
myCharacteristic.writeValue(sendMsg);
})
....
function onSendMessage() {
if (!bluetoothDevice.gatt.connected) {
log("device is disconnected")
return
}
if (!myCharacteristic){
log("no characteristic defined")
return
}
let encoder = new TextEncoder('utf-8');
let sendMsg = encoder.encode("hello");
/*
message sent but auto disconnect the peripheral
*/
myCharacteristic.writeValue(sendMsg);
}
Does anyone has same experience and any suggestion for keep connection persistence for writeValue?
Your code looks good to me. A similar code can be found as well at https://googlechrome.github.io/samples/web-bluetooth/link-loss.html
However I wonder which characteristic you're trying to write to. If that is not supported, the Android device might simply disconnect.
Check out https://www.chromium.org/developers/how-tos/file-web-bluetooth-bugs#TOC-Android and grab Android adb logs if that helps.