/** * Overrides behavior of {@link getProfileProxy} if {@link ShadowBluetoothAdapter#setProfileProxy} * has been previously called. * * If active (non-null) proxy has been set by {@link setProfileProxy} for the given {@code * profile}, {@link getProfileProxy} will immediately call {@code onServiceConnected} of the given * BluetoothProfile.ServiceListener {@code listener}. * * @return 'true' if a proxy object has been set by {@link setProfileProxy} for the given * BluetoothProfile {@code profile} */ @Implementation protected boolean getProfileProxy( Context context, BluetoothProfile.ServiceListener listener, int profile) { if (!isOverridingProxyBehavior) { return directlyOn(realAdapter, BluetoothAdapter.class) .getProfileProxy(context, listener, profile); } BluetoothProfile proxy = profileProxies.get(profile); if (proxy == null) { return false; } else { listener.onServiceConnected(profile, proxy); return true; } }
@Test public void getProfileProxy_afterSetProfileProxyWithNullArgument_doesNotCallServiceListener() { shadowBluetoothAdapter.setProfileProxy(MOCK_PROFILE1, null); BluetoothProfile.ServiceListener mockServiceListener = mock(BluetoothProfile.ServiceListener.class); boolean result = bluetoothAdapter.getProfileProxy( RuntimeEnvironment.application, mockServiceListener, MOCK_PROFILE1); assertThat(result).isFalse(); verifyZeroInteractions(mockServiceListener); }
@Test public void getProfileProxy_afterSetProfileProxy_callsServiceListener() { BluetoothProfile mockProxy = mock(BluetoothProfile.class); BluetoothProfile.ServiceListener mockServiceListener = mock(BluetoothProfile.ServiceListener.class); shadowBluetoothAdapter.setProfileProxy(MOCK_PROFILE1, mockProxy); boolean result = bluetoothAdapter.getProfileProxy( RuntimeEnvironment.application, mockServiceListener, MOCK_PROFILE1); assertThat(result).isTrue(); verify(mockServiceListener).onServiceConnected(MOCK_PROFILE1, mockProxy); }
@Test public void getProfileProxy_afterSetProfileProxy_forMultipleProfiles() { BluetoothProfile mockProxy1 = mock(BluetoothProfile.class); shadowBluetoothAdapter.setProfileProxy(MOCK_PROFILE1, mockProxy1); BluetoothProfile mockProxy2 = mock(BluetoothProfile.class); shadowBluetoothAdapter.setProfileProxy(MOCK_PROFILE2, mockProxy2); BluetoothProfile.ServiceListener mockServiceListener = mock(BluetoothProfile.ServiceListener.class); boolean result1 = bluetoothAdapter.getProfileProxy( RuntimeEnvironment.application, mockServiceListener, MOCK_PROFILE1); boolean result2 = bluetoothAdapter.getProfileProxy( RuntimeEnvironment.application, mockServiceListener, MOCK_PROFILE2); assertThat(result1).isTrue(); assertThat(result2).isTrue(); verify(mockServiceListener).onServiceConnected(MOCK_PROFILE1, mockProxy1); verify(mockServiceListener).onServiceConnected(MOCK_PROFILE2, mockProxy2); }
@Test public void closeProfileProxy_reversesSetProfileProxy() { BluetoothProfile mockProxy = mock(BluetoothProfile.class); BluetoothProfile.ServiceListener mockServiceListener = mock(BluetoothProfile.ServiceListener.class); shadowBluetoothAdapter.setProfileProxy(MOCK_PROFILE1, mockProxy); bluetoothAdapter.closeProfileProxy(MOCK_PROFILE1, mockProxy); boolean result = bluetoothAdapter.getProfileProxy( RuntimeEnvironment.application, mockServiceListener, MOCK_PROFILE1); assertThat(result).isFalse(); verifyZeroInteractions(mockServiceListener); assertThat(shadowBluetoothAdapter.hasActiveProfileProxy(MOCK_PROFILE1)).isFalse(); }
protected boolean getBluetoothProfileProxy( Context context, BluetoothProfile.ServiceListener listener, int profile) { return bluetoothAdapter.getProfileProxy(context, listener, profile); }
/** * Initiate the connection to the profile proxy service. * * @param context Context that is required to establish the service connection. * @param listener Callback that will receive the profile proxy object. */ @MainThread void registerServiceListener(Context context, ServiceStateListener listener) { context = checkNotNull(context).getApplicationContext(); serviceStateListener = checkNotNull(listener); bluetoothAdapter.getProfileProxy( context, new ServiceListener(), BluetoothProfile.HID_DEVICE); }
@Override public void subscribe(@NonNull final ObservableEmitter<ServiceEvent> emitter) throws Exception { if (!bluetoothAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { emitter.onNext(new ServiceEvent(ServiceEvent.State.CONNECTED, profile, proxy)); } @Override public void onServiceDisconnected(int profile) { emitter.onNext(new ServiceEvent(ServiceEvent.State.DISCONNECTED, profile, null)); } }, bluetoothProfile)) { emitter.onError(new GetProfileProxyException()); } } });
BluetoothAdapter.getDefaultAdapter().getProfileProxy(xdrip.getAppContext(), serviceListener, BluetoothProfile.HEADSET);
BluetoothAdapter.getDefaultAdapter().getProfileProxy(xdrip.getAppContext(), serviceListener, BluetoothProfile.HEADSET);
return; if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener, BluetoothProfile.HEALTH)) { Toast.makeText(this, "bluetooth_health_profile_not_available",