| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.os; |
| 17 |
|
| 18 |
import android.annotation.NonNull; |
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.content.Context; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.annotation.SystemApi.Client; |
| 23 |
import android.os.BluetoothServiceManager; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
@SystemApi(client = Client.MODULE_LIBRARIES) |
| 32 |
public class BluetoothServiceManager { |
| 33 |
|
| 34 |
|
| 35 |
public static final String BLUETOOTH_MANAGER_SERVICE = "bluetooth_manager"; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public BluetoothServiceManager() { |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static final class ServiceRegisterer { |
| 46 |
private final String mServiceName; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public ServiceRegisterer(String serviceName) { |
| 52 |
mServiceName = serviceName; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public void register(@NonNull IBinder service) { |
| 59 |
ServiceManager.addService(mServiceName, service); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@Nullable |
| 69 |
public IBinder get() { |
| 70 |
return ServiceManager.getService(mServiceName); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
@NonNull |
| 80 |
public IBinder getOrThrow() throws ServiceNotFoundException { |
| 81 |
try { |
| 82 |
return ServiceManager.getServiceOrThrow(mServiceName); |
| 83 |
} catch (ServiceManager.ServiceNotFoundException e) { |
| 84 |
throw new ServiceNotFoundException(mServiceName); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
@Nullable |
| 93 |
public IBinder tryGet() { |
| 94 |
return ServiceManager.checkService(mServiceName); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static class ServiceNotFoundException extends ServiceManager.ServiceNotFoundException { |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public ServiceNotFoundException(@NonNull String name) { |
| 110 |
super(name); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@NonNull |
| 118 |
public ServiceRegisterer getBluetoothManagerServiceRegisterer() { |
| 119 |
return new ServiceRegisterer(BLUETOOTH_MANAGER_SERVICE); |
| 120 |
} |
| 121 |
} |