基于Androidstudio的WiFi通信代码
时间: 2023-11-12 10:07:56 浏览: 94
以下是一个简单的基于Androidstudio的WiFi通信代码示例:
1. 添加权限
在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
```
2. 布局文件
在布局文件中添加一个EditText和一个Button:
```
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入要发送的内容"/>
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"/>
```
3. Java代码
在Java代码中实现WiFi通信的逻辑:
```
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button btnSend;
private MulticastSocket multicastSocket;
private InetAddress multicastAddress;
private int port = 8888;
private WifiManager.MulticastLock multicastLock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
btnSend = findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = editText.getText().toString();
if (TextUtils.isEmpty(message)) {
Toast.makeText(MainActivity.this, "请输入要发送的内容", Toast.LENGTH_SHORT).show();
return;
}
sendMulticastMessage(message);
}
});
// 获取WiFiManager对象
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
// 获取MulticastLock对象
multicastLock = wifiManager.createMulticastLock("multicast_lock");
// 获取MulticastSocket对象
try {
multicastSocket = new MulticastSocket(port);
multicastAddress = InetAddress.getByName("224.0.0.1");
multicastSocket.joinGroup(multicastAddress);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
// 获取MulticastLock对象
if (multicastLock != null && !multicastLock.isHeld()) {
multicastLock.acquire();
}
}
@Override
protected void onPause() {
super.onPause();
// 释放MulticastLock对象
if (multicastLock != null && multicastLock.isHeld()) {
multicastLock.release();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// 关闭MulticastSocket对象
if (multicastSocket != null) {
try {
multicastSocket.leaveGroup(multicastAddress);
multicastSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendMulticastMessage(final String message) {
new Thread(new Runnable() {
@Override
public void run() {
try {
byte[] data = message.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, multicastAddress, port);
multicastSocket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
```
以上代码演示了如何在Android应用中使用WiFi进行广播通信。在该示例中,我们使用了MulticastSocket和MulticastLock对象来实现组播通信。在发送消息时,我们将要发送的消息转换为字节数组,并使用DatagramPacket将其发送到目标地址。请注意,组播地址通常在224.0.0.0到239.255.255.255之间。
阅读全文