Hello,
Is there a way to detect if a bluetooth device is connected and/or disconnected.
I found something here (
http://forums.macrumors.com/showthread.php?t=701524) but it is a little different than what I want.
Basically a script that can find out if a specified bluetooth device is connected. Then if it is, it does something. Then it checks every like 10 seconds or so to see if it is connected. Then if it is disconnected, it does something else.
The only issue I see is after it checks, it will run the same thing over and over. So there needs to be a way for it to check but after the first time it detects if a device is connected, it does not run that part over and over untill the device is marked as disconnected.
I know this is a lot so I can re explain but someone let me know if this is possible.
The Information for each device is there and is updated when connected/disconnected.
Now all I need is a script that singles out a specific device, checks if it is connected and then does some things. Thank you for the help
After doing some testing I do not think this is possible because my phone and others do not stay connected to a computer. They only connect for transfers and all. However it would work with a headset type device. But, I am still unaware on how to set it up.
Using the script mentioned above, I could get the info about a bluetooth device. Now all I need is to be able to single one device out, and check its connected status.
You have to "parse" the file within your applescript to glean the necessary info from it. I don't ever use bluetooth but something like this is probably what you are looking for.
on idle () set myString to do shell script "system_profiler SPBluetoothDataTyp" --now you need to finagle the info you want out of it --perhaps something like this if myString contains "deviceName activated" then display dialog "connected" else display dialog "unconnected" end if return 6 --delay before repeated end idle
Obviously you can change the display dialog commands to whatever it is you want to do, but the main part of your script is going to be getting the necessary info out of your shell response and I can't really help you with that without seeing how the data is formatted.
Ok, the if mystring contains part does not work with whatever I try. Also, it does not seem to work at all with the idle part. But if you need what I get out of the system_profiler for Devices, it is this:
Devices (Paired, Favorites, etc):
Device:
Name: LGEnv2
Address: 00-1f-6b-5d-cc-c6
Type: Mobile Phone
Services: Service Discovery Server, OBEX Object Push, OBEX File Transfer, AV Audio Source, AV Remote Control Target, AV Remote Control, Phonebook Access PSE, Voice Gateway, Voice Gateway, BT DIAG, Bluetooth Modem
Paired: No
Favorite: Yes
Connected: No
Device:
Name: W580i
Address: 00-1e-dc-48-27-7e
Type: Mobile Phone
Services:
Paired: No
Favorite: Yes
Connected: No
Device:
Name: SCH-u740
Address: 00-1a-8a-a4-93-04
Type: Mobile Phone
Services: QC Voice Gateway, Dial-up Networking, Phonebook Sync, OPP server, QC Voice Gateway
Paired: No
Favorite: Yes
Connected: No
Device:
Name: SCH-U540
Address: 00-1b-98-70-7e-e1
Type: Mobile Phone
Services: QC Voice Gateway, OPP server, Phonebook Sync, Dial-up Networking, QC Voice Gateway
Paired: No
Favorite: Yes
Connected: No
Device:
Name: MotorolaQ
Address: 00-19-2c-de-9a-dd
Type: Smartphone
Services: Network Access, Network Access, Generic Serial, Dial-up Networking, Audio Gateway, Audio Gateway, OBEX Object Push, OBEX File Transfer
Paired: No
Favorite: Yes
Connected: No
Device:
Name: LGVX9700
Address: 00-21-fb-64-e7-9e
Type: Mobile Phone
Firmware Version: 0000
Services: OBEX File Transfer, Phonebook Access PSE, Object Push, HSP Service, HFP Service, Dial-up Networking, Serial Port
Paired: No
Favorite: Yes
Connected: No
Manufacturer: Broadcom (0x4, 0x420e)
Vendor ID: 0x0f
Product ID: 0000
Device:
Name: Jonathan'sIpod
Address: 00-25-00-25-59-7f
Type: Portable
Firmware Version: 0x300
Services: AVRCP Device, Wireless iAP, Audio Source
Paired: No
Favorite: Yes
Connected: No
Manufacturer: Broadcom (0x4, 0x8107)
Vendor ID: 0x5ac
Product ID: 0x1293
Device:
Name: 3XXPlantronics
Address: 00-19-7f-23-2f-80
Type: Headset
Services: HF-Audio, HS-Audio
Paired: No
Favorite: Yes
Connected: No
Manufacturer: Cambridge Silicon Radio (0x2, 0x8d1)
Device:
Name: LGGR500
Address: 00-22-a9-56-2c-74
Type: Mobile Phone
Firmware Version: 0000
Services: Serial Port 1, HS Voice Gateway, HF Voice Gateway, LG Bluetooth A2DP, Dial-up Networking, OBEX File Transfer, OBEX Object Push, LG Bluetooth AVRCP Target
Paired: No
Favorite: Yes
Connected: No
Manufacturer: Unknown (0x4, 0xf1)
Vendor ID: 0000
Product ID: 0x35
Device:
Name: Archos5
Address: 00-23-d4-50-0b-82
Type: Smartphone
Services: Voice Gateway, Voice Gateway, Audio Source, AVRCP TG
Paired: No
Favorite: Yes
Connected: No
Manufacturer: Unknown (0x4, 0x1d1f)
Device:
Name: SCH-u410
Address: 00-1e-7d-e6-28-bd
Type: Mobile Phone
Services: OBEX Object Push, Phonebook Access PSE, AUDIO Gateway, Serial Port, Voice Gateway, Dial-up Networking
Paired: No
Favorite: Yes
Connected: No
Manufacturer: Unknown (0x3, 0xe1b)
Device:
Name: LGVX9700
Address: 00-1f-e3-ea-96-b1
Type: Mobile Phone
Firmware Version: 0000
Services: OBEX File Transfer, Phonebook Access PSE, Object Push, HSP Service, HFP Service, Dial-up Networking, Serial Port
Paired: No
Favorite: Yes
Connected: No
Manufacturer: Broadcom (0x4, 0x420e)
Vendor ID: 0x0f
Product ID: 0000
Device:
Name: DR-BT101
Address: 00-1f-82-36-38-01
Type: Handsfree
Services: Headset, Hands-Free unit
Paired: Yes
Favorite: Yes
Connected: No
Manufacturer: Cambridge Silicon Radio (0x4, 0x14f3)
Device:
Name: LGVX11000
Address: 00-26-e2-67-9d-7e
Type: Mobile Phone
Firmware Version: 0000
Services: Serial Port, OBEX File Transfer, Phonebook Access PSE, Object Push, BIP Responder, Dialup Networking, HSP Audio Gateway, Advanced audio source, HFP Audio Gateway
Paired: No
Favorite: Yes
Connected: No
Manufacturer: Broadcom (0x4, 0x420e)
Vendor ID: 0x0f
Product ID: 0000
Device:
Name: DROIDX
Address: d0-37-61-46-56-8a
Type: Smartphone
Firmware Version: 0000
Services: AVRCP TG, AVRCP CT, Audio Source, Dialup Networking, OBEX Object Push, MAP MASname, OBEX Phonebook Access Server, Voice Gateway, Voice Gateway, Network Access Point Service
Paired: No
Favorite: Yes
Connected: No
Manufacturer: Unknown (0x4, 0x1d1f)
Vendor ID: 0x0a
Product ID: 0000
Incoming Serial Ports:
Serial Port 1:
Name: Bluetooth-PDA-Sync
RFCOMM Channel: 3
Requires Authentication: Yes
I need headset DR-BT101 to be tested if connected. I think simply if you just display the info for One Device you could say if it contains "name" & "connected" & "yes".
Or something similar to that.
Hope this answers your question.
I've broken up the long string into a list where each item contains the info for one device.
on idle() set myString to do shell script "system_profiler SPBluetoothDataTyp" --initial check if it's not even there if myString does not contain "DR-BT101" then display dialog "DR-BT101 does not exist." else
--find out if connected/disconnected set AppleScript's text item delimiters to "name:" set myList to the text items of myString --each item of mylist is now one of the devices
set numberOfDevices to count of myList set counter to 1 repeat numberOfDevices times --loop through each devices checking for Connected string if item counter of myList contains "DR-BT101" then if item counter of myList contains "Connected: Yes" then display dialog "DR-BT101 Connected" else if item counter of myList contains "Connected: No" then display dialog "DR-BT101 Unconnected" else display dialog "Error Parsing" --this shouldn't happen end if end if set counter to counter + 1 end repeat end if return 60 --delay of 60 seconds before repeating end idle
To use the idle part properly you have to save the script as a stay open application when saving it. Hope this helps and let us know if you have any other questions or how it works out.
Great! Thanks.
I just have two questions:
1. what does the counter do?
2. If I run this on idle with a repeat it does not seem to work. I am just hitting the run button in the editor.
Also, every 60 seconds won't a dialog box pop-up?
The counter is used to go up by one every time the innermost repeat loops. It helps iterate through the different items in the list.
In pseduo code...
set counter to 1
repeat
get item counter of list --first time counter=1, then 2, then 3 etc.
set counter to counter + counter --first time 1+1=2, then 2+1=3 etc.
end repeat
The idle loop will only work if it is saved as a stay open application and run as a stay open application, not from within scripteditor. From within script editor you would have to replace idle(),return and end idle with a giant repeat loop, but this has disadvantages from using idle.
repeat --code here delay 60 end repeat
Finally you are correct that a dialog box will pop-up every sixty seconds in the code as is. However I only put the display dialogs in so that you could replace them with your own code. I don't know exactly what you want to do with it.
I think I get the counter idea.
Ok so for testing in the script editor ill just remove the idle and return and add them later.
Since I do not specifically have anything I will use this example:
If the bluetooth device is connected, open an application.
For example ill just say itunes.
So now if I add (activate application "itunes") to the script, every minute it will either open it, or move it to the front. Is there a way to bypass this.
I would like to be able to ignore the command after the first time it realizes the device is connected and does the action, and not let it do the action again until the device is marked as disconnected. How would I do this?
Ah yes of course. I've changed the script so far to a handler which will check if bluetooth device is connected when called and return true if it is or false if it isn't. Then for the main code I'll use a repeat loop so you can test it in script editor.
repeat set statusOld to checkStatus() set statusNew to statusOld repeat while statusOld is equal to statusNew delay 1 --for 1 second checks set statusNew to checkStatus() end repeat display dialog "enter commands to preform in place of this display dialog" end repeat on checkStatus() --set myString to text returned of (display dialog "" default answer "name: DR-BT101 Connected: ") --to test it without actually plugging/unpluggin bluetooth device comment out the line below and uncomment the one above adding yes or no to simulate set myString to do shell script "system_profiler SPBluetoothDataTyp"
--initial check if it's not even there if myString does not contain "DR-BT101" then return false else
--find out if connected/disconnected set AppleScript's text item delimiters to "name:" set myList to the text items of myString --each item of mylist is now one of the devices
set numberOfDevices to count of myList set counter to 1 repeat numberOfDevices times --loop through each devices checking for Connected string if item counter of myList contains "DR-BT101" then if item counter of myList contains "Connected: Yes" then return true else if item counter of myList contains "Connected: No" then return false else display dialog "Error Parsing" --this shouldn't happen end if end if set counter to counter + 1 end repeat end if end checkStatus
How is that? Note the display dialog in the checkStatus function which you can use to simulate the bluetooth status response.
I am a little confused by this new script. I tried it and it did not work. In the script editor it just would say its running but nothing happens.
What does this do:
repeat
set statusOld to checkStatus()
set statusNew to statusOld
repeat while statusOld is equal to statusNew
delay 1 --for 1 second checks
set statusNew to checkStatus()
end repeat
display dialog "enter commands to preform in place of this display dialog"
end repeat
Shouldn't this be at the end if it for running an application?
I am also confused by what you say here:
--set myString to text returned of (display dialog "" default answer "name: DR-BT101 Connected: ")
--to test it without actually plugging/unpluggin bluetooth device comment out the line below and uncomment the one above adding yes or no to simulate
The new script is more confusing because it uses handlers. Each time you see something like "setNew status to checkStatus()" The entire second half of the script starting from "on checkStatus()" runs.
The script actually only extends from the very begining (repeat) down to the corresponding (end repeat). Everything typed after it just gets called and run from within the script.
"Shouldn't this be at the end if it is for runing an application?"
-it actually is at the end 😀
on main --this is the beginning of the code handler1() handler1() handler2() handler2() end main --this is the end of the code handler1() display dialog "hello" end handler1 handler2() display dialog "goodbye" end handler2
Above is a more simple example of handlers. Their function is so that you don't have to keep on writing the same code out over and over.
As for the testing part you could get rid of it if you like. It is merely for testing purposes where you set myString to your own little string instead of running a shell script to simulate what you would get back from a shell.
set myString to text returned of (display dialog "" default answer "name: DR-BT101 Connected: ") --to test it without actually plugging/unpluggin bluetooth device comment out the line below and uncomment the one above adding yes or no to simulate --set myString to do shell script "system_profiler SPBluetoothDataTyp"
change it to this instead and you can type in yes or no to make it seem like the device is being connected and disconnected to make sure the script works.
Hope that is clear?
edit: also you say it doesn't work, but the only thing it is ever going to do is display a dialog if you connected or disconnect the specfic devices. Otherwise appearing to do nothing is it working 😀
Thank you for clarifying. I now understand how it works.
As for the second part, I used the
set myString to text returned of (display dialog "" default answer "name: DR-BT101 Connected: ")
and it did pop up in a dialog box.
However, Wether I enter yes or no, nothing happens. And if i leave it blank, it says "enter commands to preform in place of this display dialog".
I feel as if I am missing a step or something.
Edit: It actually works, i realized i was spelling it yes instead of Yes so it was not recognizing it.
However, when it says yes it just basically loops and keeps checking until it is a no.
It needs to be reverse. I think if I just reverse the true and false it will change this.
The only thing I do not see is how it will stop re-telling an action to happen.
If you keep saying Yes then it will continue to loop until you say No. This is apparently the opposite of the behaviour you want...but it also works if you keep saying No and then say Yes! The script only looks for
changes in the response (Yes,Yes,Yes,No,TRIGGER) or (No,No,No,Yes,TRIGGER). So you really shouldn't have to reverse anything as it goes both ways 😉
As for not retelling an action to happen. Its only going to tell an action to happen when it changes states. Not every time it checks the state.
Try this within the testing mode (commented out shell script and myString=returned dialog)
name: DR-BT101 Connected: No name: DR-BT101 Connected: No name: DR-BT101 Connected: No name: DR-BT101 Connected: Yes "enter commands to preform in place of this display dialog" should show up, hit ok to continue name: DR-BT101 Connected: Yes name: DR-BT101 Connected: Yes name: DR-BT101 Connected: No "enter commands to preform in place of this display dialog" should show up, hit cancel to exit
The above illustrates how the devices is off (nonono) and then turns on (yes) which allows you to run some code. It then stays on (yesyes) and then turns off (no) which allows you to run some code.
Good?
edit: remember that when you are independently entering in the strings ending in yes or no this is just for testing. Remember how before you were complaining that it didn't do anything, that is exactly right and it won't do anything unless that device is added or removed.