0

I'm going to implement Bluetooth Low Energy onto a Android mobile application made by Gluon framework. To do that I need to use Android native code inside my Gluon project.

I have follow this manual GoNative how to implement native Android into my Gluon project code without success.

enter image description here

My issue is that I cannot import the android.util.Log library.

package com.gluonhq.charm.down.plugins.android;

import android.util.Log; // <-- The import android cannot be resolved
import com.gluonhq.charm.down.plugins.LogService;

public class AndroidLogService implements LogService {

    private final static String TAG = AndroidLogService.class.getSimpleName();

    @Override
    public void log(String message) {
        Log.v(TAG, message);
    }
}

My goal is to have communication with other devices. I'm not talking about GATT services. I'm talking about sending and receiving data such as bytes.

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();
        }
    }
}

Code source

Here I open the GoNative example from Guon's Github. enter image description here

NotZack
  • 518
  • 2
  • 9
  • 22
euraad
  • 2,467
  • 5
  • 30
  • 51
  • Go Native sources: https://github.com/gluonhq/gluon-samples/tree/master/go-native You should be able to clone it and run it as is. If your question is about Go Native, don't need to add BLE... – José Pereda Sep 03 '19 at 13:44
  • @JoséPereda Yes. That's from here I copy the code from. Did not work. – euraad Sep 03 '19 at 13:47
  • Edit your question and remove whatever is not necessary. Then make sure you copy the sources in the right folder (i.e. `LogService` should be under `src/main/java`). – José Pereda Sep 03 '19 at 13:52
  • @JoséPereda I have place the `LogService` and `LogServiceFactory` under `src/main/java` but still `android.util.Log` cannot be resolved. – euraad Sep 03 '19 at 14:13
  • Did you try running Go Native as is, without any change? – José Pereda Sep 03 '19 at 14:20
  • @JoséPereda I have and it works. But when I try to move over some files from your work, to my project. It don't work because of the android.util.Log library cannot be resolved. I don't know how it can be working in your project, but not mine. – euraad Sep 03 '19 at 14:54
  • It’s up to you to copy the files from one project to the other, in the exact same folders. Clean your project and build again. – José Pereda Sep 03 '19 at 15:05
  • @JoséPereda I did that too. I create a basic GoNative project with the same name. Then removed the `src` folder and then move over your `src` folder. Still the same error. Then I moved over the complete `go-native` folder. Still the same error. – euraad Sep 03 '19 at 15:27
  • @JoséPereda In some how I think I got fooled by the IDE. The IDE show me that there is an error, but when I compile it. The application start anyway. – euraad Sep 03 '19 at 15:57
  • @JoséPereda I just want to tell you that I have quit the idea of making bluetooth available for my application. I'm focusing on WiFi instead, eg. `Java Sockets`. Thank you for your help anyway! You do a great job as a supporter :) – euraad Sep 03 '19 at 17:52

0 Answers0