Android13 BluetoothPbapClient connect流程分析

BluetoothPbapClient的connect方法用于与外部蓝牙设备(手机)进行Pbap连接,下载电话本,代码如下:

//packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothPbapClient.java
public final class BluetoothPbapClient implements BluetoothProfile, AutoCloseable {
    public boolean connect(BluetoothDevice device) {
        if (DBG) {
            log("connect(" + device + ") for PBAP Client.");
        }
        final IBluetoothPbapClient service = getService();
        final boolean defaultValue = false;
        if (service == null) {
            Log.w(TAG, "Proxy not attached to service");
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else if (isEnabled() && isValidDevice(device)) {
            try {
                final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
                service.connect(device, mAttributionSource, recv);
                return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
            } catch (RemoteException | TimeoutException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        return defaultValue;
    }
}

调用IBluetoothPbapClient的connect方法,IBluetoothPbapClient是一个接口,由PbapClientService的内部类BluetoothPbapClientBinder实现:

//packages/modules/Bluetooth/android/app/src/com/android/bluetooth/pbapclient/PbapClientService.java
public class PbapClientService extends ProfileService {
    private static class BluetoothPbapClientBinder extends IBluetoothPbapClient.Stub
            implements IProfileServiceBinder {
        @Override
        public void connect(BluetoothDevice device, AttributionSource source,
                SynchronousResultReceiver receiver) {
            if (DBG) Log.d(TAG, "PbapClient Binder connect ");
            try {
                PbapClientService service = getService(source);
                boolean defaultValue = false;
                if (service != null) {
                    defaultValue = service.connect(device);
                } else {
                    Log.e(TAG, "PbapClient Binder connect no service");
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
        }
    }
}

PbapClientService connect

调用PbapClientService的connect方法:

//packages/modules/Bluetooth/android/app/src/com/android/bluetooth/pbapclient/PbapClientService.java
public class PbapClientService extends ProfileService {
    private Map<BluetoothDevice, PbapClientStateMachine> mPbapClientStateMachineMap =
            new ConcurrentHashMap<>();
    public boolean connect(BluetoothDevice device) {
        if (device == null) {
            throw new IllegalArgumentException("Null device");
        }
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED,
                "Need BLUETOOTH_PRIVILEGED permission");
        if (DBG) Log.d(TAG, "Received request to ConnectPBAPPhonebook " + device.getAddress());
        if (getConnectionPolicy(device) <= BluetoothProfile.CONNECTION_POLICY_FORBIDDEN) {
            return false;
        }
        synchronized (mPbapClientStateMachineMap) {
            PbapClientStateMachine pbapClientStateMachine = mPbapClientStateMachineMap.get(device); //取得PbapClientStateMachine状态机
            if (pbapClientStateMachine == null
                    && mPbapClientStateMachineMap.size() < MAXIMUM_DEVICES) {
                pbapClientStateMachine = new PbapClientStateMachine(this, device); //创建pbapClientStateMachine状态机
                pbapClientStateMachine.start(); //Start状态机
                mPbapClientStateMachineMap.put(device, pbapClientStateMachine); //将device和pbapClientStateMachine存储到Map中
                return true;
            } else {
                Log.w(TAG, "Received connect request while already connecting/connected.");
                return false;
            }
        }
    }
}

取得PbapClientStateMachine状态

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值