建立WIFI Direct APP : android.net.wifi.p2p+android.net.wifi.p2p.nsd + Wi-Fi peer-to-peer overview 翻譯
一 ) Provides classes to create peer-to-peer (P2P) connections with Wi-Fi Direct.
提供用於使用Wi-Fi Direct建立對等(P2P)連線的類。
二 ) Using these APIs, you can discover and connect to other devices when each device supports Wi-Fi Direct, then communicate over a speedy connection across distances much longer than a Bluetooth connection. The primary class you need to work with is WifiP2pManager, which you can acquire by calling getSystemService(WIFI_P2P_SERVICE). The WifiP2pManager includes APIs that allow you to:
使用以下API可以”發現”支援直連的裝置也可以連線支援WIFI直連的裝置,然後使用比藍芽連線速度更快,連線距離更長的通訊方式連線,需要使用主要WifiP2pManager類,可以通過呼叫getSystemService(WIFI_P2P_SERVICE)來獲取物件.WifiP2pManager包含API如下:
- Initialize your application for P2P connections by calling initialize()
- Discover nearby devices by calling discoverPeers()
- Start a P2P connection by calling connect()
And more
通過呼叫initialize()初始化P2P連線應用程式
- 通過呼叫discoverPeers()發現周圍的裝置
- 通過呼叫connect()開始進行P2P連線
- 還有很多
三 ) Several other interfaces and classes are necessary as well, such as:
一些其他的介面和類同樣也會用到,比如:
- The WifiP2pManager.ActionListener interface allows you to receive callbacks when an operation such as discovering peers or connecting to them succeeds or fails.
- WifiP2pManager.PeerListListener interface allows you to receive information about discovered peers. The callback provides a WifiP2pDeviceList, from which you can retrieve a WifiP2pDevice object for each device within range and get information such as the device name, address, device type, the WPS configurations the device supports, and more.
- The WifiP2pManager.GroupInfoListener interface allows you to receive information about a P2P group. The callback provides a WifiP2pGroup object, which provides group information such as the owner, the network name, and passphrase.
- WifiP2pManager.ConnectionInfoListener interface allows you to receive information about the current connection. The callback provides a WifiP2pInfo object, which has information such as whether a group has been formed and who is the group owner.
- WifiP2pManager.ActionListener介面表示當一個類發現對等裝置或正在連線對等裝置操作成功或者失敗時,允許你收到回撥
- WifiP2pManager.PeerListListener介面允許你收到已發現對等裝置的資訊,這個回撥提供一個WifiP2pDeviceList,可以從中獲取到範圍內每個裝置的WifiP2pDevice物件,獲得資訊如:裝置名稱,裝置地址,裝置型別,裝置支援的WPS配置等等.
- WifiP2pManager.GroupInfoListener 介面允許獲得P2P組資訊,回撥提供WifiP2pGroup物件,提供的組資訊如所有者,網路名稱和密碼組
- WifiP2pManager.ConnectionInfoListener介面允許獲得當前正在連線的資訊,回撥提供WifiP2pInfo物件,包含一個組是否形成所有者的資訊
In order to use the Wi-Fi P2P APIs, your app must request the following user permissions:
四 ) 使用這些API必須申請以下使用者許可權
- ACCESS_WIFI_STATE
- CHANGE_WIFI_STATE
- INTERNET (although your app doesn’t technically connect to the Internet, communicating to Wi-Fi Direct peers with standard java sockets requires Internet permission).
其中的第三個:雖然app可能不需要聯網,但第三方通過java sockets獲取網路與WIFI對等裝置直連同樣需要這個許可權
Note: Not all Android-powered devices support Wi-Fi Direct. If your application uses Wi-Fi Direct, declare so with a element in the manifest file:
並不是Android裝置都支援wifi直連,使用需要在清單元素中註冊
<manifest ...>
<uses-feature android:name="android.hardware.wifi.direct" />
...
</manifest>
五:Wi-Fi peer-to-peer (P2P) allows Android 4.0 (API level 14) or later devices with the appropriate hardware to connect directly to each other via Wi-Fi without an intermediate access point (Android’s Wi-Fi P2P framework complies with the Wi-Fi Alliance’s Wi-Fi Direct™ certification program). Using these APIs, you can discover and connect to other devices when each device supports Wi-Fi P2P, then communicate over a speedy connection across distances much longer than a Bluetooth connection. This is useful for applications that share data among users, such as a multiplayer game or a photo sharing application.
Wifi P2P支援Android API level14以上合適的裝置硬體通過wifi直連通訊而不通過ap,當每個裝置支援Wifi-P2P,就可以通過API發現和連線其他裝置,然後使用比藍芽連線速度更快,連線距離更長的通訊方式連線,這對於使用者共享資料很有用,比如多個遊戲玩家玩遊戲或者分享照片.
The Wi-Fi P2P APIs consist of the following main parts:
wifi P2P API包含一下及部分:
- Methods that allow you to discover, request, and connect to peers are defined in the WifiP2pManager class.
- 定義在WifiP2pManager類中的方法允許發現,請求和連線其他對等裝置
- Listeners that allow you to be notified of the success or failure of WifiP2pManager method calls. When calling WifiP2pManager methods, each method can receive a specific listener passed in as a parameter.
- WifiP2pManager方法呼叫返回”成功”或者”失敗”的值允許通知給監聽者
- Intents that notify you of specific events detected by the Wi-Fi P2P framework, such as a dropped connection or a newly discovered peer
- 通知檢測到特定事件的intent,如"斷開連線"或者新發現的對等裝置
You often use these three main components of the APIs together. For example, you can provide a WifiP2pManager.ActionListener to a call to discoverPeers(), so that you can be notified with the ActionListener.onSuccess() and ActionListener.onFailure() methods. A WIFI_P2P_PEERS_CHANGED_ACTION intent is also broadcast if the discoverPeers() method discovers that the peers list has changed.
會用到以下三個API的組合,比如通過WifiP2pManager.ActionListener來呼叫discoverPeers()方法,這樣當成功或者失敗的時候會收到通知.呼叫discoverPeers()可以發現對等列表被更改,傳送WIFI_P2P_PEERS_CHANGED_ACTION 廣播
六:Create a broadcast receiver for Wi-Fi P2P intents
建立Wifi P2P intent 廣播接收器
A broadcast receiver allows you to receive intents broadcast by the Android system, so that your application can respond to events that you are interested in. The basic steps for creating a broadcast receiver to handle Wi-Fi P2P intents are as follows:
允許通過接受廣播氣接受Android系統廣播,這樣你的應用可以響應你想要的事件.基本步驟去建立接收wifi p2p意圖事件如下:
Create a class that extends the BroadcastReceiver class. For the class’ constructor, you most likely want to have parameters for the WifiP2pManager, WifiP2pManager.Channel, and the activity that this broadcast receiver will be registered in. This allows the broadcast receiver to send updates to the activity as well as have access to the Wi-Fi hardware and a communication channel if needed.
1:建立一個類繼承BroadcastReceiver類.引數有"WifiP2pManager"/WifiP2pManager.Channel/activity.允許廣播接收器更新activity,如果需要也可以通過Wi-Fi 硬體通道通訊
In the broadcast receiver, check for the intents that you are interested in onReceive(). Carry out any necessary actions depending on the intent that is received. For example, if the broadcast receiver receives a WIFI_P2P_PEERS_CHANGED_ACTION intent, you can call the requestPeers() method to get a list of the currently discovered peers.
2 :在廣播接受函式中檢查任何你感興趣的intent,根據收到的intent完成任何必要的處理操作,比如收到WIFI_P2P_PEERS_CHANGED_ACTION intent ,你可以呼叫requestPeers()方法獲取當前對等裝置發現列表
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager mManager;
private Channel mChannel;
private MyWiFiActivity mActivity;
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
MyWifiActivity activity) {
super();
this.mManager = manager;
this.mChannel = channel;
this.mActivity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Check to see if Wi-Fi is enabled and notify appropriate activity
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// Call WifiP2pManager.requestPeers() to get a list of current peers
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Respond to new connection or disconnections
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
}
}
}
七:Create a Wi-Fi P2P application
建立一個wifi P2P應用
Creating a Wi-Fi P2P application involves creating and registering a broadcast receiver for your application, discovering peers, connecting to a peer, and transferring data to a peer. The following sections describe how to do this.
建立一個wifi p2p 應用涉及為你的應用建立和註冊廣播監聽,發現對等裝置,連線對等裝置,給對等裝置傳輸資料.
Initial setup
Before using the Wi-Fi P2P APIs, you must ensure that your application can access the hardware and that the device supports the Wi-Fi P2P protocol. If Wi-Fi P2P is supported, you can obtain an instance of WifiP2pManager, create and register your broadcast receiver, and begin using the Wi-Fi P2P APIs
初始設定
在使用wifi p2p API之前,確保你的應用支援wifi p2p協議,如果支援,你可以獲得一個WifiP2pManager物件,呼叫api來建立廣播接收器.
1:Request permission to use the Wi-Fi hardware on the device and also declare your application to have the correct minimum SDK version in the Android manifest:
請求允許使用wifi硬體許可權的宣告在你的應用中,並且在Android manifest中指定正確的sdk版本號
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2:Check to see if Wi-Fi P2P is on and supported. A good place to check this is in your broadcast receiver when it receives the WIFI_P2P_STATE_CHANGED_ACTION intent. Notify your activity of the Wi-Fi P2P state and react accordingly:
檢查是否啟用並支援wifi P2P.在廣播接收器接受到WIFI_P2P_STATE_CHANGED_ACTION intent時候是一個好地方.通知你的activity wifi P2P狀態作出相應反映
@Override
public void onReceive(Context context, Intent intent) {
...
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi P2P is enabled
} else {
// Wi-Fi P2P is not enabled
}
}
...
}
3:In your activity’s onCreate() method, obtain an instance of WifiP2pManager and register your application with the Wi-Fi P2P framework by calling initialize(). This method returns a WifiP2pManager.Channel, which is used to connect your application to the Wi-Fi P2P framework. You should also create an instance of your broadcast receiver with the WifiP2pManager and WifiP2pManager.Channel objects along with a reference to your activity. This allows your broadcast receiver to notify your activity of interesting events and update it accordingly. It also lets you manipulate the device’s Wi-Fi state if necessary:
在應用activity的onCreate()方法中獲取WifiP2pManager物件並通過呼叫initialize()方法向Wi-Fi P2P framework註冊這個應用,這個方法返回一個用來將應用與Wi-Fi P2P framework連線起來的WifiP2pManager.Channel物件.還可以用WifiP2pManager和WifiP2pManager.Channel 物件以及activity建立一個廣播接收的例項.允許廣播接收器通知activity根據註冊事件相應更新,同時如果需要操縱裝置的wifi狀態
WifiP2pManager mManager;
Channel mChannel;
BroadcastReceiver mReceiver;
...
@Override
protected void onCreate(Bundle savedInstanceState){
...
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
...
}
4:Create an intent filter and add the same intents that your broadcast receiver checks for:
建立意圖過濾同時新增一些廣播接收器需要關注的意圖
IntentFilter mIntentFilter;
...
@Override
protected void onCreate(Bundle savedInstanceState){
...
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
...
}
5:Register the broadcast receiver in the onResume() method of your activity and unregister it in the onPause() method of your activity:
在onResume()中註冊廣播監聽,在onPause()中取消廣播監聽
/* register the broadcast receiver with the intent values to be matched */
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
/* unregister the broadcast receiver */
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
When you have obtained a WifiP2pManager.Channel and set up a broadcast receiver, your application can make Wi-Fi P2P method calls and receive Wi-Fi P2P intents.
當獲得WifiP2pManager.Channel 和配置廣播接收器,應用就可以呼叫Wi-Fi P2P方法和接收Wi-Fi P2P意圖
Discover peers
To discover peers that are available to connect to, call discoverPeers() to detect available peers that are in range. The call to this function is asynchronous and a success or failure is communicated to your application with onSuccess() and onFailure() if you created a WifiP2pManager.ActionListener. The onSuccess() method only notifies you that the discovery process succeeded and does not provide any information about the actual peers that it discovered, if any:
在範圍內發現並連線可用的對等裝置.這個函式是非同步的,如果建立WifiP2pManager.ActionListener,則應用可以獲取到onSuccess()和onFailure()來通知發現成功與否,但不提供任何關於被發現對等裝置資訊(如果有):
mManager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
...
}
@Override
public void onFailure(int reasonCode) {
...
}
});
If the discovery process succeeds and detects peers, the system broadcasts the WIFI_P2P_PEERS_CHANGED_ACTION intent, which you can listen for in a broadcast receiver to obtain a list of peers. When your application receives the WIFI_P2P_PEERS_CHANGED_ACTION intent, you can request a list of the discovered peers with requestPeers(). The following code shows how to set this up:
如果發現對等裝置的結果是”成功”,系統廣播WIFI_P2P_PEERS_CHANGED_ACTION intent,同時監聽獲取對等列表.當應用收到WIFI_P2P_PEERS_CHANGED_ACTION intent,可以通過requestPeers()請求被發現的對等裝置:
PeerListListener myPeerListListener;
...
if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// request available peers from the wifi p2p manager. This is an
// asynchronous call and the calling activity is notified with a
// callback on PeerListListener.onPeersAvailable()
if (mManager != null) {
mManager.requestPeers(mChannel, myPeerListListener);
}
}
The requestPeers() method is also asynchronous and can notify your activity when a list of peers is available with onPeersAvailable(), which is defined in the WifiP2pManager.PeerListListener interface. The onPeersAvailable() method provides you with an WifiP2pDeviceList, which you can iterate through to find the peer that you want to connect to.
requestPeers()方法也是非同步的並且當可以通過onPeersAvailable()方法給activity提供一個對等裝置列表,需要監聽WifiP2pManager.PeerListListener介面.onPeersAvailable()方法提供WifiP2pDeviceList 列表,可以通過迭代找到你想要連線的對等裝置.
Fetch the list of peers
獲取對等裝置列表
else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// 呼叫 WifiP2pManager.requestPeers() 獲得當前peer裝置列表
if (mManager != null) {
mManager.requestPeers(mChannel, new WifiP2pManager.PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
Collection<WifiP2pDevice> refreshedPeers = peerList.getDeviceList();
if (!refreshedPeers.equals(peers)) {
peers.clear();
peers.addAll(refreshedPeers);
}
if (peers.size() == 0) {
Log.d(TAG, "No devices found");
return;
}
for(int i = 0; i< peers.size(); i++){
Log.d(TAG,"peers.size() = " + peers.size() + "\n");
Log.d(TAG,"peers.get(0).deviceName: " + peers.get(0).deviceName + "\n");
Log.d(TAG,"peers.get(0).deviceAddress: " + peers.get(0).deviceAddress + "\n");
Log.d(TAG,"peers.get(0).status: " + peers.get(0).status + "\n");
Log.d(TAG,"peers.get(0).toString(): " + peers.get(0).toString() + "\n");
Log.d(TAG,"peers.get(0).isGroupOwner(): " + peers.get(0).isGroupOwner() + "\n");
Log.d(TAG,"peers.get(0).primaryDeviceType: " + peers.get(0).primaryDeviceType + "\n");
}
}
});
}
}
Connect to a peer
When you have figured out the device that you want to connect to after obtaining a list of possible peers, call the connect() method to connect to the device. This method call requires a WifiP2pConfig object that contains the information of the device to connect to. You can be notified of a connection success or failure through the WifiP2pManager.ActionListener. The following code shows you how to create a connection to a desired device:
當在範圍內發現一個列表的對等裝置可以選擇進行連線,呼叫connect()方法與裝置連線.呼叫這個方法需要WifiP2pConfig物件,該物件包含要連線的裝置的資訊.可以通過WifiP2pManager.ActionListener來獲取連線成功與否.如下:
相關推薦
建立WIFI Direct APP : android.net.wifi.p2p+android.net.wifi.p2p.nsd + Wi-Fi peer-to-peer overview 翻譯
一 ) Provides classes to create peer-to-peer (P2P) connections with Wi-Fi Direct. 提供用於使用Wi-Fi Direct建立對等(P2P)連線的類。 二 ) Using the
Android Wi-Fi Peer-to-Peer(Android的Wi-Fi P2P對等網路)
Wi-Fi peer-to-peer(P2P,對等網路),它允許具備相應硬體的Android 4.0(API level 14)或者更高版本的裝置可以直接通過wifi而不需要其它中間中轉節點就能直接通訊(Android的Wi-Fi P2P框架符合Wi-Fi聯盟的Wi-Fi
Peer-to-Peer (P2P) communication across middleboxes
Internet Draft &nb
android wifi p2p / wifi direct
版權宣告:本文為博主原創文章,未經博主允許不得轉載。https://blog.csdn.net/h784707460/article/details/81502574 一. wifi P2P協議相關 Wi-Fi Alliance(Wi-Fi聯盟)推出的一項重要技術規範Wi-
【Android】 Android-wifi 直連 wifi direct wifi p2p
現在,Android的支援Wi -Fi的直接點對點點對點(P2P)Android系統的供電裝置和其他型別的裝置,沒有一個熱點或網際網路連線之間的連線。Android框架提供了一套Wi - Fi的P2P的API,允許你去發現和連線到其他裝置時,每個裝置的Wi -Fi的直接支援,然後溝通跨越距離遠遠長於藍芽連線迅
Android -wifi 直連(wifi direct )
現在,Android的支援Wi -Fi的直接點對點點對點(P2P)Android系統的供電裝置和其他型別的裝置,沒有一個熱點或網際網路連線之間的連線。Android框架提供了一套Wi - Fi的P2P的API,允許你去發現和連線到其他裝置時,每個裝置的Wi -Fi的直接支援
WiFi Direct的Android端呼叫說明
Wifi直連API的一個說明(給linux端的說明文件) 一、 首先有幾個wifi系統狀態需要說明 WIFI_P2P_STATE_CHANGED_ACTION:當啟用或禁用裝置上的Wi-FiDirect時,發出這個廣播。 WIFI_P2P_PEERS_CHANG
WIFI Direct/WIFI P2P
上節說過了網絡卡的選型,之所以網絡卡的選型如此重要,主要是因為Miracast網絡卡相比較於普通的網絡卡多了個P2P功能,底層可靠了,才能很好的進行接下來的上層開發,如果我們已經有了可靠的P2P網絡卡
3) 十分鐘學會android--建立第一個APP,建立簡單的用戶界面
使用 圖標 run class 屏幕寬度 情況下 不可見 文本輸入框 nes 在本小節裏,我們將學習如何用 XML 創建一個帶有文本輸入框和按鈕的界面。下一節課將學會使 APP 對按鈕做出響應——按鈕被按下時,文本框裏的內容被發送到另外一個 Acti
修改wifi direct/ hotspot/ Bluetooth預設名稱
1. wifi direct or wifi p2p 預設名稱為Android_XXXX. frameworks/opt/net/wifi/service/java/com/android/server/wifi/p2p/WifiP2pServiceImpl.java
舊手機android的linux核心編譯2-Wifi驅動加入。
經過一些時間的分析與除錯,還是把wifi的驅動調通了。 首先要分析舊手機的wifi。 1,要分析舊手機的wifi,在recovery下配通wifi 是一個不錯的選擇。在recovery已經配通了adb 介面,其實它除去沒有應用軟體外,與硬體系統相關的內容都是完整的。經過我多次償試,對我的MS
Android:使用wpa_supplicant和dhcpd簡單測試wifi
原文: http://blog.csdn.net/laojing123/article/details/7719132 Android系統中對於WIFI的設定整合到了“設定”中,其實跟手動設定差不多。這裡介紹下如何手動連線WIFI,以方便以後除錯WIFI。
Android Wi-Fi原始碼分析之WifiService操作Wi-Fi(一):分析Wifi.c中的wifi_load_driver()函式
Wi-Fi原始碼分析之WifiService操作Wi-Fi(一) 分析Wifi.c中的wifi_load_driver()函式 int wifi_load_driver() { AL
Android系統中通過shell命令實現wifi的連線控制
簡介 工作中遇到一個“變態”的需求,在android系統中不通過java層控制wifi的連線(主要是修改ap的essid和password),而是需要通過native層實現對wifi的控制。 How 接到這個需求時,第一個想法是如何找到Android
多個應用連線wifi熱點的切換問題--WifiConfiguration的新增--Android M
一、版本適配問題。在Android6.0上,APP無法更新系統儲存過的、不是由當前APP建立的WIFI配置。1、現象: 在測試過程中,發現了一個bug。場景是:在Android6.0的機器上,連線一個系統儲存過的wifi,輸入了正確的密碼後,卻始終無法連線成功,即upd
Android開機嚮導setupwizard,設定系統語言,WiFi嚮導
package com.tomorrow_p.setupwizard; import android.content.ComponentName; import android.content.Intent; import android.content.res.Configuration; import
Android 獲取本機的mac和wifi的BSSID(mac)以及其他資訊
一上班就開始總結上禮拜寫的wifi,先來說說wifi的屬性 在Android的官方文件中定義瞭如下五種狀態: WIFI_STATE_DISABLED WIFI 不可用 WIFI_STATE_DISABLING WIFI 正在關閉 WIFI_STATE_ENABL
wifi direct介紹與ad-hoc區別
There are a few key features to know about Wi-Fi Direct: Wi-Fi Direct enlists a technology known as "Soft AP": Soft AP, in a nutshell,
Android開發——自動連線指定SSID的wifi熱點(不加密/加密)
最近在做一個專案,其中涉及到一塊“自動連線已存在的wifi熱點”的功能,在網上查閱了大量資料,五花八門,但其中一些說的很簡單,即不能實現傻瓜式的拿來就用,有些說的很詳細,但其中不乏些許錯誤造成功能無法實現,經過浣熊多方努力,終於成功將功能實現,遂將一點點小成就拿出來與大家分
android 獲取當前隱藏和未隱藏wifi的加密方式(安全性)
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt