package com.github.jasonhancn.tvcursor;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
public class WifiConnectActivity extends AppCompatActivity {
private static final String TAG = "WifiConnectActivity";
private WifiP2pManager wifiP2pManager;
private WifiP2pManager.Channel channel;
private IntentFilter intentFilter;
private Button btnDiscoverPeers;
private Button btnConnect;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
btnDiscoverPeers = new AppCompatButton(this);
btnDiscoverPeers.setText("发现");
btnConnect = new AppCompatButton(this);
btnConnect.setText("链接");
linearLayout.addView(btnDiscoverPeers);
linearLayout.addView(btnConnect);
// 初始化Wi-Fi P2P管理器和通道
wifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = wifiP2pManager.initialize(this, getMainLooper(), null);
// 创建意图过滤器并添加所需的意图动作
intentFilter = new IntentFilter();
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
// 注册广播接收器
registerReceiver(broadcastReceiver, intentFilter);
// 按钮点击事件 - 搜索对等设备
btnDiscoverPeers.setOnClickListener(v -> discoverPeers());
// 按钮点击事件 - 连接到对等设备
btnConnect.setOnClickListener(v -> connectToPeer());
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION
, Manifest.permission.NEARBY_WIFI_DEVICES}, 119);
}
private void connectToPeer() {
Log.i(TAG, "connectToPeer: enter");
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, android.Manifest.permission.NEARBY_WIFI_DEVICES) != PackageManager.PERMISSION_GRANTED) {
// return;
}
Log.i(TAG, "connectToPeer: start connect");
wifiP2pManager.requestPeers(channel, wifiP2pDeviceList -> {
List<WifiP2pDevice> deviceList = new ArrayList<>(wifiP2pDeviceList.getDeviceList());
Log.i(TAG, "connectToPeer: item count:" + deviceList.size());
if (deviceList.size() == 0) {
return;
}
for (WifiP2pDevice item : deviceList) {
Log.i(TAG, "connectToPeer: item name:" + item.deviceName + "; " + item.deviceAddress);
}
// 获取选定的设备
WifiP2pDevice device = deviceList.get(0);
// 创建连接配置
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
// 连接到选定的设备
if (ActivityCompat.checkSelfPermission(WifiConnectActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(WifiConnectActivity.this, Manifest.permission.NEARBY_WIFI_DEVICES) != PackageManager.PERMISSION_GRANTED) {
// return;
}
wifiP2pManager.connect(channel, config, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Toast.makeText(WifiConnectActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int reasonCode) {
Toast.makeText(WifiConnectActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
}
});
});
}
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive: " + intent.getAction());
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// 获取连接信息
WifiP2pInfo wifiP2pInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO);
if (wifiP2pInfo != null) {
// 获取groupOwnerAddress
InetAddress groupOwnerAddress = wifiP2pInfo.groupOwnerAddress;
// 在这里可以使用groupOwnerAddress进行相应的操作
if (groupOwnerAddress!=null){
String hostAddress = groupOwnerAddress.getHostAddress();
Log.i(TAG, "onReceive: get host address:" + hostAddress);
// start socket connect
}
}
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
// 搜索对等设备
private void discoverPeers() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 检查并请求所需的运行时权限(ACCESS_FINE_LOCATION)
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 0);
// return;
}
}
wifiP2pManager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Toast.makeText(WifiConnectActivity.this, "发现对等设备", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int reasonCode) {
Toast.makeText(WifiConnectActivity.this, "搜索对等设备失败", Toast.LENGTH_SHORT).show();
}
});
}
}
wifi p2p 直连笔记
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Wifi Direct功能早在Android 4.0就以经加入Android系统了,但是一直没有很好的被支持,主要...