BluetoothManager的openGattServer方法用于打开一个本地的GATT服务器,代码如下:
//packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothManager.java
public final class BluetoothManager {
public BluetoothGattServer openGattServer(Context context,
BluetoothGattServerCallback callback, int transport, boolean eatt_support) {
if (context == null || callback == null) {
throw new IllegalArgumentException("null parameter: " + context + " " + callback);
}
// TODO(Bluetooth) check whether platform support BLE
// Do the check here or in GattServer?
try {
IBluetoothManager managerService = mAdapter.getBluetoothManager();
IBluetoothGatt iGatt = managerService.getBluetoothGatt();
if (iGatt == null) {
Log.e(TAG, "Fail to get GATT Server connection");
return null;
}
BluetoothGattServer mGattServer =
new BluetoothGattServer(iGatt, transport, mAdapter); //创建BluetoothGattServer对象
Boolean regStatus = mGattServer.registerCallback(callback, eatt_support); //调用BluetoothGattServer的registerCallback方法注册回调方法
return regStatus ? mGattServer : null;
} catch (RemoteException e) {
Log.e(TAG, "", e);
return null;
}
}
}
创建BluetoothGattServer对象,BluetoothGattServer的构造方法如下:
//packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothGattServer.java
public final class BluetoothGattServer implements BluetoothProfile {
BluetoothGattServer(IBluetoothGatt iGatt, int transport,
BluetoothAdapter adapter) {
mService = iGatt;
mAdapter = adapter;
mAttributionSource = adapter.getAttributionSource();
mCallback = null;
mServerIf = 0;
mTransport = transport;
mServices = new ArrayList<BluetoothGattService>();
}
}
调用BluetoothGattServer的registerCallback方法:
//packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothGattServer.java
public final class BluetoothGattServer implements BluetoothProfile {
/*package*/ boolean registerCallback(BluetoothGattServerCallback callback) {
return registerCallback(callback, false);
}
}
调用registerCallback方法:
//packages/modules/Bluetooth/framework/java/android/bluetooth/BluetoothGattServer.java
public final class BluetoothGattServer implements BluetoothProfile {
private final IBluetoothGatt mService;
private BluetoothGattServerCallback mCallback;
/*package*/ boolean registerCallback(BluetoothGattServerCallback callback,
boolean eatt_support) {
if (DBG) Log.d(TAG, "registerCallback()");
if (mService == null) {
Log.e(TAG, "GATT service not available");
return false;
}
UUID uuid = UUID.randomUUID();
if (DBG) Log.d(TAG, "registerCallback() - UUID=" + uuid);
synchronized (mServerIfLock) {
if (mCallback != null) {
Log.e(TAG, "App can register callback only once");
return false;
}
mCallback = callback;
try {
final SynchronousResultReceiver recv = SynchronousResultReceiver.get();
mService.registerServer(new ParcelUuid(uuid), mBluetoothGattServerCallback,
eatt_support, mAttributionSource, recv);
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, "", e);
mCallback = null;
return false;
}
try {
mServerIfLock.wait(CALLBACK_REG_TIMEOUT);
} catch (InterruptedException e) {
Log.e(TAG, "" + e);
mCallback = null;
}
if (mServerIf == 0) {
mCallback = null;
return false;
} else {
return true;
}
}
}
}
调用IBluetoothGatt的registerServer方法,IBluetoothGatt是一个接口,由GattService的内部类BluetoothGattBinder实现:
//packages/modules/Bluetooth/android/app/src/com/android/bluetooth/gatt/GattService.java
public class GattService extends ProfileService {
private static class BluetoothGattBinder extends IBluetoothGatt.Stub implements IProfileServiceBinder {
public void registerServer(ParcelUuid uuid, IBluetoothGattServerCallback callback,
boolean eattSupport, AttributionSource attributionSource,
SynchronousResultReceiver receiver) {
try {
registerServer(uuid, callback, eattSupport, attributionSource);
receiver.send(null);
} catch (RuntimeException e) {
receiver.propagateException(e);
}
}
}
}
调用registerServer方法:
//packages/modules/Bluetooth/android/app/src/com/android/bluetooth/gatt/GattService.java
public class GattService extends ProfileService {
private static class BluetoothGattBinder extends IBluetoothGatt.Stub implements IProfileServiceBinder {
private void registerServer(ParcelUuid uuid, IBluetoothGattServerCallback callback,
boolean eatt_support, AttributionSource attributionSource) {
GattService service = getService();
if (service == null) {
return;
}
service.registerServer(uuid.getUuid(), callback, eatt_support, attributionSource);
}
}
}
调用GattService的registerServer方法:
//packages/Bluetooth/android/app/src/com/android/bluetooth/gatt/GattService.java
public class GattService extends ProfileService {
void registerServer(UUID uuid, IBluetoothGattServerCallback callback, boolean eatt_support,
AttributionSource attributionSource) {
if (!Utils.checkConnectPermissionForDataDelivery(
this, attributionSource, "GattService registerServer")) {
return;
}
if (DBG) {
Log.d(TAG, "registerServer() - UUID=" + uuid);
}
mServerMap.add(uuid, null, callback, null, this);
gattServerRegisterAppNative(uuid.getLeastSignificantBits(), uuid.getMostSignificantBits(), eatt_support);
}
}
调用gattServerRegisterAppNative方法,gattServerRegisterAppNative是一个Native方法,通过查表实现代码如下:
//packages/modules/Bluetooth/android/app/jni/com_android_bluetooth_gatt.cpp
static const btgatt_interface_t* sGattIf = NULL;
const btgatt_server_interface_t* server;
static void gattServerRegisterAppNative(JNIEnv* env, jobject object,
jlong app_uuid_lsb, jlong app_uuid_msb,
jboolean eatt_support) {
if (!sGattIf) return;
Uuid uuid = from_java_uuid(app_uuid_msb, app_uuid_lsb);
sGattIf->server->register_server(uuid, eatt_support);
}
调用btgatt_server_interface_t的register_server函数,btgatt_server_interface_t是一个结构体,在bt_gatt_server.h定义,由蓝牙协议栈(BT Stack)实现。