Git Product home page Git Product logo

Comments (7)

lucaswitch avatar lucaswitch commented on June 23, 2024

Weird, i see you're using version ^11.3.2 which can be any version newer than that, can you make sure to install version react-native-ble-manager version: "11.5.2" and do a ./android/gradlew clean for a full library refresh and check if the issue is still happening?

from react-native-ble-manager.

akiannillo avatar akiannillo commented on June 23, 2024

Done, same results.
However, I read a bit of logs and code (even if I did not completely understand it) and I tried this:

        if (this.discoveredDevicesIDs.size === 0) {
            throw new Error('No devices discovered');
        }
        const deviceID = Array.from(this.discoveredDevicesIDs)[0];
        await BleManager.connect(deviceID);
        console.log('Connected to', deviceID);
        await new Promise(resolve => setTimeout(resolve, 2000));
        await BleManager.requestMTU(deviceID, 232);
        console.log('MTU requested');
        Alert.alert('MTU requested');

and it works like a charm.
It looks like there is some problem with the "command" management or something similar.
This workaround made me think that maybe it's not about the version but about the "speed" of the tablet.
The Android 14 tablet is 2024, while the Android 13 is much older.

from react-native-ble-manager.

rahulp9538 avatar rahulp9538 commented on June 23, 2024

Issue Description:

When I run my app on devices with Android 13 or lower, I receive the full data from my Bluetooth device. However, when running the same app on an Android 14 device, the data received from the Bluetooth device is truncated. Despite seeing "MTU size changed to 517 bytes" in the logs on both Android 13 and Android 14, the issue persists only on Android 14. I'm using "react-native-ble-manager": "^11.5.3". Please provide a solution urgently.

Code Example:

import BleManager from 'react-native-ble-manager';
import { Alert } from 'react-native';

const connectPeripheral = async (peripheral, connect, save) => {
try {
let ShowAlert = false;

if (peripheral) {
  if (connect) {
    console.log('Peripheral exists in the list.');
  } else {
    console.log('Peripheral does not exist in the list.');
    addOrUpdatePeripheral(peripheral.id, { ...peripheral, connecting: true });
  }

  console.log('Before connecting to peripheral...');

  // Create a promise for the connection
  const connectPromise = BleManager.connect(peripheral.id);

  // Create a timeout promise
  const timeoutPromise = new Promise((resolve, reject) => {
    setTimeout(() => {
      reject(new Error('Connection timeout'));
    }, 20000); // Adjust the timeout duration as needed
  });

  // Use Promise.race to wait for the first promise that resolves
  try {
    await Promise.race([connectPromise, timeoutPromise]);
    console.debug(`[connectPeripheral][${peripheral.id}] connected.`);

    console.log('After connecting to peripheral...');

    if (connect) {
      console.log('Peripheral exists in the list.');
    } else {
      console.log('Peripheral does not exist in the list.');
      addOrUpdatePeripheral(peripheral.id, { 
        ...peripheral, 
        connecting: false, 
        connected: true 
      });
    }

    // Allow some time for bonding and connection to finish
    await new Promise(resolve => setTimeout(resolve, 2000));

    try {
      const mtu = await BleManager.requestMTU(peripheral.id, 185);
      console.log(`MTU size changed to ${mtu} bytes`);
    } catch (error) {
      console.error(`Failed to change MTU size: ${error}`);
    }

    // Retrieve services
    const peripheralData = await BleManager.retrieveServices(peripheral.id);
    console.debug(`[connectPeripheral][${peripheral.id}] retrieved peripheral services`, JSON.stringify(peripheralData));

    storeUniqueObject('userData', peripheral, false);

    if (!movefrwd) {
      setisLoading(false);
      ShowAlert = true;
      navigation.navigate('BTStatusScreen', {
        peripheralObj: peripheral,
        BleManagerObj: BleManager
      });
      console.log(`ShowAlert: ${ShowAlert}`);
    }
  } catch (error) {
    setisLoading(false);
    await BleManager.disconnect(peripheral.id);
    if (error.message === 'Connection timeout') {
      console.log('Connection to peripheral timed out. Device may be off or not in discovery mode.');
    } else {
      console.error(`Error connecting to peripheral: ${error}`);
      if (!movefrwd) {
        // Handle other connection errors here
      }
    }
  }
}

} catch (error) {
console.error([connectPeripheral][${peripheral.id}] connectPeripheral error, error);
}
};

from react-native-ble-manager.

varun761 avatar varun761 commented on June 23, 2024

@akiannillo There is some performance issue in this module. It cannot work as expected. I think you should create your own module. I am stuck at a point too. Where nrf connect app performs well and is able to receive data in 100ms. Whereas, in this module, it sends disconnects. This module total **** me up.

from react-native-ble-manager.

lucaswitch avatar lucaswitch commented on June 23, 2024

Issue Description:

When I run my app on devices with Android 13 or lower, I receive the full data from my Bluetooth device. However, when running the same app on an Android 14 device, the data received from the Bluetooth device is truncated. Despite seeing "MTU size changed to 517 bytes" in the logs on both Android 13 and Android 14, the issue persists only on Android 14. I'm using "react-native-ble-manager": "^11.5.3". Please provide a solution urgently.

Code Example:

import BleManager from 'react-native-ble-manager'; import { Alert } from 'react-native';

const connectPeripheral = async (peripheral, connect, save) => { try { let ShowAlert = false;

if (peripheral) {
  if (connect) {
    console.log('Peripheral exists in the list.');
  } else {
    console.log('Peripheral does not exist in the list.');
    addOrUpdatePeripheral(peripheral.id, { ...peripheral, connecting: true });
  }

  console.log('Before connecting to peripheral...');

  // Create a promise for the connection
  const connectPromise = BleManager.connect(peripheral.id);

  // Create a timeout promise
  const timeoutPromise = new Promise((resolve, reject) => {
    setTimeout(() => {
      reject(new Error('Connection timeout'));
    }, 20000); // Adjust the timeout duration as needed
  });

  // Use Promise.race to wait for the first promise that resolves
  try {
    await Promise.race([connectPromise, timeoutPromise]);
    console.debug(`[connectPeripheral][${peripheral.id}] connected.`);

    console.log('After connecting to peripheral...');

    if (connect) {
      console.log('Peripheral exists in the list.');
    } else {
      console.log('Peripheral does not exist in the list.');
      addOrUpdatePeripheral(peripheral.id, { 
        ...peripheral, 
        connecting: false, 
        connected: true 
      });
    }

    // Allow some time for bonding and connection to finish
    await new Promise(resolve => setTimeout(resolve, 2000));

    try {
      const mtu = await BleManager.requestMTU(peripheral.id, 185);
      console.log(`MTU size changed to ${mtu} bytes`);
    } catch (error) {
      console.error(`Failed to change MTU size: ${error}`);
    }

    // Retrieve services
    const peripheralData = await BleManager.retrieveServices(peripheral.id);
    console.debug(`[connectPeripheral][${peripheral.id}] retrieved peripheral services`, JSON.stringify(peripheralData));

    storeUniqueObject('userData', peripheral, false);

    if (!movefrwd) {
      setisLoading(false);
      ShowAlert = true;
      navigation.navigate('BTStatusScreen', {
        peripheralObj: peripheral,
        BleManagerObj: BleManager
      });
      console.log(`ShowAlert: ${ShowAlert}`);
    }
  } catch (error) {
    setisLoading(false);
    await BleManager.disconnect(peripheral.id);
    if (error.message === 'Connection timeout') {
      console.log('Connection to peripheral timed out. Device may be off or not in discovery mode.');
    } else {
      console.error(`Error connecting to peripheral: ${error}`);
      if (!movefrwd) {
        // Handle other connection errors here
      }
    }
  }
}

} catch (error) { console.error([connectPeripheral][${peripheral.id}] connectPeripheral error, error); } };

Thats a behavior that changed on Android Bluetooth since 14.

Issue Description:

When I run my app on devices with Android 13 or lower, I receive the full data from my Bluetooth device. However, when running the same app on an Android 14 device, the data received from the Bluetooth device is truncated. Despite seeing "MTU size changed to 517 bytes" in the logs on both Android 13 and Android 14, the issue persists only on Android 14. I'm using "react-native-ble-manager": "^11.5.3". Please provide a solution urgently.

Code Example:

import BleManager from 'react-native-ble-manager'; import { Alert } from 'react-native';

const connectPeripheral = async (peripheral, connect, save) => { try { let ShowAlert = false;

if (peripheral) {
  if (connect) {
    console.log('Peripheral exists in the list.');
  } else {
    console.log('Peripheral does not exist in the list.');
    addOrUpdatePeripheral(peripheral.id, { ...peripheral, connecting: true });
  }

  console.log('Before connecting to peripheral...');

  // Create a promise for the connection
  const connectPromise = BleManager.connect(peripheral.id);

  // Create a timeout promise
  const timeoutPromise = new Promise((resolve, reject) => {
    setTimeout(() => {
      reject(new Error('Connection timeout'));
    }, 20000); // Adjust the timeout duration as needed
  });

  // Use Promise.race to wait for the first promise that resolves
  try {
    await Promise.race([connectPromise, timeoutPromise]);
    console.debug(`[connectPeripheral][${peripheral.id}] connected.`);

    console.log('After connecting to peripheral...');

    if (connect) {
      console.log('Peripheral exists in the list.');
    } else {
      console.log('Peripheral does not exist in the list.');
      addOrUpdatePeripheral(peripheral.id, { 
        ...peripheral, 
        connecting: false, 
        connected: true 
      });
    }

    // Allow some time for bonding and connection to finish
    await new Promise(resolve => setTimeout(resolve, 2000));

    try {
      const mtu = await BleManager.requestMTU(peripheral.id, 185);
      console.log(`MTU size changed to ${mtu} bytes`);
    } catch (error) {
      console.error(`Failed to change MTU size: ${error}`);
    }

    // Retrieve services
    const peripheralData = await BleManager.retrieveServices(peripheral.id);
    console.debug(`[connectPeripheral][${peripheral.id}] retrieved peripheral services`, JSON.stringify(peripheralData));

    storeUniqueObject('userData', peripheral, false);

    if (!movefrwd) {
      setisLoading(false);
      ShowAlert = true;
      navigation.navigate('BTStatusScreen', {
        peripheralObj: peripheral,
        BleManagerObj: BleManager
      });
      console.log(`ShowAlert: ${ShowAlert}`);
    }
  } catch (error) {
    setisLoading(false);
    await BleManager.disconnect(peripheral.id);
    if (error.message === 'Connection timeout') {
      console.log('Connection to peripheral timed out. Device may be off or not in discovery mode.');
    } else {
      console.error(`Error connecting to peripheral: ${error}`);
      if (!movefrwd) {
        // Handle other connection errors here
      }
    }
  }
}

} catch (error) { console.error([connectPeripheral][${peripheral.id}] connectPeripheral error, error); } };

Thats a behavior change in the bluetooth not in the library itself, take a look on https://developer.android.com/about/versions/14/behavior-changes-all for further details.

Something that i had to fix in order to work with Android 14 devices.
Before 14:
connect -> discover -> listen for characteristics changes.
After 14:
connect -> discover -> requestMtu(thats the minimum MTU to negotiate with device, probably the highest mtu size will be given to you) -> listen for characteristics changes.

from react-native-ble-manager.

lucaswitch avatar lucaswitch commented on June 23, 2024

@rahulp9538 that fixes your MTU negotiation size.

Screenshot 2024-06-10 at 15 22 38

from react-native-ble-manager.

lucaswitch avatar lucaswitch commented on June 23, 2024

Please provide a solution urgently

Can you provide a minimal repro that shows this perfomance issue?

from react-native-ble-manager.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.