LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-28-2006, 09:18 AM   #1
thoyyib
Member
 
Registered: Sep 2005
Distribution: SuSE 10
Posts: 50

Rep: Reputation: 15
shell scripting


Hi,

I need to broadcast abc.jpg to all the bluetooth devices in the rage. Please help me to write a shell script for the same.

Steps:

1) Scan bluetooth devices:

thoyyib@thoyyib:~> hcitool inq
Inquiring ...
00:11:67:21:5B:80 clock offset: 0x3c46 class: 0x102104
00:16:4E:0C:55:E0 clock offset: 0x25b5 class: 0x50020c
00:14:A7:8F:EC:FB clock offset: 0x2aa2 class: 0x50020c
00:15:A0:44:3A:52 clock offset: 0x302e class: 0x50020c

2) Take bluetooth address one by one
3) bind the address to /dev/rfcomm0,1,2...

thoyyib@thoyyib:~#rfcomm bind /dev/rffcomm0 00:11:67:21:5B:80 9

4) send file to the device

thoyyib:/home/thoyyib/Desktop/Downloads/ussp-push-0.9 # ./ussp-push /dev/rfcomm0 /home/thoyyib/abc.jpg image


Is it possible to run these scripts continously(like a broadcast server)?


Thanks,
THOYYIB
 
Old 05-28-2006, 09:41 AM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by thoyyib
Hi,

I need to broadcast abc.jpg to all the bluetooth devices in the rage. Please help me to write a shell script for the same.

Steps:

1) Scan bluetooth devices:

thoyyib@thoyyib:~> hcitool inq
Inquiring ...
00:11:67:21:5B:80 clock offset: 0x3c46 class: 0x102104
00:16:4E:0C:55:E0 clock offset: 0x25b5 class: 0x50020c
00:14:A7:8F:EC:FB clock offset: 0x2aa2 class: 0x50020c
00:15:A0:44:3A:52 clock offset: 0x302e class: 0x50020c

2) Take bluetooth address one by one
3) bind the address to /dev/rfcomm0,1,2...

thoyyib@thoyyib:~#rfcomm bind /dev/rffcomm0 00:11:67:21:5B:80 9

4) send file to the device

thoyyib:/home/thoyyib/Desktop/Downloads/ussp-push-0.9 # ./ussp-push /dev/rfcomm0 /home/thoyyib/abc.jpg image


Is it possible to run these scripts continously(like a broadcast server)?


Thanks,
THOYYIB
I'm not quite sure what you mean by continuous broadcast. If you do this:
Code:
for dev in $(hcitool inq | sed -e '/Inquiring/d' | awk '{print $1}); do
	rfcomm bind /dev/rfcomm0 ${dev}
	ussp-push /dev/rfcomm0 ~/abc.jpg image
	sleep 1
done
You will just do this once for each device. Are you looking for a script that polls to see when a new device enters the range?
 
Old 05-28-2006, 10:59 AM   #3
thoyyib
Member
 
Registered: Sep 2005
Distribution: SuSE 10
Posts: 50

Original Poster
Rep: Reputation: 15
Thank you sir,

Wow Its exactly i am looking for.
I just changed the code to:

#!/bin/bash

rfcomm release /dev/rfcomm0
echo "rfcomm0 released"
rfcomm release /dev/rfcomm1
echo "rfcomm1 released"
rfcomm release /dev/rfcomm2
echo "rfcomm2 released"
rfcomm release /dev/rfcomm3
echo "rfcomm3 released"
rfcomm release /dev/rfcomm4
echo "rfcomm4 released"
rfcomm release /dev/rfcomm5
echo "rfcomm5 released"
rfcomm release /dev/rfcomm6
echo "rfcomm6 released"
rfcomm release /dev/rfcomm7
echo "rfcomm7 released"
rfcomm release /dev/rfcomm8
echo "rfcomm8 released"
rfcomm release /dev/rfcomm9
echo "rfcomm9 released"


count=0
for dev in $(hcitool inq | sed -e '/Inquiring/d' | awk '{print $1}'); do
str="rfcomm"
count=$(( $count + 1 ))
str=$str$count
file="/dev/"$str
echo "file = "$file
echo "device = "$dev
rfcomm bind $file ${dev} 9
./ussp-push $file /home/thoyyib/image004.gif image
sleep 1
done


I am a newbi in Shell scripting. Please correct my code.
Now I am looking for a script that polls to see when a new device enters the range. Is it possible?

Thanks once again

THOYYIB
 
Old 05-28-2006, 12:29 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by thoyyib
Thank you sir,

Wow Its exactly i am looking for.
I just changed the code to:

#!/bin/bash

rfcomm release /dev/rfcomm0
echo "rfcomm0 released"
rfcomm release /dev/rfcomm1
echo "rfcomm1 released"
rfcomm release /dev/rfcomm2
echo "rfcomm2 released"
rfcomm release /dev/rfcomm3
echo "rfcomm3 released"
rfcomm release /dev/rfcomm4
echo "rfcomm4 released"
rfcomm release /dev/rfcomm5
echo "rfcomm5 released"
rfcomm release /dev/rfcomm6
echo "rfcomm6 released"
rfcomm release /dev/rfcomm7
echo "rfcomm7 released"
rfcomm release /dev/rfcomm8
echo "rfcomm8 released"
rfcomm release /dev/rfcomm9
echo "rfcomm9 released"


count=0
for dev in $(hcitool inq | sed -e '/Inquiring/d' | awk '{print $1}'); do
str="rfcomm"
count=$(( $count + 1 ))
str=$str$count
file="/dev/"$str
echo "file = "$file
echo "device = "$dev
rfcomm bind $file ${dev} 9
./ussp-push $file /home/thoyyib/image004.gif image
sleep 1
done


I am a newbi in Shell scripting. Please correct my code.
Now I am looking for a script that polls to see when a new device enters the range. Is it possible?

Thanks once again

THOYYIB
LOL, I completely forgot about the different device numbers!

As for your script:

1) Why don't you do the device `releasing' in a loop?

2) There is an off-by-one error so the first device in the loop is /dev/rfcomm1 (where I think you want /dev/rfcomm0).

3) Your script is somewhat overly verbose (for example, when using double quotes, one usually puts variables inside the string. Also, if all you are outputting to the user is $file, then why the use of the extra variable, etc.). Remember, succinct code is easier to read, understand, and maintain. I cleaned it up a bit here.
Code:
count=0
for dev in $(hcitool inq | sed -e '/Inquiring/d' | awk '{print $1}'); do
	file="/dev/rfcomm$count"
	echo "file = $file"
	echo "device = $dev"
	rfcomm bind $file $dev 9
	./ussp-push $file /home/thoyyib/image004.gif image
	((count++))
	sleep 1
done
Yes it is possible to write a script that polls for new devices. The only CLI tool that I know of that does polling is watch (which is not script-friendly), so you'd have to implement that aspect yourself. The problem is that it would probably introduce a lot of unnecessary overhead.

I don't know much about Bluetooth, so I can't give you any direct advice (maybe someone more knowledgeable would like to help). But I have a feeling that you can accomplish this using hotplug or udev (or whatever your distro uses). You will have to write scripts here as well. Research this ( Google hotplug or udev, look at already written implementations of bluetooth entry notification, etc.). If, on the other hand, you plan on doing this one time (e.g., have this already running when your friends come over and impress them by having your picture sent to them when they get close enough to the transmitter ), you might want to write a basic script that polls every so often, looking for changes of the inquiry (possibly using temporary files), and pushes to each new device (NOTE: if you know/are learning perl, it would be easier to use in this circumstance).

I do not have bluetooth stuff, so I a have a question about one thing: do multiple pushes result in multiple copies of the same file? For example, if I had my cell phone in range and executed the above script three times, you I have three copies of the picture on my phone?

I'm sorry if I just told you a bunch of stuff you may never use, but feel free to ask specifics if you don't understand something. If you have any more questions, just ask.

P.S.
Use [CODE] tags to make it easier to read the code samples in your future posts (in the AJAX editor thing it's the button with a # on it).

Last edited by osor; 05-28-2006 at 02:39 PM.
 
Old 05-29-2006, 05:40 AM   #5
thoyyib
Member
 
Registered: Sep 2005
Distribution: SuSE 10
Posts: 50

Original Poster
Rep: Reputation: 15
bluetooth

Hi Osor,

Thanks a lot for your informations. I made little changes on it:

#!/bin/bash
echo ""
while true
do
echo ""
echo "Searching for Bluetooth devices ........................................................................"
echo ""
count=0
for dev in $(hcitool inq | sed -e '/Inquiring/d' | awk '{print $1}'); do
file="/dev/rfcomm$count"
rfcomm release $file
echo "New device found with bluetooth address: "$dev
echo "Channel for the device = "$(sdptool search --bdaddr $dev OPUSH | sed -e '/Channel/!d')
channel=$(sdptool search --bdaddr $dev OPUSH | sed -e '/Channel/!d' | awk '{print $2}')
echo $dev" is binding with "$file" using channel "$channel" ........."
rfcomm bind $file ${dev} $channel
echo "Pushing data to "$dev" ........"
./ussp-push $file /home/thoyyib/Desktop/iac.txt IAC
echo "Job finished for device "$dev
echo ""
echo ""
((count++))
sleep 1
done
done



Now I am working on the code. It sends file to device more than one time.
I will contact you later. Any comments, please.......

Regards,
THOYYIB
 
Old 05-30-2006, 12:25 AM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by thoyyib
Hi Osor,

Thanks a lot for your informations. I made little changes on it:

#!/bin/bash
echo ""
while true
do
echo ""
echo "Searching for Bluetooth devices ........................................................................"
echo ""
count=0
for dev in $(hcitool inq | sed -e '/Inquiring/d' | awk '{print $1}'); do
file="/dev/rfcomm$count"
rfcomm release $file
echo "New device found with bluetooth address: "$dev
echo "Channel for the device = "$(sdptool search --bdaddr $dev OPUSH | sed -e '/Channel/!d')
channel=$(sdptool search --bdaddr $dev OPUSH | sed -e '/Channel/!d' | awk '{print $2}')
echo $dev" is binding with "$file" using channel "$channel" ........."
rfcomm bind $file ${dev} $channel
echo "Pushing data to "$dev" ........"
./ussp-push $file /home/thoyyib/Desktop/iac.txt IAC
echo "Job finished for device "$dev
echo ""
echo ""
((count++))
sleep 1
done
done



Now I am working on the code. It sends file to device more than one time.
I will contact you later. Any comments, please.......

Regards,
THOYYIB
Again, you are allowed to put referenced variables inside double quoted strings. E.g., change things like this:
Code:
echo $dev" is binding with "$file" using channel "$channel" ........."
to this:
Code:
echo "$dev is binding with $file using channel $channel ........."
to make it easier for reading (the other way is not wrong, but it's distracting to the reader).

Also, you don't need the curly braces surrounding dev --- {dev} --- unless it's embedded in a weird string (mostly when it's followed immediately by an underscore). Since you don't do this for the rest of your variables, it's best to be consistent and leave out the braces here.

I think you have an extra `done' at the end.

ONE LAST THING: Use CODE tags with blocks of code (notice the difference between my code and yours ). Fixed width fonts are easier to read, and tabs don't show up in HTML unless they're escaped (on any standards-compliant browser). Do a preview before submitting.

As you can see, the comments were mostly aesthitic (except the extra `done'), which means your code works fine as far as I can tell.
 
Old 05-30-2006, 03:21 AM   #7
thoyyib
Member
 
Registered: Sep 2005
Distribution: SuSE 10
Posts: 50

Original Poster
Rep: Reputation: 15
bluetooth

Hi Oser,

Sorry for the mistakes and thanks for your great instructions. He is the latest code:

Code:
#!/bin/bash

hcid
sdpd
hciconfig up

while true 
     do
echo "" echo $(date) echo "Searching for Bluetooth devices ........................................................................" echo "" count=0 #$dev is the bluetooth address of the remote device for dev in $(hcitool inq | sed -e '/Inquiring/d' | awk '{print $1}'); do
#$devname is the device name of the remote bluetooth device devname=$(hcitool name $dev) #$file is the system file for binding the remote device file="/dev/rfcomm$count" echo "New device found with bluetooth address: $dev ( $devname )" echo "Channel for the device = $(sdptool search --bdaddr $dev OPUSH | sed -e '/Channel/!d')" #$channel is the channel for the OPUSH service of the device(Eg: NOKIA uses channel 9 for object push, Ericson : 3, PC with MAC OS: 10 etc.... ) channel=$(sdptool search --bdaddr $dev OPUSH | sed -e '/Channel/!d' | awk '{print $2}') echo "$dev is binding with $file using channel $channel ........." #release the device from the system file rfcomm release $file #Bind the channel of the bluetooth device with system file rfcomm bind $file $dev $channel echo "Pushing data to $dev ........" #Pushing a file to the binded device ./ussp-push $file /home/thoyyib/Desktop/iac.txt IAC echo "Job finished for device $dev" echo "" echo "" ((count++)) sleep 1
done
done
Now I need your help to stop the rpeated message send. If the pushing is successful then I need to exclude that device from the operation.
Here is the O/P of one successful transfer:

Code:
New device found with bluetooth address: 00:13:FD:B5:AD:AD ( MAS )
Channel for the device =  Channel: 9
00:13:FD:B5:AD:AD is binding with /dev/rfcomm2 using channel 9 .........
Pushing data to 00:13:FD:B5:AD:AD ........
name=/home/thoyyib/Desktop/iac.txt, size=90
Connection established
Job finished for device 00:13:FD:B5:AD:AD
My Idea is to save the device ID for every successful message transfer.(we get the same from the O/P of ./ussp-push - one line with text - connection established). And check this file before trasmission. Am I right?

I used fopen,fscanf and fprintf like functions in C language for this purpose. Please help me for the same. Also I can use the same file as a log of my application.


Thunks and best regards,
THOYYIB
 
Old 05-30-2006, 03:41 AM   #8
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Just assign the output that says "connection established" to a variable and check it in an if-test.
Backticks are my suggestion for this.

ie
Code:
out=`your_command`
#Add if-test here
where your_command is the command that actually tells you if the connection was established or not.

All you need now, is the if-test that checks if the output indeed contains "Connection established" (or whatever string
you're looking for). I'd suggest making the test a little robust (ie by using regular expression matching).
 
Old 05-30-2006, 08:48 AM   #9
thoyyib
Member
 
Registered: Sep 2005
Distribution: SuSE 10
Posts: 50

Original Poster
Rep: Reputation: 15
file manipulation - shell scripting

Hi Timmeke,

I am very sorry, as i told, i am a newbie in shell scripting, I didnt undestand ur idea.
What I am looking for is, I need to write the status of every transmission in to file. and the coming transmission is according to these file. How can write, read files. Where can I get file manipulation documents?

Thank you,
THOYYIB
 
Old 05-30-2006, 09:15 AM   #10
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
No need to apologize. If you need help, we're here to give it to you.

My point was to check the output of -for instance- your ussp-push command.
This can roughly be done in 2 ways:
-either check it's return code;
-or check the text it prints as output.
The method I described above, using the backticks `` to run a command and capture it's output,
is useful for the 2nd method. I suggested that you capture whatever ussp-push prints (ie the text "Connection established") and
use a simple if-test to find out if the ussp-push worked or not.

For the first method, you can proceed as follows:
* Look for the return code of ussp-push in it's man page:
Code:
 man ussp-push
If that opens up a text document, pressing "q" (without the quotes) will most likely exit from the document (ie return to your command line).
Use your space key to skip ahead one page in the document.
This way, you can look for the return values (sometimes called return codes or exit codes or something like that).
Typically, an exit code of 0 indicates "success" and non-0 means "failure", but this is program specific.

TIP: the "man" command works on most shell commands. Like the "info" command, you can use it to access information on the usage
and functionality of those commands.

If you don't have the "man page" for ussp-push, don't worry. Not all programs have man pages installed with them.

Checking the return code of a program is easy:
Code:
ussp-push ... #your ussp-push command
if (( $? != 0 )); then
   #there probably was an error

else
   #ussp-push worked just fine

fi;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
Shell Scripting again yaadhav Linux - Newbie 8 08-04-2005 10:21 AM
Shell Scripting Help jester_69 Linux - General 15 11-05-2003 03:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration