Android提高第九篇之蓝牙传感应用.docx
- 文档编号:5351128
- 上传时间:2022-12-15
- 格式:DOCX
- 页数:16
- 大小:20.24KB
Android提高第九篇之蓝牙传感应用.docx
《Android提高第九篇之蓝牙传感应用.docx》由会员分享,可在线阅读,更多相关《Android提高第九篇之蓝牙传感应用.docx(16页珍藏版)》请在冰豆网上搜索。
Android提高第九篇之蓝牙传感应用
Android提高第九篇之蓝牙传感应用——维信科技
如果传感器本身需要包含控制电路(例如采集血氧信号需要红外和红外线交替发射),那么传感器本身就需要带一片主控IC,片内采集并输出数字信号了。
Android手机如何在不改硬件电路的前提下与这类数字传感器交互呢(维信科技提供)?
可选的通信方式就有USB和蓝牙,两种方式各有好处:
USB方式可以给传感器供电,蓝牙方式要自备电源;USB接口标准不一,蓝牙普遍支持SPP协议。
本文选择蓝牙方式做介绍,介绍Android的蓝牙API以及蓝牙客户端的用法。
在Android2.0,官方终于发布了蓝牙API(2.0以下系统的非官方的蓝牙API可以参考这里:
1.使用registerReceiver注册BroadcastReceiver来获取蓝牙状态、搜索设备等消息;
2.使用BlueAdatper的搜索;
3.在BroadcastReceiver的onReceive()里取得搜索所得的蓝牙设备信息(如名称,MAC,RSSI);
4.通过设备的MAC地址来建立一个BluetoothDevice对象;
5.由BluetoothDevice衍生出BluetoothSocket,准备SOCKET来读写设备;
6.通过BluetoothSocket的createRfcommSocketToServiceRecord()方法来选择连接的协议/服务,这里用的是SPP(UUID:
00001101-0000-1000-8000-00805F9B34FB)(维信科技提供);
7.Connect之后(如果还没配对则系统自动提示),使用BluetoothSocket的getInputStream()和getOutputStream()来读写蓝牙设备。
Stringaction=intent.getAction();
Bundleb=intent.getExtras();
Object[]lstName=b.keySet().toArray();
//显示所有收到的消息及其细节
for(inti=0;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(;i Log.e("methodname",hideMethod[i].getName()); } //取得所有常量 Field[]allFields=clsShow.getFields(); for(i=0;i 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(); } } /** *与设备配对参考源码: 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(); } /** *与设备解除配对参考源码: platform/packages/apps/Settings.git */Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java */ staticpublicbooleanremoveBond(ClassbtClass,BluetoothDevicebtDevice)throwsException{ MethodremoveBondMethod=btClass.getMethod("removeBond"); BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice); returnreturnValue.booleanValue(); } android=" android: orientation="vertical"android: layout_width="fill_parent" android: layout_height="fill_parent"> id="@+id/LinearLayout01" android: layout_height="wrap_content"android: layout_width="fill_parent"> layout_height="wrap_content"android: id="@+id/btnSearch" android: text="Search"android: layout_width="160dip"> layout_height="wrap_content" android: layout_width="160dip"android: text="Show"android: id="@+id/btnShow"> id="@+id/LinearLayout02" android: layout_width="wrap_content"android: layout_height="wrap_content"> id="@+id/ListView01"android: layout_width="fill_parent" android: layout_height="fill_parent"> packagecom.testReflect; importjava.lang.reflect.Field; importjava.lang.reflect.Method; importandroid.bluetooth.BluetoothDevice; importandroid.util.Log; publicclassClsUtils{ /** *与设备配对参考源码: 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(); } /** *与设备解除配对参考源码: platform/packages/apps/Settings.git */Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java */ staticpublicbooleanremoveBond(ClassbtClass,BluetoothDevicebtDevice)throwsException{ MethodremoveBondMethod=btClass.getMethod("removeBond"); BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice); returnreturnValue.booleanValue(); } /** * *@paramclsShow */ staticpublicvoidprintAllInform(ClassclsShow){ try{ //取得所有方法 Method[]hideMethod=clsShow.getMethods(); inti=0; for(;i Log.e("methodname",hideMethod[i].getName()); } //取得所有常量 Field[]allFields=clsShow.getFields(); for(i=0;i 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;i StringkeyName=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;i BluetoothDevicedevice=(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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 提高 第九 蓝牙 传感 应用
![提示](https://static.bdocx.com/images/bang_tan.gif)