我希望能够通过WiFi发布或广告我的设备名称,该名称是可变的,并且可以由用户更改。
例如,以文件传输应用程序Xender为例。当我们在应用程序中选择“接收”选项时,我们可以在屏幕上看到用户设置的设备名称。这是屏幕截图。 您可以在图像中看到出现了名称
我在互联网上搜索了许多结果,但无法确定它是什么。我知道主机名,但我认为通常这些应用程序不会更改它,并且我认为它需要在安卓上具有一些特殊的权限才能这样做。因此,我确定它不是主机名,我们可以轻松从IP地址获取主机名。
请注意,我没有复制其他应用程序的功能。我希望在我的音乐播放器应用程序中分享歌曲。
为此,我使用了设备之间的TCP连接。我可以成功地将歌曲从一个设备发送到另一个设备。但是,它需要设备的IP地址。这对用户来说不太友好。
以下是我基本音乐分享活动的屏幕截图,其中列出了可用的IP地址,用户必须从列表中选择一个IP。 在这里,我希望显示设备名称而不是IP地址。
发送文件的代码如下:
例如,以文件传输应用程序Xender为例。当我们在应用程序中选择“接收”选项时,我们可以在屏幕上看到用户设置的设备名称。这是屏幕截图。 您可以在图像中看到出现了名称
shah.kaushal
。我在互联网上搜索了许多结果,但无法确定它是什么。我知道主机名,但我认为通常这些应用程序不会更改它,并且我认为它需要在安卓上具有一些特殊的权限才能这样做。因此,我确定它不是主机名,我们可以轻松从IP地址获取主机名。
请注意,我没有复制其他应用程序的功能。我希望在我的音乐播放器应用程序中分享歌曲。
为此,我使用了设备之间的TCP连接。我可以成功地将歌曲从一个设备发送到另一个设备。但是,它需要设备的IP地址。这对用户来说不太友好。
以下是我基本音乐分享活动的屏幕截图,其中列出了可用的IP地址,用户必须从列表中选择一个IP。 在这里,我希望显示设备名称而不是IP地址。
发送文件的代码如下:
@Override
protected Void doInBackground(Void... voids) {
System.out.println("array list");
ArrayList<File> files = new ArrayList<>();
System.out.println("about to create.");
files.add(new File(wholePath));
System.out.println("file created..");
try {
//Receiving IP addresses which are available to send our files(Music)!!
a = getClientList();
//update the UI to display the received IP addresses!!
publishProgress();
//busy waiting for user to select appropriate IP address to send files!
while (destinationAddress.equals("-1")){
}
//User has selected something, It's time to send files there!
socket = new Socket(destinationAddress,5004);
System.out.println("Connecting...");
DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
System.out.println(files.size());
//write the number of files to the server
dos.writeInt(files.size());
dos.flush();
//write file size
for(int i = 0;i< files.size();i++){
int file_size = Integer.parseInt(String.valueOf(files.get(i).length()));
dos.writeLong(file_size);
dos.flush();
}
//write file names
for(int i = 0 ; i < files.size();i++){
dos.writeUTF(files.get(i).getName());
dos.flush();
}
//buffer for file writing, to declare inside or outside loop?
int n = 0;
byte[]buf = new byte[4092];
//outer loop, executes one for each file
for(int i =0; i < files.size(); i++){
System.out.println(files.get(i).getName());
//create new fileinputstream for each file
FileInputStream fis = new FileInputStream(files.get(i));
//write file to dos
while((n =fis.read(buf)) != -1){
dos.write(buf,0,n);
dos.flush();
}
}
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
xceptionFlag = true;
e.printStackTrace();
}
Log.i("===end of start ====", "==");
try{
if(!socket.isClosed()){
socket.close();
}
}
catch (Exception e){
xceptionFlag = true;
e.printStackTrace();
}
return null;
}
接收文件的代码如下:
@Override
protected Void doInBackground(Void... voids) {
try {
//this is done isntead of above line because it was givind error of address is already in use.
ss = new ServerSocket();
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress(5004));
System.out.println("waiting");
Socket socket = ss.accept();
System.out.println("Accepted!");
DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
//read the number of files from the client
int number = dis.readInt();
ArrayList<File>files = new ArrayList<File>(number);
System.out.println("Number of Files to be received: " +number);
ArrayList<Long> fileSize = new ArrayList<>(number);
for(int i = 0; i < number ;i++){
long size = dis.readLong();
System.out.println(size);
fileSize.add(size);
}
//read file names, add files to arraylist
for(int i = 0; i< number;i++){
File file = new File(dis.readUTF());
files.add(file);
}
int n = 0;
byte[]buf = new byte[4092];
//outer loop, executes one for each file
for(int i = 0; i < files.size();i++){
System.out.println("Receiving file: " + files.get(i).getName());
//Create new Folder for our app, if it is not there and store received files there in our separate folder.
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "File");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
// Do something on success
} else {
// Do something else on failure
}
//create a new fileoutputstream for each new file
FileOutputStream fos = new FileOutputStream("mnt/sdcard/File/" +files.get(i).getName());
//read file
while (fileSize.get(i) > 0 && (n = dis.read(buf, 0, (int)Math.min(buf.length, fileSize.get(i)))) != -1)
{
fos.write(buf,0,n);
long x = fileSize.get(i);
x = x-n;
fileSize.set(i,x);
}
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
xceptionFlag = true;
e.printStackTrace();
}
////////////////////
Log.i("== the end of read ====", "==");
try{
if(!ss.isClosed()){
ss.close();
}
}
catch (Exception e){
xceptionFlag = true;
e.printStackTrace();
}
return null;
}
我已经包含了代码以供参考。谢谢。