关于蓝牙BLE开发中要做掉的东西——除了设备和客户端外的蓝牙匹配对话框,notification 蓝牙匹配的通知栏

来源:互联网 发布:数据采集网络兼职 编辑:程序博客网 时间:2024/04/26 01:36


1、一般情况下,我们在做蓝牙BLE的时候,是不需要匹配的,应该直接取连接,这一步,不需要刻意的去做自动匹配,我认为没有那个必要,因为他在不匹配的

情况下,一样可以连接,并且进行数据的交互,假设一定要做自动匹配的话,在BluetoothDevice 这个类中有隐藏的方法

/** @hide */  //取消用户输入

public boolean cancelPairingUserInput() {

    if (sService== null) {
        Log.e(TAG, "BT not enabled. Cannot create pairing user input");
        return false;
    }
    try {
        return sService.cancelBondProcess(this);
    } catch (RemoteException e) {Log.e(TAG, "", e);}
    return false;
}


/**
 * Cancel an in-progress bonding request started with {
@link #createBond}.
 * <p>Requires {
@link android.Manifest.permission#BLUETOOTH_ADMIN}.
 *
 *
@return true on success, false on error
 *
@hide
 
*/取消用户配对
public boolean cancelBondProcess() {
    if (sService== null) {
        Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond");
        return false;
    }
    try {
        Log.i(TAG, "cancelBondProcess() for device " + getAddress() +
                " called by pid: " + Process.myPid() +
                " tid: " + Process.myTid());
        return sService.cancelBondProcess(this);
    } catch (RemoteException e) {Log.e(TAG, "", e);}
    return false;
}


在加上

/**
 * Set the pin during pairing when the pairing method is {
@link #PAIRING_VARIANT_PIN}
 * <p>Requires {
@link android.Manifest.permission#BLUETOOTH_ADMIN}.
 *
 *
@return true pin has been set
 *         false for error
 */


pin 将要匹配的密码
public boolean setPin(byte[] pin) {
    if (sService== null) {
        Log.e(TAG, "BT not enabled. Cannot set Remote Device pin");
        return false;
    }
    try {
        return sService.setPin(this, true, pin.length, pin);
    } catch (RemoteException e) {Log.e(TAG, "", e);}
    return false;
}


/**
 * Start the bonding
(pairing) process with the remote device.
 * <p>This is an asynchronous call
, it will return immediately. Register
 * for {
@link #ACTION_BOND_STATE_CHANGED} intents to be notified when
 * the bonding process completes
, and its result.
 * <p>Android system services will handle the necessary user interactions
 * to confirm and complete the bonding process.
 * <p>Requires {
@link android.Manifest.permission#BLUETOOTH_ADMIN}.
 *
 *
@return false on immediate error, true if bonding will begin
 */
进行与device进行连接的方法
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean createBond() {
    if (sService== null) {
        Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device");
        return false;
    }
    try {
        Log.i(TAG, "createBond() for device " + getAddress() +
                " called by pid: " + Process.myPid() +
                " tid: " + Process.myTid());
        return sService.createBond(this,TRANSPORT_AUTO);
    } catch (RemoteException e) {Log.e(TAG, "", e);}
    return false;
}



在定义广播,在广播中(android.bluetooth.device.action.PAIRING_REQUEST

)监听到此广播时,进行上述的与用户匹配,输入密码,取消用户输入久OK当然上面的除了没有被隐藏的方法外其他都要用反射得到


2、有的需求中是这样的,加入你的设备是BLE,但是不是向蓝牙耳机那种单片机的设备,而是有可见屏幕给用户展示信息的设备,你完全可以去

用上面的方法进行去掉匹配对话框,但是它还会有关于系统的Notification在通知栏进行显示,此时,你要做的是拦截系统的广播,当然要在你做完相应的操作后,

具体拦截的方法将在我的另一篇文章  《关于拦截系统广播的那些坑中》


感谢博客园天边的星星的博客给我启示http://www.cnblogs.com/jason-star/archive/2012/09/10/2678368.html 

阅读全文
0 0
原创粉丝点击