/* * Copyright (C) 2013 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 android.bluetooth; import static android.bluetooth.BluetoothUtils.getSyncTimeout; import android.annotation.RequiresFeature; import android.annotation.RequiresNoPermission; import android.annotation.RequiresPermission; import android.annotation.SystemService; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; import android.content.AttributionSource; import android.content.Context; import android.content.pm.PackageManager; import android.os.RemoteException; import android.util.Log; import com.android.modules.utils.SynchronousResultReceiver; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeoutException; /** * High level manager used to obtain an instance of an {@link BluetoothAdapter} * and to conduct overall Bluetooth Management. *
* Use {@link android.content.Context#getSystemService(java.lang.String)} * with {@link Context#BLUETOOTH_SERVICE} to create an {@link BluetoothManager}, * then call {@link #getAdapter} to obtain the {@link BluetoothAdapter}. *
** For more information about using BLUETOOTH, read the Bluetooth developer * guide. *
*This is not specific to any application configuration but represents
* the connection state of the local Bluetooth adapter for certain profile.
* This can be used by applications like status bar which would just like
* to know the state of Bluetooth.
*
* @param device Remote bluetooth device.
* @param profile GATT or GATT_SERVER
* @return State of the profile connection. One of {@link BluetoothProfile#STATE_CONNECTED},
* {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED},
* {@link BluetoothProfile#STATE_DISCONNECTING}
*/
@RequiresLegacyBluetoothPermission
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public int getConnectionState(BluetoothDevice device, int profile) {
if (DBG) Log.d(TAG, "getConnectionState()");
List Return the set of devices which are in state {@link BluetoothProfile#STATE_CONNECTED}
*
* This is not specific to any application configuration but represents
* the connection state of Bluetooth for this profile.
* This can be used by applications like status bar which would just like
* to know the state of Bluetooth.
*
* @param profile GATT or GATT_SERVER
* @return List of devices. The list will be empty on error.
*/
@RequiresLegacyBluetoothPermission
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public List If none of the devices match any of the given states,
* an empty list will be returned.
*
* This is not specific to any application configuration but represents
* the connection state of the local Bluetooth adapter for this profile.
* This can be used by applications like status bar which would just like
* to know the state of the local adapter.
*
* @param profile GATT or GATT_SERVER
* @param states Array of states. States can be one of {@link BluetoothProfile#STATE_CONNECTED},
* {@link BluetoothProfile#STATE_CONNECTING}, {@link BluetoothProfile#STATE_DISCONNECTED},
* {@link BluetoothProfile#STATE_DISCONNECTING},
* @return List of devices. The list will be empty on error.
*/
@RequiresLegacyBluetoothPermission
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public List> recv =
SynchronousResultReceiver.get();
iGatt.getDevicesMatchingConnectionStates(states, mAttributionSource, recv);
devices = Attributable.setAttributionSource(
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(devices),
mAttributionSource);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, "", e);
}
return devices;
}
/**
* Open a GATT Server
* The callback is used to deliver results to Caller, such as connection status as well
* as the results of any other GATT server operations.
* The method returns a BluetoothGattServer instance. You can use BluetoothGattServer
* to conduct GATT server operations.
*
* @param context App context
* @param callback GATT server callback handler that will receive asynchronous callbacks.
* @return BluetoothGattServer instance
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public BluetoothGattServer openGattServer(Context context,
BluetoothGattServerCallback callback) {
return (openGattServer(context, callback, BluetoothDevice.TRANSPORT_AUTO));
}
/**
* Open a GATT Server
* The callback is used to deliver results to Caller, such as connection status as well
* as the results of any other GATT server operations.
* The method returns a BluetoothGattServer instance. You can use BluetoothGattServer
* to conduct GATT server operations.
*
* @param context App context
* @param callback GATT server callback handler that will receive asynchronous callbacks.
* @param eatt_support idicates if server should use eatt channel for notifications.
* @return BluetoothGattServer instance
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public BluetoothGattServer openGattServer(Context context,
BluetoothGattServerCallback callback, boolean eatt_support) {
return (openGattServer(context, callback, BluetoothDevice.TRANSPORT_AUTO, eatt_support));
}
/**
* Open a GATT Server
* The callback is used to deliver results to Caller, such as connection status as well
* as the results of any other GATT server operations.
* The method returns a BluetoothGattServer instance. You can use BluetoothGattServer
* to conduct GATT server operations.
*
* @param context App context
* @param callback GATT server callback handler that will receive asynchronous callbacks.
* @param transport preferred transport for GATT connections to remote dual-mode devices {@link
* BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link
* BluetoothDevice#TRANSPORT_LE}
* @return BluetoothGattServer instance
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public BluetoothGattServer openGattServer(Context context,
BluetoothGattServerCallback callback, int transport) {
return (openGattServer(context, callback, transport, false));
}
/**
* Open a GATT Server
* The callback is used to deliver results to Caller, such as connection status as well
* as the results of any other GATT server operations.
* The method returns a BluetoothGattServer instance. You can use BluetoothGattServer
* to conduct GATT server operations.
*
* @param context App context
* @param callback GATT server callback handler that will receive asynchronous callbacks.
* @param transport preferred transport for GATT connections to remote dual-mode devices {@link
* BluetoothDevice#TRANSPORT_AUTO} or {@link BluetoothDevice#TRANSPORT_BREDR} or {@link
* BluetoothDevice#TRANSPORT_LE}
* @param eatt_support idicates if server should use eatt channel for notifications.
* @return BluetoothGattServer instance
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
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);
Boolean regStatus = mGattServer.registerCallback(callback, eatt_support);
return regStatus ? mGattServer : null;
} catch (RemoteException e) {
Log.e(TAG, "", e);
return null;
}
}
}