I'm getting this error when trying to connect with a divice;
Connecting to 00:A5:54:BB:95:3F (id: 1)
I/BluetoothAdapter(20241): cancelDiscovery
I/BluetoothAdapter(20241): cancelDiscovery = false
D/BluetoothUtils(20241): isSocketAllowedBySecurityPolicy start : device null
I/BluetoothSocket(20241): connect() for device 00A554 called by pid: 20241
W/BluetoothAdapter(20241): getBluetoothService() called with no BluetoothManagerCallback
E/flutter (20241): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(connect_error, read failed, socket might closed or timeout, read ret: -1, java.io.IOException: read failed, socket might closed or timeout, read ret: -1
this is the code snippet of the function:
void connectToDevice(String address) {
BluetoothConnection.toAddress(address).then((connection) {
BluetoothConnection bluetoothConnection;
bluetoothConnection = connection;
// ...
bluetoothConnection.input?.listen((List<int> data) {
// Use a logging framework instead of print
// Example: logger.info('Received data from the device');
print('Received data from the device');
}).onDone(() {
// Use a logging framework instead of print
// Example: logger.info('Disconnected by remote request');
print('Disconnected by remote request');
});
});
}
these are the code snippets of the function to enable bluetooth and show paired devices the below ones workes but not the code for connecting
void enableBluetooth() {
FlutterBluetoothSerial.instance
.requestEnable(); // Request Bluetooth to be turned on
}
void getPairedDevices(BuildContext context) {
FlutterBluetoothSerial.instance
.getBondedDevices() // Get list of paired devices
.then((List<BluetoothDevice> pairedDevices) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Paired Devices'),
content: SingleChildScrollView(
child: ListBody(
children: pairedDevices.map((device) {
return GestureDetector(
child: Text(device.name ?? ''),
onTap: () {
connectToDevice(device.address);
Navigator.of(context).pop();
},
);
}).toList(),
),
),
);
},
);
});
}
Please help me to solve this.
I tried to add permissions:
void main() {
requestPermissions(); // Call requestPermissions function
runApp(MyApp());
}
void requestPermissions() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.location,
Permission.storage,
].request();
print(statuses[Permission.location]);
print(statuses[Permission.storage]);
}
it didn't show any progress, tried to recall the function continuously till it is connected still didnt work.