问题描述:

I am beginner in android programming. Am trying to broadcast messages on WiFiDirect using the following code:

public class FileTransferService extends IntentService {

 public static final String host= "255.255.255.255";

InetAddress broadcastAddress = InetAddress.getByName(host);// Exception: Unknown host exception

int port = 8888;

protected void onHandleIntent(Intent intent) {

Log.d(WiFiDirectActivity.TAG,"m in 1");

Context context = getApplicationContext();

DatagramSocket socket;

try {

socket = new DatagramSocket(port);

socket.setBroadcast(true);

socket.connect(broadcastAddress, port);

String message = "Hello";

byte[] buffer = message.getBytes();

DatagramPacket packet = new DatagramPacket(

buffer, buffer.length, broadcastAddress, port);

socket.send(packet); // <----- Causes a SocketException

} catch (IOException e) {

Log.d(WiFiDirectActivity.TAG, e.getMessage(), e);

}

}

}

It shows me unknown host exception on getByName() method. Is there anyway to replace the method? Am I going on a right path? Do I need to add anything along with this to send messages.

Thanks in advance

网友答案:

Try calling public UnknownHostException (String detailMessage) to get the detailed exception message.

Another way to call getByName() can be get from here

Below link has a step by step illustration of setting up a Wi-Fi Direct broadcaster Connecting with Wi-Fi Direct

相关阅读:
Top