Log.e("Fieldname",allFields[i].getName());
}
}catch(SecurityExceptione){
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}catch(IllegalArgumentExceptione){
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
packagecom.testReflect;
importjava.util.ArrayList;
importjava.util.List;
importandroid.app.Activity;
importandroid.bluetooth.BluetoothAdapter;
importandroid.bluetooth.BluetoothDevice;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.IntentFilter;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.ArrayAdapter;
importandroid.widget.Button;
importandroid.widget.ListView;
importandroid.widget.Toast;
publicclasstestReflectextendsActivity{
ButtonbtnSearch,btnShow;
ListViewlvBTDevices;
ArrayAdapteradtDevices;
ListlstDevices=newArrayList();
BluetoothDevicebtDevice;
BluetoothAdapterbtAdapt;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSearch=(Button)this.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(newClickEvent());
btnShow=(Button)this.findViewById(R.id.btnShow);
btnShow.setOnClickListener(newClickEvent());
lvBTDevices=(ListView)this.findViewById(R.id.ListView01);
adtDevices=newArrayAdapter(testReflect.this,
android.R.layout.simple_list_item_1,lstDevices);
lvBTDevices.setAdapter(adtDevices);
lvBTDevices.setOnItemClickListener(newItemClickEvent());
btAdapt=BluetoothAdapter.getDefaultAdapter();//初始化本机蓝牙功能
if(btAdapt.getState()==BluetoothAdapter.STATE_OFF)//开蓝牙
btAdapt.enable();
//注册Receiver来获取蓝牙设备相关的结果
IntentFilterintent=newIntentFilter();
intent.addAction(BluetoothDevice.ACTION_FOUND);
intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(searchDevices,intent);
}
privateBroadcastReceiversearchDevices=newBroadcastReceiver(){
publicvoidonReceive(Contextcontext,Intentintent){
Stringaction=intent.getAction();
Bundleb=intent.getExtras();
Object[]lstName=b.keySet().toArray();
//显示所有收到的消息及其细节
for(inti=0;iStringkeyName=lstName[i].toString();
Log.e(keyName,String.valueOf(b.get(keyName)));
}
//搜索设备时,取得设备的MAC地址
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevicedevice=intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(device.getBondState()==BluetoothDevice.BOND_NONE){
Stringstr="未配对|"+device.getName()+"|"+device.getAddress();
lstDevices.add(str);//获取设备名称和mac地址
adtDevices.notifyDataSetChanged();
}
}
}
};
classItemClickEventimplementsAdapterView.OnItemClickListener{
@Override
publicvoidonItemClick(AdapterViewarg0,Viewarg1,intarg2,
longarg3){
btAdapt.cancelDiscovery();
Stringstr=lstDevices.get(arg2);
String[]values=str.split("//|");
Stringaddress=values[2];
btDevice=btAdapt.getRemoteDevice(address);
try{
if(values[0].equals("未配对"))
{
Toast.makeText(testReflect.this,"由未配对转为已配对",500).show();
ClsUtils.createBond(btDevice.getClass(),btDevice);
}
elseif(values[0].equals("已配对"))
{
Toast.makeText(testReflect.this,"由已配对转为未配对",500).show();
ClsUtils.removeBond(btDevice.getClass(),btDevice);
}
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
/**
*按键处理
*@authorGV
*
*/
classClickEventimplementsView.OnClickListener{
@Override
publicvoidonClick(Viewv){
if(v==btnSearch){//搜索附近的蓝牙设备
lstDevices.clear();
Object[]lstDevice=btAdapt.getBondedDevices().toArray();
for(inti=0;iBluetoothDevicedevice=(BluetoothDevice)lstDevice[i];
Stringstr="已配对|"+device.getName()+"|"+device.getAddress();
lstDevices.add(str);//获取设备名称和mac地址
adtDevices.notifyDataSetChanged();
}
//开始搜索
setTitle("本机蓝牙地址:
"+btAdapt.getAddress());
btAdapt.startDiscovery();
}
elseif(v==btnShow){//显示BluetoothDevice的所有方法和常量,包括隐藏API
ClsUtils.printAllInform(btDevice.getClass());
}
}
}
}
本文程序演示了以下功能:
1.所有来电自动接听;
2.所有来电自动挂断;
3.开启/关闭Radio;
4.开启/关闭数据连接(WAPorNET的连接)。
调用TelephonyManager的隐藏API是先参考Framework的/base/telephony/java/com/android/internal/telephony/ITelephony.aidl,然后自己实现一个ITelephony.aidl,最后在TelephonyManager中通过反射机制实例化自定义的ITelephony,实例化之后就可以调用ITelephony里面的函数了(维信科技提供)。
本文程序需要在AndroidManifest.xml添加以下两行代码,以获得权限:
name="android.permission.CALL_PHONE"/>
name="android.permission.MODIFY_PH