Android提高第九篇之蓝牙传感应用Word文档下载推荐.docx
- 文档编号:18332972
- 上传时间:2022-12-15
- 格式:DOCX
- 页数:16
- 大小:20.24KB
Android提高第九篇之蓝牙传感应用Word文档下载推荐.docx
《Android提高第九篇之蓝牙传感应用Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Android提高第九篇之蓝牙传感应用Word文档下载推荐.docx(16页珍藏版)》请在冰豆网上搜索。
i<
lstName.length;
i++){
StringkeyName=lstName[i].toString();
Log.e(keyName,String.valueOf(b.get(keyName)));
}
探秘蓝牙隐藏API
用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出,那么如何去使用这两项功能呢(维信科技提供)?
本文利用JAVA的反射机制去调用这两项功能对应的函数:
createBond和removeBond,具体的发掘和实现步骤如下:
1.使用Git工具下载platform/packages/apps/Settings.git,在Setting源码中查找关于建立配对和解除配对的API,知道这两个API的宿主(BluetoothDevice);
2.使用反射机制对BluetoothDevice枚举其所有方法和常量,看看是否存在:
staticpublicvoidprintAllInform(ClassclsShow){
try{
//取得所有方法
Method[]hideMethod=clsShow.getMethods();
inti=0;
for(;
hideMethod.length;
Log.e("
methodname"
hideMethod[i].getName());
//取得所有常量
Field[]allFields=clsShow.getFields();
for(i=0;
allFields.length;
Fieldname"
allFields[i].getName());
}catch(SecurityExceptione){
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}catch(IllegalArgumentExceptione){
}catch(Exceptione){
//TODOAuto-generatedcatchblock
/**
*与设备配对参考源码:
platform/packages/apps/Settings.git
*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
staticpublicbooleancreateBond(ClassbtClass,BluetoothDevicebtDevice)throwsException{
MethodcreateBondMethod=btClass.getMethod("
createBond"
);
BooleanreturnValue=(Boolean)createBondMethod.invoke(btDevice);
returnreturnValue.booleanValue();
*与设备解除配对参考源码:
staticpublicbooleanremoveBond(ClassbtClass,BluetoothDevicebtDevice)throwsException{
MethodremoveBondMethod=btClass.getMethod("
removeBond"
BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice);
<
linearlayoutxmlns:
android="
android:
orientation="
vertical"
layout_width="
fill_parent"
layout_height="
>
<
linearlayoutandroid:
id="
@+id/LinearLayout01"
wrap_content"
buttonandroid:
@+id/btnSearch"
text="
Search"
160dip"
Show"
@+id/btnShow"
@+id/LinearLayout02"
listviewandroid:
@+id/ListView01"
android:
packagecom.testReflect;
importjava.lang.reflect.Field;
importjava.lang.reflect.Method;
importandroid.bluetooth.BluetoothDevice;
importandroid.util.Log;
publicclassClsUtils{
*
*@paramclsShow
}
importjava.util.ArrayList;
importjava.util.List;
importandroid.app.Activity;
importandroid.bluetooth.BluetoothAdapter;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.IntentFilter;
importandroid.os.Bundle;
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();
//搜索设备时,取得设备的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{
publicvoidonItemClick(AdapterViewarg0,Viewarg1,intarg2,
longarg3){
btAdapt.cancelDiscovery();
Stringstr=lstDevices.get(arg2);
String[]values=str.split("
//|"
Stringaddress=values[2];
btDevice=btAdapt.getRemoteDevice(address);
if(values[0].equals("
未配对"
))
{
Toast.makeText(testReflect.this,"
由未配对转为已配对"
500).show();
ClsUtils.createBond(btDevice.getClass(),btDevice);
elseif(values[0].equals("
已配对"
{
由已配对转为未配对"
ClsUtils.removeBond(btDevice.getClass(),btDevice);
*按键处理
*@authorGV
*
classClickEventimplementsView.OnClickListener{
publicvoidonClick(Viewv){
if(v==btnSearch){//搜索附近的蓝牙设备
lstDevices.clear();
Object[]lstDevice=btAdapt.getBondedDevices().toArray();
lstDevice.length;
BluetoothDevicedevice=(BluetoothDevice)lstDevice[i];
已配对|"
//开始搜索
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添加以下两行代码,以获得权限:
uses-permissionandroid:
name="
android.permission.CALL_PHONE"
/>
android.permission.MODIFY_PH
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 提高 第九 蓝牙 传感 应用