Java Code Examples for android.bluetooth.BluetoothAdapter#SCAN_MODE_CONNECTABLE_DISCOVERABLE

The following examples show how to use android.bluetooth.BluetoothAdapter#SCAN_MODE_CONNECTABLE_DISCOVERABLE . 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 File: BluetoothAdapterStateReceiver.java    From BluetoothHidEmu with Apache License 2.0 6 votes vote down vote up
/**
 * 
 */
@Override
public void onReceive(Context context, Intent intent) {

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
        DoLog.d(TAG, "BluetoothAdapter turned off. Bailing out...");
        mActivity.finish();
    } else if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(intent.getAction())) {
        int scanMode = intent.getExtras().getInt(BluetoothAdapter.EXTRA_SCAN_MODE);
        DoLog.d(TAG, "Scan mode changed: " + scanMode);

        if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            
            SpoofMode spoofMode = Settings.getPrefEmulationMode(context);
            
            if (!mSpoofer.isSpoofed()) mSpoofer.tearUpSpoofing(spoofMode);
        } else {
            if (mSpoofer.isSpoofed()) mSpoofer.tearDownSpoofing();
        }
        
    }

}
 
Example 2
Source File: BluetoothManager.java    From libcommon with 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 3
Source File: BluetoothManager.java    From libcommon with 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 4
Source File: Settings.java    From BluetoothHidEmu with Apache License 2.0 6 votes vote down vote up
public void run() {
    
    if (BluetoothAdapter.getDefaultAdapter().getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
    
        
        if (mCountdown != Integer.MAX_VALUE && mCountdown > 0) {
            mBtDiscoverablePreference.setSummary(
                    getResources().getQuantityString(
                            R.plurals.msg_pref_summary_bluetooth_discoverable_timeout, 
                            mCountdown, 
                            mCountdown));
            mCountdown--;
            
        } else if (mCountdown == Integer.MAX_VALUE) {
            mBtDiscoverablePreference.setSummary(R.string.msg_pref_summary_bluetooth_discoverable_no_timeout);
        }
        
        mUiUpdateHandler.postDelayed(this, 1000 /* ms */);
        
    } else {
        setBluetoothDiscoverableCheck(false);
    }
    
}
 
Example 5
Source File: BluetoothServerConnectionHelper.java    From tilt-game-android with MIT License 6 votes vote down vote up
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 6
Source File: ServerLobbyFragment.java    From tilt-game-android with 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 7
Source File: RetroWatchActivity.java    From retrowatch with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure this device is discoverable by others
 */
private void ensureDiscoverable() {
	if (mService.getBluetoothScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
		Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
		intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
		startActivity(intent);
	}
}
 
Example 8
Source File: BluetoothChatFragment.java    From android-BluetoothChat with Apache License 2.0 5 votes vote down vote up
/**
 * Makes this device discoverable for 300 seconds (5 minutes).
 */
private void ensureDiscoverable() {
    if (mBluetoothAdapter.getScanMode() !=
            BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);
    }
}
 
Example 9
Source File: SwapService.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    switch (intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, -1)) {
        case BluetoothAdapter.SCAN_MODE_NONE:
            BluetoothManager.stop(SwapService.this);
            break;

        case BluetoothAdapter.SCAN_MODE_CONNECTABLE:
        case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
            BluetoothManager.start(SwapService.this);
            break;
    }
}
 
Example 10
Source File: RetroWatchActivity.java    From retrowatch with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure this device is discoverable by others
 */
private void ensureDiscoverable() {
	if (mService.getBluetoothScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
		Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
		intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
		startActivity(intent);
	}
}
 
Example 11
Source File: GilgaService.java    From gilgamesh with GNU General Public License v3.0 5 votes vote down vote up
private void startBroadcasting() {
  //  if(D) Log.d(TAG, "ensure discoverable");
   if (mBluetoothAdapter.getScanMode() !=
      BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
    	
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600);
        discoverableIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(discoverableIntent);
        
    }
   
   if (!mBluetoothAdapter.isDiscovering())
	   mBluetoothAdapter.startDiscovery();

   if (mDirectChatSession == null)
   {
	   mDirectChatSession = new DirectMessageSession(this, mHandler);
	   mDirectChatSession.start();
   }
   else {
       // Only if the state is STATE_NONE, do we know that we haven't started already
       if (mDirectChatSession.getState() == DirectMessageSession.STATE_NONE) {
         // Start the Bluetooth chat services
    	   mDirectChatSession.start();
       }
       
   }
    
}
 
Example 12
Source File: GilgaMeshActivity.java    From gilgamesh with GNU General Public License v3.0 5 votes vote down vote up
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 13
Source File: MainActivity.java    From BTChat with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Ensure this device is discoverable by others
 */
private void ensureDiscoverable() {
	if (mService.getBluetoothScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
		Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
		intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
		startActivity(intent);
	}
}
 
Example 14
Source File: SerialInterface_BT.java    From PodEmu with GNU General Public License v3.0 5 votes vote down vote up
private synchronized void ensureDiscoverable()
{
    if(this.isBtAskingDiscoverability) return;

    //if(!btAdapter.isEnabled() )// ||
    //        ((statusBT != STATUS_DISCOVERABLE) && (statusBT != STATUS_CONNECTED)))
    if(btAdapter!=null && btAdapter.isEnabled())
    {
        PodEmuLog.debug("SIBT: Bluetooth adapter is enabled");
        if(btAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
        {
            PodEmuLog.debug("SIBT: Bluetooth discovery turned off. Turning on...");
            this.isBtAskingDiscoverability = true;
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            //discoverableIntent.putExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", mPC.time_to_discoverable);
        }
        else
        {
            PodEmuLog.debug("SIBT: Bluetooth discovery is on.");
            //if(mBtService == null)
            //    initService();
        }
    }
    /*else
    {
        PodEmuLog.debug("SIBT: Bluetooth adapter is not enabled");
        //if(mBtService == null)
        //    initService();
    }*/
}
 
Example 15
Source File: MainActivity.java    From Car_remote_control with GNU General Public License v3.0 5 votes vote down vote up
private void ensureDiscoverable()
{
    if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
    {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);
    }
}
 
Example 16
Source File: LocalDevice.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
public int getDiscoverable() {
	int scanMode = DiscoveryAgent.adapter.getScanMode();
	switch (scanMode) {
		case BluetoothAdapter.SCAN_MODE_CONNECTABLE:
			return DiscoveryAgent.LIAC;
		case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
			return DiscoveryAgent.GIAC;
		case BluetoothAdapter.SCAN_MODE_NONE:
		default:
			return DiscoveryAgent.NOT_DISCOVERABLE;
	}
}
 
Example 17
Source File: BluetoothListFragment.java    From AndroidDemo with MIT License 5 votes vote down vote up
/**
 * 蓝牙开放搜索
 */
private void requestBluetoothDiscoverable() {
    if (bluetoothAdapter.getScanMode() !=
            BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivityForResult(intent, RequestBluetoothDiscoverable);
    }
}
 
Example 18
Source File: BluetoothMainActivity.java    From AndroidDemo with MIT License 5 votes vote down vote up
private void deviceDiscoverable() {
    if (bluetoothAdapter.getScanMode() !=
            BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(intent);
    }
}
 
Example 19
Source File: BluetoothUtil.java    From Rumble with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isDiscoverable() {
    BluetoothAdapter adapter = BluetoothUtil.getBluetoothAdapter(RumbleApplication.getContext());
    if ( adapter == null)
        return false;
    return (adapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
}
 
Example 20
Source File: ChromeBluetoothAdapter.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@CalledByNative
private boolean isDiscoverable() {
    return isPresent()
            && mAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
}