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

On Android, calls to BleManager.read() never resolve for discovered, but not connected peripherals #1244

Open
ventsislavhristov opened this issue Jul 2, 2024 · 0 comments

Comments

@ventsislavhristov
Copy link

Describe the bug
On Android, calls to BleManager.read() do not resolve when called for a peripheral which was discovered, but is not currently connected. The call never returns, nor throws an error.
On iOS, the same call to read() resolves with an error " Could not find service with UUID XXX on peripheral with UUID YYYY".

To Reproduce
Steps to reproduce the behavior:

  1. On Android, run a scan and discover the device, but do not connect to it.
  2. Call BleManager.read() with the given peripheralId and existing service and characteristic UUIDs.
  3. The read() call never completes.

Expected behavior
The read() call should result in an error due to the peripheral not being currently connected, or at least some other type of error.

Screenshots
If applicable, add screenshots to help explain your problem.

Smartphone (please complete the following information):

  • Device: Huawei P30 Pro, Samsung Galaxy A14
  • OS: Android 10, Android 13
  • react-native-ble-manager version: 11.5.5
  • react-native version: 0.73.6

Additional context
When the BleManager.read() is called before the device is discovered, the call completes correctly with an error "Peripheral not found".

The following code snippet can be used to reproduce the issue:

testForReadBug: async () => {
        // replace with the peripheral ID of your device on iOS and Android
        const peripheralId = Platform.OS === 'android' ? "00:80:81:82:83:84" : "bd377951-2070-d4ea-3a44-3d2d0f8401a3";
        const serviceUUID = "180a";
        const characteristicUUID = "2a26";

        console.log("Will try to read before discovery");
        try {
            const characteristicValue = await BleManager.read(peripheralId, serviceUUID, characteristicUUID);
            console.log("Characteristic value: " + characteristicValue);
        } catch (error) {
            console.log("Error reading characteristic before discovery: " + error);
        }

        const eventEmitter = new NativeEventEmitter(NativeModules.BleManager);
        const logDiscoveredPeripheral = (peripheral: Peripheral) => {
            if (peripheralId == peripheral.id) {
                console.log("Discovered peripheral with id " + peripheral.id);
                
                const readValueAfterDiscovery = async () => {
                    await BleManager.stopScan();
                    console.log("Will try to read after discovery");
                    try {
                        const characteristicValue = await BleManager.read(peripheralId, serviceUUID, characteristicUUID);
                        console.log("Characteristic value: " + characteristicValue);
                    } catch (error) {
                        console.log("Error reading characteristic after discovery: " + error);
                    }
                }

                readValueAfterDiscovery();
            }
        };

        const discoverEventListener = eventEmitter.addListener('BleManagerDiscoverPeripheral', logDiscoveredPeripheral);
        const stopScanEventListener = eventEmitter.addListener('BleManagerStopScan', () => {
            console.log("Scan stopped");
            discoverEventListener.remove();
            stopScanEventListener.remove();
        });
        
        
        console.log("Will start scan");
        await BleManager.scan([],30, false);
        console.log("Scan started");
    }

On Android, the console shows the following - note that no error message or a message with the characteristic value is shown after "Will try to read after discovery":

 Will try to read before discovery
 Error reading characteristic before discovery: Peripheral not found
 Will start scan
 Scan started
 Discovered peripheral with id 00:80:81:82:83:84
 Will try to read after discovery
 Scan stopped

On iOS, the results are the following - notice that an error is thrown after the attempt to call read() after the device is discovered:

 Will try to read before discovery
 Error reading characteristic before discovery: Could not find peripheral with UUID bd377951-2070-d4ea-3a44-3d2d0f8401a3
 Will start scan
 Scan started
 Discovered peripheral with id bd377951-2070-d4ea-3a44-3d2d0f8401a3
 Scan stopped
 Will try to read after discovery
 Error reading characteristic after discovery: Could not find service with UUID 180a on peripheral with UUID bd377951-2070-d4ea-3a44-3d2d0f8401a3
```



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

No branches or pull requests

2 participants