flutter_bluetooth_serial
flutter_bluetooth_serial copied to clipboard
Connect error, cant connect to device
java.io.IOException: read failed, socket might closed or timeout, read ret: -1 at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:970) at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:984) at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:536) at io.github.edufolly.flutterbluetoothserial.BluetoothConnection.connect(BluetoothConnection.java:57) at io.github.edufolly.flutterbluetoothserial.BluetoothConnection.connect(BluetoothConnection.java:64) at io.github.edufolly.flutterbluetoothserial.FlutterBluetoothSerialPlugin$FlutterBluetoothSerialMethodCallHandler.lambda$onMethodCall$4$FlutterBluetoothSerialPlugin$FlutterBluetoothSerialMethodCallHandler(FlutterBluetoothSerialPlugin.java:1007) at io.github.edufolly.flutterbluetoothserial.-$$Lambda$FlutterBluetoothSerialPlugin$FlutterBluetoothSerialMethodCallHandler$Q4kMxByjf_xgX66iy8HZNeJ0z8I.run(Unknown Source:10) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:923)
@awaisjamil-vd did you find any solution for this?
@awaisjamil-vd did you find any solution for this?
Nope, I had to write whole bluetooth module in native android and then used it with flutter.
Oh, okay. Can you share some sample or something which could help ease the integration for me and others as well.
Oh, okay. Can you share some sample or something which could help ease the integration for me and others as well.
class BluetoothChatUtils { private val bluetoothAdapter: BluetoothAdapter private var connectThread: ConnectThread? = null private var acceptThread: AcceptThread? = null private var connectedThread: ConnectedThread? = null private val APP_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66") private val APP_NAME = "BluetoothChatApp" private val executorService = Executors.newSingleThreadExecutor()
@set:Synchronized
var state: Int
private val STATE_NONE = 0
private val STATE_LISTEN = 1
private val STATE_CONNECTING = 2
private val STATE_CONNECTED = 3
private val FLAG_MSG = 0
companion object {
private var handler: Handler? = null
@Volatile
private var instance: BluetoothChatUtils? = null
fun getInstance(handler: Handler?): BluetoothChatUtils? {
this.handler = handler
if (instance == null) {
synchronized(BluetoothChatUtils::class.java) {
if (instance == null) {
instance = BluetoothChatUtils()
}
}
}
return instance
}
}
init {
state = STATE_NONE
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
}
@Synchronized
private fun start() {
if (connectThread != null) {
connectThread!!.cancel()
connectThread = null
}
if (acceptThread == null) {
acceptThread = AcceptThread()
acceptThread!!.start()
}
if (connectedThread != null) {
connectedThread!!.cancel()
connectedThread = null
}
state = STATE_LISTEN
}
@Synchronized
fun stop() {
if (connectThread != null) {
connectThread!!.cancel()
connectThread = null
}
if (acceptThread != null) {
acceptThread!!.cancel()
acceptThread = null
}
if (connectedThread != null) {
connectedThread!!.cancel()
connectedThread = null
}
state = STATE_NONE
}
fun connect(device: BluetoothDevice?) {
if (state == STATE_CONNECTING) {
connectThread!!.cancel()
connectThread = null
}
connectThread = ConnectThread(device!!)
connectThread!!.start()
if (connectedThread != null) {
connectedThread!!.cancel()
connectedThread = null
}
state = STATE_CONNECTING
}
fun write(msg: String?) {
var connThread: ConnectedThread?
synchronized(this) {
if (state != STATE_CONNECTED) {
return
}
connThread = connectedThread
}
connThread!!.write(msg)
}
private inner class AcceptThread : Thread() {
private val serverSocket: BluetoothServerSocket?
override fun run() {
var socket: BluetoothSocket? = null
try {
socket = serverSocket!!.accept()
} catch (e: IOException) {
Log.e("Accept->Run", e.toString())
try {
serverSocket!!.close()
} catch (e1: IOException) {
Log.e("Accept->Close", e.toString())
}
}
if (socket != null) {
when ([email protected]) {
STATE_LISTEN, STATE_CONNECTING -> connected(socket, socket.remoteDevice)
STATE_NONE, STATE_CONNECTED -> try {
socket.close()
} catch (e: IOException) {
Log.e("Accept->CloseSocket", e.toString())
}
}
}
}
fun cancel() {
try {
serverSocket!!.close()
} catch (e: IOException) {
Log.e("Accept->CloseServer", e.toString())
}
}
init {
var tmp: BluetoothServerSocket? = null
try {
tmp = bluetoothAdapter.listenUsingRfcommWithServiceRecord(APP_NAME, APP_UUID)
} catch (e: IOException) {
Log.e("Accept->Constructor", e.toString())
}
serverSocket = tmp
}
}
private inner class ConnectThread(private val device: BluetoothDevice) : Thread() {
private val socket: BluetoothSocket?
override fun run() {
try {
socket!!.connect()
} catch (e: IOException) {
Log.e("Connect->Run", e.toString())
try {
socket!!.close()
} catch (e1: IOException) {
Log.e("Connect->CloseSocket", e.toString())
}
connectionFailed()
return
}
synchronized(this@BluetoothChatUtils) { connectThread = null }
connected(socket, device)
}
fun cancel() {
try {
socket!!.close()
} catch (e: IOException) {
Log.e("Connect->Cancel", e.toString())
}
}
init {
var tmp: BluetoothSocket? = null
try {
tmp = device.createRfcommSocketToServiceRecord(APP_UUID)
} catch (e: IOException) {
Log.e("Connect->Constructor", e.toString())
}
socket = tmp
}
}
private inner class ConnectedThread(private val socket: BluetoothSocket) : Thread() {
private val inputStream: InputStream?
private val outputStream: OutputStream?
private val outData: DataOutputStream
private val inData: DataInputStream
override fun run() {
while (true) {
try {
when (inData.readInt()) {
FLAG_MSG -> {
val msg = inData.readUTF()
handler!!.obtainMessage(MainActivity.BLUE_TOOTH_READ, -1, -1, msg).sendToTarget()
}
}
} catch (e: IOException) {
connectionLost()
break
}
}
}
fun write(msg: String?) {
executorService.execute {
try {
outData.writeInt(FLAG_MSG)
outData.writeUTF(msg)
} catch (e: Throwable) {
Log.i("error", "Failed to send")
}
handler!!.obtainMessage(MainActivity.BLUE_TOOTH_WRITE, -1, -1, msg).sendToTarget()
}
}
fun cancel() {
try {
socket.close()
} catch (e: IOException) {
}
}
init {
var tmpIn: InputStream? = null
var tmpOut: OutputStream? = null
try {
tmpIn = socket.inputStream
tmpOut = socket.outputStream
} catch (e: IOException) {
}
inputStream = tmpIn
outputStream = tmpOut
outData = DataOutputStream(outputStream)
inData = DataInputStream(inputStream)
}
}
private fun connectionLost() {
handler!!.obtainMessage(MainActivity.BLUE_TOOTH_TOAST, -1, -1, "Connection Lost").sendToTarget()
[email protected]()
}
@Synchronized
private fun connectionFailed() {
handler!!.obtainMessage(MainActivity.BLUE_TOOTH_TOAST, -1, -1, "Cant connect to the device").sendToTarget()
[email protected]()
}
@Synchronized
private fun connected(socket: BluetoothSocket?, device: BluetoothDevice) {
if (connectThread != null) {
connectThread!!.cancel()
connectThread = null
}
if (connectedThread != null) {
connectedThread!!.cancel()
connectedThread = null
}
connectedThread = ConnectedThread(socket!!)
connectedThread!!.start()
handler!!.obtainMessage(MainActivity.BLUE_TOOTH_SUCCESS, -1, -1, device).sendToTarget()
state = STATE_CONNECTED
}
}
@awaisjamil-vd thanks.
have you update this?
no. I haven't.
Hi @awaisjamil-vd,
I would like to know an example of flutter application.
How are you using this native class?
If there is any flutter project at github, could you share with me?
I'm looking forward to hearing from you.
Hi @awaisjamil-vd ,
is it possible for you to share an example of how you are using the andriod library with flutter?
Hi @awaisjamil-vd ,
Can you share the flutter integration? it will be helpful for many.
Thanks
Hi @awaisjamil-vd ,
Can you please tell me what was the occurance rate of this issue using this library?
After integrating your library was this issue completely resolved for you?
Thanks
@sasikdar, @amiruljack , @BrenoNaSa is this issue resolved for you?
@sasikdar, @amiruljack , @BrenoNaSa is this issue resolved for you?
no.
I did not check if there are new fixes. But it did not work for me. I ended up using Flutter_nearby. There is a similar package from Huwaeii too :-)
@amiruljack okay.
@sasikdar okay.
@sasikdar were you able to solve this connection issue using this flutter_nearby package?