android 蓝牙程序控制绑定与删除绑定
发表于1年前(2014-08-06 16:20)
阅读( 1518) | 评论( 1)
9人收藏此文章,
我要收藏
赞0
[9月19日上海技术沙龙]O2O电商如何借助“云+APM”低成本打造最佳用户体验 
如何实现android蓝牙开发 自动配对连接,并不弹出提示框
(2013-01-05 15:27:36)
转载▼
我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。
在源码 BluetoothDevice 类中还有两个隐藏方法
cancelBondProcess()和cancelPairingUserInput()
这两个方法一个是取消配对进程一个是取消用户输入
下面是自动配对的代码
Mainfest,xml注册
1 |
<</code>receiver android:name = ".BluetoothConnectActivityReceiver" > |
3 |
<</code>action android:name = "android.bluetooth.device.action.PAIRING_REQUEST" /> |
4 |
</</code>intent-filter > |
自己在收到广播时处理并将预先输入的密码设置进去
01 |
public class BluetoothConnectActivityReceiver extends BroadcastReceiver |
07 |
public void onReceive(Context context, Intent intent) |
09 |
// TODO Auto-generated method stub |
10 |
if (intent.getAction().equals( |
11 |
"android.bluetooth.device.action.PAIRING_REQUEST" )) |
13 |
BluetoothDevice btDevice = intent |
14 |
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
16 |
// byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234"); |
17 |
// device.setPin(pinBytes); |
18 |
Log.i( "tag11111" , "ddd" ); |
21 |
ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对 |
22 |
ClsUtils.createBond(btDevice.getClass(), btDevice); |
23 |
ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice); |
27 |
// TODO Auto-generated catch block |
002 |
import java.lang.reflect.Field; |
003 |
import java.lang.reflect.Method; |
005 |
import android.bluetooth.BluetoothDevice; |
006 |
import android.util.Log; |
007 |
public class ClsUtils |
014 |
static public boolean createBond(Class btClass, BluetoothDevice btDevice) |
017 |
Method createBondMethod = btClass.getMethod( "createBond" ); |
018 |
Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); |
019 |
return returnValue.booleanValue(); |
026 |
static public boolean removeBond(Class btClass, BluetoothDevice btDevice) |
029 |
Method removeBondMethod = btClass.getMethod( "removeBond" ); |
030 |
Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice); |
031 |
return returnValue.booleanValue(); |
034 |
static public boolean setPin(Class btClass, BluetoothDevice btDevice, |
035 |
String str) throws Exception |
039 |
Method removeBondMethod = btClass.getDeclaredMethod( "setPin" , |
042 |
Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, |
045 |
Log.e( "returnValue" , "" + returnValue); |
047 |
catch (SecurityException e) |
049 |
// throw new RuntimeException(e.getMessage()); |
052 |
catch (IllegalArgumentException e) |
054 |
// throw new RuntimeException(e.getMessage()); |
059 |
// TODO Auto-generated catch block |
067 |
static public boolean cancelPairingUserInput(Class btClass, |
068 |
BluetoothDevice device) |
072 |
Method createBondMethod = btClass.getMethod( "cancelPairingUserInput" ); |
073 |
// cancelBondProcess() |
074 |
Boolean returnValue = (Boolean) createBondMethod.invoke(device); |
075 |
return returnValue.booleanValue(); |
079 |
static public boolean cancelBondProcess(Class btClass, |
080 |
BluetoothDevice device) |
084 |
Method createBondMethod = btClass.getMethod( "cancelBondProcess" ); |
085 |
Boolean returnValue = (Boolean) createBondMethod.invoke(device); |
086 |
return returnValue.booleanValue(); |
093 |
static public void printAllInform(Class clsShow) |
098 |
Method[] hideMethod = clsShow.getMethods(); |
100 |
for (; i < hideMethod.length; i++) |
102 |
Log.e( "method name" , hideMethod[i].getName() + ";and the i is:" |
106 |
Field[] allFields = clsShow.getFields(); |
107 |
for (i = 0 ; i < allFields.length; i++) |
109 |
Log.e( "Field name" , allFields[i].getName()); |
112 |
catch (SecurityException e) |
114 |
// throw new RuntimeException(e.getMessage()); |
117 |
catch (IllegalArgumentException e) |
119 |
// throw new RuntimeException(e.getMessage()); |
124 |
// TODO Auto-generated catch block |
执行时直接使用:
01 |
public static boolean pair(String strAddr, String strPsw)
|
03 |
boolean result = false ; |
04 |
BluetoothAdapter bluetoothAdapter = BluetoothAdapter |
07 |
bluetoothAdapter.cancelDiscovery(); |
09 |
if (!bluetoothAdapter.isEnabled()) |
11 |
bluetoothAdapter.enable(); |
14 |
if (!BluetoothAdapter.checkBluetoothAddress(strAddr)) |
17 |
Log.d( "mylog" , "devAdd un effient!" ); |
20 |
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr); |
22 |
if (device.getBondState() != BluetoothDevice.BOND_BONDED) |
26 |
Log.d( "mylog" , "NOT BOND_BONDED" ); |
27 |
ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对 |
28 |
ClsUtils.createBond(device.getClass(), device); |
29 |
remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDevice |
34 |
// TODO Auto-generated catch block |
36 |
Log.d( "mylog" , "setPiN failed!" ); |
43 |
Log.d( "mylog" , "HAS BOND_BONDED" ); |
46 |
ClsUtils.createBond(device.getClass(), device); |
47 |
ClsUtils.setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对 |
48 |
ClsUtils.createBond(device.getClass(), device); |
49 |
remoteDevice = device; // 如果绑定成功,就直接把这个设备对象传给全局的remoteDevice |
54 |
// TODO Auto-generated catch block |
55 |
Log.d( "mylog" , "setPiN failed!" ); |
|