[G1 Android Development] juanmasg:
Ok, I didn't like to open a new thread about OBEX and android, but, as I explain in this post http://xdaforums.com/showpost.php?p=3932525&postcount=57 , I got OBEX working in my G1, and now I'm thinking about integrating a clean and elegant OBEX support into our ROMS.

Yes, there is already an application in the market for receiving and sending files via OBEX, but I simply don't like to pay for a feature that should be supported by default in every mobile phone.

I was wondering how would be the first approach for integrating the obexserver by default, and it would be really easy executing at boot time something like:

sdptool add --channel=4 OPUSH
test -d /sdcard/bluetooth_received || mkdir /sdcard/bluetooth_received
cd /sdcard/bluetooth_received && ( while true; do obexserver; done ) &

The only problem, is that disabling bluetooth and enabling it again, it loses the OPUSH service added by sdptool.

So, I'm now trying to find a way to tell the bluetooth daemons that they should add the OPUSH service when they are enabled, and here is where I need your help. Anybody knows how can this be done?

Also, another way would be simply executing a GScript when the OPUSH service is needed.

What do you think?

Thanks!

EDIT: Here is a simple script that enables the OBEX profile and receives 1 file. It is necessary to re-execute it to receive more files.

obex_get.sh:

#!/system/bin/sh

obex_status=`sdptool browse local 2>&1| egrep "^Service Name: OBEX Object Push|^Failed to connect"`

case "$obex_status" in
Failed*)
# bt disabled
echo "Please, enable bluetooth first!"
exit
;;
"")
# bt enabled, but no OPUSH profile
sdptool add --channel=4 OPUSH
;;
*)

# bt enabled, opush enabled
;;
esac

test -d /sdcard/bluetooth_downloads || mkdir /sdcard/bluetooth_downloads

cd /sdcard/bluetooth_downloads && obexserver

EDIT: Daemonized version of the script. It can be run as service at boot time, and every 3 seconds it will check if the opush profile has been lost.

#!/system/bin/sh

# create the download directory if it doesn't exist
test -d /sdcard/bluetooth_downloads || mkdir /sdcard/bluetooth_downloads

while sleep 3; do

# get bluetooth status
hcid_status_now="`getprop init.svc.hcid`"

# do nothing if bluetooth is stopped
test "$hcid_status_now" == "stopped" && continue

# if bluetooth is enabled, get opush profile status
obex_status=`sdptool browse local 2>&1| egrep "^Service Name: OBEX Object Push|^Failed to connect"`

case "$obex_status" in
"")
# bt enabled, but no OPUSH profile
sdptool add --channel=4 OPUSH; echo
;;
Failed*)
# bt disabled
#echo "Please, enable bluetooth first!"; echo
continue
;;
*)
# bt enabled, opush enabled
;;
esac

# if obexserver isn't already running, execute it.
pidof obexserver >/dev/null || ( cd /sdcard/bluetooth_downloads && obexserver ) &
done

EDIT: Ok, a definitely better version of the daemon:

#!/system/bin/sh

# create the download directory if it doesn't exist
test -d /sdcard/bluetooth_downloads || mkdir /sdcard/bluetooth_downloads
# in case /sdcard/bluetooth_downloads already exists, and is not a directory. Exit
test -d /sdcard/bluetooth_downloads || exit

# execute the obexserver loop (for it to be multifile)
( cd /sdcard/bluetooth_downloads && while true; do obexserver; done ) &

# execute the sdptool add OPUSH loop
while sleep 3; do

# get bluetooth status
hcid_status_now="`getprop init.svc.hcid`"

# do nothing if bluetooth is stopped
test "$hcid_status_now" == "stopped" && continue

# if bluetooth is enabled and no obex profile exists, add it
sdptool browse local 2>&1| egrep "^Service Name: OBEX Object Push" >/dev/null || sdptool add --channel=4 OPUSH

done

Still don't like to query the "sdptool browse local" every 3 seconds :(

Bugs and corrections are welcome.