error on oncreate() method - java

I am begginer in Android App and using Java as when I add this code :
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
I had error on :
SENDER_ID
Log
TAG
the error "cannot be resolved to available "

As I commented if you're a beginner you shouldn't start with GCM, but seems that you're a beginner not only on Android but also in Java (this is not bad, everybody was a beginner, and I'm not that advanced).
I suggest you to start following some basic tutorials on Java, then start with some basic tutorial for Android and so on.
GCM requires also a server side, so this is going to be quite advanced.
Anyway, the error is simple. SENDER_ID is a field that you haven't defined anywhere.
You have to declare it somewhere, like:
String SENDER_ID = "mySenderId";
or at the top of your class:
public class MyClass {
private static final SENDER_ID = "mySenderID";
}

Related

USB Serial Driver Java Plugin not working in Unity Android build

I'm trying to make an Android app with the Unity editor in order to read from an USB device. I'm using Unity 2019.4.12f1 and the android device I'm building to is a Xiaomi Mi Box S, running Android 9.
What I've done so far is to use Android Studio to compile some java code in order to gain access to the USB port on Xiaomi.
I'm using this repo: https://github.com/mik3y/usb-serial-for-android as the base functionality of the library I'm trying to build.
This is the code I wrote in my Java library:
public class Plugin extends Activity {
private static UsbSerialPort port;
private static final int READ_WAIT_MILLIS = 2000;
public static String Initialize(Context unityContext) throws IOException {
UsbManager manager = (UsbManager) unityContext.getSystemService(Context.USB_SERVICE);
ProbeTable customTable = new ProbeTable();
customTable.addProduct(0x067b, 0x2303, CdcAcmSerialDriver.class);
UsbSerialProber prober = new UsbSerialProber(customTable);
List<UsbSerialDriver> availableDrivers = prober.findAllDrivers(manager);
if (availableDrivers.isEmpty()) {
return "Drivers list is empty";
}
// Open a connection to the first available driver.
UsbSerialDriver driver = availableDrivers.get(0);
UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
if (connection == null) {
return "Connection is null";
}
port = driver.getPorts().get(0); // Most devices have just one port (port 0)
port.open(connection);
port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
return "Connection Successful";
}
public static String ReadPort() throws IOException {
if(port == null || !port.isOpen())
return "Null";
byte[] buffer = new byte[8192];
int len = port.read(buffer, READ_WAIT_MILLIS);
return buffer.toString();
}
public static void CloseSerialPort() throws IOException {
if(port != null && port.isOpen())
port.close();
}
And this is the C# code i'm using on the Unity side:
void Start()
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaClass pluginClass = new AndroidJavaClass("com.hoho.android.usbserial.Plugin");
string response = pluginClass.CallStatic<string>("Initialize", context);
Debug.Log(response);
if (response.Contains("Successful"))
{
string read = pluginClass.CallStatic<string>("ReadPort");
Debug.Log(read);
}
else
{
Debug.Log("Init failed");
}
}
You can see that I've created a custom "Prober" for my device with the it's Vendor and Product ID's, so in theory the Java side should know what to look for. Unfortunately the collection "availableDrivers" is empty after the code executes.
On the C# side, I'm sending my unity activity for the Java to get it's context from. I see that the USB manager needs that context in order to be instanced. I did check and the variable "unityContext" is not null after is passed into the Java library from C#. Right now I'm investigating if maybe the context I'm sending to Java is not the right one, causing the USB manager not being able to access the USB devices.
The return String that I get from the Java code to Unity is: "Drivers list is empty", hence the conclusion that the USB drivers list is empty.
Any help or any kind of leading in the right direction is going to be helpful.
PS. Things I've checked:
I know that the USB device is able to communicate with the Android device because I've used an app to check and everything seemed fine. (https://play.google.com/store/apps/details?id=de.kai_morich.serial_usb_terminal&hl=en)
I'm answering my own question here. Maybe someone else will stumble upon this.
There were a couple of issues that I was not aware of:
The Xiaomi USB port will not continue to communicate with the sensor if it is set to USB Debugging Mode;
Again, even if the Xiaomi USB Debugging mode was turned off, it will not work. To make it work I had to restart the device.
The conclusion being that if you are working with Xiaomi Mi Box S make sure the hardware connection is working before you start changing anything on your code.
To make all of this work:
Make sure you understand how Java <-> C# communication works;
You will need to be a little bit familiar with Android Studio library builds;
Follow the instructions on the repo I posted.
You should be good to go at this point. Let me know if you need any further help.

Carrier custom application

I need to create an Android application to set carrier configuration(VoLte e.g.). The application should fetch configs from our Back-End and apply them on the phone.
In Android documentation I found the following article: This article says, that I can create my own application and override CarrierService.
public class SampleCarrierConfigService extends CarrierService {
private static final String TAG = "SampleCarrierConfigService";
public SampleCarrierConfigService() {
Log.d(TAG, "Service created");
}
#Override
public PersistableBundle onLoadConfig(CarrierIdentifier id) {
Log.d(TAG, "Config being fetched");
PersistableBundle config = new PersistableBundle();
config.putBoolean(
CarrierConfigManager.KEY_CARRIER_VOLTE_AVAILABLE_BOOL, true);
config.putBoolean(
CarrierConfigManager.KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL, false);
config.putInt(CarrierConfigManager.KEY_VOLTE_REPLACEMENT_RAT_INT, 6);
// Check CarrierIdentifier and add more config if needed…
return config;
}
}
I created an app with this service, but the method onLoadConfig(CarrierIdentifier id) is never called by the system.
So what I want from the system to call my overridden method, not system's. What should I do?
I found your question when researching how to do something similar.
In the article you linked it says:
The carrier app in question must be signed with the same certificate found on the SIM card, as documented in UICC Carrier Privileges.
Since we can't get the certificate from your carrier (they will never give it to you) I think we can't implement our own flavour sadly :-(

Trying to add google maps to a Vaadin view

I'm trying to use the Vaadin GoogleMaps plugin on our web application to show a small window with Google maps. I have this:
private GoogleMap googleMap;
private LatLon mapMarkerlatLon;
private GoogleMapMarker googleMapMarker;
private final String apiKey = "https://maps.googleapis.com/maps/api/js?key=<ourKey>=initMap";
#Override
public void enter(ViewChangeEvent event) {
//googleMap = new GoogleMap(apiKey, null, null);
//As suggested:
googleMap = new GoogleMap(apiKey, null, "english");
mapMarkerlatLon = new LatLon(0,0);
googleMap.setCenter(mapMarkerlatLon);
googleMapMarker = new GoogleMapMarker("Tal", mapMarkerlatLon, false);
googleMap.addMarker(googleMapMarker);
initializeViewContent();
}
A bit further down my class..
protected CssLayout initializeViewContent() {
CssLayout sideBarLayout = new CssLayout();
sideBarLayout.addStyleName("sidebar");
/* ... */
sideBarLayout.addComponent(googleMap);
return sideBarLayout;
}
There's more stuff around the class,but basically this is the "important" bit. When I load the page, I got this:
More or less, translated to english, it says "An error occured. This webpage did not load Google Maps correctly. Discover the technical details on the Javascript console."
EDITED: Firfeox Console is as empyt as my knowledge of this topic.
Chrome's console is telling me about a missing keyMapError.
I've tried changin the key format to:
{ourKey}
{ourKey}=initMap
key={ourKey}=initMap
And, well, I'm getting tired of playing with combinatory. The key works, as it was given to me with a small demo that works flawlessly.
What I'm doing wrong?

Google Cast - Cannot Resolve mSelectedDevice

I am having a problem making a Google Cast Service. I can not seem to find what to use for mSelectedDevice. Both tutorials that I am using do not provide enough explanation for this, and neither go into detail of what mSelectedDevice should be.
public class CastMediaRouterCallback extends MediaRouter.Callback{
#Override
public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo info) {
mSelectedDevice = CastDevice.getFromBundle(info.getExtras());
String routeId = info.getId();
//Startd NanoHTTPD, Find URI of photo/video, and display on Cast device
}
#Override
public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo info) {
teardown();
mSelectedDevice = null;
}
}
(Tutorials I am using: https://developers.google.com/cast/docs/android_sender /// https://www.binpress.com/tutorial/building-an-android-google-cast-sender-app/161)
mSelecteDevice is an instance variable that is of type CastDevice. Not sure what you mean by "Google Cast Service" in your question but it seems you might be better off grabbing a sample project from oue GitHub repo as your starting point.

Android - Using JavaScript variable in code

I need to pull a JavaScript var off a site so I can use it in my code. Following this tutorial, I was able to display the string in an alert message. But what do I have to do to use the string outside of the alert message? Thanks.
EDIT: My code is basically the same as in the tutorial.
Instead of calling AlertDialog, just do something in Java with the value of the "html" parameter, unless I'm completely misunderstanding what you are asking.
String savedHtml = null;
/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
#SuppressWarnings("unused")
public void showHTML(String html)
{
savedHtml = html; // this ought to work.
}
}

Categories