如何查找某个蓝牙设备是否已连接? [英] How to find whether a certain bluetooth device is connected?

查看:93
本文介绍了如何查找某个蓝牙设备是否已连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 applescript 进行定期(每秒)检查以查看是否连接了特定的蓝牙设备,如果已连接,则显示快速通知.为了构图,我想在 Airpods 连接时弹出一个窗口,因为有时当我将它们拉出时,连接到我的电脑,有时连接到我的 iPhone.

I want to use applescript to do a periodic (every second) check to see if a specific bluetooth devices is connected, and if so, to flash up a quick notification. To frame it, I want a popup when my Airpods connect, since sometimes when I pull them out, the connect to my computer, and sometimes to my iPhone.

我已经弄清楚了,除了蓝牙检查部分.我已将此用作起点,但无法使其正常工作.任何帮助将不胜感激.

I've got everything figured out, except for the bluetooth check part. I've used this as a starting point, but can't get it to work. Any help would be appreciated.

repeat
set statusOld to checkStatus()
set statusNew to checkStatus()
repeat while statusOld is equal to statusNew
    delay 1 --for 1 second checks
    set statusNew to checkStatus()
end repeat
if statusNew is true then
    display dialog "Device Added - put some real code here"
else
    display dialog "Device Removed - put some real code here"
end if
end repeat

on checkStatus()

(*Delete the 2 lines below when done testing*)
--set myString to button returned of (display dialog "Connected?" buttons {"Yes", "No"})
--set myString to "name: DR-BT101 Connected: " & myString

(*uncomment line below when done testing*)
set myString to do shell script "system_profiler SPBluetoothDataTyp"

--initial check if it's not even there
if myString does not contain "Christian’s AirPods" 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 "Christian’s AirPods" 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

推荐答案

您缺少 e:

set myString to do shell script "system_profiler SPBluetoothDataType"
                                                                   ^

这篇关于如何查找某个蓝牙设备是否已连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆