我是android编程的初学者。我试图使用以下代码在WiFiDirect上广播消息:
public class FileTransferService扩展了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);
}
}
}
它显示了getByName()方法中的未知主机异常。反正有没有替换方法?我正走在正确的道路上吗?我是否需要添加任何内容来发送消息。
提前致谢
答案 0 :(得分:0)
尝试拨打public UnknownHostException (String detailMessage)
以获取detailed exception消息。
另一种调用getByName()的方法是从here
以下链接提供了设置Wi-Fi Direct广播公司的逐步说明 Connecting with Wi-Fi Direct