I am making an android app which connects to a raspberry pi acting as an access point. The phone connects to the access point like this:
String SSID = "Alarm";
String pass = "Alarm123"
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + SSID + "\"";
conf.preSharedKey = "\"" + pass + "\"";
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
if(!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
int netId = wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
And this works just fine. After this, the phone needs to broadcast an UDP packet to the network, which I did like this:
try {
DatagramSocket socket = new DatagramSocket();
socket.setBroadcast(true);
byte[] message = "013A2610E3882D08".getBytes();
DatagramPacket packet = new DatagramPacket(message, message.length, InetAddress.getByName("255.255.255.255"), 2002);
socket.send(packet);
System.out.println("Packet sent");
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
The problem is that the packet is not received where I need to receive it when mobile data is enabled on the phone. I think that this is caused by the fact that the access point from the raspberry pi does not have internet access, so mobile data stays enabled as well, and the packets are sent to the mobile connection instead of the wifi network. When I disable mobile data, it does work.
I looked for a way to disable mobile data programmatically but I found this is no longer possible as of Android 4.4.
I could of course show a prompt asking the user to disable mobile data, but I'd rather find a way to broadcast the packet to the wifi network instead of the mobile network, even though both are enabled. Is there a way to do this?