Xamarin Forms Android Serial Communication to ESP32 - android

I have tried to research a solution to my problem, but I think I lack the mental capacity to fully solve it from the examples I've found!
I have a project split into 2 halves. An ESP32 to read a RFID and iButton and an android front end to run on a kiosk.
I have the ESP set to spit out the serial numbers of the device connected/tapped and this works nicely on a windows pc with system.io.ports. What i thought would be easy is actually frustratingly difficult.
I've used the usbmanager to identify the USB devices and this works:
UsbManager manager = (UsbManager)GetSystemService(Context.UsbService);
System.Diagnostics.Debug.WriteLine("Starting USB");
var DeviceList = manager.DeviceList;
foreach (var device in DeviceList.Values)
{
var Class = device.Class;
var DeviceClass = device.DeviceClass;
var DeviceId = device.DeviceId;
var DeviceName = device.DeviceName;
var DeviceProtocol = device.DeviceProtocol;
var DeviceSubClass = device.DeviceSubclass;
var Type = device.GetType();
var ManufacturerName = device.ManufacturerName;
var ProductId = device.ProductId;
var ProductName = device.ProductName;
//var SerialNumber = device.SerialNumber;
var VendorId = device.VendorId;
var Version = device.Version;
System.Diagnostics.Debug.WriteLine(DeviceName + " " + VendorId);
}
However, how do I connect to one of the serial ports? namely one called /dev/bus/usb/006/002 4292 and read the string from it?
I've looked at several nuget libraries, but I'm struggling to get my head around these.
I'd really appreciate it if anyone could help me out! I'd rather not go back to using a windows PC in the kiosk. But serial coms on windows are so simple - although windows does have plenty of its own problems :)
Thanks
Andrew

Related

Accessing SerialPort from Android Phone with React Native

I have developed a React Native app for Android device to connect with a HW board and one of the functionality is to communicate with HW board with Serial I/F Adapter from Mobile .
I have tried out multiple npm packages and none of them I could get to work.
Here is my sample code
import SerialPortAPI from 'react-native-serial-port-api';
const path = await SerialPortAPI.devicePaths(paths => {
console.log("List paths", paths)
})
const connectDevice = async (cfg) => {
const { baudRate, serialPortName, stopBits } = cfg
serialPort = await SerialPortAPI.open(serialPortName, { baudRate, stopBits});
const sub = serialPort.onReceived(buff => {
const str = buff.toString('hex').toUpperCase()
console.log(str);
})
await serialPort.send('A7B7');
}
It is NOT listing the device List connected and also not able to open/write/read.
Other packages I tried are:
react-native-usbserial
react-native-serialport
react-native-usb-serialport
react-native-serial-port-api
I any pointers and working sample will be of great help.
Regards
Raghu VT
Are you connecting the phone to your pc or it should work as host? I think that is key point to understand.
My phone is Host and I started with Android code.
Would recommend to use Android to test if possible.
You want to pay attention to the port type used.
If you use a USB AB connector you need to use an otg cable or adapter.
if case of type C , this will be detected automatically.
Hope could provide some hints

"AppendTo" href doesn't work in Android Chrome?

I have this code:
jQuery(document).ready(function( $ ){
var magyar = $(".lang-item-hu a").attr("href");
$("#magyar").attr("href", magyar);
var angol = $(".lang-item-en a").attr("href");
$("#angol").attr("href", angol);
});
on my site. It works well on desktop Chrome, but doesn't work on Android Chrome.
Why? And what could be the remedy for this symptom?
Question edit/update:
On my desktop I use the latest version of Chrome 95.0.4638.69 / 64 bits on Windows 11 Pro ver: 21H2
On my mobile I also use the currently updated version of Chrome: 95.0.4638.50 on Android 11, One UI-version: 3.1
There are no related error messages on the desktop version.
I don't seem to find a debugger on Android's Chrome...
2nd edit (the solution):
No, the browser is fine.
I just had to change the id-s to classes, like so:
jQuery(document).ready(function( $ ){
var magyar = $(".lang-item-hu a").attr("href");
$(".ma").attr("href", magyar);
var angol = $(".lang-item-en a").attr("href");
$(".an").attr("href", angol);
});
...but I don't quite know why this worked. Anyone who knows leave an answer below. Thank you in advance!
One of my suggestion is try with .prop() Instead of .attr()
jQuery(document).ready(function( $ ){
var magyar = $(".lang-item-hu a").attr("href");
$("#magyar").attr("href", magyar);
var angol = $(".lang-item-en a").attr("href");
$("#angol").attr("href", angol);
});
Changed to
jQuery(document).ready(function( $ ){
var magyar = $(".lang-item-hu a").prop("href");
$("#magyar").prop("href", magyar);
var angol = $(".lang-item-en a").prop("href");
$("#angol").prop("href", angol);
});
More Details, pease check this link: https://stackoverflow.com/a/5876747/3073842
Similar Question: jquery attr() not working on mobile - Android - Chrome
No, the browser is fine.
I just had to change the id-s to classes, like so:
jQuery(document).ready(function( $ ){
var magyar = $(".lang-item-hu a").attr("href");
$(".ma").attr("href", magyar);
var angol = $(".lang-item-en a").attr("href");
$(".an").attr("href", angol);
});
...but I don't quite know why this worked. Anyone who knows leave an answer below. Thank you in advance!

Saving data on Godot android export

I am programming an android game with Godot 3.2.2. Of course I want to save the game data, therefore I created a new scene called SaveGame.tscn with SaveGame.gd. I exported the project to my windows pc and everything worked fine, all datas were saved. After that I also exported it to my android phone, but this time no data was saved. Instead it showed me that it could not find the save path. Maybe anybody of you can help me with this problem so that data is also saved on android.
SaveGame.gd:
extends Control
const FILE_NAME = "user://save_game_data_one.json"
var save_game_data
func _ready() -> void:
Signals.connect("load_data", self, "load")
Signals.connect("game_quit", self, "save")
save_game_data = {
"highscore": Globals.highscore,
"coins": Globals.coins,
"music_volume": Globals.music_volume,
"sound_volume": Globals.sound_volume,
"button_gameplay": Globals.button_gameplay,
"button_gameplay_side": Globals.button_gameplay_side,
}
func save():
update_data()
var file = File.new()
file.open(FILE_NAME, File.WRITE)
file.store_string(to_json(save_game_data))
file.close()
func load():
var file = File.new()
if file.file_exists(FILE_NAME):
file.open(FILE_NAME, File.READ)
var data = parse_json(file.get_as_text())
file.close()
if typeof(data) == TYPE_DICTIONARY:
save_game_data = data
else:
printerr("Corrupted data")
else:
self.show() <---- Here it shows me the hidden scene (white rectangle), this means it could not find FILE_NAME
export_data()
func export_data():
Globals.highscore = save_game_data["highscore"]
Globals.coins = save_game_data["coins"]
Globals.music_volume = save_game_data["music_volume"]
Globals.sound_volume = save_game_data["sound_volume"]
Globals.button_gameplay = save_game_data["button_gameplay"]
Globals.button_gameplay_side = save_game_data["button_gameplay_side"]
func update_data():
if Globals.score_round >= Globals.highscore:
save_game_data["highscore"] = Globals.score_round
save_game_data["coins"] += Globals.coins_round
I also would like to know the answer. I am trying to figure a way to have "persistent time" in my android game where "time passes" in game even when closed. Best solution I figure so far is getting the unix time on the games first start and check against it when reopening the app. My problem is finding a way to save that original unix time to call upon for checks.

Find my air application version in AS3 on iOS and Android

Is there an as3 API in Air (I'm using 3.2) to access my application version ? The one I give on the App Store or Android Market ?
Yeah you can pull it directly from the application xml descriptor. Something like this should work:
var descriptor:XML = NativeApplication.nativeApplication.applicationDescriptor;
var ns:Namespace = descriptor.namespace();
var version:String = descriptor.ns::version[0];
Looks like it's different for Air 4.0
This worked for me:
var descriptor:XML = NativeApplication.nativeApplication.applicationDescriptor;
var ns:Namespace = descriptor.namespace();
var version:String = descriptor.ns::versionNumber;
var _descriptor:XML = nativeApplication.applicationDescriptor;
var ns:Namespace = _descriptor.namespace();
var version:String = _descriptor.ns::versionNumber;
This is what works for me. "descriptor" var is used in AIR 3.2 for a UIComponentDescriptor, so I couldn't use that variable name. Also, statically accessing nativeApplication (NativeApplication.nativeApplication) gave me a null pointer reference, so I just grabbed it directly.
Lastly, versionNumber is what stores the version in AIR 3.2.

android device ID (not IMEI)

I use the command: adb devices to list the attached devices.
On my computer I get :
List of devices attached
HT9CTP820988 device
My question is: how can I get this id (HT9CTP820988) programmatically ?
What you're seeing with the adb devices command is the serial number:
Serial number — A string created by
adb to uniquely identify an
emulator/device instance by its
console port number. The format of the
serial number is -.
Here's an example serial number:
emulator-5554
(refererence: http://developer.android.com/guide/developing/tools/adb.html)
When you ask "how can I get this id programmatically" what exactly do you mean? From an Android app or from a desktop app?
How about this one?
http://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID
edit: Hmm I recall this, it can't be right; the ANDROID_ID is supposed to be 64-bit. Maybe the string you see is given by the USB driver?
Look at Settings.ACTION_DEVICE_INFO_SETTINGS and answers that have already been given in the past How to find serial number of Android device?
I think the emulator id's purpose is to identify the emulator and devices in the development environment. And it may not be accessible from the phone.
It's possible by changing *strings_dev* struct from drivers/usb/gadget/android.c
I'm Using the following code...
String aid = Settings.Secure.getString(getContext().getContentResolver(), "android_id");
Object obj = null;
try {
((MessageDigest) (obj = MessageDigest.getInstance("MD5"))).update( aid.getBytes(), 0, aid.length());
obj = String.format("%032X", new Object[] { new BigInteger(1, ((MessageDigest) obj).digest()) });
} catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
obj = aid.substring(0, 32);
}

Categories

Resources