Jump to content
Collins

How can i detect my iPhone anytime i connect it

Recommended Posts

Collins

From search and help on here. I am able to detect my iPhone when its plugged in and i run my script..

I want to know how i can detect my iPhone anytime i remove and insert it back without having to run the script again.

 

Below is the code in detecting 

#include <MsgBoxConstants.au3>

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20

$WMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
$aItems = $WMI.ExecQuery("SELECT * FROM Win32_PnPEntity", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)


    If IsObj($aItems) Then
        For $item In $aItems
            If StringInStr($item.Description, "iPhone") Then
                Msgbox($MB_SYSTEMMODAL,"device found","Your device is found" & $item.Description )
                ;Do Stuff
            EndIf
        Next
    Else
        Msgbox($MB_SYSTEMMODAL,"WMI Output","No WMI Objects Found for class: " & "Win32_PnPEntity" )
    Endif

 

Share this post


Link to post
Share on other sites
AutoBert
Posted (edited)

you can use a loop for solving your problem: Do ... Until

Edited by AutoBert

Share this post


Link to post
Share on other sites
Collins
Posted (edited)

Thanks for the help. I have been able to do it. But i want the message box to run once anytime the device is connected .. 

The message box keeps showing until i disconnect the phone...

 

 

Below is the script

#include <MsgBoxConstants.au3>

Do
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20

$WMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
$aItems = $WMI.ExecQuery("SELECT * FROM Win32_PnPEntity", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)



    If IsObj($aItems) Then
        For $item In $aItems
            If StringInStr($item.Description, "iPhone") Then
                Msgbox($MB_SYSTEMMODAL,"device found","Your device is found " & $item.Description )


            EndIf
        Next
    Else
        Msgbox($MB_SYSTEMMODAL,"WMI Output","No WMI Objects Found for class: " & "Win32_PnPEntity" )
    Endif

    Until False

 

Edited by Collins
problem solved

Share this post


Link to post
Share on other sites
Bilgus
Posted (edited)

either set a flag or exit the loop..

#include <MsgBoxConstants.au3>
Global Enum $eDeviceUnplugged = 0, $eDevicePlugged, $eDeviceConnected
Global $iDeviceState = $eDeviceUnplugged

Do
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20

$WMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
$aItems = $WMI.ExecQuery("SELECT * FROM Win32_PnPEntity", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)



    If IsObj($aItems) Then
        $iDeviceState = BitAnd($iDeviceState, BitNot($eDevicePlugged)) ;Remove Plugged flag but leave Connected flag if it exists
        For $item In $aItems
            If StringInStr($item.Description, "iPhone") Then
                
                if Not BitAnd($iDeviceState, $eDeviceConnected) Then
                    Msgbox($MB_SYSTEMMODAL,"device found","Your device is found " & $item.Description )
                    $iDeviceState = BitOr($eDeviceConnected, $eDevicePlugged) ;Keep message from appearing again till device unplugged + re-plugged
                Else
                    $iDeviceState = BitOr($iDeviceState, $eDevicePlugged) ;Message has already been shown and device is still connected
                Endif
                ExitLoop ;We are done here no need to check anything else
            EndIf
        Next
        if Not BitAnd($iDeviceState, $eDeviceConnected) Then $iDeviceState = $eDeviceUnplugged ;Device not found reset to unplugged state 
    Else
        Msgbox($MB_SYSTEMMODAL,"WMI Output","No WMI Objects Found for class: " & "Win32_PnPEntity" )
    Endif

Until False

 

Edited by Bilgus

Share this post


Link to post
Share on other sites
Collins

Thank you for your help.. I am grateful. I am totally new to autoit..

I am having another problem here. when i try to run , it says

 

 

 ==> Incorrect number of parameters in function call.:
$iDeviceState = BitNot($iDeviceState, $eDevicePlugged)
$iDeviceState = ^ ERROR
>Exit code: 1    Time: 0.5109

Share this post


Link to post
Share on other sites
Bilgus

sorry that should be $iDeviceState = BitAnd($iDeviceState, BitNot($eDevicePlugged))

Share this post


Link to post
Share on other sites
Collins
On 5/31/2018 at 9:32 PM, Bilgus said:

sorry that should be $iDeviceState = BitAnd($iDeviceState, BitNot($eDevicePlugged))

Thank you

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×