I need to be able to receive broadcasts on the Wifi of this phone. It's an HTC Desire, about 2 years old, running Android 2.2.
I've followed the docs and some example code I found, and ended up with this code to enable the multicast:
mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = mWifiManager.createMulticastLock("net.inside.broadcast");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
Log.i("BCXXX", "multicastLock.isHeld() = " + multicastLock.isHeld());
Log.i("BCXXX", "multicastLock.toString() = " + multicastLock.toString());
The log output follows here:
I/BCXXX ( 3302): multicastLock.isHeld() = true
I/BCXXX ( 3302): multicastLock.toString() = MulticastLock{ 45fa2b88; held; refcounted: refcount = 1 }
This seems to confirm that the multicastLock should be held, meaning multicast should work. However, it doesn't. The Android app simply does not receive any broadcast packets except for its own, i.e. the one it sends out to the others. The other machines I should mention can see the Android phone just fine and receive its broadcast messages. I've tried addressing the packets to 255.255.255.255 as well as 192.168.0.255 (with 192.168.0. obviously being the net everyone is connected to in the LAN).
This is a project built with Unity 3.5 and both the Windows and OS X builds work as expected. The Android plugin does get called (as is evident from the log entries).
Any ideas anyone?
Related
Situation:
VS 2022, 17.0.4
Maui App,
net6.0-android
AndroidManifest.xml contains also:
android.permission.INTERNET
android.permission.CHANGE_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
Connected mobile Phone:
Samsung SM-G960F (Android 10.0 - API 29)
OS: Windows 11, latest patch.
All firewalls are down (for testing purpose only!)
While debugging the develop computer is only connected to a Wifi network; computers ethernet card is disabled.
Mobile phone is connected to this dev computer via USB cable (to be able to debug) and to the same Wifi network as the computer.
App starts and works fine, app can be debugged. No issue at all - except:
After the application is fully initialized and ready to accept user interactions -> Click on button -> Desired method is called -> Code is worked out -> The code should make a simple UDP call but it does not (or the packet does not reach the UDP listener due to missing configuration?).
The UDP receiver works fine and is capable to receive UDP packets.
My mobile phone and the UDP receiver app are using the same port.
I read/found already that in the previous cross-platform framework, means “Xamarin (Android SDK 12)”, some permissions must be set (I did, see above) and that the multicastlock must be set over the WifiManager …
I tried this in my MAUI app. But could not find anything guiding me nor figured it out by myself.
My MAUI sending code:
var dataToBeSend = "What ever ...";
var data = Encoding.UTF8.GetBytes(dataToBeSend);
var UdpClient = new UdpClient();
// UdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
UdpClient.ExclusiveAddressUse = false;
UdpClient.EnableBroadcast = true;
// UdpClient.Client.Bind(new IPEndPoint(IPAddress.Parse("255.255.255.255"), BroadcastPort));
// UdpClient.Client.Bind(new IPEndPoint(IPAddress.Any, BroadcastPort));
UdpClient.Send(data, data.Length, "255.255.255.255", BroadcastPort);
As said: very easy and straight forward.
Notice that I also tried binding UDP code …
So please can someone be so kind to guide me or give me a hint?
Thank you very much in advance!
ANSWER:
After two days I found a solution - and would like to share it because may be it helps someone else.
The code to make the UDP call msut be placed in a THREAD (not task!)
codesnippet:
var communication = new Communication();
var udpThread = new Thread(new ThreadStart(communication.FireUDPCall));
udpThread.Start();
The firewalls can stay turned on / active!
Code:
MulticastSocket s = new MulticastSocket();
InetAddress addr = InetAddress.getByName("230.230.230.1");
s.joinGroup(addr);
//...
On Ubuntu 14.04, when I run it, I can see IGMPv2 "Membership Report group 230.230.230.1" message out. But on Android, no such packet is seen.
Such packet is a must to notify router about IGMP membership when user calls joinGroup (or setsockopt in C), right?
It would appear that many devices are shipped without support for multicast built into the kernel. Open an adb shell and do
cat /proc/net/igmp
If it's not there, the kernel has been built without CONFIG_IP_MULTICAST. You're basically SOL without rooting your device and/or flashing custom firmware.
This is a sad state of affairs.
I am trying to write a script, which will connect a device (radiomodem), that have a bluetooth with my Nexus 7 (Android 4.4)
The task is to send a command via bluetooth, and then get an answer from radiomodem.
After sending a command I don't get an answer from device (or I get it, but cannot read bluetooth buffer), and my script stops while reading. It doesn't send me any mistakes, just stops.
I've tried to send commands from Nexus to PC, and I've seen them in virtual COM on PC.
I've tried to send from PC to Nexus, and from radiomodem to Nexus long lines and read them. it was fine, too.
But writing-reading doesn't work. What I'm doing wrong?
Here is my code:
import sl4a
import time
droid = sl4a.Android()
uuid = '00001101-0000-1000-8000-00805F9B34FB'
adr = '6B:E2:00:DA:18:01'
droid.toggleBluetoothState(True) # connection is always successful
droid.bluetoothConnect(uuid,adr)
time.sleep(2)
i = 0
while i < 3:
res = droid.dialogGetInput().result
res = res + '\r'
droid.bluetoothWrite(res)
time.sleep(0.6) # here I've tried different timeouts
ans = droid.bluetoothRead(4096).result
if ans is None:
print('no answer')
else:
w = str(ans)
droid.dialogCreateAlert("+", w)
droid.dialogSetPositiveButtonText('OK')
i += 1
Half a year ago I did a small app with PY4A and I had trouble with connecting to my Bluetooth heart rate monitor from a galaxy S2. To solve the connection issue's I switched to pybluez. Using the Bluetooth device worked from there. See the working example that helped me here.
http://cuu508.wordpress.com/2011/02/21/hxm-t-display-heart-rate-from-zephyrs-hxm/
I hope this helps.
Regards.
I am willing to create a server and client program on my android mobile devices.
The devices communicate with each other on the same wifi network, therefore, some simple scanning mechanism must be implemented - The client phones search for a server phone through some kind of broadcast.
What I did:
My protocol - the client phone broadcasts a message port p on the wifi, the server listens on port p. when the server gets the broadcast message it sends a message back, therefore discovering itself to the client.
My code -
I have opened a broadcast socket on my app, it sends a broadcast message.
Meanwhile there is a python script on my PC that listens and replies - I use python so that my testing will be easier - Wireshark on the PC and I can see everything.
What happens:
When I use one of my Galaxy S phones - it works and I get a response.
When I use the other Galaxy S phone - it doesn't work.
Now this is what I know:
The phone that works actually has Nexus ROM on it Ver. 4.1.1
The phone that doesn't work has 2.3.3 regular galaxy ROM
The python code says it receives both of the broadcasts sent from both phones, and replies to both of them without raising any exception.
So far I was thought the problem may be
1. the older version'd phone.
2. the windows firewall
3. the router firewall
So I have opened Wireshark, and Indeed I saw that both phones are sending their broadcasts - it was logged on Wireshark.
But the python script only responded to the first one.
So this is why 1 & 3 are irrelevant - if the router firewall was blocking my UDP I would have still seen the python server response, same with the older versioned phone.
To get rid of 2 i just disabled the windows firewall - still same problem.
Does anyone has a clue to why this effect might happen?
Thanks!
Edit
My python code:
def scan(data, addr, sock):
print "received scan message:", data, addr
name = u"name".encode("utf-16-le")
data = "DISC" + short2bytes(len(name)) + name
print "sending back %s to %s" % (data, addr)
sock.sendto(data, addr)
if __name__ == "__main__":
sock = socket(AF_INET, SOCK_DGRAM)
sock.bind(('', UDP_PORT))
while 1:
data, addr = sock.recvfrom(1500)
print "received packet " + data
if data.startswith("SCAN"):
scan(data, addr, sock)
edit 2:
Okay! Seems like my code and protocol DID work.
As it turns out the 2.3.3 phone had some severe ARP problems.
After some resets it works flawlessly!
im testing the new technology wifi direct and im having some issues using
the wifi direct demo from the samples that come with the android-sdk.
So, I have two devices A and B, both with android 4.0.3.
First, from device A, I send a file to B. Nothing wrong here, B
receives the file.
Then A disconnects from B.
Now, from device B I try to send a file to A.
But the device that receives the file is B, instead of A.
To fix, i need to turn off and on both devices...
Also, sometimes when i click disconnect and try to
connect again, connection fails and i have to disable and
enable wifi direct...
Anyone else experiencing this?
Is it because the new technology is not mature yet or maybe
something wrong with my build/driver/etc or maybe this demoapp
doesnt support two-way sharing.
Any ideas and/or explanations would be apreciated.
When providing a WifiP2pConfig instance to the connect() function, you can set the groupOwnerIntent property of this configuration object as follows:
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = "..."; // insert ip here
config.groupOwnerIntent = 0;
config.wps.setup = WpsInfo.PBC;
manager.connect(..., config, ...);
From the android reference:
This (the groupOwnerIntent) is an integer value between 0 and 15 where
0 indicates the least inclination to be a group owner and 15 indicates
the highest inclination to be a group owner.
Furthermore, the demo probably repeatedly sends the file to the same device because there is always made a socket connection to the ip-address obtained from:
WifiP2pInfo.groupOwnerAddress
If you would like to support bidirectional communication, the first step in setting this up would be sending the ip-address of the non group owner to the group owner.
As far as the disconnect/reconnect problem goes, I seem to have the same inconsistencies with Android 4.0.2 devices.
I have been trying for a while to transfer files between two devices using wifi direct. I have use the Android SDK WifiDirectDemo as base. My experience:
GO address is always the same (at least in Samsung Nexus), but this is not really a problem, because you can use this to know who is the server (or client).
Another strange thing was that MAC address of devices were different when you got it from Android WifiManager and when you read it from "/proc/net/arp" file.
At the end I did it, and you can see the code here.
I hope it helps you!
I have been struggling with the same problem lately. I suppose this is an OS issue. To give you a brief background, I have installed Wi-Fi Direct application to both devices with different OS versions, one with OS 4.0.1 and one with OS 4.0.2. The connection fails from time to time when I disconnect and reconnect the devices. It goes same while searching for devices too. But the thing is, this only happens on the device with OS 4.0.2. Other device does not crash or disconnect.
While searching for that problem, I have found the links below. People discussed about that and they share the same idea. Apparently this is an OS 4.0.2 issue. I am not sure if it is the same for OS 4.0.3 but there is no problem with the previous version OS 4.0.1 for sure.
Here are the links:
http://code.google.com/p/android/issues/detail?id=24402
http://osdir.com/ml/android-platform/2012-01/msg00226.html