As the title suggests, I’m having trouble getting my Android app to send data to my Mirror. The issue could also be that the React app running on the mirror isn’t receiving the data, but it’s hard to tell, since there isn’t any official way of debugging the two applications. Any help to find out what’s going on would be appreciated.
Here’s the code I’m using in the Android app:
public class MainActivity extends AppCompatActivity {
private static final String MIRROR_TEMPLATE = "default";
private MirrorContextManager mirrorContextManager;
private Dictionary mirrorDictionary;
private EditText yourNameEditText;
private EditText routeEditText;
private Button saveButton;
private Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.initializeMirrorContextManager();
this.initializeViews();
this.setupSaveButtonClickListener();
}
private void initializeMirrorContextManager() {
this.mirrorContextManager = MirrorContextManager.createMirrorContextManager(this);
}
private void initializeViews() {
this.yourNameEditText = findViewById(R.id.edit_text_your_name);
this.routeEditText = findViewById(R.id.edit_text_route);
this.saveButton = findViewById(R.id.button_save);
}
private void setupSaveButtonClickListener() {
this.saveButton.setOnClickListener(new View.OnClickListener() {
private MainActivity parent = MainActivity.this;
@Override
public void onClick(View view) {
String name = yourNameEditText.getText().toString();
String route = routeEditText.getText().toString();
parent.setupMirrorDictionary(name, route);
parent.setupMirrorDisplayHandler();
parent.hideSoftKeyboard();
parent.displayToast("Settings updated", Toast.LENGTH_LONG);
}
});
}
private void setupMirrorDictionary(String name, String route) {
this.mirrorDictionary = new Dictionary();
this.mirrorDictionary.setTemplate(MIRROR_TEMPLATE);
this.mirrorDictionary.put("name", name);
this.mirrorDictionary.put("route", route);
}
private void setupMirrorDisplayHandler() {
this.mirrorContextManager.clearDisplayRequests();
this.mirrorContextManager.display(this.mirrorDictionary, Zone.NEAR, new DisplayCallback() {
private MainActivity parent = MainActivity.this;
@Override
public void onDataDisplayed(MirrorDevice mirrorDevice) {
parent.displayToast("Data successfully mirrored!", Toast.LENGTH_LONG);
}
@Override
public void onFinish() {
parent.displayToast("Device has disconnected from the Mirror", Toast.LENGTH_LONG);
}
@Override
public void onFailure(MirrorException exception) {
parent.displayToast("Something went wrong", Toast.LENGTH_LONG);
}
@Override
public void onData(JSONObject data) {
parent.displayToast("Data received from Mirror", Toast.LENGTH_LONG);
}
});
}
private void displayToast(String message, int length) {
if (this.toast != null) {
this.toast.cancel();
}
this.toast = Toast.makeText(MainActivity.this, message, length);
this.toast.show();
}
private void hideSoftKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
And inside of the React component responsible for displaying Mirror data, I’m using componentWillMount()
to setup the Mirror API:
componentWillMount() {
mirror.init();
mirror.listen(Mirror.Events.USER_DATA, {
ondata: (event) => {
const receievedData = event.data;
const {
name,
route,
} = receievedData;
if (name === undefined || route === undefined) {
return;
}
this.setState({
name,
route,
});
},
ondisconnect: () => {
this.setState({
name: '',
route: '',
});
},
});
}