1

I'm new on Ble-Plx and React-Native, I'm working on a project where we should be able to connect to a device and change the behavior, just writing new characteristics and it should change... I used a Ble Starter that I found on the internet, and I was able to write the characteristics and change the behavior. So I know the peripheral can do it and that I only need to develop the code. I have something already but when I write the characteristics the device doesn't change as we spect, the device does NOTHING and I don't get any error or something Just the device does nothing. Here is a piece of code if someone can help would be amazing!

HERE IS THE MAIN PART OF THE CODE:

 import { BleManager } from 'react-native-ble-plx';

  const manager = new BleManager();
  
  const Example=()=>{

    useEffect(() => {
      manager.onStateChange((state) => {
        const subscription = manager.onStateChange((state) => {
            if (state === 'PoweredOn') {
                scanAndConnect();
                subscription.remove();
            }
        }, true);
        return () => subscription.remove();
      });
    }, [manager])
  
    function scanAndConnect() {

      console.log('Escanear')
      manager.startDeviceScan(null,null,async(error,device)=>{
        console.log(device.id)
      if(device.id ==='D1:42:78:C8:AB:FB' || device.id ==='D1:42:BF:F1:D9:3C'){
 
          manager.stopDeviceScan()
          console.log("ID del dispositivo: ", device.id)
          console.log("Nombre del dispositivo: ", device.name)
          console.log("RRSI del dispositivo: ", device.rssi)
          console.log("MTU del dispositivo: ", device.mtu)
   
          device.connect()
          .then((device) => {
             const services =  device.discoverAllServicesAndCharacteristics()

             console.log(services)
          })
         .catch((error) => {
          // Handle errors
          console.log(error)
      });
        }
        if (error) {
          console.log(error)
          return
      }
      })
 }
 const writeChar=async()=> {
  var text = (Buffer.from("AA0100000200").toString('base64'));
  manager.writeCharacteristicWithoutResponseForDevice(
    'D1:42:BF:F1:D9:3C',
    'aae0',
    'aae1',
    text,
  )
  .then(() => {
    console.log("Write: " + text);
  })
  .catch((error) => {
    console.log(error);
  });
 }

AND HERE IS WHAT THE CONSOLE.LOG GIVE ME BACK.

 D1:42:BF:F1:D9:3C
 LOG  ID del dispositivo:  D1:42:BF:F1:D9:3C
 LOG  Nombre del dispositivo:  Bluetrum-MX
 LOG  RRSI del dispositivo:  -73
 LOG  MTU del dispositivo:  23
 LOG  {"_U": 0, "_V": 0, "_W": null, "_X": null}
 LOG  Write: QUEwMTAwMDAwMjAw

BUT even that there is no error the device doesn't change so there is my problem, I'm NOT able to make the device change the behavior and start the vibration (just by writing the characteristics)

Richard Wilson
  • 297
  • 4
  • 17
Federico
  • 11
  • 2
  • Please use a generic BLE scanner app such as [nRF Connect](https://www.nordicsemi.com/Products/Development-tools/nrf-connect-for-mobile) first before developing your own app. Can you get your device to vibrate if you send the message via nRF Connect? – Michael Kotzjan Jul 26 '21 at 12:15
  • YEs, I did use a Ble Starter App that I found on internet and I was able to write the characteristics and the change the behave. So I know the periperhal can do it and that i only need to develop the code, I tried to copy the code but is on Kotlin and in Android Studio and by native modules, and I'm working in JavaScript with react-native on Visual Studio code. – Federico Jul 26 '21 at 12:32

1 Answers1

0

Put await before manager while writing characteristics

and try below code

const writeChar=async()=> {
  var text = (Buffer.from("AA0100000200").toString('base64'));
  await manager.writeCharacteristicWithoutResponseForDevice(
    'D1:42:BF:F1:D9:3C',
    'aae0',
    'aae1',
    text,
  )
  .then((res) => {
    console.log("Write: " + text);
    console.log("res",res)
  })
  .catch((error) => {
    console.log(error);
  });
 }
Muhammad Tahir
  • 977
  • 1
  • 3
  • 11