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状态

最低0.47元/天 解锁文章
3089

被折叠的 条评论
为什么被折叠?



