Skip to content

Instantly share code, notes, and snippets.

@AppWerft
Created November 10, 2018 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AppWerft/428f323fdf4fca8cacb2572afb9aeb52 to your computer and use it in GitHub Desktop.
Save AppWerft/428f323fdf4fca8cacb2572afb9aeb52 to your computer and use it in GitHub Desktop.
package de.appwerft.disto;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import ch.leica.sdk.Devices.BleDevice;
import ch.leica.sdk.Devices.Device;
import ch.leica.sdk.ErrorHandling.ErrorObject;
import ch.leica.sdk.Listeners.ErrorListener;
import ch.leica.sdk.Listeners.ReceivedDataListener;
import ch.leica.sdk.commands.ReceivedData;
public class DeviceProxy extends KrollProxy implements
Device.ConnectionListener, ErrorListener, ReceivedDataListener {
private Device currentDevice;
private MessageDispatcher messageDispatcher;
public DeviceProxy() {
super();
}
public DeviceProxy(Device device) {
super();
messageDispatcher = new MessageDispatcher(this);
currentDevice = device;
currentDevice.setConnectionListener(this);
currentDevice.setErrorListener(this);
currentDevice.setReceiveDataListener(this);
}
@Kroll.method
public void connect(KrollDict opts) {
messageDispatcher.registerCallbacks(opts);
currentDevice.connect();
}
@Override
public void onConnectionStateChanged(final Device device,
final Device.ConnectionState connectionState) {
try {
if (connectionState == Device.ConnectionState.connected) {
try {
if (currentDevice != null
&& currentDevice instanceof BleDevice) {
currentDevice
.startBTConnection(new Device.BTConnectionCallback() {
@Override
public void onFinished() {
messageDispatcher
.dispatchDevice(currentDevice);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
} catch (Exception e) {
}
}
@Override
public void onError(ErrorObject errorObject, Device device) {
messageDispatcher.dispatchError(errorObject);
}
@Override
public void onAsyncDataReceived(ReceivedData receivedData) {
messageDispatcher.dispatchData(receivedData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment