前面文章讲解了Android的蓝牙基本用法,本文讲得深入些,探讨下蓝牙方面的隐藏API。用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出,那么如何去使用这两项功能呢?本文利用JAVA的反射机制去调用这两项功能对应的函数:createBond和removeBond,具体的发掘和实现步骤如下:

1.使用Git工具下载platform/packages/apps/Settings.git,在Setting源码中查找关于建立配对和解除配对的API,知道这两个API的宿主(BluetoothDevice);

2.使用反射机制对BluetoothDevice枚举其所有方法和常量,看看是否存在:

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();

}

}

结果如下:

11-29 09:19:12.012: method name(452): cancelBondProcess

11-29 09:19:12.020: method name(452): cancelPairingUserInput

11-29 09:19:12.020: method name(452): createBond11-29 09:19:12.020: method name(452): createInsecureRfcommSocket

11-29 09:19:12.027: method name(452): createRfcommSocket

11-29 09:19:12.027: method name(452): createRfcommSocketToServiceRecord

11-29 09:19:12.027: method name(452): createScoSocket

11-29 09:19:12.027: method name(452): describeContents

11-29 09:19:12.035: method name(452): equals

11-29 09:19:12.035: method name(452): fetchUuidsWithSdp

11-29 09:19:12.035: method name(452): getAddress

11-29 09:19:12.035: method name(452): getBluetoothClass

11-29 09:19:12.043: method name(452): getBondState

11-29 09:19:12.043: method name(452): getName

11-29 09:19:12.043: method name(452): getServiceChannel

11-29 09:19:12.043: method name(452): getTrustState

11-29 09:19:12.043: method name(452): getUuids

11-29 09:19:12.043: method name(452): hashCode

11-29 09:19:12.043: method name(452): isBluetoothDock

11-29 09:19:12.043: method name(452): removeBond11-29 09:19:12.043: method name(452): setPairingConfirmation

11-29 09:19:12.043: method name(452): setPasskey

11-29 09:19:12.043: method name(452): setPin

11-29 09:19:12.043: method name(452): setTrust

11-29 09:19:12.043: method name(452): toString

11-29 09:19:12.043: method name(452): writeToParcel

11-29 09:19:12.043: method name(452): convertPinToBytes

11-29 09:19:12.043: method name(452): getClass

11-29 09:19:12.043: method name(452): notify

11-29 09:19:12.043: method name(452): notifyAll

11-29 09:19:12.043: method name(452): wait

11-29 09:19:12.051: method name(452): wait

11-29 09:19:12.051: method name(452): wait

3.如果枚举发现API存在(SDK却隐藏),则自己实现调用方法:

/**

* 与设备配对 参考源码: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();

}

此处注意:SDK之所以不给出隐藏的API肯定有其原因,也许是出于安全性或者是后续版本兼容性的考虑,因此不能保证隐藏API能在所有Android平台上很好地运行。

本文程序运行效果如下图所示:

main.xml源码如下:

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_height="wrap_content" android:layout_width="fill_parent">

android:text="Search" android:layout_width="160dip">

android:layout_width="160dip" android:text="Show" android:id="@+id/btnShow">

android:layout_width="wrap_content" android:layout_height="wrap_content">

android:layout_height="fill_parent">

工具类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 adtDevices;

List lstDevices = new ArrayList();

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(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程序开发有一定的借鉴帮助作用。

android 蓝牙 底层api,Android提高之蓝牙隐藏API探秘相关推荐

  1. android蓝牙底层通道,底层之旅——Android蓝牙系统分析

    蓝牙系统分为四个层次,内核层.BlueZ库.BlueTooth的适配库.BlueTooth的JNI部分.Java框架层.应用层.下面先来分析Android的蓝牙协议栈. Android的蓝牙协议栈采用 ...

  2. 【Android应用开发】Android 蓝牙低功耗 (BLE) ( 第一篇 . 概述 . 蓝牙低功耗文档 翻译)

    转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50515359 参考 :  -- 官方文档 : https://develope ...

  3. android ble蓝牙接收不到数据_Android蓝牙4.0 Ble读写数据详解 -2

    Android蓝牙4.0 Ble读写数据详解 -2 上一篇说了如何扫描与链接蓝牙 这篇文章讲讲与蓝牙的数据传输,与一些踩到的坑. 先介绍一款调试工具,专门调试Ble蓝牙的app.名字叫:nRF-Con ...

  4. android蓝牙聊天设备,Android蓝牙开发——实现蓝牙聊天

    最近课上刚好需要做一个课程设计关于蓝牙的就挑选了个蓝牙聊天室,其实关键还是在于对蓝牙API的了解 一.蓝牙API 与蓝牙开发主要的相关类是以下四个 BluetoothAdapter 字面上则理解为蓝牙 ...

  5. Android 蓝牙开发(2)——低功耗蓝牙

    低功耗蓝牙官方文档 本文章是参考官网,然后加入自己实践中的理解完成!没有看上一篇的读者,可以先阅读一下前一篇,这是一个系列. 官网地址:developer.android.com/guide/topi ...

  6. 蓝牙HID——将android设备变成蓝牙鼠标/触控板(BluetoothHidDevice)

    前言 本篇为蓝牙HID系列篇章之一,本篇以红米K30(MIUI13即Android 12)手机作为蓝牙HID设备,可以与电脑.手机.平板等其他蓝牙主机进行配对从而实现鼠标触控板的功能. 蓝牙HID系列 ...

  7. Android蓝牙开发—经典蓝牙和BLE(低功耗)蓝牙的区别

    找到一篇介紹BT与BLE使用差别的文章, 写的很清晰,看完基本明白了 ----------------------------------------------------------------- ...

  8. Android 停车地图及停车导航,停车场蓝牙定位导航方案

    停车场蓝牙定位导航方案基于微能信息开发的蓝牙定位系统方案,与固有停车场管理系统深度结合,为顾客在智能手机终端提供全方面的停车场空余车位导航.记录停车位.反向寻车.查找路线.查找公共设施等服务. 停车场 ...

  9. 基于android的智能锁,android实现基于多级安全机制的蓝牙智能门锁源码

    项目介绍 基于多级安全机制的蓝牙智能门锁Android客户端实现. 项目客户端基于 Android 平台,通过 CC2541 蓝牙芯片和底层进行通信,门锁部分以 HT32F1656 单片机为控制核心, ...

  10. 蓝牙HID——将android设备变成蓝牙键盘(BluetoothHidDevice)

    前言 前段时间发现自己的老笔记本键盘失灵了,又没有多的键盘,于是苦恼了好久.于是萌生了自己做一个键盘的想法.这段时间一直在研究蓝牙HID,通过蓝牙HID将android手机变成一个蓝牙键盘,这样就不用 ...

最新文章

  1. Codeforces 1254C/1255F Point Ordering (交互题)
  2. Java实现字符串反转的四种方式代码示例
  3. ionic + cordova 使用 cordova-gallery-api 获取本地相册所有图片
  4. 计算机网络重要机制(一)可靠数据传输
  5. windows2016服务器优化,Windows Server 2012 服务器优化图文方法
  6. 取当前进程对应之静态映像文件的绝对路径/proc/self/exe
  7. [洛谷P3391] 文艺平衡树 (Splay模板)
  8. 从数据传输速率的视角思考信道利用率
  9. 云服务器怎么增加d盘_怎么租用美国云服务器比较便宜?
  10. Java计算机毕业设计电费管理系统源码+系统+数据库+lw文档
  11. 图像压缩相关内容简介
  12. TYPE g_date_tbl_typ IS TABLE OF DATE INDEX BY VARCHAR2(1000);
  13. gst-launch-1.0用法小记
  14. 音频信号的基波和谐波介绍
  15. 概率论 事件关系 古典概型与几何概型
  16. win10安装hypermesh无法启动_最最详细的win10原版系统安装教程(包含制作启动安装)...
  17. p值>0.05,统计意义上不显著?
  18. GPLT PAT 编程练习
  19. 简单工厂 工厂方法 抽象工厂
  20. chroot jail

热门文章

  1. 人脸生成黑科技:实现人脸转变特效,让人脸自动戴墨镜
  2. c语言图像对比度增强,图像对比度增强实验分析报告.doc
  3. 已知相关系数求解联合分布律
  4. IADS Revision Note: Asymptotic Notations
  5. CUDA学习(十一) 利用npp做图像处理
  6. MATLAB画qpsk的矢量图,matlab仿真QPSK.doc
  7. Android手机如何修改Mac地址,安卓手机怎么修改mac地址
  8. 手把手教你破解无线路由器密码
  9. 程序员眼中的“鼠标宏”
  10. 记一次中Phobos家族Devos勒索病毒