Jithin U. Ahmed
Jithin U. Ahmed

Reputation: 1801

Flutter blue instance connection disconnects automatically in Android

I have completed an application written in Flutter with BLE and was working perfectly in iOS.

But when I run the same application in Android, it was not working. So I checked the log and found that the application is not connected to the Bluetooth Device stable and as soon as it is connected, it disconnects.

So I wrote a code to listen to the state change in bluetooth connection and reconnect when the state change to disconnect and found in the log that it keep on connecting and disconnecting like in a loop.

Please note the same is working perfectly in iOS.

Launching lib/main.dart on TRT L21A in debug mode...
Built build/app/outputs/apk/debug/app-debug.apk.
I/flutter (10374): getHOME, statusCode = 200
I/flutter (10374): Body Batch: {status: success, target_steps: 10000, today_steps: 0, calorie_target: 2000, today_calories: 0, totaldistance: 0, team_leader: no}
I/hwaps   (10374): JNI_OnLoad
I/BluetoothAdapter(10374): getBluetoothLeScanner
I/flutter (10374):
I/BluetoothAdapter(10374): getBluetoothLeScanner
I/BluetoothGatt(10374): connect is called
I/BluetoothGatt(10374): connect is called
I/art     (10374): Do partial code cache collection, code=30KB, data=30KB
I/art     (10374): After code cache collection, code=30KB, data=30KB
I/art     (10374): Increasing code cache capacity to 128KB
I/flutter (10374): State is BluetoothDeviceState.disconnected
I/BluetoothGatt(10374): connect is called
I/flutter (10374): State is BluetoothDeviceState.disconnected
I/BluetoothGatt(10374): connect is called
I/flutter (10374): State is BluetoothDeviceState.disconnected
I/BluetoothGatt(10374): connect is called
I/flutter (10374): State is BluetoothDeviceState.connected
I/flutter (10374): State is BluetoothDeviceState.connected
I/flutter (10374): State is BluetoothDeviceState.connected
I/flutter (10374): State is BluetoothDeviceState.connected

Upvotes: 0

Views: 3982

Answers (1)

MαπμQμαπkγVπ.0
MαπμQμαπkγVπ.0

Reputation: 6729

There is an open GitHub issue related to your question. Some have a quick fix including this comment:

I simply keep a flag hasConnected, which i set on connected, and then I only care about disconnects after hasConnected is true. Might not solve everything, but should take you pass the first one. Also I discovered a 'disconnect' and then a second 'disconnect' when there was an error, so simply ignoring the first message if its a disconnect solves it for me.

Upvotes: 1

Related Questions