Android之Bluetooth配對

前言

我們關(guān)注藍牙建立連接的過程
1.通信的建立一定是異步的過程,自然涉及回調(diào)
2.如果有回調(diào),一定有一處代碼進行分發(fā)處理

apk -- jni -- hal 
apk的監(jiān)聽一定來自jni,我們關(guān)注jni的注冊

解讀JniCallbacks

/*
 * Copyright (C) 2012-2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.btservice;

import android.util.Log;

final class JniCallbacks {

    private RemoteDevices mRemoteDevices;
    private AdapterProperties mAdapterProperties;
    private AdapterState mAdapterStateMachine;
    private BondStateMachine mBondStateMachine;

    JniCallbacks(AdapterState adapterStateMachine,AdapterProperties adapterProperties) {
        mAdapterStateMachine = adapterStateMachine;
        mAdapterProperties = adapterProperties;
    }

    void init(BondStateMachine bondStateMachine, RemoteDevices remoteDevices) {
        mRemoteDevices = remoteDevices;
        mBondStateMachine = bondStateMachine;
    }

    void cleanup() {
        mRemoteDevices = null;
        mAdapterProperties = null;
        mAdapterStateMachine = null;
        mBondStateMachine = null;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    void sspRequestCallback(byte[] address, byte[] name, int cod, int pairingVariant,
            int passkey) {//藍牙芯片通知配對確認
        mBondStateMachine.sspRequestCallback(address, name, cod, pairingVariant,
            passkey);
    }
    void devicePropertyChangedCallback(byte[] address, int[] types, byte[][] val) {//用于記錄藍牙設(shè)備的數(shù)據(jù),主要涉及的類為RemoteDevices.DeviceProperties。例如藍牙地址 藍牙uuid
        mRemoteDevices.devicePropertyChangedCallback(address, types, val);
    }

    void deviceFoundCallback(byte[] address) {//掃描設(shè)備的回調(diào)
        mRemoteDevices.deviceFoundCallback(address);
    }

    void pinRequestCallback(byte[] address, byte[] name, int cod, boolean min16Digits) {
        mBondStateMachine.pinRequestCallback(address, name, cod, min16Digits);
    }

    void bondStateChangeCallback(int status, byte[] address, int newState) {//配對狀態(tài)更改
        mBondStateMachine.bondStateChangeCallback(status, address, newState);
    }

    void aclStateChangeCallback(int status, byte[] address, int newState) {//配置的設(shè)備狀態(tài)更改,例如遠程設(shè)備關(guān)閉了藍牙,會通知出來,具體的時間是多久?
        mRemoteDevices.aclStateChangeCallback(status, address, newState);
    }

    void stateChangeCallback(int status) {//本設(shè)備的藍牙打開和關(guān)閉通知
        mAdapterStateMachine.stateChangeCallback(status);
    }

    void discoveryStateChangeCallback(int state) {//掃描開始和掃描結(jié)束,開始是1 結(jié)束是0
        mAdapterProperties.discoveryStateChangeCallback(state);
    }

    void adapterPropertyChangedCallback(int[] types, byte[][] val) {//不是很清楚用來做什么
        mAdapterProperties.adapterPropertyChangedCallback(types, val);
    }

}

場景:配對建立連接過程

設(shè)備端:
UI端點擊配對 -- 系統(tǒng)服務(wù) -- Bluetooth.apk -- jni -- hal ---->  接受方


接受方:
接受方 -- 發(fā)送pin指令進行配對 --> 設(shè)備端

設(shè)備端
設(shè)備端收到指令hal -- jni -- Bluetooth.apk -- UI端接受點擊確認
JniCallbacks.sspRequestCallback 接收到回調(diào)之后,進行事件分發(fā)

案例:藍牙配對連接
注:
配對的前提,設(shè)備已知,代碼分析要從設(shè)備信息出發(fā)

1.UI調(diào)用方法
BluetoothDevice.createBond

    public boolean createBond() {
        ······
        try {
            return sService.createBond(this, TRANSPORT_AUTO);//sService為Bluetooth.apk中的服務(wù)
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }
    注:
    sService的獲取來自系統(tǒng)服務(wù)BluetoothManagerService

2.藍牙app及jni
1)AdapterService.AdapterServiceBinder.createBond

AdapterService.AdapterServiceBinder.createBond --> AdapterService.createBond

     boolean createBond(BluetoothDevice device, int transport, OobData oobData) {
        ······
        // Pairing is unreliable while scanning, so cancel discovery
        // Note, remove this when native stack improves
        cancelDiscoveryNative();//注意配對的時候需要取消掃描

        Message msg = mBondStateMachine.obtainMessage(BondStateMachine.CREATE_BOND);
        msg.obj = device;
        msg.arg1 = transport;

        if (oobData != null) {
            Bundle oobDataBundle = new Bundle();
            oobDataBundle.putParcelable(BondStateMachine.OOBDATA, oobData);
            msg.setData(oobDataBundle);
        }
        mBondStateMachine.sendMessage(msg);//mBondStateMachine為狀態(tài)機。要熟悉狀態(tài)機的運用
        return true;
    }

2)BondStateMachine.StableState接受來自消息CREATE_BOND

mBondStateMachine的默認狀態(tài)為mStableState
    private BondStateMachine(AdapterService service,
            AdapterProperties prop, RemoteDevices remoteDevices) {
        super("BondStateMachine:");
        addState(mStableState);
        addState(mPendingCommandState);
        ······
        setInitialState(mStableState);
    }
    
    private class StableState extends State {
        ······
        @Override
        public boolean processMessage(Message msg) {
            ·······
            switch(msg.what) {
              ·······
              case CREATE_BOND:
                  OobData oobData = null;
                  if (msg.getData() != null)
                      oobData = msg.getData().getParcelable(OOBDATA);

                  createBond(dev, msg.arg1, oobData, true);
                  break;
              ······
            }
            ······
        }
    }
    
    private boolean createBond(BluetoothDevice dev, int transport, OobData oobData,
                               boolean transition) {
        if (dev.getBondState() == BluetoothDevice.BOND_NONE) {
            infoLog("Bond address is:" + dev);
            byte[] addr = Utils.getBytesFromAddress(dev.getAddress());
            boolean result;
            if (oobData != null) {
                result = mAdapterService.createBondOutOfBandNative(addr, transport, oobData);
            } else {
                result = mAdapterService.createBondNative(addr, transport);//調(diào)用jni進行通信
            }

            if (!result) {
                sendIntent(dev, BluetoothDevice.BOND_NONE,
                           BluetoothDevice.UNBOND_REASON_REMOVED);
                return false;
            } else if (transition) {
                transitionTo(mPendingCommandState);//同步把狀態(tài)切換成帶處理狀態(tài)(mPendingCommandState)
            }
            return true;
        }
        return false;
    }

3)AdapterService.createBondNative

/*package*/ native boolean createBondNative(byte[] address, int transport);

com_android_bluetooth_btservice_AdapterService.cpp
static jboolean createBondNative(JNIEnv* env, jobject obj, jbyteArray address, jint transport) {
    ALOGV("%s:",__FUNCTION__);

    jbyte *addr;
    jboolean result = JNI_FALSE;

    if (!sBluetoothInterface) return result;

    addr = env->GetByteArrayElements(address, NULL);
    if (addr == NULL) {
        jniThrowIOException(env, EINVAL);
        return result;
    }

    int ret = sBluetoothInterface->create_bond((bt_bdaddr_t *)addr, transport);//通過jni調(diào)進藍牙協(xié)議棧中
    env->ReleaseByteArrayElements(address, addr, 0);
    result = (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;

    return result;
}

這里是沒有對應(yīng)的回調(diào)傳給jni,那自然是jni主動被調(diào)用,這樣才能把配對的狀態(tài)告知app

3.藍牙協(xié)議棧--省略

4.Bluetooth.apk接收回調(diào)
1)關(guān)注JniCallbacks接收回調(diào)的地方

    void bondStateChangeCallback(int status, byte[] address, int newState) {//配對狀態(tài)更改
        mBondStateMachine.bondStateChangeCallback(status, address, newState);
    }
    
    void sspRequestCallback(byte[] address, byte[] name, int cod, int pairingVariant,
            int passkey) {//藍牙芯片通知配對確認
        mBondStateMachine.sspRequestCallback(address, name, cod, pairingVariant,
            passkey);
    }

2)BondStateMachine
a)關(guān)注配對狀態(tài)的回調(diào)

    void bondStateChangeCallback(int status, byte[] address, int newState) {
        ······
        Message msg = obtainMessage(BONDING_STATE_CHANGE);//狀態(tài)機目前處于mPendingCommandState
        msg.obj = device;

        if (newState == BOND_STATE_BONDED)
            msg.arg1 = BluetoothDevice.BOND_BONDED;
        else if (newState == BOND_STATE_BONDING)
            msg.arg1 = BluetoothDevice.BOND_BONDING;
        else
            msg.arg1 = BluetoothDevice.BOND_NONE;
        msg.arg2 = status;

        sendMessage(msg);
    }
    
    private class PendingCommandState extends State {
        ······
        @Override
        public boolean processMessage(Message msg) {
            ······
            switch (msg.what) {
                ······
                case BONDING_STATE_CHANGE:
                    int newState = msg.arg1;
                    int reason = getUnbondReasonFromHALCode(msg.arg2);
                    sendIntent(dev, newState, reason);//發(fā)送廣播出去
                    ······
                    break;
                ······
            }
            ······
        }
    }
    
    private void sendIntent(BluetoothDevice device, int newState, int reason) {
        DeviceProperties devProp = mRemoteDevices.getDeviceProperties(device);
        int oldState = BluetoothDevice.BOND_NONE;
        if (devProp != null) {
            oldState = devProp.getBondState();
        }
        if (oldState == newState) return;
        mAdapterProperties.onBondStateChanged(device, newState);

        Intent intent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//關(guān)注廣播
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.putExtra(BluetoothDevice.EXTRA_BOND_STATE, newState);
        intent.putExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, oldState);
        if (newState == BluetoothDevice.BOND_NONE)
            intent.putExtra(BluetoothDevice.EXTRA_REASON, reason);
        mAdapterService.sendBroadcastAsUser(intent, UserHandle.ALL,
                AdapterService.BLUETOOTH_PERM);
        infoLog("Bond State Change Intent:" + device + " OldState: " + oldState
                + " NewState: " + newState);
    }
    
    總結(jié):
    針對配對的狀態(tài),其他應(yīng)用關(guān)注廣播:BluetoothDevice.ACTION_BOND_STATE_CHANGED

b)關(guān)注配置過程確認pin碼

    void sspRequestCallback(byte[] address, byte[] name, int cod, int pairingVariant,
            int passkey) {
        ······
        Message msg = obtainMessage(SSP_REQUEST);//狀態(tài)機目前處于mPendingCommandState
        msg.obj = device;
        if(displayPasskey)
            msg.arg1 = passkey;
        msg.arg2 = variant;
        sendMessage(msg);
    }
    
    private class PendingCommandState extends State {
        ······
        @Override
        public boolean processMessage(Message msg) {
            ······
            switch (msg.what) {
                ······
                case SSP_REQUEST:
                    int passkey = msg.arg1;
                    int variant = msg.arg2;
                    sendDisplayPinIntent(devProp.getAddress(), passkey, variant);//發(fā)送廣播
                    break;
                ······
            }
            ······
        }
    }
    
    private void sendDisplayPinIntent(byte[] address, int pin, int variant) {
        Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);//關(guān)注此廣播
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mRemoteDevices.getDevice(address));
        if (pin != 0) {
            intent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, pin);
        }
        intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, variant);
        intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        mAdapterService.sendOrderedBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM);
    }
    
    總結(jié):
    針對配對過程獲取pin碼,其他應(yīng)用關(guān)注廣播:BluetoothDevice.ACTION_PAIRING_REQUEST
    

參考學習

http://m.itdecent.cn/p/a150d55e29ca
https://blog.csdn.net/WHB20081815/article/details/88653177
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容