简单介绍下 Wi-Fi Direct 的功能:
就是实现在没有AP 情况下的P2P(peer to peer)连接 .
具体可以看下:
网上对该技术很看好,感觉对BT的冲击比较大.
好象目前android 4.0 已经支持了,但是没有见过真机,所以没办法亲自体验,最近手上有两台2.3的测试机,虽然不是4.0,但是wpa_supplicant 是0.8,已经支持p2p了,就通过adb拍命令试验了下 :
1. 修改/system/etc/wifi/wpa_supplicant.conf 和 /data/misc/wifi/wpa_supplicant.conf =>
注释掉最后3行
##### wpa_supplicant configuration file template #####
update_config=1
ctrl_interface=wlan0:0
#eapol_version=1
#ap_scan=1
#fast_reauth=1
2. load wifi sdio module => insmod /system/lib/modules/wl12xx_sdio.ko
3. 启动wpa_supplicant demon
chmod 777 /data/misc/wifi/sockets
chmod 777 /data/misc/wifi/wpa_supplicant.conf
/system/bin/wpa_supplicant -d -Dnl80211 -iwlan0 -c/data/misc/wifi/wpa_supplicant.conf -B &
4. wlan up
A机 => ifconfig wlan0 192.168.3.3 netmask 255.255.255.0 up
B机 => ifconfig wlan0 192.168.3.4 netmask 255.255.255.0 up
5. chmod 777 wlan0:0
wpa_cli -i wlan0 -p /data/misc/wifi/wlan0:0
6. 通过wpa_cli 输入 p2p_find ,p2p_peers命令发现peer端及其mac 地址
输入p2p_find 输出类似下面info:
OK
<3>P2P-DEVICE-FOUND d4:94:a1:ea:93:07 p2p_dev_addr=d4:94:a1:ea:93:07 pri_dev_typ
e=0-00000000-0 name='' config_methods=0x188 dev_capab=0x21 group_capab=0x9
7. A机 => p2p_connect 44:c1:5c:55:6f:6d pbc auth go_intent=7
B机 => p2p_connect d4:94:a1:ea:93:07 pbc
到这步应该就可以连上了,通过ping 192.168.3.3 测试可以ping 通:
感觉不过瘾,再写个socket 玩下:
使用SL4A (具体见 http://blog.chinaunix.net/uid-20514606-id-3023485.html)
python 的socket 部分没什么区别,网上找了段代码,修改了下:
Server端:
点击(此处)折叠或打开
- import android
- import socket, struct
- droid=android.Android()
- droid.makeToast('Running...')
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- #droid.makeToast('1')
- sock.bind(('192.168.3.3', 8001))
- #droid.makeToast('2')
- sock.listen(5)
- #droid.makeToast('3')
- while True:
- droid.makeToast('start listen')
- connection,address = sock.accept()
- droid.makeToast('accept connect')
- try:
- connection.settimeout(5)
- buf = connection.recv(1024)
- if buf == '1':
- #droid.makeToast('someone pick wifi direct')
- connection.send('welcome to server!')
- else:
- connection.send('please go out!')
- connection.close()
- return
- except socket.timeout:
- droid.makeToast('time out')
- connection.close()
Client 端:
点击(此处)折叠或打开
- import android
- import socket, struct
- import time
- droid=android.Android()
- droid.makeToast('Running...')
- droid.dialogCreateAlert("wifi direct test")
- droid.dialogSetItems(['pick server', 'close server'])
- droid.dialogShow()
- response = droid.dialogGetResponse().result
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- ipaddr = '192.168.3.3'
- droid.dialogDismiss()
- if response.has_key("item"):
- result=response["item"]
- print result
- if result==0:
- #sock.connect(('192.168.3.3', 8001))
- sock.connect((ipaddr, 8001))
- #droid.makeToast('2')
- time.sleep(2)
- sock.send('1')
- buf=sock.recv(1024)
- droid.makeToast(buf)
-
- elif result==1:
- #sock.connect(('192.168.3.3', 8001))
- sock.connect((ipaddr, 8001))
- #droid.makeToast('2')
- time.sleep(2)
- sock.send('2')
- buf=sock.recv(1024)
- droid.makeToast(buf)
- else:
- print "Unknown response=",response
- droid.makeToast('close client')
- sock.close()
A机执行server.py, B机执行client.py 出现alter dialog,点 pick server ,可以收到A机发来的:
“someone pick wifi direct”
点close server ,关闭A机server 程序
测试正常
最后提几个wifi direct 的手机应用:
1. wifi 对讲机
2. wifi camera
3. 代替BT 应用
4. wifi out door 输入设备,远程控制
5. wifi out door 资料同步
等等,结束