3

I'm trying to make an app that sends commands to a BLE device and i cant get any feedback. I am using a library called ble.plx which has an option to monitor characteristics but it wont output anything for me. I need to read values from notifications to use later in my code. This is my first time working with BLE in general so i have no idea what i am doing wrong. I know serviceUUID and characteristicUUID are correct. I am out of ideas.

Here is my code:

function scanAndConnect() {
BLTManager.startDeviceScan(null, null, (error, device) => {
    if (error) {
        // Handle error (scanning will be stopped automatically)
        return
    }

    // Check if it is a device you are looking for based on advertisement data
    // or other criteria.
    if (device.name=='Audio PCM Streamer') {
        console.log(device.name);
        // Stop scanning as it's not necessary if you are scanning for one device.
        BLTManager.stopDeviceScan();

        device.connect()
        .then((device) => {
          return device.discoverAllServicesAndCharacteristics()
        })
        .then( (device) => {
          device.monitorCharacteristicForService(SERVICE_UUID,CHARACTERISTIC_UUID,(err,result)=>{
            if(err) {
              console.log(err)
              return;
            }
            console.log(result);
          }); Subscription  
          device.requestMTU(251)
          let data = Uint8Array(9);
          data[0]=0xA5;
          data[1]=0xA5;
          data[2]=0xA5;
          data[3]=0xA5;
          var b64encoded = btoa(decoder.decode(data));
          device.writeCharacteristicWithoutResponseForService(SERVICE_UUID,CHARACTERISTIC_UUID,b64encoded);

        
        })
        .catch((error) => {
    // Handle errors
        });

       }
    });

}

rerboi
  • 73
  • 8
  • 1
    Have you tried logging out errors in your catch blocks? From the code here, you would never know if you were getting an error. – Abe Sep 24 '22 at 03:11
  • i need to read values from notifications but notifications dont work sorry i wasnt specific enough i will edit the question. – rerboi Sep 24 '22 at 08:41

1 Answers1

1

I did this completely wrong do not use this or try to fix this its just bad.

rerboi
  • 73
  • 8
  • 2
    Would be good with a link or some other hint how to solve it when other people read the question – P-A Nov 24 '22 at 12:56
  • Well it was for a job so i cant really give out code but the problem occured because the app continued without waiting for the response from device. It was fixed by implementing an algorithm in the callback that calls the specific methods needed at specific times. Hope this helps – rerboi Nov 27 '22 at 12:36