Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

bluetooth - Sending data to BLE device with javascript won't work

I have a bluetooth device and want to turn it on with the p5ble library. https://itpnyu.github.io/p5ble-website/

The UUID of the device is f000aa60-0451-4000-b000-000000000000 When sending 0xAEAA01646456 to the characteristics UUId f000aa61-0451-4000-b000-000000000000 it should turn on. I tested this with the NRF Connect App on Android and it works perfectly find.

With my code i can connect and find the correct characteristics and also send the data. But it won't turn on. This is the output i get from the script:

found: f000aa61-0451-4000-b000-000000000000 Writing 192045191029846 to Characteristic... Disconnecting from Bluetooth Device...

What could be the problem here?

const serviceUuid = "f000aa60-0451-4000-b000-000000000000";
const characteristicsUUID = {
  led:"f000aa61-0451-4000-b000-000000000000"
}
let ledCharacteristic;
let myBLE;

let buttonValue = 0;

function setup() {

  myBLE = new p5ble();

  // Create a 'Connect and Start Notifications' button
  const connectButton = createButton('Connect and Start Notifications')
  connectButton.mousePressed(connectAndStartNotify);
  
  // Create a 'Toggle' button
  const ledButton = createButton('LED on');
  ledButton.position(15, 15);
  ledButton.mousePressed(LEDon);
}

function connectAndStartNotify() {
  // Connect to a device by passing the service UUID
  myBLE.connect(serviceUuid, gotCharacteristics);
}

// A function that will be called once got characteristics
function gotCharacteristics(error, characteristics) {
  if (error) console.log('error: ', error);
  for(let i = 0; i < characteristics.length;i++){
  if(characteristics[i].uuid == characteristicsUUID.led){
      ledCharacteristic = characteristics[i];
    console.log("found: "+characteristicsUUID.led)
    }
  }
}

// A function that turns on the LED
function LEDon(){
  myBLE.write(ledCharacteristic, 0xAEAA01646456);
  myBLE.disconnect();
}
question from:https://stackoverflow.com/questions/65937328/sending-data-to-ble-device-with-javascript-wont-work

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...