Trying to make extensions for Android need some help

Hi Friends, may yor work be easy,
I am trying to make useful extensions for Android Api using Lime.system.JNI as a bridge,
First I am trying to make an extension for Bluetooth this my Java and Haxe code

Java :

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

public class BluetoothManager {
    private BluetoothAdapter adapter;
    private BluetoothDevice device;
    private BluetoothSocket socket;
    private BluetoothServerSocket serverSocket;
    private InputStream inputStream;
    private OutputStream outputStream;
    
    public BluetoothManager() {
        adapter = BluetoothAdapter.getDefaultAdapter();
    }
    
    public boolean isEnabled() {
        return adapter.isEnabled();
    }
    
    public List<String> getPairedDevices() {
        Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
        List<String> deviceNames = new ArrayList<String>();
        for (BluetoothDevice device : pairedDevices) {
            deviceNames.add(device.getName() + ":" + device.getAddress());
        }
        return deviceNames;
    }
    
    public boolean startDiscovery() {
        return adapter.startDiscovery();
    }
    
    public boolean cancelDiscovery() {
        return adapter.cancelDiscovery();
    }
    
    public boolean pairDevice(String address) {
        BluetoothDevice device = adapter.getRemoteDevice(address);
        try {
            return device.createBond();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean unpairDevice(String address) {
        BluetoothDevice device = adapter.getRemoteDevice(address);
        try {
            return device.removeBond();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean connectToDevice(String address) {
        device = adapter.getRemoteDevice(address);
        try {
            socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            socket.connect();
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public void send(byte[] data) {
        try {
            outputStream.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public byte[] receive() {
        try {
            byte[] buffer = new byte[1024];
            int numBytes = inputStream.read(buffer);
            byte[] data = new byte[numBytes];
            System.arraycopy(buffer, 0, data, 0, numBytes);
            return data;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public void disconnect() {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public boolean startServer() {
        try {
            serverSocket = adapter.listenUsingRfcommWithServiceRecord("MyApp", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            socket = serverSocket.accept();
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean connectToServer(String address) {
        device = adapter.getRemoteDevice(address);
        try {
            socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            socket.connect();
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public void accept() {
        try {
            socket = serverSocket.accept();
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void closeServer() {
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Haxe :

import lime.system.JNI;
import haxe.io.Bytes;
import haxe.ds.StringMap;

class MyBluetooth {
    public static function isEnabled():Bool {
        var methodName = "isEnabled";
        varclassName = "com.example.bluetoothmanager.BluetoothManager";
        var result = JNI.callStaticMethod(className, methodName, [], "Z");
        return result;
    }
    
    public static function getPairedDevices():Array<String> {
        var methodName = "getPairedDevices";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        var result = JNI.callStaticMethod(className, methodName, [], "[Ljava.lang.String;");
        return JNIHelper.convertJavaArray(result);
    }
    
    public static function startDiscovery():Bool {
        var methodName = "startDiscovery";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        var result = JNI.callStaticMethod(className, methodName, [], "Z");
        return result;
    }
    
    public static function cancelDiscovery():Bool {
        var methodName = "cancelDiscovery";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        var result = JNI.callStaticMethod(className, methodName, [], "Z");
        return result;
    }
    
    public static function pairDevice(address:String):Bool {
        var methodName = "pairDevice";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        var result = JNI.callStaticMethod(className, methodName, [address], "Z");
        return result;
    }
    
    public static function unpairDevice(address:String):Bool {
        var methodName = "unpairDevice";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        var result = JNI.callStaticMethod(className, methodName, [address], "Z");
        return result;
    }
    
    public static function connectToDevice(address:String):Bool {
        var methodName = "connectToDevice";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        var result = JNI.callStaticMethod(className, methodName, [address], "Z");
        return result;
    }
    
    public static function send(data:Bytes):Void {
        var methodName = "send";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        JNI.callStaticMethod(className, methodName, [data], "V");
    }
    
    public static function receive():Bytes {
        var methodName = "receive";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        var result = JNI.callStaticMethod(className, methodName, [], "[B");
        return JNIHelper.convertJavaByteArray(result);
    }
    
    public static function disconnect():Void {
        var methodName = "disconnect";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        JNI.callStaticMethod(className, methodName, [], "V");
    }
    
    public static function startServer():Bool {
        var methodName = "startServer";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        var result = JNI.callStaticMethod(className, methodName, [], "Z");
        return result;
    }
    
    public static function connectToServer(address:String):Bool {
        var methodName = "connectToServer";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        var result = JNI.callStaticMethod(className, methodName, [address], "Z");
        return result;
    }
    
    public static function accept():Void {
        var methodName = "accept";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        JNI.callStaticMethod(className, methodName, [], "V");
    }
    
    public static function closeServer():Void {
        var methodName = "closeServer";
        var className = "com.example.bluetoothmanager.BluetoothManager";
        JNI.callStaticMethod(className, methodName, [], "V");
    }
}

class JNIHelper {
    public static function convertJavaArray(javaArray:Dynamic):Array<String> {
        var haxeArray:Array<String> = [];
        var length:Int = JNI.getArrayLength(javaArray);
        for (i in 0...length) {
            haxeArray.push(JNI.getObjectArrayElement(javaArray, i));
        }
        return haxeArray;
    }
    
    public static function convertJavaByteArray(javaArray:Dynamic):Bytes {
        var byteArray:Array<Int> = [];
        var length:Int = JNI.getArrayLength(javaArray);
        for (i in 0...length) {
            byteArray.push(JNI.getByteArrayElement(javaArray, i));
        }
        return Bytes.ofData(byteArray);
    }
}

But not confirmed how to make extensions and use them.

1 Like

Hi Umar,

I can’t help on this unfortunately, but I thought I’d mention there is an OpenFL Discord ( Discord ) where you can ask

Such a question might have better chances of getting an answer on the Haxe Discord ( Discord ) (there is also an OpenFL channel there)

If you get a solution from Discord please consider taking time to come back here and share it, since Discord is not searchable from the web

1 Like

Easiest way would be take a look at other extensions and use the base from them, just replacing the functionality to yours:

1 Like