akexorcist / BluetoothSPPLibrary

[UNMAINTAINED][Android] Bluetooth Serial Port Profile which comfortable to developer application to communication with microcontroller via bluetooth
Apache License 2.0
1.7k stars 572 forks source link

Recreating/Restarting connection #68

Open poralcode opened 7 years ago

poralcode commented 7 years ago

How you recreate or restart the connection after losing it or disconnected from server or client?

I tried to call bt.stopService() and recall onStart();but it's crashing the application

 bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
            public void onDeviceConnected(String named, String addresses) {
                // Do something when successfully connected
            }

            public void onDeviceDisconnected() {
                // Do something when connection was disconnected
                 try{
                    bt.stopService(); //stop service and kill all connection if I'm not mistaken
                    onStart(); // Call onStart to restart the service 
                  }catch (Exception e){}
            }

            public void onDeviceConnectionFailed() {
                // Do something when connection failed
                try{
                    bt.stopService(); //stop service and kill all connection if I'm not mistaken
                    onStart(); // Call onStart to restart the service 
                }catch (Exception e){}

            }
        });