javascriptiosreact-nativebluetooth-lowenergy

React Native BLE Manager: Over The Air Update Write Callback Issue


I'm using the react-native-ble-manager library for Over The Air (OTA) updates. During the update, I send arrays of data to the target device using the BleManager.write command with the size of the array matching the MTU. The OTA update completes successfully, but when I try to track the write operation in JavaScript, all the data seems to be pushed at once.

In contrast, when I previously developed this program in Swift, I could track each write operation using callbacks. By adding logs in the ObjC code within the iOS folder of the library, I can see that the callbacks are successfully triggered with each write operation.

Is there a way to create a bridge between the ObjC code in the iOS folder of the library and my JavaScript code to receive these callbacks? Alternatively, is there another method to track the write operations and calculate the progress percentage based on the total size and the sent data size?

Thank you!


Solution

  • Library Settings (react-native-ble-manager in the library);

    index.d.ts file;

    I added writeCallback as an event.

      export enum BleEventType {
        BleManagerDidUpdateState = 'BleManagerDidUpdateState',
        BleManagerStopScan = 'BleManagerStopScan',
        BleManagerDiscoverPeripheral = 'BleManagerDiscoverPeripheral',
        BleManagerDidUpdateValueForCharacteristic = 'BleManagerDidUpdateValueForCharacteristic',
        BleManagerConnectPeripheral = 'BleManagerConnectPeripheral',
        BleManagerDisconnectPeripheral = 'BleManagerDisconnectPeripheral',
        BleManagerPeripheralDidBond = 'BleManagerPeripheralDidBond', 
        BleManagerCentralManagerWillRestoreState = 'BleManagerCentralManagerWillRestoreState', 
        BleManagerDidUpdateNotificationStateFor = 'BleManagerDidUpdateNotificationStateFor',
        BleManagerWriteCallback = 'BleManagerWriteCallback' // Added Event
      }
    
      // Export Callback Function
      export interface BleManagerWriteCallbackEvent {
        data: any;
      }
    

    BleManager.m file;

    When the writing process occurs, the code below is used to send data to the event within the feedback function.

    - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
    
        // Send Data Event
        if (hasListeners) {
            [self sendEventWithName:@"BleManagerWriteCallback" body:@{@"data": @500}];
        }
    
    }
    

    App Settings;

    bleanager.tsx file;

    An event is called from the library in the Bluetooth management page.

    BleManager.start({showAlert: true}).then(() => {
      const listeners = [
        bleManagerEmitter.addListener(
          'BleManagerDiscoverPeripheral',
          handleDiscoverPeripheral,
        ),
        bleManagerEmitter.addListener(
          'BleManagerDisconnectPeripheral',
          handleDisconnectedPeripheral,
        ),
        bleManagerEmitter.addListener(
          'BleManagerDidUpdateValueForCharacteristic',
          handleUpdateValueForCharacteristic,
        ),
        // Added Event
        bleManagerEmitter.addListener(
          'BleManagerWriteCallback',
          handleBleManagerWriteCallback,
        ),
      ];
    
      return () => {
        for (const listener of listeners) {
          listener.remove();
        }
      };
    });
    
    // Callback Function
    const handleBleManagerWriteCallback = data => { 
      console.log(data);
    };