first i'm sorry if my english is not very good because i'm french.
I'm a beginner on bluetooth development with pyBluez (i already know TCP and UDP sockets) and i have a little problem with the server.
when i run the following code, my computer seems to be in a listening state but when my phone(nokia xpress music 5800) try to detect it, it doesn't see anything!
and there is two surprising things for me:
1)there is no fonction in my code configuring a name like "my computer". maybe my phone find my address but,because it can't resolve the name by lookup_name()-like function, it considers my computers isn't on?
2)when i replace serversock.bind(("",PORT_ANY)) by serversock.bind(("00:12:34:56:78:9A",PORT_ANY)) where 00:12:34:56:78:9A is the address given by hciconfig tool for my bluetooth device, i'm getting :
_bluetooth.error: (98, 'Address already in use'
just a stupid question because i think the answer is yes bacause of channels:
is it possible to have multiple program listening on the SAME bluetooth interfaces.
i run the program on root
from bluetooth import *
serversock=BluetoothSocket(RFCOMM)
serversock.bind(("",PORT_ANY))
serversock.listen(1)
advertise_service(serversock,"SampleServer",service_classes = [ SERIAL_PORT_CLASS ],profiles = [ SERIAL_PORT_PROFILE ] )
clientsock,clientinfo = serversock.accept()
print "Accepted connection from " , clientinfo
clientsock.send("PyBluez server says Hello!! ")
data = clientsock.recv(1024)
print "received: " , data
clientsock.close()
serversock.close()
thank you
Check the server's visibility with hciconfig:
# hciconfig
hci0: Type: USB
BD Address: 00:11:22:12:34:56 ACL MTU: 192:8 SCO MTU: 64:8
UP RUNNING PSCAN ISCAN
RX bytes:296 acl:0 sco:0 events:26 errors:0
TX bytes:536 acl:0 sco:0 commands:22 errors:0
In case your local device is invisible, set visibility on with:
hciconfig hci0 piscan
Turn off visibility if needed with:
hciconfig hci0 noscan
Related
I am running python 3.7 on macOS
I am trying to have my code scan my wifi network to see when my phone joins the specified wifi network, i provide the routers ip address ("ip") and the phones assigned ip address when it connects to that specified wifi ("device")
When i run my code i get no errors but i also get no output
I have tried using "subprocess.Popen(["say, 'Iphone just connected to the network'"]) to speak but that did not work so i tried echo and say, which also did not work.
At this point i'm not sure what part of my code is the part that is not working, this is my first try at automation so any help would be greatly appreciated.
Thank you
import sys
import subprocess
import os
from decouple import config
ip = "192.168.*.***"
device = "192.168.*.***"
proc = subprocess.Popen(["ping", ip],stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
connected_ip = line.decode('utf-8').split()[3]
if connected_ip == device:
os.system("echo 'Iphone just connected to the network'")
os.system("say 'Iphone just connected to the network'")
'''subprocess.Popen(["say, 'Iphone just connected to the network'"])'''
I am attempting to communicate with a BraggMETER Interrogator that supports SCPI.
OS: Windows 10
Connection hardware: j5create JUH470 USB 3.0 Multi-Adapter Gigabit Ethernet / 3-Port USB 3.0 HUB
Part of my confusion: Should I access as a USB device or as a TCPIP device?
When I connect via Telnet, all goes well. The IP Address and Port are 10.0.0.10 and 3500.
> telnet
> open 10.0.0.10 3500
:IDEN?
:ACK:HBM FiberSensing:FS22SI v3.0:08:046 840 200 898:20190116
:STAT?
:ACK:1
In Python, I am usig the pyvisa library.
import easy_scpi as scpi
import pyvisa
DeviceAddress = '10.0.0.10'
DevicePort = '3500'
VISADevice = f'TCPIP0::{DeviceAddress}::{DevicePort}::SOCKET'
# Doesn't work either --> VISADevice = 'ASRL10::INSTR'
rm = pyvisa.ResourceManager()
print(rm.list_resources())
inst = rm.open_resource(VISADevice)
print(inst.query("*IDEN?"))
inst.close()
The error is always on rm.open_resource. I have tried numerous connection strings. They give different errors. Here are three of them:
pyvisa.errors.VisaIOError: VI_ERROR_INTF_NUM_NCONFIG (-1073807195): The interface type is valid but the specified interface number is not configured.
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
pyvisa.errors.VisaIOError: VI_ERROR_RSRC_NFOUND (-1073807343): Insufficient location information or the requested device or resource is not present in the system.
Update 1
I downloaded National Instruments NI-Max and used their NI I/O trace. This connection string "works":
TCPIP::10.0.0.10::3500::SOCKET
However, I still get the timeout error. Tried ensuring that the newline termination character is sent and upped the timeout to 5 seconds (which did take effect, as it delayed the logging of the timeout error). No dice. Still gives a timeout error.
Update 2
While not the same setup, someone else reports a problem who is using an NI GPIB-to-USB card (GPIB-USB-HS). The common thread is a USB adapter...
https://community.keysight.com/thread/37567
I'm not able to comment so I'm commenting here
Have you tried using a normal socket?
import socket
DeviceAddress = '10.0.0.10'
DevicePort = '3500'
BUFSIZ = 1024
ADDR = (DeviceAddress, DevicePort)
cmd = "IDN?" # or "*IDEN?" as you've put?
braggMeterSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
braggMeterSocket.connect(self.ADDR)
braggMeterSocket.send(cmd + "\n") #maybe with new line depending on what the device terminator is.
mesg = braggMeterSocket.recv(BUFSIZ)
mesg = mesg.strip() # Remove \n at end of mesg
print(mesg)
The problem was that the device exects CRLF (carriage return plus linefeed) as an SCPI command terminator. I was only sending one of those two characters, "\n".
Python I/O does not adapt to the OS like some languages I have used, which will interpret "\n" as "\r\n" in some situations.
Likewise, NI-Max only sent the "\n" and omitted the "\r".
I have a problem. So, i connected 3 huawei e303 modems to my computer. I can see their interfaces and IPs that were given them by system. Here is an example
`modems = []
modem_temp = modem_sys()
for temp in interfaces():
addr = ifaddresses(temp)
if addr.get(AF_INET, None):
if '58:2c:80' in addr[AF_INET][0]['addr']: # 58:2c:80 is huawei
modem_temp.set_ip_adress(addr[AF_INET][0]['addr'])
print(modem_temp.ip_adress)
modem_temp.set_modem_interface(temp)
print(modem_temp.modem_interface)
modems.append(modem_temp)`
Where modem_sys is my class that contains only info about modem.
Output is:
169.254.169.233 {E549A313-E2CF-489A-A08B-A263CA7CBDB0}
192.168.1.100 {F3477F61-FC2D-4752-9FD9-0CF76529EA60}
169.254.170.23 {6F59AD83-A773-4D24-BF01-24FE7D9B6723}
But I can use to execute xml commands only 1 modem, because i can do them only on 192.168.1.1 address. So how can simultaniously use all modems to execute xml-commands? For example, after detecting modems, print device info by response = requests.get('http://192.168.1.1/api/device/information'). I'm using python3. I will be very appriciate for any help.
Im trying to make a algorithm in python to detect if my phone is in the area. Im using this to find my device:
bluetooth.discover_devices()
But it only detects my phone if I set my Bluetooth on my phone to "visible".
Is there a function or command to detect my phone when it's set to hidden?
Im fairly new to python so any form of help is very welcome!
Thanks in advance!
You could attempt to connect to your phone. If it's nearby, the connection will succeed. Devices can be connectable when they are not discoverable. You would have to already know the device address of your phone (via discovery when your phone was visible) in order to initiate the connection.
Not sure if a solution is still needed (I think this is right, just haven't been succesful with it). A book entitled "Violent Python" gives a solution for this in chapter 5, but I haven't been successful in implementing it. Supposedly you just need to increment the MAC address of the devices wifi adapter by one to calculate the Bluetooth MAC.
def retBtAddr(addr):
btAddr=str(hex(int(addr.replace(':', ''), 16) + 1))[2:]
btAddr=btAddr[0:2]+":"+btAddr[2:4]+":"+btAddr[4:6]+":"+\
btAddr[6:8]+":"+btAddr[8:10]+":"+btAddr[10:12]
return btAddr
and then something like the following (where OUI is the first 24 bytes of BT MAC)
def checkBluetooth(btAddr):
btName = lookup_name(btAddr)
if btName:
print '[+] Detected Bluetooth Device: ' + btName
else:
print '[-] Failed to Detect Bluetooth Device.'
def wifiPrint(pkt):
iPhone_OUI = 'd0:23:db'
if pkt.haslayer(Dot11):
wifiMAC = pkt.getlayer(Dot11).addr2
if wifiMAC != None and iPhone_OUI == wifiMAC[:8]:
print '[*] Detected iPhone MAC: ' + wifiMAC
btAddr = retBtAddr(wifiMAC)
print '[+] Testing Bluetooth MAC: ' + btAddr
checkBluetooth(btAddr)
I have a Bluetooth client on Android that must connect to my computer by RFCOMM and using the UUID 00001101-0000-1000-8000-00805F9B34FB
The PyBluez library is used, as well as Python 2.6. I used the following code from the PyBluez Documentation
from bluetooth import *
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "00001101-0000-1000-8000-00805F9B34FB"
advertise_service( server_sock, "SampleServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
)
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
try:
while True:
data = client_sock.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
except IOError:
pass
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
It works perfectly fine under Windows 7.
But I have no reaction with another computer on Windows XP, i.e. my code stays at the line "Waiting for connection on RFCOMM channel". My Bluetooth client warns me it is connected, though.
This Windows XP computer Bluetooth configuration allows me to set-up a COM port for serial communications with Bluetooth. If I listen on this configured COM port, I can see my data from the Bluetooth client.
I prefer to have a code working on any Windows computer, and I do not want to configure virtual COM port on these computers. So I would like the above program also working with the Windows XP computer.
What do I miss? It is as if the Windows XP computer do not forward Bluetooth data to my program, even if I disable its capability of reproducing data on a virtual serial port.
It seems the UUID I use is a well known one, but I must use this one to have my program compatible with another specific platform.
The Bluetooth stack on the Windows 7 computer is from Atheros Communications, the one on the Windows XP computer is from Widcomm.
The Widcomm Bluetooth stack installed on your XP has its own API and driver; this API is totally incompatible with the MS Bluetooth stack! (furthermore, you need XP SP2 to use MS Stack, with a compatible dongle too... I've never found one)
So, on Win7, you can use 'sockets' with the MS Stack (even a Broadcom/Widcomm will work, using the MS stack and not the specific API!)
On XP, you need to use another code, compatible with the Widcomm stack...
You can eventually use COMports on XP, connected to the virtual COMs...but you'll get no notification from the stack...