龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 移动开发 > Android开发 >

Android提高之蓝牙隐藏API探秘(2)

时间:2014-08-10 02:36来源:网络整理 作者:网络 点击:
分享到:
工具类ClsUtils.java源码如下: package com.testReflect;import java.lang.reflect.Field;import java.lang.reflect.Method;import android.bluetooth.BluetoothDevice;import android.util.Log;public cla

工具类ClsUtils.java源码如下:

package com.testReflect;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.bluetooth.BluetoothDevice;
import android.util.Log;
public class ClsUtils {
 /**
 * 与设备配对 参考源码:platform/packages/apps/Settings.git
 * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
 */
 static public boolean createBond(Class btClass,BluetoothDevice btDevice) throws Exception {
 Method createBondMethod = btClass.getMethod("createBond");
 Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
 return returnValue.booleanValue();
 }
 /**
 * 与设备解除配对 参考源码:platform/packages/apps/Settings.git
 * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
 */
 static public boolean removeBond(Class btClass,BluetoothDevice btDevice) throws Exception {
 Method removeBondMethod = btClass.getMethod("removeBond");
 Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
 return returnValue.booleanValue();
 }
 /**
 * 
 * @param clsShow
 */
 static public void printAllInform(Class clsShow) {
 try {
  // 取得所有方法
  Method[] hideMethod = clsShow.getMethods();
  int i = 0;
  for (; i < hideMethod.length; i++) {
  Log.e("method name", hideMethod[i].getName());
  }
  // 取得所有常量
  Field[] allFields = clsShow.getFields();
  for (i = 0; i < allFields.length; i++) {
  Log.e("Field name", allFields[i].getName());
  }
 } catch (SecurityException e) {
  // throw new RuntimeException(e.getMessage());
  e.printStackTrace();
 } catch (IllegalArgumentException e) {
  // throw new RuntimeException(e.getMessage());
  e.printStackTrace();
 } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }
}

主程序testReflect.java的源码如下:

package com.testReflect;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class testReflect extends Activity {
 Button btnSearch, btnShow;
 ListView lvBTDevices;
 ArrayAdapter<String> adtDevices;
 List<String> lstDevices = new ArrayList<String>();
 BluetoothDevice btDevice;
 BluetoothAdapter btAdapt;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 btnSearch = (Button) this.findViewById(R.id.btnSearch);
 btnSearch.setOnClickListener(new ClickEvent());
 btnShow = (Button) this.findViewById(R.id.btnShow);
 btnShow.setOnClickListener(new ClickEvent());
 lvBTDevices = (ListView) this.findViewById(R.id.ListView01);
 adtDevices = new ArrayAdapter<String>(testReflect.this,
  android.R.layout.simple_list_item_1, lstDevices);
 lvBTDevices.setAdapter(adtDevices);
 lvBTDevices.setOnItemClickListener(new ItemClickEvent());

 btAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能
 if (btAdapt.getState() == BluetoothAdapter.STATE_OFF)// 开蓝牙
  btAdapt.enable();
 // 注册Receiver来获取蓝牙设备相关的结果
 IntentFilter intent = new IntentFilter();
 intent.addAction(BluetoothDevice.ACTION_FOUND);
 intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
 registerReceiver(searchDevices, intent);

 }
 private BroadcastReceiver searchDevices = new BroadcastReceiver() {
 public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  Bundle b = intent.getExtras();
  Object[] lstName = b.keySet().toArray();
  // 显示所有收到的消息及其细节
  for (int i = 0; i < lstName.length; i++) {
  String keyName = lstName[i].toString();
  Log.e(keyName, String.valueOf(b.get(keyName)));
  }
  // 搜索设备时,取得设备的MAC地址
  if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  BluetoothDevice device = intent
   .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  if (device.getBondState() == BluetoothDevice.BOND_NONE) {
   String str = "未配对|" + device.getName() + "|" + device.getAddress();
   lstDevices.add(str); // 获取设备名称和mac地址
   adtDevices.notifyDataSetChanged();
  }
  }
 }
 };
 class ItemClickEvent implements AdapterView.OnItemClickListener {

 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  long arg3) {
  btAdapt.cancelDiscovery();
  String str = lstDevices.get(arg2);
  String[] values = str.split("//|");
  String address=values[2];
  btDevice = btAdapt.getRemoteDevice(address);
  try {
  if(values[0].equals("未配对"))
  { 
   Toast.makeText(testReflect.this, "由未配对转为已配对", 500).show();
   ClsUtils.createBond(btDevice.getClass(), btDevice);
  }
  else if(values[0].equals("已配对"))
  {
   Toast.makeText(testReflect.this, "由已配对转为未配对", 500).show();
   ClsUtils.removeBond(btDevice.getClass(), btDevice);
  }
  } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
 }
 }
 /**
 * 按键处理
 * @author GV
 *
 */
 class ClickEvent implements View.OnClickListener {
 @Override
 public void onClick(View v) {
  if (v == btnSearch) {//搜索附近的蓝牙设备
  lstDevices.clear();
  Object[] lstDevice = btAdapt.getBondedDevices().toArray();
  for (int i = 0; i < lstDevice.length; i++) {
   BluetoothDevice device=(BluetoothDevice)lstDevice[i];
   String str = "已配对|" + device.getName() + "|" + device.getAddress();
   lstDevices.add(str); // 获取设备名称和mac地址
   adtDevices.notifyDataSetChanged();
  }
  // 开始搜索
  setTitle("本机蓝牙地址:" + btAdapt.getAddress());
  btAdapt.startDiscovery();
  }
  else if(v==btnShow){//显示BluetoothDevice的所有方法和常量,包括隐藏API
  ClsUtils.printAllInform(btDevice.getClass());
  }
 }
 }
}

希望本文实例能够对大家进行Android程序开发有一定的借鉴帮助作用。

精彩图集

赞助商链接