How to get mac address and ip in Nativescript? - android

I want to get IP address from my mobile Android.
var context = application.android.context;
var wifiMgr = context.getSystemService("wifi");
var wifiInfo = wifiMgr.getConnectionInfo();
var ip = wifiInfo.getIpAddress();
console.log('ip', ip)
The result is: JS: ip -2029999936
But in fact this is not my IP.
Can you ask me any idea?
Update:
I follow this . I have this code:
Step1. In my component add this code:
import app = require("application");
app.android.context;
constructor() {
var context = android.content.Context;
var wifiManager = app.android.context.getSystemService(context.WIFI_SERVICE);
var wInfo = wifiManager.getConnectionInfo();
var mac = wInfo.getMacAddress();
}
Step2.
In AndroidManifest.xml add
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Error: [ts] Cannot find name 'android'. [2304] in this line: var
context = android.content.Context;
error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.

You should have ACCESS_WIFI_STATE permission in AndroidManifest.xml for capturing IP address.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Then all you have to is,
import * as application from 'tns-core-modules/application';
declare var android;
const wifiManager = application.android.context.getSystemService(android.content.Context.WIFI_SERVICE);
const connectionInfo = wifiManager.getConnectionInfo();
const ip = android.text.format.Formatter.formatIpAddress(connectionInfo.getIpAddress());
declare var android; is to avoid TS errors while access native apis. An alternative is to install tns-platform-declarations plugin and point the declaration files in your references.d.ts.
Regarding the Mac Address, since Android 6.0
To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.
So it doesn't seem officially supported.

Related

How to open Android hotspot using flutter

I want to set ssid and password of wifi hotspot in android using flutter.
I used wifi_iot but most of it's functions are deprecated.
is there any other way to do so?
I think using method channels would be better but I need code to open hotspot from kotlin/
give me full example of kotlin code please to open hotspot and set ssid, password
val wifiManager =
applicationContext.getSystemService(Context.WIFI_SERVICE) as
WifiManager val method =
wifiManager.javaClass.getMethod("setWifiApEnabled",
WifiConfiguration::class.java, Boolean::class.javaPrimitiveType) val
wifiConfig = WifiConfiguration() wifiConfig.SSID = "MyHotSpot"
wifiConfig.preSharedKey = "mypassword"
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK)
method.invoke(wifiManager, wifiConfig, true)

Xamarin Forms Android Serial Communication to ESP32

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

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

Unable to return IP address using react-native-device-info's getIPAddress()

I need to return the IP address for the device running my React Native app (an Android smart tv app). I am making use of react-native-device-info which has allowed me to get the model, manufacturer and operating system. However I am unable to get the ip address.
This is my code
deviceInfo = DeviceInfo.getIPAddress().then(ip => {
return ip;
});
However on the front end it appears as [object Object]. I can see in the console it is returning an object like this:
wifi:
_40: 0
_55: null
_65: 0
_72: null
I would have hoped to just return a string of the correct IP address.
I have also added the right permissions in my AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Also worth noting I am passing the information back by value: ${JSON.stringify(deviceInfo)}
Any one experienced this issue before?
I have used below library:
https://www.npmjs.com/package/react-native-network-info
And it is working fine, Below is the code:
// Get IPv4 IP (priority: WiFi first, cellular second)
NetworkInfo.getIPV4Address().then(ipv4Address => {
console.log(ipv4Address); //result e.g 192.168.1.100
});
Since its returning a promise try putting inside a async function and try to get the result.
Ex:
const getIpAddress = async()=>{
const ip = await DeviceInfo.getIPAddress();
console.log(ip);
return ip;
}
Hope it helps. Thank you.

Android Connect to wifi with programmatic WifiConfiguration

I have a problem when I want to connect my device to an existing network wifi.
That is the situation:
I am looking to an existing configured network, if there is not the one I am looking for I build it and then I try to connect the phone.
When the wifi configuration is built manually using the phone interface, I can find it and then connect to it.
But when I tried to build the configuration programmaticly Android cannot connect the phone to the network.
I have got the following message : "Association request to the driver failed".
I am wondering if the association that failed is the association between the scanned network and the configurated network. BUt I don't know why and so I don't know how to manage this problem.
EDIT: And I forget to say, that my WifiConfiguration is exactly the same as the one is created manually because I get the info from the conf with some logs.
EDIT2 : Here is my wificonf (network is protected by a wep key) :
WifiConfiguration wifiConf = new WifiConfiguration();
wifiConf.SSID = "\"ssid\"";
wifiConf.wepKeys[0] = "\"password\"";
wifiConf.wepTxKeyIndex = 0;
wifiConf.hiddenSSID = false;
wifiConf.status = WifiConfiguration.Status.DISABLED;
wifiConf.priority = 40;
wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);//+
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);//+
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
EDIT3 : I already add the permission in AndroidManifest :
uses-permission android:name="android.permission.ACCESS_WIFI_STATE"
uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"
uses-permission android:name="android.permission.CHANGE_WIFI_STATE"
I hope my problem is clear and hope too that someone could help me.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
This is the correct form of this setting:
wifiConfiguration.SSID = "\"" + networkSSID + "\"";

Categories

Resources