question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android] Error: Device ? is already connected

See original GitHub issue

I’m using following, very simple, piece of boilerplate code.


...

manager.startDeviceScan(null, null, (error, device) => {
      if (error) {
        console.warn(error)
        return
      }

      if (device.name === this.DEVICE_NAME) {
         this.connectTo(device, false)
      }
    })

...

connectTo = (device, isLeft) => {
   device.connect()
      .then((device) => {
         console.log("[Info][connectTo][success]", device)
      })
      .catch((error) => {
         console.log('[Error][connectTo][error]: ', error)
      })
}

... 

The code runs as expected on iOS devices, but fails on Android devices.

Every-time I try to connect a device, I’m getting BleErrorCode.DeviceAlreadyConnected error, viz.

[Error][connectTo]:  Error: Device ? is already connected
    at new BleError (blob:http://192.168.86.39:19001/fed3bf61-e367-4015-871f-d29b37905b0e:114897:110)
    at parseBleError (blob:http://192.168.86.39:19001/fed3bf61-e367-4015-871f-d29b37905b0e:114917:18)
    at BleManager._callPromise$ (blob:http://192.168.86.39:19001/fed3bf61-e367-4015-871f-d29b37905b0e:115168:51)
    at tryCatch (blob:http://192.168.86.39:19001/fed3bf61-e367-4015-871f-d29b37905b0e:17030:19)
    at Generator.invoke [as _invoke] (blob:http://192.168.86.39:19001/fed3bf61-e367-4015-871f-d29b37905b0e:17203:24)
    at Generator.prototype.(anonymous function) [as throw] (blob:http://192.168.86.39:19001/fed3bf61-e367-4015-871f-d29b37905b0e:17073:23)
    at tryCatch (blob:http://192.168.86.39:19001/fed3bf61-e367-4015-871f-d29b37905b0e:17030:19)
    at invoke (blob:http://192.168.86.39:19001/fed3bf61-e367-4015-871f-d29b37905b0e:17106:22)
    at blob:http://192.168.86.39:19001/fed3bf61-e367-4015-871f-d29b37905b0e:17118:15
    at tryCallOne (blob:http://192.168.86.39:19001/fed3bf61-e367-4015-871f-d29b37905b0e:16359:14)

[Error][connectTo]:  Error: Operation was cancelled
    at new BleError (blob:http://192.168.86.39:19001/d1e699a5-985a-40dc-a7a0-7b2a4f49e47a:114897:110)
    at parseBleError (blob:http://192.168.86.39:19001/d1e699a5-985a-40dc-a7a0-7b2a4f49e47a:114917:18)
    at BleManager._callPromise$ (blob:http://192.168.86.39:19001/d1e699a5-985a-40dc-a7a0-7b2a4f49e47a:115168:51)
    at tryCatch (blob:http://192.168.86.39:19001/d1e699a5-985a-40dc-a7a0-7b2a4f49e47a:17030:19)
    at Generator.invoke [as _invoke] (blob:http://192.168.86.39:19001/d1e699a5-985a-40dc-a7a0-7b2a4f49e47a:17203:24)
    at Generator.prototype.(anonymous function) [as throw] (blob:http://192.168.86.39:19001/d1e699a5-985a-40dc-a7a0-7b2a4f49e47a:17073:23)
    at tryCatch (blob:http://192.168.86.39:19001/d1e699a5-985a-40dc-a7a0-7b2a4f49e47a:17030:19)
    at invoke (blob:http://192.168.86.39:19001/d1e699a5-985a-40dc-a7a0-7b2a4f49e47a:17106:22)
    at blob:http://192.168.86.39:19001/d1e699a5-985a-40dc-a7a0-7b2a4f49e47a:17118:15
    at tryCallOne (blob:http://192.168.86.39:19001/d1e699a5-985a-40dc-a7a0-7b2a4f49e47a:16359:14)

This sequence basically repeats until somehow devices reconnects on it’s own. I don’t handle reconnection from my side.

On the other hand, when I try to call

device.isConncted()

I’m getting false.

As well as when I try to force device disconnect before I try connecting it back again via

device.cancelConnection()

I’m getting BleErrorCode.DeviceNotConnected

Is this a bug? Any suggestions how to bypass this for now?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
HualiangLIcommented, Dec 15, 2020

You may check the connection before connect the device.

    device
      .isConnected() // check whether it's already conncted
      .then((isConnected) => {
        console.log('isConnected::', isConnected);
        return isConnected ? device : device.connect();
      })
      .then((d) => {
        console.log('connect::', d);
        return d.discoverAllServicesAndCharacteristics();
      })
      .then((d) => {
        console.log('discoverAllServicesAndCharacteristics::', d);
        return d.services();
      })
2reactions
asurarecommented, Jul 26, 2019

I don’t really understand why this this issue is closed… Any proper way to fix that error ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Android] Error: Device ? is already connected #271 - GitHub
It seems there is no easy way to tell if the device is connected though. It would be ideal to write code like...
Read more >
setupNotification returns "Error already connected" whereas ...
I establish a BLE connection with a specified device, later while connected I read and write characteristic with no problem, but when i...
Read more >
Company portal enrolment issues: Your device is already ...
The error occuring for my users is "Your device is already connected to your ... From your android mobile Go to Settings >...
Read more >
Not able to Register My Account - Android Enterprise ...
Whenever I try to register my account in google app device policy application it always shows the message that network error try again...
Read more >
Why did I get a maximum devices error on my mobile app
This message indicates you have exceeded the maximum number of active devices. Please log out of a device to solve the issue.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found