Wednesday, April 18, 2012

Ubuntu 11.10 Bluetooth Issue & Exploration

Command Line Bluetooth Transfer
This blog talks about the way to mount bluetooth enabled mobile phone for file transfer via obexfs
and also a very nice way to transfer files without a GUI using sdptool & ussp-push

Don't forget the power of sudo whenever you get any permission denied error ;)

Since I had upgraded my ubuntu version from 11.04 to 11.10.
I never succeeded in sending files via bluetooth to my mobile phone.
And finally the day has come,
I Googled for the error that I use to get while trying to send files via bluetooth
"Permission Denied (13)" and landed Here
From there I got redirected to Launch pad where I found many people reporting the same issue.
I got a chance to know some decent amount of distinct phone models too ;)
And I got him & HEEEE was the Guy whom I was looking for.

Also The issue with 11.10 can be resolved by upgrading your blueman service too via ppa

I just tried his way
$ sudo apt-get install obexfs
$ hcitool scan
Scanning ...
  3C:8B:FE:F6:1B:3A Tamilmani
$ obexfs -b 3C:8B:FE:F6:1B:3A /media/tamil
#Once I completed my transfer I unmounted it via
$ fusermount -u /media/tamil
It worked like a charm :) my phone got mounted & I'm able to transfer data
From man pages
fusermount : mount/unmount fuse filesystem [-u==unmount in the above example]
obexfs : mount obexFTP(Object Exchange FTP) capable devices
obexfs -b[bluetooth]
Learn about FUSE Here
From now my exploration started..
What is this hcitool do?
From man pages : Configure Bluetooth Connections & send some special Commands to bluetooth devices
Some Interesting Commands that we can send via hcitool:
scan: Scans all available and visible bluetooth devices
dev : Display local device [the host where you run hcitool from]
name [addr] : Displays the name of the specified device[addr]
info [addr] : Print device name, version  and  supported  features
and many more
[addr refers to BADDR of device -> unique radio frequency identifier, ex: 3C:8B:FE:F6:1B:3A]

What more ?
I learn't how to transfer data without a gui :P
$ sdptool browse 3C:8B:FE:F6:1B:3A
Service Name: Object Exchange
Service RecHandle: 0x10005
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
  Channel: 5
   "OBEX" (0x0008)
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100
$ sudo apt-get install ussp-push
$ ussp-push 3C:8B:FE:F6:1B:3A@5 /home/tamil/image.jpg image.jpg
A Brief explanation of the above
sdp -> Service Discovery Protocol
 sdptool -> Tool to send sdp queries to bluetooth devices
 Available commands:
  search : search for a service
  browse : browse all available services
  add    : add a service to sdpd
  and many more
So, using sdptool I figured out a list of services available in my destination bluetooth device
Out of all the services, I found the channel [5 -> highlighted in the output] of OBEX Object Push service
then using
ussp-push -> program that can be used to send files using OBEX (OBject EXchange) protocol over Bluetooth
I did transfer my file from my machine to my phone
$ ussp-push [addr]@channel src dst #dst is just a filename not a path

Friday, April 13, 2012

Interesting Javascript Facts

Wishes to the web development world :)
In the past 2 to 3 months I had chance to know about loads & loads of innovative JS Libraries
All of them made me wonder "Is there a thing that we can't do in JS?"
I wish to share some facts which got me mad in to this language & very beginnerish ;)

What is {} + []?
As soon as I saw this question my reply was "[object Object]"
But the real answer was 0 :O
Couldn't get any good guess on this, so posted @stackoverflow
And I myself couldn't believe I missed it :P
{} => Empty Block Scope & +[] => 0
So, cometh thy answer :)
Some more of the same kind
{} + {} = NaN
[] + [] = ""
[] + {} = "[object Object]"

Why obj === +obj?
I found this @ the source of Backbone.js
I was wondering why should they check JS number type in such a way?
Once again I got back to Stackoverflow
The reply once again stunned me up :D
It was to save the network bandwidth of end user :)
After compressing the code:
typeof(obj)=='number' [24 bytes]
obj===+obj [13 bytes]
So, 11 Bytes saved :D

So why is this $.each("Boolean Number String Function Array Date RegExp Object".split(" "), function () {})
This one is from jQuery :)
The moment I saw this as a javascript kid
Why not ["Boolean", "Number", "String", "Function", "Array", "Date", "RegExp", "Object"] this???
But this time I didn't seek Stackoverflow's help :P
After Compressing the code:
["Boolean","Number","String","Function","Array","Date","RegExp","Object"] (74 Bytes)
"Boolean Number String Function Array Date RegExp Object".split(" ") (69 Bytes)
So we saved 5 Bytes :)

And Finally one Crockford fact:
The guy who renamed logical && and || meaningfully :P
Because both the logical operators doesn't knock you back with the boolean result of the evaluation
&& => Guard Operator
Statement: expr1 && expr2
Return Val: expr1 if expr1 evaluates to false else expr2
     var a = 1 && 10, b, c = b && a ;
     console.log(a); // Will be 10 rather true
     console.log(c); // Will be undefined rather false
|| => Default Operator
Statement: expr1 || expr2
Return val: expr1 if expr1 evaluates to true else expr2
     var a = 10 || 1, b, c = b || a ;
     console.log(a); // Will be 10 rather true
     console.log(c); // Will be 10 rather true

Default Operator is most widely used to handle browser level inconsistencies like
evt = event || window.event;
src  = || evt.srcElement;

Also the Guard Operator
body = document && document.body; //If document exists return document body
complexBody = document && (document.body || document.getElementsByTagName(body)[0]) //Using both guard and default

Enjoy Javascript :) It has the most crazy useful parts than anyother language does :D