vitorpamplona / react-native-ble-advertiser

Bluetooth Advertiser/Scanner for React Native.
60 stars 38 forks source link

How Can I use it in background. #16

Closed thekundankamal closed 3 years ago

thekundankamal commented 4 years ago

I have used headless.js for the background but I am not getting any scan device callback. Advertisement and scan request is working but not getting any device.

export async function startBleScan() {
 // requestLocationPermission();
 console.log('Went to background');

  //stopBle()

  const eventEmitter = new NativeEventEmitter(NativeModules.BLEAdvertiser);
  this.onDeviceFound = eventEmitter.addListener('onDeviceFound', (event) => {
    console.log('onDeviceFound', event);
    if (event.serviceUuids) {
      for(let i=0; i< event.serviceUuids.length; i++){
        if (event.serviceUuids[i] && event.serviceUuids[i].endsWith('00'))
          this.addDevice(event.serviceUuids[i], event.deviceName, event.deviceAddress, event.rssi, new Date(),event.manufData)   
      }
    }
  });

  var udid=await getUdId();
  console.log("udid1233------"+udid) 
  console.log(udid, "Starting Advertising");
    BLEAdvertiser.broadcast(udid,MANUF_DATA,{
    advertiseMode: BLEAdvertiser.ADVERTISE_MODE_BALANCED, 
    txPowerLevel: BLEAdvertiser.ADVERTISE_TX_POWER_MEDIUM, 
    connectable: false})
    .then(sucess => console.log(udid, "Adv Successful", sucess))
    .catch(error => console.log(udid, "Adv Error", error));

  console.log(udid, "Starting Scanner");
    BLEAdvertiser.scan(MANUF_DATA,{})
     .then(sucess => console.log(udid, "Scan Successful", sucess))
     .catch(error => console.log(udid, "Scan Error", error));
}
kirantripathi commented 4 years ago

Hello, @thekundankamal I am also trying to work with this package,I just want to know. did it work correctly on both ios/android and also about while broad casting what is "uuid" and "MANUF_DATA", .I want my app to act as beacon and also I have to transmit custom payload.can you guide me on this?

thekundankamal commented 4 years ago

@kirantripathi Yes it's working on both platforms but only on the foreground state not in the background state. And for custom payload, you can only send the advertising data with some length limitation.

kirantripathi commented 4 years ago

@thekundankamal In which field were you be able to send custom payload.

vitorpamplona commented 4 years ago

Folks, background processing has been severely limited by both Android and iOS in the latest months. iOS will kill any process that is running. Android is easier, but it can also kill the process. On Android, you have to register a service to start the adv and scanning there.

Try using the https://www.npmjs.com/package/react-native-background-fetch to register the services and run Scheduled tasks that activate both scanning and advertising.

thekundankamal commented 4 years ago

@vitorpamplona I have tried it but it's not working for me but now I have created custom for android. which is running in the background and also now I am able to send longer data in advertisement.

thekundankamal commented 4 years ago

@kirantripathi In that library, you can only send the data MANUF_DATA BLEAdvertiser.broadcast(udid,MANUF_DATA,{

But you cannot send the longer data in it.

kirantripathi commented 4 years ago

@thekundankamal can you share your approach , as I also need to achieve something like that.

thekundankamal commented 4 years ago

@kirantripathi https://github.com/theappbusiness/Tracy Use the above library for this one you need to only send the data from native to code react native with headlessjs of maybe another approach. Currently, I am using HeadlesJs for android.