Running a App if a phone is found in Bluetooth

Hey, lunixninja here.
I want to know how to do something with Applescript and bluetooth. Im still just a student so, it would be great if someone could help me.
If Applescript finders a bluetooth phone/device with the name “12345” , It will run a program

Heres the script to turn on Bluetooth if needed:




-- Check the current bluetooth status and turn it on if necessary.
tell application "System Events" to tell the front menu bar of process "SystemUIServer"
	set menu_extras to value of attribute "AXDescription" of menu bar items
	repeat with x from 1 to the length of menu_extras
		if item x of menu_extras is "bluetooth menu extra" then exit repeat
	end repeat
	tell menu bar item x
		click
		tell 2nd menu item of front menu
			if name ends with "Off" then
				-- Current status is on; it says "turn bluetooth off"
				set last_bluetooth_setting to "on"
			else if name ends with "On" then
				-- Current status is off; it says "turn bluetooth on"
				set last_bluetooth_setting to "off"
				click
			end if
		end tell
		-- Unclick if it was on (do nothing but make the menu disappear)
		if last_bluetooth_setting is "on" then
			click
		end if
	end tell
end tell



Thanks very much
lunixninja

Hi,

as far as I know this is not possible with AppleScript,
because there is no way to detect in the background if a device is connected

Surely you could use maybe like iSync to do what I want to do,
like this:

  1. Turn on Bluetooth if necessary.
  2. Run iSync.
  3. Wait for iSync to finish, then quit it.
  4. Turn Bluetooth off if it was initially off.
-- Before you use this script, launch /Applications/AppleScript/AppleScript Utility.app and check "Enable GUI Scripting."
-- The Bluetooth menu item must be turned on in the Bluetooth system preferences pane.

-- Check the current bluetooth status and turn it on if necessary.
tell application "System Events" to tell the front menu bar of process "SystemUIServer"
	set menu_extras to value of attribute "AXDescription" of menu bar items
	repeat with x from 1 to the length of menu_extras
		if item x of menu_extras is "bluetooth menu extra" then exit repeat
	end repeat
	tell menu bar item x
		click
		tell 2nd menu item of front menu
			if name ends with "Off" then
				-- Current status is on; it says "turn bluetooth off"
				set last_bluetooth_setting to "on"
			else if name ends with "On" then
				-- Current status is off; it says "turn bluetooth on"
				set last_bluetooth_setting to "off"
				click
			end if
		end tell
		-- Unclick if it was on (do nothing but make the menu disappear)
		if last_bluetooth_setting is "on" then
			click
		end if
	end tell
end tell

-- Do the sync and wait for it to finish
tell application "iSync"
	synchronize
	repeat while (syncing is true)
		delay 5
	end repeat
	set syncStatus to sync status
	
	if syncStatus = 2 then
		-- Success
		quit
	else
		if syncStatus = 3 then
			set syncStatus to "completed with warnings"
		else if syncStatus = 4 then
			set syncStatus to "completed with errors"
		else if syncStatus = 5 then
			set syncStatus to "last sync cancelled"
		else if syncStatus = 6 then
			set syncStatus to "last sync failed to complete"
		else if syncStatus = 7 then
			set syncStatus to "never synced"
		end if
		display dialog "syncStatus: " & syncStatus
		syncStatus
	end if
end tell


-- Set the bluetooth status to what it was before.
tell application "System Events" to tell the front menu bar of process "SystemUIServer"
	set menu_extras to value of attribute "AXDescription" of menu bar items
	repeat with x from 1 to the length of menu_extras
		if item x of menu_extras is "bluetooth menu extra" then exit repeat
	end repeat
	tell menu bar item x
		click
		if last_bluetooth_setting is "off" then
			-- Turn it off again
			click 2nd menu item of front menu
		else
			-- Just close the menu
			click
		end if
	end tell
end tell

Update: here is a way to detect if a Bluetooth device is connected

BTdevice_connected("K750i")

on BTdevice_connected(theDevice)
	try
		set theData to do shell script "system_profiler SPBluetoothDataType"
		set {TID, text item delimiters} to {text item delimiters, theDevice}
		set theData to text item 3 of theData
		set text item delimiters to "Connected: "
		set theData to word 1 of text item 2 of theData
		set text item delimiters to TID
		return theData is "Yes"
	on error
		return false
	end try
end BTdevice_connected

AFAIK,

when you connect with a BT device, it gets added to a favourites list.
Which is always displayed in the result window. Even if the device is not found…

maybe you should look at a app with bluetooth proximity detection.

Like Salling clicker or blue phone elite, which can run AS on detection.

Proximity

See this thread: http://bbs.applescript.net/viewtopic.php?id=21897