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.

How can I list connected devices?

See original GitHub issue

Question

How can I list the connected devices?

Steps:

  • I start the device scan
  • I find the device by name and connect to it to check the services and characteristics
  • I make some changes on the app and run it again
  • This time the scan doesn’t include my device, as it says on the introduction: “However when device is connected it won’t broadcast and needs to be disconnected from central to be scanned again.”
  • Ok, so how can I find the connected device so I can disconnect it, or use it?

I tried let connectedDevices = await manager.connectedDevices(null) or () - gives an error - Attempt to invoke interface method ‘int.com.facebook.react.bridge.ReadableArray.size()’ on a null object reference let connectedDevices = await manager.connectedDevices([]) - gives an empty array

The connectedDevices method needs a serviceUUIDs parameter but how can I get the connected devices regardless of their serviceUUIDs?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
amb-silvertreecommented, Dec 15, 2021

This is still an issue: I specifically run into it during development all the time with the following set up on Android:

  1. use react-native-ble-plx to discover and connect to my BLE device.
  2. As a developer using Metro for react native, press ‘r’ to reload my app.
  3. Now the app has lost the connection state normally returned by connectedDevices(). I get 0 devices listed even though my BLE device is happily still connected to my Android phone.

I can verify this is the problem because I use this (unacceptable for production) quick workaround: if you disable and enable Bluetooth on your Android phone, that will disconnect from your BLE hardware, and now your app state will match the hardware.

While dev mode is the easiest to reproduce this in, it’s relatively easy to see how this would happen in production: literally any crash in your app or killing of your app by a 3rd party service or Android OS will cause this bad state to exist

Until this issue is resolved I have resorted to a dual- library approach: using this library and ALSO react-native-ble-manager together. This is the only solution as of December 2021 that doesn’t require a user to disable and enable Bluetooth on Android.

My shim:

import BleManager from 'react-native-ble-manager'
import { NativeModules, NativeEventEmitter } from 'react-native'

const BleManagerModule = NativeModules.BleManager

// This shim replaces react-native-ble-plx's BleManager.connectedDevices because the latter does not return devices connected that 
// might have been connected before the app was started (or, more likely, before the app was crashed and restarted.)
// This functionality means that we can't use the react-native-ble-plx's BleManager.connectedDevices because our connected device
// won't show up on it. 
const getConnectedDeviceIds = async (serviceUUIDs) => {
    const connectedPeripherals = await BleManager.getConnectedPeripherals(serviceUUIDs)
    return connectedPeripherals.map(peripheral => peripheral.id)
}
export { getConnectedDeviceIds }

And an excerpt of how I use it (in my React Native provider load function):

      // SHIM: Sync the PLX bleManager wearables list with the devices connected in hardware
      // since the PLX bleManager doesn't return them right now.
      const ids = await getConnectedDeviceIds([SILVERTREE_SERVICE_UUID])
      for (id of ids){
        // Sync up PLX's state with the hardware state:
        const connectedDevice = await bleManager.connectToDevice(id, { refreshGatt: 'OnConnected',  autoConnect: false });
        
      } 
      // Now our app state should match the hardware state.
2reactions
nicolas-wlcommented, May 6, 2021

@nbastoWM Did u ever solve this? I’m getting the same error “Attempt to invoke interface method ‘int com.facebook.react.bridge.ReadableArray.size()’ on a null object reference”. However only on android, on iOS it works fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to See Who's Connected to Your Wi-Fi Network
The best way to check who is connected to your network is with your router's app or web interface. Try typing “10.0.0.1” or...
Read more >
How to Find Devices On Your Network - Comparitech
Enter the net view command to view devices connected to your network You will then see a list of devices connected to your...
Read more >
3 Ways to See Who Is Connected to Your Wireless Network
1. Open a browser. You can use a web browser to log in to the web interface for your wireless router. You can...
Read more >
How to see all the devices connected to your network
Log into your router to see the most up-to-date information about which devices are connected. Most models will support this as it's a ......
Read more >
How to See Who's On Your Wi-Fi - PCMag
You can open your router's management page by typing its IP address in your browser's address bar. Once there, look for an option...
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