导航:首页 > 操作系统 > androidwifiservice

androidwifiservice

发布时间:2023-05-25 11:03:15

⑴ 如何从C++代码直接访问android framework层的WifiService

说到底,java层的service就是就C++层的binder的封装,所以从原理上来讲通过C++代码直接访问android framework层唯洞的service是完全可能的,这篇文章以访问WifiService为例,讲解如何去实现这个功能。

费话少说,直接上代码:

WifiTest.cpp

[cpp] view plain在CODE上查看代码片派生到我的代码片
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>

#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>

#include <utils/String16.h>
//#include <utils/Vector.h>
#include <槐拦utils/List.h>
#include <utils/PropertyMap.h>

using namespace android;

#define WIFI_SERVICE "wifi"
const String16 WIFI_DESCRIPTOR("android.net.wifi.IWifiManager");

class ScanResult
{

private:
ScanResult& operator=(const ScanResult& o);

public:
ScanResult(const ScanResult& o):
mSsid(o.mSsid),mBssid(o.mBssid),mCaps(o.mCaps),mLevel(o.mLevel),mFrequency(o.mFrequency),mTimestamp(o.mTimestamp)
{
}

ScanResult(String8 ssid,String16 bssid,String16 caps,int level,int freq,int64_t timeStamp):
mSsid(ssid),mBssid(bssid),mCaps(caps),mLevel(level),mFrequency(freq),mTimestamp(timeStamp)
{
}
void mp()
{
char temp[130];
int size;
memset(temp,0,sizeof(temp));
printf("ssid %s \n",mSsid.string());

size = mBssid.size();
if(size > sizeof(temp)/2 - 1)
{
size = sizeof(temp)/2 - 1;
}
utf16_to_utf8(mBssid.string(), size, temp);
printf("Bssid %s \n"铅山胡,temp);

size = mCaps.size();
if(size > sizeof(temp)/2 - 1)
{
size = sizeof(temp)/2 - 1;
}
utf16_to_utf8(mCaps.string(), size, temp);
printf("ssid %s \n",temp);

printf("level %d \n",mLevel);
printf("freq %d \n",mFrequency);
printf("freq %ld \n",mTimestamp);
}
private:
String8 mSsid;
String16 mBssid;
String16 mCaps;
int mLevel;
int mFrequency;
int64_t mTimestamp;
};

class IWifiService: public android::IInterface {
public:
DECLARE_META_INTERFACE(WifiService)

virtual void startScan(int forceActive) = 0;
virtual int getScanResults(List<ScanResult> &list) = 0;
virtual bool setWifiEnabled(bool enable) = 0;
};

class BpWifiService: public android::BpInterface<IWifiService>
{
enum
{
FIRST_CALL_TRANSACTION = 1,
TRANSACTION_getConfiguredNetworks = (android::IBinder::FIRST_CALL_TRANSACTION + 0),
TRANSACTION_addOrUpdateNetwork = (android::IBinder::FIRST_CALL_TRANSACTION + 1),
TRANSACTION_removeNetwork = (android::IBinder::FIRST_CALL_TRANSACTION + 2),
TRANSACTION_enableNetwork = (android::IBinder::FIRST_CALL_TRANSACTION + 3),
TRANSACTION_disableNetwork = (android::IBinder::FIRST_CALL_TRANSACTION + 4),
TRANSACTION_pingSupplicant = (android::IBinder::FIRST_CALL_TRANSACTION + 5),
TRANSACTION_startScan = (android::IBinder::FIRST_CALL_TRANSACTION + 6),
TRANSACTION_getScanResults = (android::IBinder::FIRST_CALL_TRANSACTION + 7),
TRANSACTION_disconnect = (android::IBinder::FIRST_CALL_TRANSACTION + 8),
TRANSACTION_reconnect = (android::IBinder::FIRST_CALL_TRANSACTION + 9),
TRANSACTION_reassociate = (android::IBinder::FIRST_CALL_TRANSACTION + 10),
TRANSACTION_getConnectionInfo = (android::IBinder::FIRST_CALL_TRANSACTION + 11),
TRANSACTION_setWifiEnabled = (android::IBinder::FIRST_CALL_TRANSACTION + 12),
TRANSACTION_getWifiEnabledState = (android::IBinder::FIRST_CALL_TRANSACTION + 13),
TRANSACTION_setCountryCode = (android::IBinder::FIRST_CALL_TRANSACTION + 14),
TRANSACTION_setFrequencyBand = (android::IBinder::FIRST_CALL_TRANSACTION + 15),
TRANSACTION_getFrequencyBand = (android::IBinder::FIRST_CALL_TRANSACTION + 16),
TRANSACTION_isDualBandSupported = (android::IBinder::FIRST_CALL_TRANSACTION + 17),
TRANSACTION_saveConfiguration = (android::IBinder::FIRST_CALL_TRANSACTION + 18),
TRANSACTION_getDhcpInfo = (android::IBinder::FIRST_CALL_TRANSACTION + 19),
TRANSACTION_acquireWifiLock = (android::IBinder::FIRST_CALL_TRANSACTION + 20),
TRANSACTION_updateWifiLockWorkSource = (android::IBinder::FIRST_CALL_TRANSACTION + 21),
TRANSACTION_releaseWifiLock = (android::IBinder::FIRST_CALL_TRANSACTION + 22),
TRANSACTION_initializeMulticastFiltering = (android::IBinder::FIRST_CALL_TRANSACTION + 23),
TRANSACTION_isMulticastEnabled = (android::IBinder::FIRST_CALL_TRANSACTION + 24),
TRANSACTION_acquireMulticastLock = (android::IBinder::FIRST_CALL_TRANSACTION + 25),
TRANSACTION_releaseMulticastLock = (android::IBinder::FIRST_CALL_TRANSACTION + 26),
TRANSACTION_setWifiApEnabled = (android::IBinder::FIRST_CALL_TRANSACTION + 27),
TRANSACTION_getWifiApEnabledState = (android::IBinder::FIRST_CALL_TRANSACTION + 28),
TRANSACTION_getWifiApConfiguration = (android::IBinder::FIRST_CALL_TRANSACTION + 29),
TRANSACTION_setWifiApConfiguration = (android::IBinder::FIRST_CALL_TRANSACTION + 30),
TRANSACTION_startWifi = (android::IBinder::FIRST_CALL_TRANSACTION + 31),
TRANSACTION_stopWifi = (android::IBinder::FIRST_CALL_TRANSACTION + 32),
TRANSACTION_addToBlacklist = (android::IBinder::FIRST_CALL_TRANSACTION + 33),
TRANSACTION_clearBlacklist = (android::IBinder::FIRST_CALL_TRANSACTION + 34),
TRANSACTION_getWifiServiceMessenger = (android::IBinder::FIRST_CALL_TRANSACTION + 35),
TRANSACTION_getWifiStateMachineMessenger = (android::IBinder::FIRST_CALL_TRANSACTION + 36),
TRANSACTION_getConfigFile = (android::IBinder::FIRST_CALL_TRANSACTION + 37),
TRANSACTION_captivePortalCheckComplete = (android::IBinder::FIRST_CALL_TRANSACTION + 38),
};
public:
BpWifiService(const android::sp<android::IBinder>& impl): android::BpInterface<IWifiService>(impl)
{
}

void startScan(int forceActive)
{
android::Parcel data, reply;
data.writeInterfaceToken(WIFI_DESCRIPTOR);
if (forceActive)
{
data.writeInt32(1);
}
else
{
data.writeInt32(0);
}
remote()->transact(TRANSACTION_startScan, data, &reply, 0);
}

virtual int getScanResults(List<ScanResult> &list)
{
Parcel data, reply;
data.writeInterfaceToken(WIFI_DESCRIPTOR);
remote()->transact(TRANSACTION_getScanResults, data, &reply, 0);
if (0 != reply.readExceptionCode())
{
return 0;
}
int count = reply.readInt32();
for (int i=0;i<count;i++)
{
String8 ssid;
int res = reply.readInt32();
if (res != 0)
{
reply.readInt32();
reply.readInt32();
int length = reply.readInt32();
ssid = String8((const char*)reply.readInplace(length),length);
}

// utf16_to_utf8(const char16_t* src, size_t src_len, char* dst);

String16 BSSID = reply.readString16();
String16 caps = reply.readString16();
int level = reply.readInt32();
int frequency = reply.readInt32();
int64_t timestamp = reply.readInt64();
ScanResult result(ssid,BSSID,caps,level,frequency,timestamp);
list.push_back(result);
}
return count;
}

bool setWifiEnabled(bool enable)
{
Parcel data, reply;
data.writeInterfaceToken(WIFI_DESCRIPTOR);

if(enable)
data.writeInt32(1);
else
data.writeInt32(0);
remote()->transact(TRANSACTION_setWifiEnabled, data,&reply,0);
reply.readExceptionCode();
return 0!=reply.readInt32();
}

};

IMPLEMENT_META_INTERFACE(WifiService, WIFI_DESCRIPTOR)

int main(int argc, char *argv[])
{

android::sp<android::IServiceManager> sm = android::defaultServiceManager();
android::sp<android::IBinder> binder;
android::sp<IWifiService> wifi;

binder = sm->getService(android::String16(WIFI_SERVICE));

if (binder == 0)
{
return 1;
}

wifi = android::interface_cast<IWifiService>(binder);

wifi->setWifiEnabled(true);
printf("+++++scan start");
wifi->startScan(1);

for(int i=0;i<10;i++)
{
usleep(1*1000*1000);
List<ScanResult> list;
wifi->getScanResults(list);
if(list.size() > 0)
{
for(List<ScanResult>::iterator it = list.begin();it != list.end();++it)
{
(*it).mp();
}
break;
}
}

return(0);
}

基本的思路很简单:
先通过:
android::defaultServiceManager()->getService(android::String16(WIFI_SERVICE));
获取binder接口,再通过Parcel读与binder,具体的实现可以参考IWifiManager.java的代码。

编译后运行以上的代码可以得到类似以下的输出:

ssid wifitest
Bssid b8:55:10:84:13:57
ssid [WPA-PSK-CCMP][WPA2-PSK-CCMP][WPS][ESS]
level -55
freq 2447
time stamp 1073922473
ssid test
Bssid 08:bd:43:c3:a9:96
ssid [WPA2-PSK-CCMP][WPS][ESS]
level -66
freq 2462
time stamp 1073922473

⑵ Android 怎样默认打开便携式WIFI热点 M

目前判断Wifi开机后是否需要的自动开启或燃实现是在wifiService.java的checkAndStartWifi() 函数里,类似的,可以参照wifi在wifiService.java里添加一下如下函数:
public void checkAndStartSoftAp() {
mWifiStateMachine.autoConnectInit();
boolean isAirplaneModeOn = isAirplaneModeOn();
int persistedWifiState = getPersistedWifiState();
SXlog.d(TAG, isAirplaneModeOn: + isAirplaneModeOn + , getPersistedWifiState: + persistedWifiState);
mAirplaneModeOn.set(isAirplaneModeOn);
mPersistWifiState.set(persistedWifiState);

if (mWifiStateMachine.hasCustomizedAutoConnect() && mAirplaneModeOn.get()) { //AirplaneMode下不打开SoftAp,客户也可以看情况腔腊不限制
SXlog.i(TAG, Don't enable softAp when airplane mode is on for customization.);
} else {
Slog.i(TAG, WifiService starting up with softAp);

setWifiApEnabled(null, true);
} } 然后,在systemServer.java里搜索wifi.checkAndStartWifi(), 可以找见它在衫圆虚ServerThread的run里,将wifi.checkAndStartWifi()替换成wifi.checkAndStartSoftAp(),然后,编译测试。

⑶ 在android上查看wifi状态,代码如下:

不知道你的程序报的是什么错,给你一个例子吧,不过方法和你的有点差别:
public class WifiAdmin {
/**WiFi Manager**/
private WifiManager wifimanager;
/**WiFi Information**/
private WifiInfo wifiinfo;
/**Activity NetWork List**/
private List<ScanResult> netlist;
/**Configured NetWork List**/
private List<WifiConfiguration> connectedlist;
/**WiFi Lock**/
private WifiLock wifilock;

public WifiAdmin(Context context) {
wifimanager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}

/**
* WiFi Operate
* */
/**Open WiFi**/
public void openWifi() {
if (!wifimanager.isWifiEnabled()) {
wifimanager.setWifiEnabled(true);
}
}
/**Close WiFi**/
public void closeWifi() {
if (wifimanager.isWifiEnabled()) {
wifimanager.setWifiEnabled(false);
}
}
/**WiFi State**/
public int checkState() {
return wifimanager.getWifiState();
}

/**
* WiFiLock Operate
* */
/**Create WiFiLock**/
public void creatWifiLock() {
wifilock = wifimanager.createWifiLock("Test");
}
/**Lock WiFi**/
public void acquireWifiLock() {
wifilock.acquire();
}
/**Clear WiFiLock**/
public void releaseWifiLock() {
if (wifilock.isHeld()) {
wifilock.release();
}
}
/**
* WiFiInfo Operate
* */
/**Get WiFiInfo**/
public WifiInfo getWifiInfo(){
wifiinfo = wifimanager.getConnectionInfo();
return wifiinfo;
}

/**
* NetWork Operate
* */
/**Scan**/
public void startScan() {
wifimanager.startScan();
netlist = wifimanager.getScanResults();
connectedlist = wifimanager.getConfiguredNetworks();
}
/**Get Configured NetWork**/
public List<WifiConfiguration> getConfiguration() {
return connectedlist;
}
/**Get Activity NetWork**/
public List<ScanResult> getWifiList() {
return netlist;
}
/**Connect a NetWork**/
public void connectConfiguration(int index) {
if (index < 0 || index > connectedlist.size()) {
return;
}
wifimanager.enableNetwork(connectedlist.get(index).networkId,true);
}
/**Add a NetWork**/
public void addNetwork(WifiConfiguration wcg) {
int wcgID = wifimanager.addNetwork(wcg);
wifimanager.enableNetwork(wcgID, true);
}
/**DisConnect a NetWork**/
public void disconnectWifi(int netId) {
wifimanager.disableNetwork(netId);
wifimanager.disconnect();
}
}

⑷ Android中怎么打开Wifi的组播功能

打开Android中Wifi的组播功能。主要有以下几个步骤:
在Manifest文件中加入:android.permission.CHANGE_WIFI_MULTICAST_STATE,这个权限
获取到MulticastLock对象,这个对象不能直接实例化,要通过WifiManager间接得到,工厂模式
调用MulticastLock对象的acquire方法,获取到组播锁
相应的,用完组播,为了不浪费电力,要调用MulticastLock的release方法释放锁
下面写了个简单示例,通过组播发现服务器。
Activity写的比较简单
public class MulticastDemoActivity extends Activity {
MulticastLock multicastLock;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
allowMulticast();
try {
NetUtil.findServerIpAddress();
} catch (IOException e) {
throw new RuntimeException(e);
}
Log.d("multicast.demo", "find ip ok.");
multicastLock.release();
}
private void allowMulticast(){
WifiManager wifiManager=(WifiManager)getSystemService(Context.WIFI_SERVICE);
multicastLock=wifiManager.createMulticastLock("multicast.test");
multicastLock.acquire();
}
}
在Activity中打开和释放组播锁。使用组播发送报文和接收ip地址信息的工具类代码:
public class NetUtil {
private static final String TAG="Net.Utils";
private static final int MULTICAST_PORT=5111;
private static final String GROUP_IP="224.5.0.7";
private static byte[] sendData;
static{
sendData=new byte[4];
// 0xEE78F1FB
sendData[3] = (byte) 0xEE;
sendData[2] = (byte) 0×78;
sendData[1] = (byte) 0xF1;
sendData[0] = (byte) 0xFB;
}
public static String findServerIpAddress() throws IOException{
String ip=null;
MulticastSocket multicastSocket=new MulticastSocket(MULTICAST_PORT);
multicastSocket.setLoopbackMode(true);
InetAddress group = InetAddress.getByName(GROUP_IP);
multicastSocket.joinGroup(group);
DatagramPacket packet=new DatagramPacket(sendData, sendData.length,group,MULTICAST_PORT);
for(;;){
multicastSocket.send(packet);
Log.d(TAG,">>>send packet ok");
byte[] receiveData=new byte[256];
packet=new DatagramPacket(receiveData, receiveData.length);
multicastSocket.receive(packet);
String packetIpAddress=packet.getAddress().toString();
packetIpAddress=packetIpAddress.substring(1, packetIpAddress.length());
Log.d(TAG,"packet ip address: "+packetIpAddress);
StringBuilder packetContent=new StringBuilder();
for(int i=0;i<receiveData.length;i++){
if(receiveData[i]==0){
break;
}
packetContent.append((char)receiveData[i]);
}
ip=packetContent.toString();
Log.d(TAG,"packet content ip is: "+ip);
if(ip.equals(packetIpAddress)){
Log.d(TAG,"find server ip address: "+ip);
break;
}else{
Log.d(TAG,"not find server ip address, continue …");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
return ip;
}
}

⑸ 如何从C++代码直接访问android framework层的WifiService

在写android代码时,我们基本不会出现new MyActivity(), new MyService()等等这样的代码,要知道android app的编写语言是java,java的特点是什么:“一切皆对象”。那么问题来了,我们写的activity,service等什么时候被new出来了的呢,它们是怎样被new出来的,带着这个问题我们继续看下去。 android framework层架构采用了ioc方式,程序员实现的activity,service等式在android的框架层new出来的,程序猿在完成一个activity后都需要在mainfest注册。然后android framework层利用反射的方式动态的创建其对象。所以android采用这样的方式将控制权全部掌握在框架层,客户端清则程序员只需要按照其方式实现就行。 但是紧接着新的问题又会出现,既然我们客户端程序员不需要new 组件对象,也就是说我们的各个组件间是相互独立的。然后新的问题产生各组件之间的交互该如何处理呢,android framework为我们想到了这个问题,所以Intent信使产生了,android设计者通过Intent信使实现各个组件间的交互,不得不说框架的设计确实很好,这里膜拜大神。 销键android的框架层牢牢掌控住客户端的,包括对象的创建调用等。其中运用的很多好的设计模式以及方法值得我们学习。 答斗棚Service 有个onStartCommand()方法,这个方法是在每次调用 startService()方法时,都会调用的一个方法,如果要实现Service一直运行在后台,那么就可以在onStartCommand方法中做一些处理。另外,这个方法的返回值是一个Int类型的。从Android官方文档中,我们知道onStartCommand有4种返回值: START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。START_NOT_STICKY:“非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。START_REDELIVER_INTENT:重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。

⑹ 如何在android 中获取Wifi设备的IP地址

1)第一步获取WifiManager
Get an instance of this class by calling Context.getSystemService(Context.WIFI_SERVICE).
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
通过wifiManager实例可以获取:
getConfigureNetwork(): wifi的配置网络接口的属性
getConnectionInfo(): 当前wifi 连接的信息
getDhcpInfo(): 返回DHCP信息
getScanResults():返回可接入点的列表
getWifiState():返回wifi的连接状态

⑺ android 代码怎么实时拿到连接当前wifi热点的设备信息

WifiManager wifi_service = (WifiManager) getSystemService(WIFI_SERVICE); WifiInfo wifiinfo = wifi_servicemask + "乱者"); Log.v("dns1", dhcpinfo.dns1 + ""); Log.v("dns1", dhcpinfo.dns2 + ""); Log.v("serverAddress"宏配, dhcpinfo.serverAddress + ""蔽陪指);

⑻ android应用有一个开机自动联网的service,但手机开机时没有连上WiFi,service会不会中断

如果你的呢移动数据是开启的,是不会断的,而在您连好wifi之后,该服务会自动切换为WiFi模式

⑼ android开发中如果我想代码实现打开wifi热点如何实现

1·申请权限:
android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.WAKE_LOCK
2·获取WifiManager
wifiManager
=
(WifiManager)
this.getSystemService(Context.WIFI_SERVICE);
3·开启、关闭wifi
if
(wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
}
else
{
wifiManager.setWifiEnabled(true);
}
4·注意
如果遇到force-close,
选wait即可,
因为启动wifi需要几秒钟,
UI如果5妙钟还没反映的话,
系统会给你这个force
close
exception
PS:我以前做过设计读取系统硬件信息的时候用敬敬过启氏,悄稿散但是很长时间没用了,这段注释是从网上来的,希望能帮到你。

⑽ android 2.3 通过wifi调用webservice 很慢,怎么解决

跟踪发现是Android API里的HttpUrlConnection这个类getOutputStream和getInputStream方法很慢,不是ksoap2的问题,冤枉他了。

解决的途径是慧激
运升1. 不启用无线路由的DHCP服务,使用静态IP,服务器都使用静态IP连路由。
2. 如何在平板上设置静态IP,点设置 >> 无线和网络 >旁碧老> Wi-Fi设置 >> 点击菜单按钮(屏幕下边三个按钮最左边一个) >> 高级 >> 使用静态IP 勾上 >> 设置 IP地址,网关,网络掩码,域名1(一定记得输入0.0.0.0),最后点击菜单按钮选保存。

只要保证平板使用静态IP连上无线路由,并且域名为0.0.0.0,安卓就不会再花20S+通过DNS解释什么域名了。
我在多台平板上测试多没有问题。

阅读全文

与androidwifiservice相关的资料

热点内容
单片机编程取反 浏览:894
51单片机课程设计课题 浏览:897
手机淘宝登录怎么加密码 浏览:484
linux快捷方式图标 浏览:37
阳光车险的app叫什么名字 浏览:461
购买单片机的器件时需要给商家啥 浏览:534
并行编译技术的发展 浏览:549
阿里云服务器安装管理 浏览:550
java手机开发教程 浏览:674
我的世界怎么删除服务器数据 浏览:671
linux内存子系统 浏览:972
加密思维币 浏览:690
魅族访客文件夹 浏览:52
添加的文件夹怎么找 浏览:617
程序员涉黄 浏览:700
maven编译resources下的js 浏览:521
ubuntu文件移动命令 浏览:229
安卓i怎么查找苹果手机 浏览:951
云服务器宕机概率 浏览:232
在线买药用什么app知乎 浏览:815