IP address/ OS version and device key - ibm-mobilefirst

I have MobileFirst (V7.O) Hybrid app . How can I get the current IP address, OS version (android nnn, Iphone nnn), Device unique Id for the device. Basically, I am recording some information at various point in my app.
Can you please provide some hints and how to get this information
Thanks for your help

You can be found deviceID & IP address below information
unique deviceID
WL.Device.getID({onSuccess : function(o) {
alert(o.deviceID);
}, onFailure : function(e) {
WL.Logger.info("Error getting ID: " + e);
}});
IP address
WL.Device.getNetworkInfo(function (networkInfo) {
alert (networkInfo.ipAddress);
});
Device OS version use cordova
var deviceVersion = device.version;
check cordova api https://cordova.apache.org/docs/en/3.0.0/cordova/device/device.html
more this link http://www-01.ibm.com/support/knowledgecenter/SSZH4A_6.2.0/com.ibm.worklight.apiref.doc/html/refjavascript-client/html/WL.Device.html?cp=SSZH4A_6.2.0%2F10-0-0-1-9

Related

React Native / Expo : Fetch throws “Network request failed”

I saw several posts on the subject but without result. I have on the one hand a form which collects information (name, first name etc) then saves it in database (mongodb). Everything works when I use postman to send my information via the route / signup, i can see my new user in mongodb. but when i'm starting the app on Expo he throw me "Network request failed".
Frontend fetch :
submitForm = () => {
var signupData = JSON.stringify({
first_name: this.state.firstName,
last_name: this.state.lastName,
email: this.state.email,
password: this.state.password
});
fetch(`https://localhost:3000/signup`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: signupData
})
.then(response => {
console.log(response);
return response.json();
})
.then(data => {
if (data.result) {
this.props.handleUserValid(
this.state.firstName,
this.state.lastName,
this.state.email,
data.user.token
);
this.props.navigation.navigate("Account");
}
})
.catch(error => {
console.error(error);
});
};
And Backend route :
router.post("/signup", function(req, res, next) {
var salt = uid2(32);
console.log("Signup is running...");
const newUser = new userModel({
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
password: SHA256(req.body.password + salt).toString(encBase64),
token: uid2(32),
salt: salt
});
newUser.save(function(error, user) {
console.log("LOG: user", user);
res.json({ result: true, user });
});
});
module.exports = router;
And here is a screenshot of the error
Again when using Postman, the fetch is working good, my console log is printed and the user added to my data base.
Thanks for the help.
-- EDIT --
I launched the application in a web browser via Expo and everything works perfectly. My sign in / sign up pages and my account page. But on my phone it's not working (IOS), it's a network problem from my phone (maybe a certificate problem, wrong IP ?)
if you have an idea i'm interested, i've been stuck on it for 2 days
Had the same issue with React-native Expo and Python Django back-end.
The problem is about a conflict between an emulator localhost and server localhost.
Your back-end-server might be ruunning on 127.0.0.1:8000, but an emulator can't find this.
In terminal find your Ipv4-Address with a command 'ipconfig'.
For ex., it will be 192.138.1.40
After this put it into your fetch (
'http://192.138.1.40:8000/').
And what is also important - run your back-end-server with the same host and port.
On python Django for example:
py manage.py runserver 192.138.1.40:8000
On Django you will also need to add ALLOWED_HOSTS = ['192.138.1.40'] in settings.py
Instead of 'localhost', while using expo, use your device's (computer's) IP address (http://192.168.x.x:3000/'signup'). This method worked for me. Make sure that your PC and mobile are connected to the same network. Type ipconfig/all in the command prompt to find IP address.
Update:
Seems like this was my problem coupled with my roommates hogging the wifi bandwidth. Slow internet connection may also be a problem. ATB with your problem.
I had the same issue with Expo: fetch error. For my backend. I use json-server to mock API data. In my case, the json-server runs on http://localhost:3000/playlist
Instead of fetch("http://localhost:3000/playlist"), I did fetch(http://10.0.2.2:3000/playlist), then it worked. Using the Android emulator, it could not find the server's address.
For the reason why using 10.0.2.2, check here. why do we use 10.0.2.2 to connect to local web server instead of using computer ip address in android client
I had the same issue - what worked for me was to:
Run my local server on host 0.0.0.0
Go to network preferences and find my LAN IP address (e.g. 192.168.1.1)
Replace the host in my url in the mobile app with the LAN IP (e.g. http://192.168.1.1:3000/signup)
Reload and test
For anyone using Serverless, I used this command to run on 0.0.0.0
ENV=local serverless offline -s local -o 0.0.0.0
I had a similar issue. Apparently, the emulator does not understand or see 'localhost' as host.
What I did:
run ipconfig on your cmd, copy the ipv4 address, then use that to replace 'localhost' for your server host.
you should check the URL
https://localhost:3000/signup (X)
http://localhost:3000/signup (O)
NOT HTTPS
If anyone facing this issue with a hosted backend server this is for your knowledge.
Used react native expo cli
Backend(Spring Boot) hosted on a Azure server ( Eg URL : https://abcd-spring-app.azurewebsites.net). HTTPS used.
But still I faced the below issue.
Network request failed at node_modules\whatwg-fetch\dist\fetch.umd.js:535:17 in setTimeout$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:383:16 in callTimers
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
After some research I found that this is because the slow response time of the server. The network request failed due to the timeout.
So Before testing your app with the backend server, send some requests from the web(or any other way) and up your server. Because some servers get inactive after some time. Make sure you have a good connection.

How to read from AzureIOT only messages from one device

I have an Azure IOT solution where data from 2 devices go to the same IOT hub. From my computer I need to read the messages only from one of the devices. I implemented the ReadDeviceToCloudMessages.js in https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-node-node-getstarted
var client = EventHubClient.fromConnectionString(connectionString);
client.open()
.then(client.getPartitionIds.bind(client))
.then(function (partitionIds) {
return partitionIds.map(function (partitionId) {
return client.createReceiver('todevice', partitionId, { 'startAfterTime' : Date.now()}).then(function(receiver) {
console.log('Created partition receiver: ' + partitionId)
receiver.on('errorReceived', printError);
receiver.on('message', printMessage);
});
});
})
.catch(printError);
But I am getting all the messages in the IOThub. How do I get messages only from one device.
You can route the expected device message to build-in endpoint: events. Then you can only receive the selected device message from your above code.
Create the route:
Turn "Device messages which do not match any rules will be written to the 'Events (messages/events)' endpoint." to off and make sure the route is enabled.

Mobile First Auto-update fails in iOS devices while changing the Server URL at run time

I'm working on an IBM MobileFirst Platform 7.1 project where we have many remote servers say, Dev, Test etc. The requirement was to change the servers at run time so that we would not be required to build and distribute multiple apps.
I followed the Official IBM tutorial.
Here is the code I use to connect to the server after the server is changed.
WL.Client.connect({onSuccess: function(){
WL.SimpleDialog.show(
"Connection Success", "Successfully connected to the server URL. Your app will reload once you press OK",
[{text: "OK", handler: function() {WL.Client.reloadApp();}}]
);
}, onFailure: function(error) {
WL.SimpleDialog.show(
"Connection Falied", error.status +" "+error.errorCode + " : " + error.errorMsg,
[{text: "Close", handler: function() {}}]
);
}});
It works perfectly fine in Android. But, in iOS devices, the server gets changed and gets connected to the new server. When the Auto-update runs in order to fetch the new code from the changed server, it gets Stuck indefinitely
Any help would be greatly appreciated. Thanks in advance.

chrome.usb.findDevices not working shows error message Failed to open device: Entity not found in chrome://device-log/

Reposting from https://groups.google.com/a/chromium.org/d/topic/chromium-apps/noGfn29Aed8/discussion for public reply.
I'm trying to develop a chrome app to communicate with an HID using the chrome.usb API. I am using these functions.
chrome.usb.findDevices
chrome.usb.controlTransfer
What I've tried so far does not work. The info in chrome://device-log/ shows this error message.
USB Event [13:54:01] Failed to open device: Entity not found
I am using right VendorId and ProductId in decimal format, Which I have extracted from device manager info.
Here is the code snippet I am using.
chrome.usb.findDevices(DEVICE_INFO, function (devices) {
if (!devices || !devices.length) {
console.log('device not found');
}
console.log("Devices: " + devices);
console.log(devices[0]);
var TransferData = {
"requestType": "class",
"recipient": "interface",
"direction": "out",
"request": 0xx9,
"value": 0xxx0,
"index": 0,
"data": new Uint8Array([00,05, 47, 76, 66, 48, 47,00]).buffer
};
setTimeout(function () {
chrome.usb.controlTransfer(devices[0], TransferData, function (config) {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError);
} else {
console.log('Data Transfer completed');
}
});
}, 3000);
});
I see two issues in your question.
Check your vendor/product ids. You said you had decimal ids but the DEVICE_INFO definition is missing. I see in the snippet of code that you're using 0xx9 in the request field of TransferData which is not a valid hex literal. Are you doing the same thing in the DEVICE_INFO? If the vid is 1234 decimal, make sure you're not saying 0x1234.
There are separate chrome. APIs for USB and HID. I'm pretty sure the USB interface will not allow you to manipulate devices that advertise as HID. You must instead use the chrome.hid interface for these devices.
There are a few questions here that could have some helpful info for you:
Can I access USB HID device via a Chrome App?
HID compliant device will not enumerate using chrome.hid API on ChromeOS

Titanium XHR cannot connect the server

I' trying to let android app using titanium connect the localhost via xhr, here is the code:
function e (){
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function()
{
alert(xhr.responseText);
};
xhr.onerror = function()
{
alert("Error");
};
xhr.open("GET","http://localhost/test.php");
xhr.send();}
I get an error, and this appears in the process trace:
I/TiHttpClient( 211): (TiHttpClient-1) [16,27717] Sending error
Connection to http://localhost refused
the Android Emulator has its own network interface, so localhost is the emulator, not your host machine. You need to use the IP address 10.0.2.2 to access the machine that the emulator is running on. See http://juristr.com/blog/2009/10/accessing-host-machine-from-your/
Try to open with your IP. Like
xhr.open("GET","http://xxx.xxx.xxx.xxx/test.php");
(I've done this with Objective C not with Titanium. But you can give it try)
EDIT
Define Port number
xhr.open("GET","http://xxx.xxx.xxx.xxx:8888/test.php");