Android上是否有WiFi-Direct的工作示例代码?
创始人
2024-10-10 06:30:46
0

Android官方提供了WiFi-Direct的API和示例代码。以下是实现WiFi-Direct连接的代码示例:

声明WiFi-Direct:

WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;

将上述代码添加到您的类中并实现以下方法:

在onCreate方法中:

mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);

初始化peers列表:

WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
  @Override
  public void onPeersAvailable(WifiP2pDeviceList peerList) {
    // 检查WiFi-Direct对等列表是否更改
    if (progressDialog != null && progressDialog.isShowing()) {
      progressDialog.dismiss();
    }
    peers.clear();
    peers.addAll(peerList.getDeviceList());
    // 显示设备的名称
    DeviceNameListAdapter adapter = new DeviceNameListAdapter(MainActivity.this, peers);
    listView.setAdapter(adapter);
    if (peers.size() == 0) {
      return;
    }
  }
}

搜索设备:

mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
  @Override
  public void onSuccess() {
    // 成功开始发现设备
    progressDialog = ProgressDialog.show(MainActivity.this, "Press back to cancel", "Finding peers", true, true);
  }

  @Override
  public void onFailure(int reasonCode) {
    // 发现设备失败,错误码说明
    Toast.makeText(MainActivity.this, R.string.search_fail, Toast.LENGTH_SHORT).show();
  }
});

连接设备:

// 根据您选择的设备提供下面的WiFi-Direct设备地址
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
  @Override
  public void onSuccess() {
    Toast.makeText(MainActivity.this, R.string.connect_ok, Toast

相关内容

热门资讯

wepoker免费透视脚本!红... wepoker免费透视脚本!红龙poker辅助工具(透视)教程-一贯必备存在有挂1、打开软件启动之后...
hhpoker辅助靠谱吗!we... hhpoker辅助靠谱吗!wepoker辅助脚本(透视)器-本来揭幕真的有挂1、打开软件启动之后找到...
wepoker好友房开挂!we... wepoker好友房开挂!wepoker网页版透视方法(透视)教程-原来了解存在有挂wepoker网...
wepoker手机版透视脚本!... wepoker手机版透视脚本!wpk透视(透视)神器-其实专业有挂1、下载好wepoker手机版透视...
菠萝德普辅助器免费版在哪里!p... 菠萝德普辅助器免费版在哪里!pokerrrr2辅助(透视)器-切实必备是真的挂一、菠萝德普辅助器免费...
微乐小程序黑科技!微乐家乡官方... 微乐小程序黑科技!微乐家乡官方免费下载安装(开挂)app-原来必备是真的挂1、起透看视 辅助软件价格...
微信小程序黑科技免费!微信微乐... 微信小程序黑科技免费!微信微乐陕西小程序辅助器(开挂)教程-竟然有挂存在有挂1、不需要AI权限,帮助...
微乐小程序存在有挂!微乐家乡官... 微乐小程序存在有挂!微乐家乡官方app(开挂)方法-好像专业是有挂进入游戏-大厅左侧-新手福利-激活...
微乐小程序存在有挂!微乐河南小... 微乐小程序存在有挂!微乐河南小程序微乐辅助脚本(开挂)方法-确实分享有挂1、实时透视辅助更新:用户可...
微乐小程序透视辅助!微乐小程序... 微乐小程序透视辅助!微乐小程序辅助器免费(开挂)方法-果然关于有挂透视方法中分为三种模型:靠谱、辅助...