Java Code Examples for android.bluetooth.BluetoothAdapter#getScanMode()

The following examples show how to use android.bluetooth.BluetoothAdapter#getScanMode() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: libcommon   File: BluetoothManager.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 他の機器から探索可能になるように要求する
 * bluetoothに対応していないか無効になっている時はIllegalStateException例外を投げる
 * @param activity
 * @param duration 探索可能時間[秒]
 * @return 既に探索可能であればtrue
 * @throws IllegalStateException
 */
public static boolean requestDiscoverable(@NonNull final Activity activity, final int duration) throws IllegalStateException {
	final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
	if ((adapter == null) || !adapter.isEnabled()) {
		throw new IllegalStateException("bluetoothに対応していないか無効になっている");
	}
	if (adapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
		final Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
		intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, duration);
		activity.startActivity(intent);
	}
	return adapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
}
 
Example 2
Source Project: libcommon   File: BluetoothManager.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 他の機器から探索可能になるように要求する
 * bluetoothに対応していないか無効になっている時はIllegalStateException例外を投げる
 * @param fragment
 * @param duration 0以下ならデフォルトの探索可能時間で120秒、 最大300秒まで設定できる
 * @return
 * @throws IllegalStateException
 */
public static boolean requestDiscoverable(@NonNull final Fragment fragment, final int duration) throws IllegalStateException {
	final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
	if ((adapter == null) || !adapter.isEnabled()) {
		throw new IllegalStateException("bluetoothに対応していないか無効になっている");
	}
	if (adapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
		final Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
		if ((duration > 0) && (duration <= 300)) {
			intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, duration);
		}
		fragment.startActivity(intent);
	}
	return adapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
}
 
Example 3
public void setDiscoverable() {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter == null) {
        return;
    }

    // change name if necessary
    String originalName = adapter.getName();
    if (originalName.lastIndexOf(_deviceNamePostFix) != originalName.length() - _deviceNamePostFix.length()) {
        if (_connectionHelperListener != null) {
            _connectionHelperListener.onAdapterNameChange(originalName);
        }

        Log.d(TAG, "setDiscoverable: Bluetooth device name set to " + (originalName + _deviceNamePostFix));
        adapter.setName(originalName + _deviceNamePostFix);
    }

    // set discoverable if necessary
    if (adapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        _activity.startActivity(discoverableIntent);
    }
}
 
Example 4
Source Project: tilt-game-android   File: ServerLobbyFragment.java    License: MIT License 6 votes vote down vote up
private void setDiscoverable() {
    Log.d(TAG, "setDiscoverable: ");

    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter == null) {
        return;
    }

    // change name if necessary
    String originalName = adapter.getName();
    if (originalName.lastIndexOf(GoogleFlipGameApplication.DEVICE_POSTFIX) != originalName.length() - GoogleFlipGameApplication.DEVICE_POSTFIX.length()) {
        GoogleFlipGameApplication.setOriginalBluetoothDeviceName(originalName);

        Log.d(TAG, "setDiscoverable: Bluetooth device name set to " + (originalName + GoogleFlipGameApplication.DEVICE_POSTFIX));
        adapter.setName(originalName + GoogleFlipGameApplication.DEVICE_POSTFIX);
    }

    // set discoverable if necessary
    if (adapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        Log.d(TAG, "setDiscoverable: scan mode is not discoverable, starting activity with code " + ActivityRequestCode.REQUEST_ENABLE_SCAN);
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120);
        getActivity().startActivityForResult(discoverableIntent, ActivityRequestCode.REQUEST_ENABLE_SCAN);
    }
}
 
Example 5
public static boolean isWorking() {
    BluetoothAdapter adapter = BluetoothUtil.getBluetoothAdapter(RumbleApplication.getContext());
    if ( adapter == null)
        return false;
    return ((adapter.getScanMode() == BluetoothAdapter.STATE_CONNECTING) ||
            (adapter.getScanMode() == BluetoothAdapter.STATE_DISCONNECTING) );
}
 
Example 6
private void resetTitle ()
 {
     BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

 	if (bluetoothAdapter.getScanMode() ==
          BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {        		

       setTitle(getString(R.string.app_name) 
       		+ " @"	+ mLocalAddress);
}
else    
	setTitle(getString(R.string.app_name) + ": " + getString(R.string.listen_mode));
 }
 
Example 7
public static boolean isDiscoverable() {
    BluetoothAdapter adapter = BluetoothUtil.getBluetoothAdapter(RumbleApplication.getContext());
    if ( adapter == null)
        return false;
    return (adapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
}