Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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).
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.......
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.
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.
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).
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?
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;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.