Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[classical] devicedisconnected not being fired, sometimes.... #40

Open
sixbladeknife opened this issue Jun 8, 2015 · 0 comments
Open

Comments

@sixbladeknife
Copy link

hi,
I am developing an app that tries to auto-reconnect whenever it looses connections. I've done this by binding on the 'devicedisconnected' event, just before the connect routine (Javascript).

In devices with android >= 4.4.4, SOMETIMES, the app misses to emit that event, so the reconnect procedure has to be made manually. same behaviour on ios 8.3

here is the code

connectDevice : function(dev)
{
console.log("connecting device:" + dev.deviceName);
dev.addEventListener('devicedisconnected', DG.onDeviceDisconnected);
dev.addEventListener('deviceconnected', DG.onDeviceConnected);
BTMod.connectDevice(dev, DG.receiveDataCallback);
},

//this ain't going to be called on disconnect event (from BluetoothSerialService)
onDeviceDisconnected : function()
{
console.log('DG.onDeviceDisconnected ' + DG.connectionError);

    if(DG.connectionError)
    {
        console.log("connection lost...reconnecting");
        BTMod.StartDiscovery(DG.autoReconnect);
    }
},

//BTMod
connectDevice : function(dev, rxCallback)
{
var secure = false; //FIXME set to true
var connectSuccess = function()
{
console.log(BTMod.connectedDevice.deviceName + " connected successfully!");
BTMod.connectedDevice.rfcommSubscribe(rxCallback);
};

    var connectError = function()
    {

        console.log("Error occured connecting device "+BTMod.connectedDevice.deviceName + ", attempting new connection");
        setTimeout(function(){BTMod.connectedDevice.connect(connectSuccess, connectError, BTMod.uuid, secure);}, 200);
    };

    console.log("connecting with " + dev.deviceName + "...");
    BTMod.StopDiscovery();

    if($.isFunction(rxCallback))
    {
        BTMod.connectedDevice = dev;
        BTMod.connectedDevice.connect(connectSuccess, connectError, BTMod.uuid, secure);
    }
    else
    {
        console.error("rxCallback provided is not a function");
    }
    console.log("BTMod.connectDevice " + JSON.stringify(BTMod.connectedDevice));
},

here is some logs:
06-08 14:22:57.819: E/BluetoothSerialService(20718): disconnected
06-08 14:22:57.819: E/BluetoothSerialService(20718): java.io.IOException: bt socket closed, read return: -1
06-08 14:22:57.819: E/BluetoothSerialService(20718): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:435)
06-08 14:22:57.819: E/BluetoothSerialService(20718): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
06-08 14:22:57.819: E/BluetoothSerialService(20718): at java.io.InputStream.read(InputStream.java:162)
06-08 14:22:57.819: E/BluetoothSerialService(20718): at org.bcsphere.bluetooth.BluetoothSerialService$ConnectedThread.run(BluetoothSerialService.java:586)
06-08 14:22:57.821: I/BluetoothSerial(20718): BluetoothSerialService.STATE_NONE
06-08 14:22:57.822: I/chromium(20718): [INFO:CONSOLE(1478)] "rfcommWrite - bc.jswAPHCdMAAQDWiME=", source: file:///android_asset/www/plugins/org/www/org.bcsphere/bc.js (1478)
06-08 14:22:57.844: I/chromium(20718): [INFO:CONSOLE(173)] "sending data: {"0":192,"1":3,"2":199,"3":9,"4":211,"5":0,"6":1,"7":0,"8":214,"9":136,"10":193}", source: file:///android_asset/www/js/bluetooth.js (173)
06-08 14:22:57.846: I/chromium(20718): [INFO:CONSOLE(177)] "Error while sending data!{"1":"there is no connection on device:00:07:80:25:BE:53"}", source: file:///android_asset/www/js/bluetooth.js (177)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant