This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

iPhone presence detection
#1
Hi

I am looking for an iPhone presence detection solution.

I found many iPhone detection bash scripts like this one

Code:
script (/opt/openhab/iphonedetect.sh)
#!/bin/bash
MAC=${2//-/:}
declare -a DEVICES
hping3 -2 -c 10 -p 5353 -i u1 $1 -q >/dev/null 2>&1
DEVICES=arp -an | awk '{print $4}'
CHECK=$MAC
if [[ ${DEVICES[*]} =~ $CHECK ]]
then
echo "ON"
else
echo "OFF"
fi
[*]


But nothing in lua.

Did anyone over here already implemented a script and wants to share this?

Thanks
Nicky
Reply
#2
Here is Bluetooth device presence script which changes value of respective grp addresses based on Bluetooth device appearance.

Code:
require('ble')

-- enable ble
ble.up()

list = {}

-- mac -> group address mapping
known = {
 ['88:0F:10:10:F2:8A'] = '1/1/1',
 ['88:0F:10:4A:2C:36'] = '1/1/2',
}

ctime = os.time()

function callback(addr)
 local now, delta

 now = os.time()
 delta = now - ctime

 if addr then
   if not list[ addr ] then
     log('BLE found ' .. addr)

     if known[ addr ] then
       grp.write(known[ addr ], true, dt.bool)
     end
   end

   list[ addr ] = now
 end

 -- check for missing devices each 5 seconds
 if delta < 0 or delta > 5 then
   for addr, time in pairs(list) do
     delta = now - time

     if delta < 0 or delta > 60 then
       log('BLE missing ' .. addr)
       list[ addr ] = nil

       if known[ addr ] then
         grp.write(known[ addr ], false, dt.bool)
       end
     end
   end

   ctime = now
 end
end

ble.scan(callback)
Reply
#3
That shell solution assumes that your iOS device has a fixed IP, you can do in Lua like this. I cannot guarantee that this code will work, you have to try and debug it yourself Smile

Code:
function checkiosdev(ip)
  local sock, res

  sock = require('socket').udp()
  for i = 1, 10 do
    sock:sendto('123', ip, 5353)
    os.sleep(1)
  end

  res = os.execute('ping -c 2 -W 10 ' .. ip)
  return res == 0
end

seen = checkiosdev('192.168.1.10')
grp.update('1/1/1', seen)

When phone/tablet is locked (in sleep state) sending UDP packets to port 5353 causes it to wake for a short period. During that period device can be pinged to check if it's in the network or not.
Reply
#4
Perfect, that is the solution I was looking for.

Thanks a lot
Reply
#5
superb! created a user library with the function and a resident for the few IPs I need to check.
Reply
#6
Hi Admin,


I tested this script, created a log on the output and i see that there is a lot of toggle (on/off) even when the phone is all the time in the network.

Could it be that the wake-up methode by udp is not stable? You send it 10 times now for a special reason?

BR,

Erwin
Reply
#7
Hi,

i found a useful app, https://packetsender.com/download, used this to send to port 5353. Then I had used a command prompt with ping -t to my iphone. I noticed that if you try to send multiple times the iphone would not respond to the ping.

Thanks,

Roger
Reply
#8
Well, I don't have a device to test this script so any corrections are welcome. Another approach is to get DHCP client list from a router, then you don't have to set static IP and there's no need for UDP quirks Smile
Reply
#9
Hi,

I tried the Bluetooth device presence script but I have some problems.
  • I can only find BLE devices (watches and bands). I'd like to use it to detect smartphones (Android/Iphone).
  • The grp.write function doesn't work. I'm having trouble debugging this. I tried putting log('debug') in many places and never get results...

Thank you in advance,
André Neves
Reply
#10
Hello,

I´ve tried this script but it doesn´t work if iphone is locked. When I unlock if work perfectly.
With android devices it works perfectly.

Any Idea?

Thanks

Code:
function checkiosdev(ip)
 local sock, res

 sock = require('socket').udp()
 for i = 1, 10 do
   sock:sendto('123', ip, 5353)
   os.sleep(1)
 end

 res = os.execute('ping -c 2 -W 10 ' .. ip)
 return res == 0
end

seen = checkiosdev('192.168.1.10')
grp.update('1/1/1', seen)
Reply
#11
Hi,

When iphone is locked the system turns off wifi. I think each 5min the system turns on wifi to search new inputs and then turns off again.

I was trying this. You can try it doing an infinite ping with cmd and lock/unlock the iphone. You will see!
Reply
#12
There's no 100% correct way of doing this, the best approach is to get DHCP lease list from router and check if certain MAC is present or not.
Reply
#13
Here is example with Microtik router
https://forum.logicmachine.net/showthrea...57#pid7657
------------------------------
Ctrl+F5
Reply
#14
I´ve seen microtic example but I don´t know how to do that with my livebox fibra...
Reply
#15
(26.08.2016, 06:14)edgars Wrote: Here is Bluetooth device presence script which changes value of respective grp addresses based on Bluetooth device appearance.

Code:
require('ble')

-- enable ble
ble.up()

list = {}

-- mac -> group address mapping
known = {
 ['88:0F:10:10:F2:8A'] = '1/1/1',
 ['88:0F:10:4A:2C:36'] = '1/1/2',
}

ctime = os.time()

function callback(addr)
 local now, delta

 now = os.time()
 delta = now - ctime

 if addr then
   if not list[ addr ] then
     log('BLE found ' .. addr)

     if known[ addr ] then
       grp.write(known[ addr ], true, dt.bool)
     end
   end

   list[ addr ] = now
 end

 -- check for missing devices each 5 seconds
 if delta < 0 or delta > 5 then
   for addr, time in pairs(list) do
     delta = now - time

     if delta < 0 or delta > 60 then
       log('BLE missing ' .. addr)
       list[ addr ] = nil

       if known[ addr ] then
         grp.write(known[ addr ], false, dt.bool)
       end
     end
   end

   ctime = now
 end
end

ble.scan(callback)



Where should i put that code? In Common Functions or in Resident script?
This code is executed once or like resident script?
Reply
#16
Hi
If you have BLE then just use general profile for presence. LM will see only broadcast messages but not every mobile is sending them.
If it does then you will see your device on the list. Works well with all the wearables.
BR
------------------------------
Ctrl+F5
Reply
#17
I have a LM5 Power with Trust 18187 and I´ve done this tutorial:
http://openrb.com/lm-as-bluetooth-4-0-gateway/.

I've imported General Profile but Ble tab only discover 1 device. A TV but nothing with iphone ore android phones..
Reply
#18
This is because smartphones usually do not send anything only want to receive. On android I found an app which can generate beacon and then it worked on LM. Probably you can find something like that for iPhone.
------------------------------
Ctrl+F5
Reply
#19
(07.08.2018, 14:37)Daniel. Wrote: This is because smartphones usually do not send anything only want to receive. On android I found an app which can generate beacon and then it worked on LM. Probably you can find something like that for iPhone.

what is the application for android?
Reply
#20
Beacon Simulator or something like that. I just tried it and uninstalled afterwords.
------------------------------
Ctrl+F5
Reply


Forum Jump: