Android BluetoothAdapter - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android BluetoothAdapter class.

This code example shows how to use the following methods:isEnabled
        shadowBluetoothAdapter = shadowOf(bluetoothAdapter);
    } 
 
    @Test 
    public void testAdapterDefaultsDisabled() { 
        assertFalse(bluetoothAdapter.isEnabled());
    } 
 
    @Test 
    public void testAdapterCanBeEnabled() { 
        shadowBluetoothAdapter.setEnabled(true);
        assertTrue(bluetoothAdapter.isEnabled());
    } 
 
    @Test 
    public void canGetAndSetAddress() throws Exception { 
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        shadowOf(adapter).setAddress("expected");
        assertEquals("expected", adapter.getAddress());
    } 
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:cancelDiscovery, isEnabled
                Log.e(TAG, "Socket create() failed", e);
                return; 
            } 
            mmSocket = tmp;
            // Make a connection to the BluetoothSocket 
            if (mAdapter.isEnabled()) mAdapter.cancelDiscovery();
            try { 
                // This is a blocking call and will only return on a 
                // successful connection or an exception 
                if (D)  Log.d(TAG, getId() + ":Connecting to socket...");
                mmSocket.connect();
                if (D) Log.d(TAG, "connected with remote device: "
                        + mmDevice.getName() + " at address " + mmDevice.getAddress());
                connected(mmSocket);
            } catch (IOException e) {
                Log.w(TAG, getId() + ":connect failed.", e);
                return; 
            } 
        } 
 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getName, isEnabled
        getContext().unregisterReceiver(mReceiver);
    } 
 
    private void setSummaryToName() { 
        BluetoothAdapter adapter = mLocalManager.getBluetoothAdapter();
        if (adapter.isEnabled()) {
            setSummary(adapter.getName());
        } 
    } 
 
    @Override 
    protected boolean persistString(String value) {
        BluetoothAdapter adapter = mLocalManager.getBluetoothAdapter();
        adapter.setName(value);
        return true; 
    } 
 
    @Override 
    protected void onClick() { 
        super.onClick(); 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:isEnabled
		return this.btAdapter.disable();
	} 
 
	@Override 
	public boolean isBluetoothEnabled() { 
		return this.btAdapter.isEnabled();
	} 
 
	@Override 
	public void setApplication(Application application) {
		// unneeded - not implemented 
	} 
} 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:cancelDiscovery, isDiscovering
        if(device == null) {
            Log.w(TAG, "Can't setup socket -- device is " + device);
            throw new BluetoothException(); 
        } 
 
        if(mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        } 
 
        Log.d(TAG, "Scanning services on " + device);
        BluetoothSocket socket = null;
        try { 
            socket = device.createRfcommSocketToServiceRecord(
                    RFCOMM_UUID);
        } catch(IOException e) {
            String error = "Unable to open a socket to device " + device;
            Log.w(TAG, error);
            throw new BluetoothException(error, e);
        } 
 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:cancelDiscovery, isDiscovering, startDiscovery
 
        // Indicate scanning in the title 
        setProgressBarIndeterminateVisibility(true);
 
        // If we're already discovering, stop it 
        if (mBtAdapter.isDiscovering()) {
            mBtAdapter.cancelDiscovery();
        } 
 
        // Request discover from BluetoothAdapter 
        mBtAdapter.startDiscovery();
    } 
 
    // The BroadcastReceiver that listens for discovered devices and 
    // changes the title when discovery is finished 
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override 
        public void onReceive(Context context, Intent intent) { 
            String action = intent.getAction(); 
 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getAddress, getName, isDiscovering, isEnabled, startDiscovery
  } 
 
  private void configureBluetooth() { 
    bluetooth = BluetoothAdapter.getDefaultAdapter();
    String toastText;
    if (bluetooth.isEnabled()) {
      toastText = bluetooth.getName() + " : " + bluetooth.getAddress();
    } else { 
      toastText = "No bluetooth";
      // 监测状态 
      String actionStateChanged = BluetoothAdapter.ACTION_STATE_CHANGED;
      String actionRequestEnable = BluetoothAdapter.ACTION_REQUEST_ENABLE;
      registerReceiver(bluetoothState, new IntentFilter(actionStateChanged));
      startActivityForResult(new Intent(actionRequestEnable), 0);
    } 
 
    Toast.makeText(this, toastText, Toast.LENGTH_LONG).show();
 
    // 搜索蓝牙的等待过程 
    registerReceiver(discoveryMonitor, new IntentFilter(dStarted));

10
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getScanMode, getState, isDiscovering
	@Override 
	public String getLogValue() {
		if(m_btAdapter == null)
			return "No Device"; 
		else{ 
			String rstr = "" + getStateStr(m_btAdapter.getState()) + " " + getScanModeStr(m_btAdapter.getScanMode()) + " " + m_btAdapter.isDiscovering();
			return rstr;
		} 
		 
	} 
	 
	private String getStateStr(int code){
		switch(code){
		case 10: return "STATE_OFF"; 
		case 12: return "STATE_ON"; 
		case 13: return "STATE_TURNING_OFF"; 
		case 11: return "STATE_TURNING_ON"; 
		default: return "UNKNOWN_CODE"; 
		} 
	} 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:disable, enable, isEnabled
 
    private void syncBluetoothState() { 
        int bluetoothState;
 
        if (mAdapter != null) {
            bluetoothState = mAdapter.isEnabled()
                    ? BluetoothAdapter.STATE_ON
                    : BluetoothAdapter.STATE_OFF;
        } else { 
            bluetoothState = BluetoothAdapter.ERROR;
        } 
 
        setBluetoothStateInt(bluetoothState);
    } 
 
    public void setBluetoothEnabled(boolean enabled) {
        boolean wasSetStateSuccessful = enabled
                ? mAdapter.enable()
                : mAdapter.disable();
 

6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:disable, enable, getState
			this.btAdapter = btAdapter;
		} 
		 
		public boolean onPreferenceClick(Preference preference) {
			if (null != btAdapter && BLUETOOTH_STATE.equals(preference.getKey())) {
				switch (btAdapter.getState()) {
				case BluetoothAdapter.STATE_OFF:
					btAdapter.enable();
					break; 
 
				case BluetoothAdapter.STATE_ON:
					btAdapter.disable();
					break; 
				} 
				return true; 
			} 
			return false; 
		}		 
	} 
	 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java

Related Android BluetoothAdapter Questions & Answers:

See Code Examples for Android BluetoothAdapter Methods: