Search in sources :

Example 1 with BluetoothProfile

use of android.bluetooth.BluetoothProfile in project android_frameworks_base by ParanoidAndroid.

the class BluetoothTestUtils method connectProfile.

/**
     * Connects a profile from the local device to a remote device and checks to make sure that the
     * profile is connected and that the correct actions were broadcast.
     *
     * @param adapter The BT adapter.
     * @param device The remote device.
     * @param profile The profile to connect. One of {@link BluetoothProfile#A2DP},
     * {@link BluetoothProfile#HEADSET}, or {@link BluetoothProfile#INPUT_DEVICE}.
     * @param methodName The method name to printed in the logs.  If null, will be
     * "connectProfile(profile=<profile>, device=<device>)"
     */
public void connectProfile(BluetoothAdapter adapter, BluetoothDevice device, int profile, String methodName) {
    if (methodName == null) {
        methodName = String.format("connectProfile(profile=%d, device=%s)", profile, device);
    }
    int mask = (ConnectProfileReceiver.STATE_CONNECTING_FLAG | ConnectProfileReceiver.STATE_CONNECTED_FLAG);
    long start = -1;
    if (!adapter.isEnabled()) {
        fail(String.format("%s bluetooth not enabled", methodName));
    }
    if (!adapter.getBondedDevices().contains(device)) {
        fail(String.format("%s device not paired", methodName));
    }
    BluetoothProfile proxy = connectProxy(adapter, profile);
    assertNotNull(proxy);
    ConnectProfileReceiver receiver = getConnectProfileReceiver(device, profile, mask);
    int state = proxy.getConnectionState(device);
    switch(state) {
        case BluetoothProfile.STATE_CONNECTED:
            removeReceiver(receiver);
            return;
        case BluetoothProfile.STATE_CONNECTING:
            // Don't check for received intents since we might have missed them.
            mask = 0;
            break;
        case BluetoothProfile.STATE_DISCONNECTED:
        case BluetoothProfile.STATE_DISCONNECTING:
            start = System.currentTimeMillis();
            if (profile == BluetoothProfile.A2DP) {
                assertTrue(((BluetoothA2dp) proxy).connect(device));
            } else if (profile == BluetoothProfile.HEADSET) {
                assertTrue(((BluetoothHeadset) proxy).connect(device));
            } else if (profile == BluetoothProfile.INPUT_DEVICE) {
                assertTrue(((BluetoothInputDevice) proxy).connect(device));
            }
            break;
        default:
            removeReceiver(receiver);
            fail(String.format("%s invalid state: state=%d", methodName, state));
    }
    long s = System.currentTimeMillis();
    while (System.currentTimeMillis() - s < CONNECT_DISCONNECT_PROFILE_TIMEOUT) {
        state = proxy.getConnectionState(device);
        if (state == BluetoothProfile.STATE_CONNECTED && (receiver.getFiredFlags() & mask) == mask) {
            long finish = receiver.getCompletedTime();
            if (start != -1 && finish != -1) {
                writeOutput(String.format("%s completed in %d ms", methodName, (finish - start)));
            } else {
                writeOutput(String.format("%s completed", methodName));
            }
            removeReceiver(receiver);
            return;
        }
        sleep(POLL_TIME);
    }
    int firedFlags = receiver.getFiredFlags();
    removeReceiver(receiver);
    fail(String.format("%s timeout: state=%d (expected %d), flags=0x%x (expected 0x%x)", methodName, state, BluetoothProfile.STATE_CONNECTED, firedFlags, mask));
}
Also used : BluetoothProfile(android.bluetooth.BluetoothProfile)

Example 2 with BluetoothProfile

use of android.bluetooth.BluetoothProfile in project android_frameworks_base by ParanoidAndroid.

the class BluetoothTestUtils method disconnectProfile.

/**
     * Disconnects a profile between the local device and a remote device and checks to make sure
     * that the profile is disconnected and that the correct actions were broadcast.
     *
     * @param adapter The BT adapter.
     * @param device The remote device.
     * @param profile The profile to disconnect. One of {@link BluetoothProfile#A2DP},
     * {@link BluetoothProfile#HEADSET}, or {@link BluetoothProfile#INPUT_DEVICE}.
     * @param methodName The method name to printed in the logs.  If null, will be
     * "connectProfile(profile=&lt;profile&gt;, device=&lt;device&gt;)"
     */
public void disconnectProfile(BluetoothAdapter adapter, BluetoothDevice device, int profile, String methodName) {
    if (methodName == null) {
        methodName = String.format("disconnectProfile(profile=%d, device=%s)", profile, device);
    }
    int mask = (ConnectProfileReceiver.STATE_DISCONNECTING_FLAG | ConnectProfileReceiver.STATE_DISCONNECTED_FLAG);
    long start = -1;
    if (!adapter.isEnabled()) {
        fail(String.format("%s bluetooth not enabled", methodName));
    }
    if (!adapter.getBondedDevices().contains(device)) {
        fail(String.format("%s device not paired", methodName));
    }
    BluetoothProfile proxy = connectProxy(adapter, profile);
    assertNotNull(proxy);
    ConnectProfileReceiver receiver = getConnectProfileReceiver(device, profile, mask);
    int state = proxy.getConnectionState(device);
    switch(state) {
        case BluetoothProfile.STATE_CONNECTED:
        case BluetoothProfile.STATE_CONNECTING:
            start = System.currentTimeMillis();
            if (profile == BluetoothProfile.A2DP) {
                assertTrue(((BluetoothA2dp) proxy).disconnect(device));
            } else if (profile == BluetoothProfile.HEADSET) {
                assertTrue(((BluetoothHeadset) proxy).disconnect(device));
            } else if (profile == BluetoothProfile.INPUT_DEVICE) {
                assertTrue(((BluetoothInputDevice) proxy).disconnect(device));
            }
            break;
        case BluetoothProfile.STATE_DISCONNECTED:
            removeReceiver(receiver);
            return;
        case BluetoothProfile.STATE_DISCONNECTING:
            // Don't check for received intents since we might have missed them.
            mask = 0;
            break;
        default:
            removeReceiver(receiver);
            fail(String.format("%s invalid state: state=%d", methodName, state));
    }
    long s = System.currentTimeMillis();
    while (System.currentTimeMillis() - s < CONNECT_DISCONNECT_PROFILE_TIMEOUT) {
        state = proxy.getConnectionState(device);
        if (state == BluetoothProfile.STATE_DISCONNECTED && (receiver.getFiredFlags() & mask) == mask) {
            long finish = receiver.getCompletedTime();
            if (start != -1 && finish != -1) {
                writeOutput(String.format("%s completed in %d ms", methodName, (finish - start)));
            } else {
                writeOutput(String.format("%s completed", methodName));
            }
            removeReceiver(receiver);
            return;
        }
        sleep(POLL_TIME);
    }
    int firedFlags = receiver.getFiredFlags();
    removeReceiver(receiver);
    fail(String.format("%s timeout: state=%d (expected %d), flags=0x%x (expected 0x%x)", methodName, state, BluetoothProfile.STATE_DISCONNECTED, firedFlags, mask));
}
Also used : BluetoothProfile(android.bluetooth.BluetoothProfile)

Example 3 with BluetoothProfile

use of android.bluetooth.BluetoothProfile in project platform_frameworks_base by android.

the class BluetoothTestUtils method connectProfile.

/**
     * Connects a profile from the local device to a remote device and checks to make sure that the
     * profile is connected and that the correct actions were broadcast.
     *
     * @param adapter The BT adapter.
     * @param device The remote device.
     * @param profile The profile to connect. One of {@link BluetoothProfile#A2DP},
     * {@link BluetoothProfile#HEADSET}, or {@link BluetoothProfile#INPUT_DEVICE}.
     * @param methodName The method name to printed in the logs.  If null, will be
     * "connectProfile(profile=&lt;profile&gt;, device=&lt;device&gt;)"
     */
public void connectProfile(BluetoothAdapter adapter, BluetoothDevice device, int profile, String methodName) {
    if (methodName == null) {
        methodName = String.format("connectProfile(profile=%d, device=%s)", profile, device);
    }
    int mask = (ConnectProfileReceiver.STATE_CONNECTING_FLAG | ConnectProfileReceiver.STATE_CONNECTED_FLAG);
    long start = -1;
    if (!adapter.isEnabled()) {
        fail(String.format("%s bluetooth not enabled", methodName));
    }
    if (!adapter.getBondedDevices().contains(device)) {
        fail(String.format("%s device not paired", methodName));
    }
    BluetoothProfile proxy = connectProxy(adapter, profile);
    assertNotNull(proxy);
    ConnectProfileReceiver receiver = getConnectProfileReceiver(device, profile, mask);
    int state = proxy.getConnectionState(device);
    switch(state) {
        case BluetoothProfile.STATE_CONNECTED:
            removeReceiver(receiver);
            return;
        case BluetoothProfile.STATE_CONNECTING:
            // Don't check for received intents since we might have missed them.
            mask = 0;
            break;
        case BluetoothProfile.STATE_DISCONNECTED:
        case BluetoothProfile.STATE_DISCONNECTING:
            start = System.currentTimeMillis();
            if (profile == BluetoothProfile.A2DP) {
                assertTrue(((BluetoothA2dp) proxy).connect(device));
            } else if (profile == BluetoothProfile.HEADSET) {
                assertTrue(((BluetoothHeadset) proxy).connect(device));
            } else if (profile == BluetoothProfile.INPUT_DEVICE) {
                assertTrue(((BluetoothInputDevice) proxy).connect(device));
            }
            break;
        default:
            removeReceiver(receiver);
            fail(String.format("%s invalid state: state=%d", methodName, state));
    }
    long s = System.currentTimeMillis();
    while (System.currentTimeMillis() - s < CONNECT_DISCONNECT_PROFILE_TIMEOUT) {
        state = proxy.getConnectionState(device);
        if (state == BluetoothProfile.STATE_CONNECTED && (receiver.getFiredFlags() & mask) == mask) {
            long finish = receiver.getCompletedTime();
            if (start != -1 && finish != -1) {
                writeOutput(String.format("%s completed in %d ms", methodName, (finish - start)));
            } else {
                writeOutput(String.format("%s completed", methodName));
            }
            removeReceiver(receiver);
            return;
        }
        sleep(POLL_TIME);
    }
    int firedFlags = receiver.getFiredFlags();
    removeReceiver(receiver);
    fail(String.format("%s timeout: state=%d (expected %d), flags=0x%x (expected 0x%x)", methodName, state, BluetoothProfile.STATE_CONNECTED, firedFlags, mask));
}
Also used : BluetoothProfile(android.bluetooth.BluetoothProfile)

Example 4 with BluetoothProfile

use of android.bluetooth.BluetoothProfile in project platform_frameworks_base by android.

the class Tethering method setBluetoothTethering.

private void setBluetoothTethering(final boolean enable, final ResultReceiver receiver) {
    final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter == null || !adapter.isEnabled()) {
        Log.w(TAG, "Tried to enable bluetooth tethering with null or disabled adapter. null: " + (adapter == null));
        sendTetherResult(receiver, ConnectivityManager.TETHER_ERROR_SERVICE_UNAVAIL);
        return;
    }
    adapter.getProfileProxy(mContext, new ServiceListener() {

        @Override
        public void onServiceDisconnected(int profile) {
        }

        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            ((BluetoothPan) proxy).setBluetoothTethering(enable);
            // TODO: Enabling bluetooth tethering can fail asynchronously here.
            // We should figure out a way to bubble up that failure instead of sending success.
            int result = ((BluetoothPan) proxy).isTetheringOn() == enable ? ConnectivityManager.TETHER_ERROR_NO_ERROR : ConnectivityManager.TETHER_ERROR_MASTER_ERROR;
            sendTetherResult(receiver, result);
            if (enable && isTetherProvisioningRequired()) {
                scheduleProvisioningRechecks(ConnectivityManager.TETHERING_BLUETOOTH);
            }
            adapter.closeProfileProxy(BluetoothProfile.PAN, proxy);
        }
    }, BluetoothProfile.PAN);
}
Also used : ServiceListener(android.bluetooth.BluetoothProfile.ServiceListener) BluetoothProfile(android.bluetooth.BluetoothProfile) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 5 with BluetoothProfile

use of android.bluetooth.BluetoothProfile in project android_frameworks_base by DirtyUnicorns.

the class BluetoothTestUtils method disconnectProfile.

/**
     * Disconnects a profile between the local device and a remote device and checks to make sure
     * that the profile is disconnected and that the correct actions were broadcast.
     *
     * @param adapter The BT adapter.
     * @param device The remote device.
     * @param profile The profile to disconnect. One of {@link BluetoothProfile#A2DP},
     * {@link BluetoothProfile#HEADSET}, or {@link BluetoothProfile#INPUT_DEVICE}.
     * @param methodName The method name to printed in the logs.  If null, will be
     * "connectProfile(profile=&lt;profile&gt;, device=&lt;device&gt;)"
     */
public void disconnectProfile(BluetoothAdapter adapter, BluetoothDevice device, int profile, String methodName) {
    if (methodName == null) {
        methodName = String.format("disconnectProfile(profile=%d, device=%s)", profile, device);
    }
    int mask = (ConnectProfileReceiver.STATE_DISCONNECTING_FLAG | ConnectProfileReceiver.STATE_DISCONNECTED_FLAG);
    long start = -1;
    if (!adapter.isEnabled()) {
        fail(String.format("%s bluetooth not enabled", methodName));
    }
    if (!adapter.getBondedDevices().contains(device)) {
        fail(String.format("%s device not paired", methodName));
    }
    BluetoothProfile proxy = connectProxy(adapter, profile);
    assertNotNull(proxy);
    ConnectProfileReceiver receiver = getConnectProfileReceiver(device, profile, mask);
    int state = proxy.getConnectionState(device);
    switch(state) {
        case BluetoothProfile.STATE_CONNECTED:
        case BluetoothProfile.STATE_CONNECTING:
            start = System.currentTimeMillis();
            if (profile == BluetoothProfile.A2DP) {
                assertTrue(((BluetoothA2dp) proxy).disconnect(device));
            } else if (profile == BluetoothProfile.HEADSET) {
                assertTrue(((BluetoothHeadset) proxy).disconnect(device));
            } else if (profile == BluetoothProfile.INPUT_DEVICE) {
                assertTrue(((BluetoothInputDevice) proxy).disconnect(device));
            }
            break;
        case BluetoothProfile.STATE_DISCONNECTED:
            removeReceiver(receiver);
            return;
        case BluetoothProfile.STATE_DISCONNECTING:
            // Don't check for received intents since we might have missed them.
            mask = 0;
            break;
        default:
            removeReceiver(receiver);
            fail(String.format("%s invalid state: state=%d", methodName, state));
    }
    long s = System.currentTimeMillis();
    while (System.currentTimeMillis() - s < CONNECT_DISCONNECT_PROFILE_TIMEOUT) {
        state = proxy.getConnectionState(device);
        if (state == BluetoothProfile.STATE_DISCONNECTED && (receiver.getFiredFlags() & mask) == mask) {
            long finish = receiver.getCompletedTime();
            if (start != -1 && finish != -1) {
                writeOutput(String.format("%s completed in %d ms", methodName, (finish - start)));
            } else {
                writeOutput(String.format("%s completed", methodName));
            }
            removeReceiver(receiver);
            return;
        }
        sleep(POLL_TIME);
    }
    int firedFlags = receiver.getFiredFlags();
    removeReceiver(receiver);
    fail(String.format("%s timeout: state=%d (expected %d), flags=0x%x (expected 0x%x)", methodName, state, BluetoothProfile.STATE_DISCONNECTED, firedFlags, mask));
}
Also used : BluetoothProfile(android.bluetooth.BluetoothProfile)

Aggregations

BluetoothProfile (android.bluetooth.BluetoothProfile)17 BluetoothAdapter (android.bluetooth.BluetoothAdapter)5 ServiceListener (android.bluetooth.BluetoothProfile.ServiceListener)5