MAC Addresses in JavaScript

MAC addresses in JavaScript

I concur with all the previous answers that it would be a privacy/security vulnerability if you would be able to do this directly from Javascript. There are two things I can think of:

  • Using Java (with a signed applet)
  • Using signed Javascript, which in FF (and Mozilla in general) gets higher privileges than normal JS (but it is fairly complicated to set up)

is it possible to get the mac address using the javascript?

No!
Javascript is quite a high level language and doesn't have access to this sort of information. And such information via JavaScript would be a security vulnerability as well.

How would one generate a MAC address in Javascript?

"XX:XX:XX:XX:XX:XX".replace(/X/g, function() {
return "0123456789ABCDEF".charAt(Math.floor(Math.random() * 16))
});

jsfiddle http://jsfiddle.net/guest271314/qhbC9/

Javascript get nearby Bluetooth devices Mac Address

Available filters for Web Bluetooth are "Services", "Name", "Name prefix", and "Manufacturer data" as described at https://web.dev/bluetooth/#request.

You can try some combination at https://googlechrome.github.io/samples/web-bluetooth/device-info.html and https://googlechrome.github.io/samples/web-bluetooth/manufacturer-data-filter.html

Regarding the MAC address, it depends on Bluetooth devices. Some devices broadcast either an ephemeral one for privacy, a "static" one, a name which is used for "device.name" when there's one instead of the MAC address.

Which device are you trying to access? Check out about:bluetooth-internals#devices and try to identify one thing that makes it unique.

Regular Expression to filter MAC ADDRESS

^[a-zA-Z0-9]{6}G0-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-1923-785FEABCD128$

Explanation:

^ matches beginning of the string

[a-zA-Z0-9]{6} matches any alphanumeric character 6 times

G0- matches that text exactly

[a-zA-Z0-9]{4}- any alphanumeric character 4 times followed by a hyphen (appears twice)

1923-785FEABCD128 matches that text exactly

$ matches the end of the string

Incrementing hexadecimal string from MAC Address

Math in non-base 10 in JS

There is a surprisingly easy answer to this. The function parseInt() accepts a second argument, the incoming value's radix. For example

parseInt("F0", 16) // Tells us that this is a base 16 (hex) number.

This will let us convert to decimal. Now the toString() method for Number types also accepts a radix for the outgoing value. So if we put it together it looks like this.

(parseInt("F0", 16) + 1 ).toString(16) // returns f1

This takes the value F0, a hex number, converts it to decimal adds the 1, and returns back a hex string.

I hope this helps!

Edit

To answer more specifically to your question, the context of a mac address is 00-FF so this below will properly pad with a leading zero, and using modulus 'wrap' any numbers over FF back down to 00 and up again.

("0"+ ((parseInt("FF", 16) + 1) % 256 ).toString(16)).substr(-2)


Related Topics



Leave a reply



Submit