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 . 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
/**
 * 
 */
@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 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 3
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 4
Source Project: BluetoothHidEmu   File: Settings.java    License: 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
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 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 7
/**
 * 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 8
Source Project: AndroidDemo   File: BluetoothMainActivity.java    License: 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 9
Source Project: AndroidDemo   File: BluetoothListFragment.java    License: 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 10
Source Project: J2ME-Loader   File: LocalDevice.java    License: 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 11
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 12
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 13
/**
 * 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
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 15
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 16
Source Project: retrowatch   File: RetroWatchActivity.java    License: 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 17
@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 18
Source Project: retrowatch   File: RetroWatchActivity.java    License: 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 19
@CalledByNative
private boolean isDiscoverable() {
    return isPresent()
            && mAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
}
 
Example 20
public static boolean isDiscoverable() {
    BluetoothAdapter adapter = BluetoothUtil.getBluetoothAdapter(RumbleApplication.getContext());
    if ( adapter == null)
        return false;
    return (adapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
}