当前位置:操作系统 > 安卓/Android >>

android 取消蓝牙配对框 实现自动配对

我看了几个文章,主要是接受配对广播,然后设置pin,实现配对,但是网上的大部分手机是不可以的,android.bluetoothdevice 下 action_pair_request ,没有定义这个,开始困扰了我一点时间,实现难度:是否能进入那个广播响应

定义了一个类,这个是网上的可以直接用

// [java]
package zicox.esc; 
 
import java.lang.reflect.Method; 
import java.lang.reflect.Field; 
 
 
import android.bluetooth.BluetoothAdapter; 
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  www.zzzyk.com
    { 
        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(); 
    } 
  
    static public boolean setPin(Class btClass, BluetoothDevice btDevice, 
            String str) throws Exception 
    { 
        try 
        { 
            Method removeBondMethod = btClass.getDeclaredMethod("setPin", 
                    new Class[] 
                    {byte[].class}); 
            Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, 
                    new Object[] 
                    {str.getBytes()}); 
            Log.e("returnValue", "" + returnValue); 
        } 
        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(); 
        } 
        return true; 
  
    } 
  
    // 取消用户输入 
    static public boolean cancelPairingUserInput(Class btClass, 
            BluetoothDevice device) 
  
    throws Exception 
    { 
        Method createBondMethod = btClass.getMethod("cancelPairingUserInput"); 
        // cancelBondProcess() 
        Boolean returnValue = (Boolean) createBondMethod.invoke(device); 
        return returnValue.booleanValue(); 
    } 
  
    // 取消配对 
    static public boolean cancelBondProcess(Class btClass, 
            BluetoothDevice device) 
  
    throws Exception 
    { 
        Method createBondMethod = btClass.getMethod("cancelBondProcess"); 
        Boolean returnValue = (Boolean) createBondMethod.invoke(device); 
        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() + ";and the i is:" 
                        + i); 
   &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,