0

I have a project in NativeScript and i'm accessing the bluetooth android api via JavaScript as in this example:

android.bluetooth.BluetoothAdapter.getDefaultAdapter();  

I am able to find the paired devices and connect to them. I wanted some example of how to forward data to the device and have a return of this device.

I've seen something in the android documents about: getOutputStream()
and getInputStream()

but I do not know how to use this via JavaScript.

I want is to send an command to an Elm327 OBD2 device and receive the data from this device.

Sample native code:

private OutputStream outputStream;
private InputStream inStream;

private void init() throws IOException {
BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
if (blueAdapter != null) {
    if (blueAdapter.isEnabled()) {
        Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();

        if(bondedDevices.size() > 0) {
            Object[] devices = (Object []) bondedDevices.toArray();
            BluetoothDevice device = (BluetoothDevice) devices[position];
            ParcelUuid[] uuids = device.getUuids();
            BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
            socket.connect();
            outputStream = socket.getOutputStream();
            inStream = socket.getInputStream();
        }

        Log.e("error", "No appropriate paired devices.");
    } else {
        Log.e("error", "Bluetooth is disabled.");
    }
  }
}

public void write(String s) throws IOException {
   outputStream.write(s.getBytes());
}

public void run() {
   final int BUFFER_SIZE = 1024;
   byte[] buffer = new byte[BUFFER_SIZE];
   int bytes = 0;
   int b = BUFFER_SIZE;

while (true) {
    try {
        bytes = inStream.read(buffer, bytes, BUFFER_SIZE - bytes);
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}
AndersonL
  • 13
  • 4
  • May I ask why don't you prefer [nativescript-bluetooth](https://github.com/EddyVerbruggen/nativescript-bluetooth) plugin? – Manoj Feb 10 '19 at 04:25
  • Thank you @Manoj, but the plugin nativescrip-bluetooth is not compatible with bluetooth classic. – AndersonL Feb 10 '19 at 05:12
  • If you understand marshalling from the [Java to JS docs here](https://docs.nativescript.org/core-concepts/android-runtime/marshalling/java-to-js), you should be able to marshall any Android (Java) apis into JavaScript. If you are not sure about specific apis, please share more details on what you are looking for, may be with some Java examples. – Manoj Feb 10 '19 at 07:36
  • Thank you friend! I added an example, I got the first few steps but I do not know how to use the methods to send and listen to the returns ... – AndersonL Feb 10 '19 at 16:07
  • I found this tutorial, that's what I want to do. but accessing the api via Javascript: http://www.decom.ufop.br/imobilis/tutorial-android-desenvolvendo-com-bluetooth-para-comunicacao-veicular-parte-2/ – AndersonL Feb 11 '19 at 21:21

0 Answers0