0

I am making an algorithm for access to Bluetooth Classic devices in which I get from an instance obtained directly from an Android service a collection of type java.util.Set, I need to interact in this collection to get the properties of the objects there present and build a new Array , according to an entity model built in NativeScript with the JavaScript language.

In what way should such an interest be performed in NativeScript using the JavaScript language?

Here is the code suggested below so you can get a sense of what I want to do:

Bluetooth.getBoundedDevices = function(){
  java.util.Set<android.bluetooth.BluetoothDevice> pairedDevices = adapter.getBondedDevices();
  var obArrayDevices = new ObservableArray();
  // If there are paired devices
  if (pairedDevices.size() > 0) {
    // Loop through paired devices
    for (deviceAndroid of pairedDevices.toArray([{}])) {
      var device = new Observable({
        type: 'scanResult',
        UUID: deviceAndroid.getAddress(),
        name: deviceAndroid.getName(),
        RSSI: 0,
        state: 'disconnected'
      });
      console.log("dispositivo Encontrado:");
      console.dir(device);
      obArrayDevices.add(device);
    }
  }
  return obArrayDevices;
}

1 Answer 1

1

Set.toArray() returns a java.lang.Object[] in Java, which will be marshalled to a JavaScript array when used in JavaScript.

...
const pairedDevices = adapter.getBondedDevices().toArray();
pairedDevices.forEach((device) => {
  let newDevice = {
    type: 'scanResult',
    UUID: device.getAddress(),
    name: device.getName(),
    RSSI: 0,
    state: 'disconnected'
  }

  obArrayDevices.add(newDevice);
});
....
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @pkanev, I was in doubt if it would be that simple and I was afraid of being wrong. I'll test and come back here to confirm putting your answer as being correct. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.