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

connect throw error right after disconnect #162

Closed
ghost opened this issue Sep 21, 2015 · 8 comments
Closed

connect throw error right after disconnect #162

ghost opened this issue Sep 21, 2015 · 8 comments

Comments

@ghost
Copy link

ghost commented Sep 21, 2015

hello, I have a user case like this:

User can choose different devices to connect.

But when I try to connect the other one right after I disconnected from the old one,
error throws.

var mac1 = 'xxxx';

var mac2 = 'yyyy';


var macX; // mac address current using
function switchDevice (mac, done) {

    var connectedYes = function() {
        // check if mac changed
        // if yes nothing will be done
        // else connet
        macX === mac ? done() : bluetoothSerial.disconnect(connect);
    };

    var connectError = function(err) {
        // we always get err here
    };

    var connect = function() {
        macX = mac;
        bluetoothSerial.connect(mac, done, connectError);
    };

    bluetoothSerial.isConnected(connectedYes, connect);
}
@don
Copy link
Owner

don commented Sep 21, 2015

Can you run adb logcat and find the stack trace or more specific error messages and post the details?

If this specific to any Android version or Android hardware?

@ghost
Copy link
Author

ghost commented Sep 22, 2015

adb logcat

I/Web Console(32120): try to check bluetoothSerial isConnected:376
I/Web Console(32120): try to connect:376
V/BluetoothEventManager(30252): Received android.bluetooth.device.action.NAME_CHANGED
D/KeyguardViewMediator(  523): Received android.bluetooth.device.action.ACL_CONNECTED deviceClass = 1028
I/BluetoothConnectionReceiver(30343): onReceive(context, Intent { act=android.bluetooth.device.action.ACL_CONNECTED flg=0x8000010 cmp=com.google.android.googlequicksearchbox/com.google.android.search.core.service.BluetoothConnectionReceiver (has extras) }, [BluetoothDevice: address=8C:DE:52:D9:73:9E, alias=null, name=Gprinter, majorDeviceClass=1024, deviceClass=1028])
I/Web Console(32120): connectSuccess:376
I/Web Console(32120): try to check bluetoothSerial isConnected:376
I/Web Console(32120): already connectted 8C:DE:52:D9:73:9E:376
I/Web Console(32120): try to disconnect first.:376
I/Web Console(32120): try to connect after disconnect:376
E/BluetoothSerialService(32120):    at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:872)
E/BluetoothSerialService(32120):    at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
I/Web Console(32120): after disconnect, but still got an error :376
D/KeyguardViewMediator(  523): Received android.bluetooth.device.action.ACL_CONNECTED deviceClass = 7936
I/BluetoothConnectionReceiver(30343): onReceive(context, Intent { act=android.bluetooth.device.action.ACL_CONNECTED flg=0x8000010 cmp=com.google.android.googlequicksearchbox/com.google.android.search.core.service.BluetoothConnectionReceiver (has extras) }, [BluetoothDevice: address=00:18:E4:2D:7F:41, alias=null, name=Gprinter, majorDeviceClass=7936, deviceClass=7936])
D/KeyguardViewMediator(  523): Received android.bluetooth.device.action.ACL_DISCONNECTED connectedBTforSmartLock = false
V/BluetoothEventManager(30252): Received android.bluetooth.device.action.ACL_DISCONNECTED
I/BluetoothConnectionReceiver(30343): onReceive(context, Intent { act=android.bluetooth.device.action.ACL_DISCONNECTED flg=0x8000010 cmp=com.google.android.googlequicksearchbox/com.google.android.search.core.service.BluetoothConnectionReceiver (has extras) }, [BluetoothDevice: address=8C:DE:52:D9:73:9E, alias=null, name=Gprinter, majorDeviceClass=1024, deviceClass=1028])
V/BluetoothEventManager(30252): Received android.bluetooth.device.action.NAME_CHANGED

@ghost
Copy link
Author

ghost commented Sep 22, 2015

the last lines were loged by switching to the other devices, and no response any more. thank you for so fast reply from you, @don .

@ghost
Copy link
Author

ghost commented Sep 22, 2015

I guess that the disconnect callback was invoked before the connection had really closed.

@don
Copy link
Owner

don commented Sep 23, 2015

The plugin returns success after the service is stopped. But I guess the actual socket takes some time to close. I don't think there's a better Android callback to hook into.

Can you add a delay after disconnecting and before attempting the next connection?

@ghost
Copy link
Author

ghost commented Sep 23, 2015

I will try. thank you very much! And I suggest there should be a better way to switch between devices.

@ghost ghost closed this as completed Sep 23, 2015
@don
Copy link
Owner

don commented Sep 23, 2015

@uxnow I agree adding a timeout isn't a good solution. The right way to do this could be to stay connected to both devices. See #58 for how some other people have done that running multiple versions of the plugin.

@ghost
Copy link
Author

ghost commented Sep 23, 2015

thank you, I will try it.

This issue was closed.
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