Transmitting a File Over Web Bluetooth - javascript

I'm developing a web app that has to transmit files over Bluetooth. Is this possible, and if so, how would I go about doing that? Example code would be much appreciated. I can't find any good documentation online. Also, it must be able to run on mobile devices. I'm very new to JavaScript. Thanks

Although I would strongly advise against using bluetooth as a beginner (or in general at this time due to it being a WIP for many browsers):
Web Bluetooth is NOT available for any mobile browser except Chrome & Opera for Android and Samsung Browser
The best resource is probably MDN and the specification.
Something along the lines of:
// Discovery options match any devices advertising:
// . The standard heart rate service.
// . Both 16-bit service IDs 0x1802 and 0x1803.
// . A proprietary 128-bit UUID service c48e6067-5295-48d3-8d5c-0395f61792b1.
// . Devices with name "ExampleName".
// . Devices with name starting with "Prefix".
//
// And enables access to the battery service if devices
// include it, even if devices do not advertise that service.
let options = {
filters: [
{services: ['<Your Device UUID>']}
]
}
navigator.bluetooth.requestDevice(options).then(function(device) {
console.log('Name: ' + device.name);
return device.gatt.getPrimaryService();
})
.then(function(service) {
return service.getCharacheteristic('<Your Charachteristic UUID>');
})
.then(function(characteristic) {
// Do something with the characteristic
})
.catch(function(error) {
console.log("Something went wrong. " + error);
});

Related

How do you obtain permissions from web-nfc API?

I'm trying to get Web NFC to work through the Web NFC API, but I can't get it past an error message of NotAllowedError: NFC permission request denied.
I'm using this on Chrome 89 Dev on a Windows 10 computer, and the source code is being run locally.
I have tried the examples posted on the Internet also, including the Google sample but it returns the same error. I'm not concerned with it being experimental at this point as referring to this does show it has successfully passed the necessary tests, including permissions.
The HTML/JS code I'm using is below, and I've read the specification point 9.3, but I can't make sense of it to write it as code, so is there a guideline algorithm that would be helpful here to resolve this?
async function readTag() {
if ("NDEFReader" in window) {
const reader = new NDEFReader();
try {
await reader.scan();
reader.onreading = event => {
const decoder = new TextDecoder();
for (const record of event.message.records) {
consoleLog("Record type: " + record.recordType);
consoleLog("MIME type: " + record.mediaType);
consoleLog("=== data ===\n" + decoder.decode(record.data));
}
}
} catch(error) {
consoleLog(error);
}
} else {
consoleLog("Web NFC is not supported.");
}
}
async function writeTag() {
if ("NDEFWriter" in window) {
const writer = new NDEFWriter();
try {
await writer.write("helloworld");
consoleLog("NDEF message written!");
} catch(error) {
consoleLog(error);
}
} else {
consoleLog("Web NFC is not supported.");
}
}
function consoleLog(data) {
var logElement = document.getElementById('log');
logElement.innerHTML += data + '\n';
};
<!DOCTYPE html>
<html>
<head>
<script src="webnfc.js"></script>
</head>
<body>
<p>
<button onclick="readTag()">Test NFC Read</button>
<button onclick="writeTag()">Test NFC Write</button>
</p>
<pre id="log"></pre>
</body>
</html>
From https://web.dev/nfc/#security-and-permissions
Web NFC is only available to top-level frames and secure browsing contexts (HTTPS only). Origins must first request the "nfc" permission while handling a user gesture (e.g a button click). The NDEFReader scan() and write() methods trigger a user prompt, if access was not previously granted.
I guess you are running from a file:// URL as you said "locally" which is not supported.
You need to host it from a local web server using a https:// URL
Once in the right scope trying to scan or write should trigger a user prompt.
You can also check permissions see https://web.dev/nfc/#check-for-permission
Update:
So I tried the sample page https://googlechrome.github.io/samples/web-nfc/
And this works for me on Android Chrome 87 with "Experimental Web Platform features" enabled
When you hit the scan button A dialog asking for permission pops up.
Comparing the code in this sample to yours I notice that does:-
ndef.addEventListener("reading" , ({ message, serialNumber }) => { ...
Where as yours does:-
ndef.onreading = event => { ...
I don't know if it is the style setting what happens on the Event or something else (Hey this is all experimental)
Update2
To answer the question from the comments of Desktop support.
So you should be some of the desktop/browser combinations at the moment and may be in the future there will be wider support as this is no longer experimental standards. Obviously as your test link suggest Chrome on a Linux Desktop should work as this is really similar to Android Support, with all the NFC device handling done by libnfc and the browser just has to know about this library instead of every type usb or other device than can do NFC.
From what seen of NFC support on Windows, most of this is focussed on direct controlling the NFC reader via USB as just another USB device, while there is a libnfc equivalent in Windows.Networking.Proximity API's I've not come across any NFC reader saying they support this or anybody using it.
For Mac Deskstop, given that Apple are behind the curve with NFC support in iOS, I feel their desktop support will be even further behind even though it could be similar to Linux.
As you can read at https://web.dev/nfc/#browser-support, Web NFC only supports Android for now which is why you get "NotAllowedError: NFC permission request denied." error on Windows.

Pushwoosh Cordova API: Can two different devices ever generate the same HWID?

Our app uses HWIDs generated by Pushwoosh as a key to differentiate devices. Looking over traffic logs, I am seeing what looks like the same device submitting HTTP requests from several different ISPs over short timeframes.
It appears that different devices from all over the internet are generating the same HWID, which our app is treating as the same device causing issues with users interfering with each other. Our data is showing about 50 requests appear to be from different devices, but using the same HWID.
This makes no sense to me -- from what I've read about HWIDs, they are based on the device serial number, so they should always be unique.
Our mobile app is written in Cordova, and we are getting HWIDs with this code:
get_hwid: (evt) =>
_this = #
regid = device.uuid
if evt? && evt.detail?
push_notification_id = evt.detail.deviceToken
else
push_notification_id = ""
pushNotification = cordova.require("pushwoosh-cordova- plugin.PushNotification")
pushNotification.getPushwooshHWID (hwid) ->
_this.debug 'in getPushwooshHWID callback'
_this.debug ' Pushwoosh HWID: ', hwid
_this.debug ' push_notification_id: ', push_notification_id
_this.debug ' regid: ', regid
_this.emit 'retrieved-hwid',
regid: regid
push_notification_id: push_notification_id
hwid: hwid
Has anyone observed the PushWoosh API generate HWIDs that weren't always unique?
The PushWoosh docs say that sometimes HWIDs can change on the same device, but I can't find anything that suggests that they can't be expected to be unique.
Thanks!
HWID's (which are IDFV/IDFA) are unique. The only way they may change (to another unique value) is when user restores a backup on a device.
If you see the same HWID's make sure your Pushwoosh SDK are >= 4.1.2
as outlined here
https://www.pushwoosh.com/blog/pushwoosh-sdk-update-ios-10-makes-difference/

Read the phone number of device using javascript [duplicate]

Is there any way to fetch user’s phone number in Firefox OS?
If so, any help would be appreciated.
According to Mozilla's app permissions page, there is an permission called "phonenumberservice" but there is no information about it. Anyway, the permision is listed under the "Internal (Certified) app permissions", which means that, when available, it can only be used by "system-level apps and default apps created by Mozilla/operators/OEMs".
With Firefox 2.0 you should be able to use Mobile Identity API:
https://wiki.mozilla.org/WebAPI/MobileIdentity
https://bugzilla.mozilla.org/show_bug.cgi?id=1021594
I believe the permission is:
"permissions": {
"mobileid": {} }
And it is privileged.
So, as #Jason said, the Mobile Identity API provides this capability, and not just for certified, but for privileged applications. So it is no longer just for OEMs.
The Mozilla Wiki site shows the API:
dictionary MobileIdOptions {
boolean forceSelection = false;
};
partial interface Navigator {
Promise getMobileIdAssertion(optional MobileIdOptions options);
};
The site also provides a sample code skeleton for this:
function verifyAssertion(aAssertion) {
// Make use of the remote verification API
// and return the verified msisdn.
// NB: This is necessary to make sure that the user *really* controls this phone number!
}
// Request a mobile identity assertion and force the chrome UI to
// allow the user to change a possible previous selection.
navigator.getMobileIdAssertion({ forceSelection: true })
.then(
(assertion) => {
verifyAssertion(assertion)
.then(
(msisdn) => {
// Do stuff with the msisdn.
}
);
},
(error) {
// Process error.
};
);
For this to work, you need to add the mobileid permission in the manifest file, for example like this (I made up the description):
"permissions": {
"mobileid": {
"description": "Required for sending SMS for two factor authentication",
"access": "readonly"
}
}
PS: I made this answer, because most answers are outdated, and the one that isn't, does not contain all useful information.
References:
App Manifest Documentation
Firefox Remote Verification

How to detect microphone type

I use webRTC (getUserMedia) for recording sound and uploading it to backend server. All works well except i am unable to determine the microphone type (is it a built-in mic, usb mic, headset mic, sth else?)
Does anybody know how can i detect the type?
You can use navigator.mediaDevices.enumerateDevices() to list the user's cameras and microphones, and try to infer types from their labels (there's no mic-type field unfortunately).
The following code works in Firefox 39 and Chrome 45 *:
var stream;
navigator.mediaDevices.getUserMedia({ audio:true })
.then(s => (stream = s), e => console.log(e.message))
.then(() => navigator.mediaDevices.enumerateDevices())
.then(devices => {
stream && stream.stop();
console.log(devices.length + " devices.");
devices.forEach(d => console.log(d.kind + ": " + d.label));
})
.catch(e => console.log(e));
var console = { log: msg => div.innerHTML += msg + "<br>" };
<div id="div"></div>
In Firefox on my system, this produces:
5 devices.
videoinput: Logitech Camera
videoinput: FaceTime HD Camera (Built-in)
audioinput: default (Logitech Camera)
audioinput: Built-in Microphone
audioinput: Logitech Camera
Now, there are some caveats: By spec the labels only show if device access is granted, which is why the snippet asks for it (try it both ways).
Furthermore, Chrome 45 requires persistent permissions (a bug?) which is not available in insecure HTTP, so you may need to reload this question in HTTPS first to see labels. If you do that, don't forget to revoke access in the URL bar afterwards, or Chrome will persist it, which is probably a bad idea on stackoverflow!
Alternatively, try https://webrtc.github.io/samples/src/content/devices/input-output which works in regular Chrome thanks to the adapter.js polyfill, but requires you to grant persistent permission and reload the page before you see labels (because of how it was written).
(*) EDIT: Apparently, enumerateDevices just got put back under an experimental flag in Chrome 45, so you need to enable it as explained here. Sorry about that. Shouldn't be long I hope.

location data for symbian

I want to retrieve location data (latitude and longitude) from symbian phone with its web browser with getlocation api. Is there anyway I can do it? any api to recommend to get location data from gps enabled symbian devices? Thanks!!
I would recommend checking out phonegap http://www.phonegap.com
They have a geolocation api, but I don't know which versions of Symbian that support it.
Also look into APIbridge on forum.nokia.com.
Nota that whether this is possible may depend very much on the version of Symbian you are targeting.
You may want to check out the open source geo-location-javascript JavaScript wrapper. Using it is as easy as this:
if (geo_position_js.init()){
geo_position_js.getCurrentPosition(function (p) {
alert(p.coords.latitude.toFixed(2) + ', ' + p.coords.longitude.toFixed(2));
},
function (p) {
alert('Error: ' + p.code);
});
}
else {
alert('Handset does not have client-side geolocation capabilities');
}
This wrapper was also discussed on Dive into HTML 5.

Categories

Resources