Created
November 10, 2018 16:51
-
-
Save AppWerft/428f323fdf4fca8cacb2572afb9aeb52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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