Java Code Examples for android.bluetooth.BluetoothAdapter#ACTION_REQUEST_DISCOVERABLE

The following examples show how to use android.bluetooth.BluetoothAdapter#ACTION_REQUEST_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
Source Project: DataLogger   File: DisplayActivity.java    License: MIT License 6 votes vote down vote up
public void onClick(View v) {
    int state = SharedPreferencesHelper.getBluetoothStatus(DisplayActivity.this);
    switch (state) {
        case Constants.BLUETOOTH_STATE_DISABLED:
            startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 0);
            break;
        case Constants.BLUETOOTH_STATE_ENABLED:
        case Constants.BLUETOOTH_STATE_CONNECTING:
        case Constants.BLUETOOTH_STATE_CONNECTED:
            boolean isMaster = (mDeviceLocation == Constants.DEVICE_LOCATION_HAND);
            if (isMaster) {
                Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
                startActivity(discoverableIntent);
            } else {
                Intent serverIntent = new Intent(DisplayActivity.this, BluetoothDeviceListActivity.class);
                startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
            }
            break;
    }
}
 
Example 2
Source Project: J2ME-Loader   File: LocalDevice.java    License: Apache License 2.0 6 votes vote down vote up
public boolean setDiscoverable(int mode) throws BluetoothStateException {
	if ((mode != DiscoveryAgent.GIAC) && (mode != DiscoveryAgent.LIAC) && (mode != DiscoveryAgent.NOT_DISCOVERABLE)
			&& (mode < 0x9E8B00 || mode > 0x9E8B3F)) {
		throw new IllegalArgumentException("Invalid discoverable mode");
	}

	if (lock || mode == DiscoveryAgent.NOT_DISCOVERABLE)
		return true;

	Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
	lock = true;
	ContextHolder.getActivity().startActivityForResult(discoverableIntent, 1);
	while (lock) {
		synchronized (monitor) {
			try {
				monitor.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
				lock = false;
			}
		}
	}
	return true;
}
 
Example 3
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 4
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 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
private void ensureBluetoothDiscoverableThenStart() {
    Utils.debugLog(TAG, "Ensuring Bluetooth is in discoverable mode.");
    if (bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        Utils.debugLog(TAG, "Not currently in discoverable mode, so prompting user to enable.");
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600); // 1 hour
        startActivityForResult(intent, REQUEST_BLUETOOTH_DISCOVERABLE);
    }
    BluetoothManager.start(this);
}
 
Example 7
Source Project: BluetoothHidEmu   File: Settings.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param duration
 * @return
 */
public static Intent createBluetoothDiscoverableIntent(final int duration) {
    Intent bluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    bluetoothIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, duration);
    
    return bluetoothIntent;
}
 
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
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.enable_visibility:
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            enableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 600);
            startActivityForResult(enableIntent, Params.REQUEST_ENABLE_VISIBILITY);
            break;
        case R.id.discovery:
            if (bluetoothAdapter.isDiscovering()) {
                bluetoothAdapter.cancelDiscovery();
            }
            if (Build.VERSION.SDK_INT >= 6.0) {
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        Params.MY_PERMISSION_REQUEST_CONSTANT);
            }

            bluetoothAdapter.startDiscovery();
            break;
        case R.id.disconnect:
            bluetoothAdapter.disable();
            deviceList.clear();
            listAdapter.notifyDataSetChanged();
            toast("蓝牙已关闭");
            break;
    }
    return super.onOptionsItemSelected(item);

}
 
Example 11
/**
 * Enable the current {@link BluetoothAdapter} to be discovered (available for pairing) for
 * the next {@link #DISCOVERABLE_TIMEOUT_MS} ms.
 */
private void enableDiscoverable() {
    Log.d(TAG, "Registering for discovery.");
    Intent discoverableIntent =
            new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
            DISCOVERABLE_TIMEOUT_MS);
    startActivityForResult(discoverableIntent, REQUEST_CODE_ENABLE_DISCOVERABLE);
}
 
Example 12
/**
 * 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 13
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 14
@Kroll.method
public void makeDiscoverable() 
{	
	if(bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
	{		
		TiApplication appContext = TiApplication.getInstance();
		Activity activity = appContext.getCurrentActivity();
		
		Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
	    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
	    activity.startActivity(discoverableIntent);
	}
}
 
Example 15
/**
 * Send the F-Droid APK via Bluetooth.  If Bluetooth has not been
 * enabled/turned on, then enabling device discoverability will
 * automatically enable Bluetooth.
 */
public void sendFDroidBluetooth() {
    if (bluetoothAdapter.isEnabled()) {
        sendFDroidApk();
    } else {
        Intent discoverBt = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverBt.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120);
        startActivityForResult(discoverBt, REQUEST_BLUETOOTH_ENABLE_FOR_SEND);
    }
}
 
Example 16
@Override
public void onNearby() {
    // If Bluetooth has not been enabled/turned on, then
    // enabling device discoverability will automatically enable Bluetooth
    Intent discoverBt = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverBt.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 121);
    startActivityForResult(discoverBt, REQUEST_ENABLE_BLUETOOTH);
    // if this is successful, the Bluetooth transfer is started
}
 
Example 17
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 18
/**
 * 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
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 20
Source Project: BlueToothEatPhone   File: MainActivity.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void onClick(View view) {
    if (mBluetoothAdapter == null)
        return;
    switch (view.getId()) {
        case R.id.btn_start:
            /**如果本地蓝牙没有开启,则开启*/
            if (!mBluetoothAdapter.isEnabled()) {
                // 我们通过startActivityForResult()方法发起的Intent将会在onActivityResult()回调方法中获取用户的选择,比如用户单击了Yes开启,
                // 那么将会收到RESULT_OK的结果,
                // 如果RESULT_CANCELED则代表用户不愿意开启蓝牙
                Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(mIntent, ENABLE_BLUE);
            } else {
                Toast.makeText(this, "蓝牙已开启", Toast.LENGTH_SHORT).show();
                getBondedDevices();
            }
            break;
        case R.id.btn_public:
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 180);//180可见时间
            startActivity(intent);
            break;
        case R.id.btn_search:

            showProgressDailog();
            // 如果正在搜索,就先取消搜索
            if (mBluetoothAdapter.isDiscovering()) {
                mBluetoothAdapter.cancelDiscovery();
            }
            // 开始搜索蓝牙设备,搜索到的蓝牙设备通过广播返回
            mBluetoothAdapter.startDiscovery();
            break;
        case R.id.btn_stop:
            /**关闭蓝牙*/
            if (mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.disable();
            }
            break;
        case R.id.btn_play:
            AudioUtils.INSTANCE.playMedai(MainActivity.this);
            break;
        case R.id.btn_stop_play:
            AudioUtils.INSTANCE.stopPlay();
            break;
        default:
            break;
    }
}