ProfessorSocks
ProfessorSocks

Reputation: 11

Why isn't bluetooth sending the message?

I'm trying to send bytes to my device that runs on python to display the message.

const sendStringToDevice = async () => {
        try {
          // Request Bluetooth device
          const device = await navigator.bluetooth.requestDevice({
            filters: [{ name: 'monocle' }],
            optionalServices: [0x2A00],
          });
      
          // Connect to the device
          const server = await device.gatt.connect();
      
          // Get the specified service
          const service = await server.getPrimaryServices();
          console.log("service got")
      
          // Get the specified characteristic
          const characteristic = await service.getCharacteristic();
      
          // Convert the string to a UInt8Array (assuming ASCII encoding)
          const encoder = new TextEncoder('utf-8');
          const data = encoder.encode(message);
      
          // Send the data to the characteristic
          await characteristic.writeValue(data);
      
          console.log(`String "${message}" sent successfully to monocle`);
        } catch (error) {
          console.error('Error sending string to Bluetooth device:', error);
        }
      };

I tried to restart the application and run it again but nothing. It's going to a python file from javascript so I am not sure if that affects anything. When it runs neither the console.log(String "${message}" sent successfully to monocle); or console.error('Error sending string to Bluetooth device:', error); run.

It's stuck in limbo and I don't know why!

Upvotes: 1

Views: 60

Answers (1)

It looks to me that you haven't used await characteristic.startNotifications()

Upvotes: 0

Related Questions