如何?android藍芽開發 自動配對串連,並不彈出提示框

來源:互聯網
上載者:User

標籤:

之前做一個android版的藍芽 與血壓計通訊的項目,遇到最大的難題就是自動配對.上網查資料說是用反射createBond()和setPin(),但測試時進行配對還是會出現提示,但配對是成功了我就開始尋找怎麼關閉這個藍芽配對提示框,後面還是偉大的android源碼協助了我。在源碼 BluetoothDevice 類中還有兩個隱藏方法cancelBondProcess()和cancelPairingUserInput()這兩個方法一個是解除配對進程一個是取消使用者輸入下面是自動配對的代碼Mainfest,xml註冊 <receiver android:name=".BluetoothConnectActivityReceiver" >    <intent-filter>        <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />    </intent-filter></receiver>自己在收到廣播時處理並將預先輸入的密碼設定進去public class BluetoothConnectActivityReceiver extends BroadcastReceiver{     String strPsw = "0";     @Override    public void onReceive(Context context, Intent intent)    {        // TODO Auto-generated method stub        if (intent.getAction().equals(                "android.bluetooth.device.action.PAIRING_REQUEST"))        {            BluetoothDevice btDevice = intent                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);             // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");            // device.setPin(pinBytes);            Log.i("tag11111", "ddd");            try            {                ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手機和藍芽採集器配對                ClsUtils.createBond(btDevice.getClass(), btDevice);                ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);            }            catch (Exception e)            {                // TODO Auto-generated catch block                e.printStackTrace();            }        }      }} /************************************ 藍芽配對函數 * **************/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();    }     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);            }            // 取得所有常量            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();        }    }} 執行時直接使用:public static boolean pair(String strAddr, String strPsw)    {        boolean result = false;        BluetoothAdapter bluetoothAdapter = BluetoothAdapter                .getDefaultAdapter();         bluetoothAdapter.cancelDiscovery();         if (!bluetoothAdapter.isEnabled())        {            bluetoothAdapter.enable();        }         if (!BluetoothAdapter.checkBluetoothAddress(strAddr))        { // 檢查藍芽地址是否有效             Log.d("mylog", "devAdd un effient!");        }         BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr);         if (device.getBondState() != BluetoothDevice.BOND_BONDED)        {            try            {                Log.d("mylog", "NOT BOND_BONDED");                ClsUtils.setPin(device.getClass(), device, strPsw); // 手機和藍芽採集器配對                ClsUtils.createBond(device.getClass(), device);                remoteDevice = device; // 配對完畢就把這個裝置對象傳給全域的remoteDevice                result = true;            }            catch (Exception e)            {                // TODO Auto-generated catch block                 Log.d("mylog", "setPiN failed!");                e.printStackTrace();            } //         }        else        {            Log.d("mylog", "HAS BOND_BONDED");            try            {                ClsUtils.createBond(device.getClass(), device);                ClsUtils.setPin(device.getClass(), device, strPsw); // 手機和藍芽採集器配對                ClsUtils.createBond(device.getClass(), device);                remoteDevice = device; // 如果綁定成功,就直接把這個裝置對象傳給全域的remoteDevice                result = true;            }            catch (Exception e)            {                // TODO Auto-generated catch block                Log.d("mylog", "setPiN failed!");                e.printStackTrace();            }        }        return result;    }

 

如何?android藍芽開發 自動配對串連,並不彈出提示框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.