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

相关内容

热门资讯

此事引发广泛关注!hhpoke... 此事引发广泛关注!hhpoker是内部控制吗,打哈儿麻将辅助下载,切实有辅助app(哔哩哔哩)1、每...
据权威媒体报道!hhpoker... 据权威媒体报道!hhpoker辅助挂下载,九游破解辅助插件,本来真的是有辅助工具(哔哩哔哩)1、每一...
网友热议!wepoker透视脚... 网友热议!wepoker透视脚本免费,约局吧辅助辅助外开挂,本来是真的有辅助方法(哔哩哔哩)1、约局...
据玩家消息!wepoker透视... 据玩家消息!wepoker透视脚本免费,禅游指尖四川作必弊,一直存在有辅助教程(哔哩哔哩)禅游指尖四...
记者获悉!hhpkoer辅助器... 记者获悉!hhpkoer辅助器,wepoker辅助软件视频,切实存在有辅助软件(哔哩哔哩)1、每一步...
近日!德州透视是真的假的,哥哥... 近日!德州透视是真的假的,哥哥打大a辅助,总是真的有辅助神器(哔哩哔哩)1、起透看视 德州透视是真的...
今日!hhpoker破解工具,... 今日!hhpoker破解工具,潮汕汇app辅助,其实有辅助方法(哔哩哔哩)1、实时潮汕汇app辅助透...
有玩家发现!pokemmo手机... 有玩家发现!pokemmo手机脚本辅助器,微乐兰州麻将小程序辅助,一直是真的有辅助攻略(哔哩哔哩)小...
昨日!wepoker透视版下载... 昨日!wepoker透视版下载,闲来辅助工具,确实是真的有辅助工具(哔哩哔哩)1、下载好闲来辅助工具...
有了最新消息!hhpoker有... 您好,杭州都莱到底有没有挂这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...