Java Code Examples for android.bluetooth.BluetoothDevice#BOND_NONE

The following examples show how to use android.bluetooth.BluetoothDevice#BOND_NONE . 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: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public final void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final int status) {
	if (status == BluetoothGatt.GATT_SUCCESS) {
		// The value has been written. Notify the profile and proceed with the initialization queue.
		profile.onCharacteristicWrite(gatt, characteristic);
		operationInProgress = false;
		nextRequest();
	} else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
		if (gatt.getDevice().getBondState() != BluetoothDevice.BOND_NONE) {
			// This should never happen but it used to: http://stackoverflow.com/a/20093695/2115352
			DebugLogger.w(TAG, ERROR_AUTH_ERROR_WHILE_BONDED);
			onError(gatt.getDevice(), ERROR_AUTH_ERROR_WHILE_BONDED, status);
		}
	} else {
		DebugLogger.e(TAG, "onCharacteristicWrite error " + status);
		onError(gatt.getDevice(), ERROR_WRITE_CHARACTERISTIC, status);
	}
}
 
Example 2
Source File: ShareTest.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
        final int state        = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
        final int prevState    = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);
        if (state == BluetoothDevice.BOND_BONDED) {
            Log.d(TAG, "CALLBACK RECIEVED Bonded");
            currentGattTask = GATT_SETUP;
            mBluetoothGatt.discoverServices();
        } else if (state == BluetoothDevice.BOND_NONE){
            Log.d(TAG, "CALLBACK RECIEVED: Not Bonded");
            Toast.makeText(getApplicationContext(), "unBonded", Toast.LENGTH_LONG).show();
        } else if (state == BluetoothDevice.BOND_BONDING) {
            Log.d(TAG, "CALLBACK RECIEVED: Trying to bond");
            Toast.makeText(getApplicationContext(), "trying to bond", Toast.LENGTH_LONG).show();
        }
    }
}
 
Example 3
Source File: DeviceListActivity.java    From AndroBluetooth with Apache License 2.0 6 votes vote down vote up
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    
    if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {	        	
    	 final int state 		= intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
    	 final int prevState	= intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);
    	 
    	 if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {
    		 showToast("Paired");
    	 } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){
    		 showToast("Unpaired");
    	 }
    	 
    	 mAdapter.notifyDataSetChanged();
    }
}
 
Example 4
Source File: DexShareCollectionService.java    From xDrip-Experimental with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    Log.d(TAG, "characteristic wrote " + status);
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.d(TAG, "Wrote a characteristic successfully " + characteristic.getUuid());
        if (mAuthenticationCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_authSucess = true;
            gatt.readCharacteristic(mHeartBeatCharacteristic);
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to");
        }
    } else {
        Log.e(TAG, "Unknown error writing Characteristic");
    }
}
 
Example 5
Source File: DexShareCollectionService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    Log.d(TAG, "characteristic wrote " + status);
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.d(TAG, "Wrote a characteristic successfully " + characteristic.getUuid());
        if (mAuthenticationCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_authSucess = true;
            gatt.readCharacteristic(mHeartBeatCharacteristic);
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to: oncharacteristicwrite code: "+status+ "bond: "+gatt.getDevice().getBondState());
        }
    } else {
        Log.e(TAG, "Unknown error writing Characteristic");
    }
}
 
Example 6
Source File: BluetoothDeviceListAdapter.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * When a view is clicked, attempt to pair the bluetooth device at that position with the phone.
 *
 * @param view the view containing the selected bluetooth device
 */
@RequiresPermission(allOf = {permission.BLUETOOTH_ADMIN, permission.BLUETOOTH})
@Override
public void onClick(View view) {
  ComparableBluetoothDevice clickedDevice = getItem(viewHolder.getAdapterPosition());
  if (clickedDevice.getBondState() == BluetoothDevice.BOND_NONE) {
    clickedDevice.startPairing();
  } else {
    clickedDevice.reconnect(
        descriptionManager.reconnectingUnsupportedTitle(),
        descriptionManager.reconnectingUnsupportedMessage(),
        descriptionManager.launchBluetoothSettingsButtonText());
  }

  viewHolder.updateConnectionDescriptionText(clickedDevice);
}
 
Example 7
Source File: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public final void onDescriptorWrite(final BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, final int status) {
	if (status == BluetoothGatt.GATT_SUCCESS) {
		// The value has been written. Notify the profile and proceed with the initialization queue.
		profile.onDescriptorWrite(gatt, descriptor);
		operationInProgress = false;
		nextRequest();
	} else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
		if (gatt.getDevice().getBondState() != BluetoothDevice.BOND_NONE) {
			// This should never happen but it used to: http://stackoverflow.com/a/20093695/2115352
			DebugLogger.w(TAG, ERROR_AUTH_ERROR_WHILE_BONDED);
			onError(gatt.getDevice(), ERROR_AUTH_ERROR_WHILE_BONDED, status);
		}
	} else {
		DebugLogger.e(TAG, "onDescriptorWrite error " + status);
		onError(gatt.getDevice(), ERROR_WRITE_DESCRIPTOR, status);
	}
}
 
Example 8
Source File: DexShareCollectionService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    Log.d(TAG, "characteristic wrote " + status);
    if (status == BluetoothGatt.GATT_SUCCESS) {
        Log.d(TAG, "Wrote a characteristic successfully " + characteristic.getUuid());
        if (mAuthenticationCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_authSucess = true;
            gatt.readCharacteristic(mHeartBeatCharacteristic);
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to: oncharacteristicwrite code: "+status+ "bond: "+gatt.getDevice().getBondState());
        }
    } else {
        Log.e(TAG, "Unknown error writing Characteristic");
    }
}
 
Example 9
Source File: DexShareCollectionService.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    final BluetoothDevice bondDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

    if (mBluetoothGatt != null && mBluetoothGatt.getDevice() != null && bondDevice != null) {
        if (!bondDevice.getAddress().equals(mBluetoothGatt.getDevice().getAddress())) {
            Log.d(TAG, "Bond state wrong device");
            return; // That wasnt a device we care about!!
        }
    }

    if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
        final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
        if (state == BluetoothDevice.BOND_BONDED) {
            Log.d(TAG, "CALLBACK RECIEVED Bonded");
            authenticateConnection();
        } else if (state == BluetoothDevice.BOND_NONE) {
            Log.d(TAG, "CALLBACK RECIEVED: Not Bonded");
        } else if (state == BluetoothDevice.BOND_BONDING) {
            Log.d(TAG, "CALLBACK RECIEVED: Trying to bond");
        }
    }
}
 
Example 10
Source File: GattUtils.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Will take an int from the bond state intent extra and translate it into a string
 *
 * @param bondState The bond state integer
 * @return The string value
 */

public String getBondStateDescription(int bondState) {
    switch (bondState) {
        case BluetoothDevice.BOND_NONE:
            return "NONE";
        case BluetoothDevice.BOND_BONDED:
            return "BONDED";
        case BluetoothDevice.BOND_BONDING:
            return "BONDING";
        default:
            return "UNKNOWN";
    }
}
 
Example 11
Source File: EasyBLE.java    From easyble-x with Apache License 2.0 5 votes vote down vote up
/**
 * 解除配对
 *
 * @param address 设备地址
 */
@SuppressWarnings("all")
public void removeBond(@NonNull String address) {
    checkStatus();
    try {
        BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(address);
        if (remoteDevice.getBondState() != BluetoothDevice.BOND_NONE) {
            remoteDevice.getClass().getMethod("removeBond").invoke(remoteDevice);
        }
    } catch (Exception ignore) {
    }
}
 
Example 12
Source File: BluetoothService.java    From NovarumBluetooth with MIT License 5 votes vote down vote up
public boolean connect(String devicemac)
{
	
	Log.w("TAG","connect() called on BluetoothService");
	
	if(devicemac == null)
	   return false;
	
	bluetoothDevice = bluetoothAdapter.getRemoteDevice(devicemac);
	
	if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) 
	{
		if(pairDevice(bluetoothDevice))
		{
			
			return socketConnect(bluetoothDevice);
		}
		else
		{
			postError("Could not pair device");
			return false;
		}
	}
	else
	{
		return socketConnect(bluetoothDevice);				
	}

}
 
Example 13
Source File: DexShareCollectionService.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
        Log.d(TAG, "Characteristic onDescriptorWrite ch " + characteristic.getUuid());
        if(mHeartBeatCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_notifSetupSucess = true;
            setCharacteristicIndication(mReceiveDataCharacteristic);
        }
        if(mReceiveDataCharacteristic.getUuid().equals(characteristic.getUuid())) {
            setCharacteristicIndication(mResponseCharacteristic);
        }
        if(mResponseCharacteristic.getUuid().equals(characteristic.getUuid())) {
            attemptRead();
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to");
        }
    } else {
        Log.e(TAG, "Unknown error writing descriptor");
    }
}
 
Example 14
Source File: DexShareCollectionService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
        Log.d(TAG, "Characteristic onDescriptorWrite ch " + characteristic.getUuid());
        if(mHeartBeatCharacteristic.getUuid().equals(characteristic.getUuid())) {
            state_notifSetupSucess = true;
            setCharacteristicIndication(mReceiveDataCharacteristic);
        }
        if(mReceiveDataCharacteristic.getUuid().equals(characteristic.getUuid())) {
            setCharacteristicIndication(mResponseCharacteristic);
        }
        if(mResponseCharacteristic.getUuid().equals(characteristic.getUuid())) {
            attemptRead();
        }
    } else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
        if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
            device = gatt.getDevice();
            state_authInProgress = true;
            bondDevice();
        } else {
            Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug? Have the dexcom forget whatever device it was previously paired to: ondescriptorwrite code: "+status+ "bond: "+gatt.getDevice().getBondState());
        }
    } else {
        Log.e(TAG, "Unknown error writing descriptor");
    }
}
 
Example 15
Source File: P_NativeDeviceWrapper.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
boolean isNativelyUnbonded(int bondState)
{
	return bondState == BluetoothDevice.BOND_NONE;
}
 
Example 16
Source File: P_NativeDeviceWrapper.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
boolean isNativelyUnbonded()
{
	return getNativeBondState() == BluetoothDevice.BOND_NONE;
}
 
Example 17
Source File: CreateBondTransaction.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
        BluetoothDevice extraDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        /*
         * equals handled inside of {@link FitbitBluetoothDevice} for comparison to an
         * {@link BluetoothDevice}
         */
        //noinspection EqualsBetweenInconvertibleTypes
        if (getConnection().getDevice().equals(extraDevice)) {
            int oldState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.BOND_NONE);
            int newState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
            GattUtils util = new GattUtils();
            Timber.d("[%s] Bond state changed from %s to %s",
                    getConnection().getDevice(),
                    util.getBondStateDescription(oldState),
                    util.getBondStateDescription(newState));
            switch (newState) {
                case BluetoothDevice.BOND_BONDED:
                    Timber.d("[%s] Bond state changed to BONDED",getDevice());
                    // success
                    bondSuccess();
                    break;
                case BluetoothDevice.BOND_NONE:
                    Timber.w("[%s] Bond state changed to NONE",getDevice());
                    // if we are here, we should go ahead and release the lock
                    // failure
                    synchronized (NAME) {
                        NAME.notify();
                    }
                    break;
                case BluetoothDevice.BOND_BONDING:
                    Timber.d("[%s] Bond state changed to BONDING",getDevice());
                    // in progress
                    break;
                default:
                    Timber.w("[%s] Bond state changed to UNKNOWN",getDevice());
                    // could be error, but perhaps not, we don't know
                    break;
            }
        } else {
            Timber.i("[%s] Received Bond result, but for %s",
                    getConnection().getDevice(),
                    extraDevice);
        }
    }
}
 
Example 18
Source File: NovarumbluetoothModule.java    From NovarumBluetooth with MIT License 4 votes vote down vote up
@Kroll.method
public boolean connect(String devicemac)
{
	if(devicemac == null)
	   return false;
	
	//Check if we should use the service//
	if(useService)
	{
		
		//Start Service//
		try
		{
			//Get Current activity//
			TiApplication appContext = TiApplication.getInstance();
			Activity activity = appContext.getCurrentActivity();
			
			BluetoothServiceIntent = new TiIntentWrapper(new Intent(activity,BluetoothService.class));
			BluetoothServiceIntent.getIntent().putExtra("MacAddress",devicemac);
			appContext.startService(BluetoothServiceIntent.getIntent());
			
			return true;
		}
		catch(Exception e)
		{
			Log.w(TAG,"error on creating bluetooth service: "+e.getMessage());
			return false;					
		}
		
		
	}
	else
	{		
		bluetoothDevice = bluetoothAdapter.getRemoteDevice(devicemac);
		
		if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) 
		{
			if(pairDevice(bluetoothDevice))
			{
				
				return socketConnect(bluetoothDevice);
			}
			else
			{
				postError("Could not pair device");
				return false;
			}
		}
		else
		{
			return socketConnect(bluetoothDevice);				
		}
	}
}
 
Example 19
Source File: BluetoothConnectionHelper.java    From DataLogger with MIT License 4 votes vote down vote up
/**
 * Start the ConnectThread to initiate a connection to a remote device.
 *
 * @param address The address to connect
 */
public synchronized void connect(String address, int index) {
    Log.i(TAG, "::connect Trying to connect to address " + address + ". ");

    // Cancel any thread attempting to make a connection
    if (mStates[index] == STATE_CONNECTING) {
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }
    }

    // Cancel any thread currently running a connection
    if (mConnectedThreads[index] != null) {
        mConnectedThreads[index].cancel();
        mConnectedThreads[index] = null;
    }

    // Get the BluetoothDevice object
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    switch (device.getBondState()){
        case BluetoothDevice.BOND_NONE:
            Log.i(TAG, "The remote device is not bonded (paired)");
            break;
        case BluetoothDevice.BOND_BONDING:
            Log.i(TAG, "Bonding (pairing) is in progress with the remote device");
            break;
        case BluetoothDevice.BOND_BONDED:
            Log.i(TAG, "The remote device is bonded (paired)");
            boolean remoteUuid = false;
            for (ParcelUuid uuid : device.getUuids()){
                if (mUUIDs[index].equals(uuid.toString())) {
                    remoteUuid = true;
                }
                Log.i(TAG, "::connect "+ uuid.toString() + ". Is target uuid: " + mUUIDs[index].equals(uuid.toString()));
            }
            if (!remoteUuid) {
                Log.e(TAG, "::connect Service UUID (" + mUUIDs[index] + ") not supported in remote device.");
            }
            break;
    }

    // Start the thread to connect with the given device
    mConnectThread = new ConnectThread(device, index);
    mConnectThread.start();
    setState(STATE_CONNECTING, index);

}
 
Example 20
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onCharacteristicWrite(final BluetoothGatt gatt,
								  final BluetoothGattCharacteristic characteristic,
								  final int status) {
	final byte[] data = characteristic.getValue();

	if (status == BluetoothGatt.GATT_SUCCESS) {
		log(Log.INFO, "Data written to " + characteristic.getUuid() +
				", value: " + ParserUtils.parse(data));

		BleManagerHandler.this.onCharacteristicWrite(gatt, characteristic);
		if (request instanceof WriteRequest) {
			final WriteRequest wr = (WriteRequest) request;
			final boolean valid = wr.notifyPacketSent(gatt.getDevice(), data);
			if (!valid && requestQueue instanceof ReliableWriteRequest) {
				wr.notifyFail(gatt.getDevice(), FailCallback.REASON_VALIDATION);
				requestQueue.cancelQueue();
			} else if (wr.hasMore()) {
				enqueueFirst(wr);
			} else {
				wr.notifySuccess(gatt.getDevice());
			}
		}
	} else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION
			|| status == 8 /* GATT INSUF AUTHORIZATION */
			|| status == 137 /* GATT AUTH FAIL */) {
		log(Log.WARN, "Authentication required (" + status + ")");
		if (gatt.getDevice().getBondState() != BluetoothDevice.BOND_NONE) {
			// This should never happen but it used to: http://stackoverflow.com/a/20093695/2115352
			Log.w(TAG, ERROR_AUTH_ERROR_WHILE_BONDED);
			postCallback(c -> c.onError(gatt.getDevice(), ERROR_AUTH_ERROR_WHILE_BONDED, status));
		}
		// The request will be repeated when the bond state changes to BONDED.
		return;
	} else {
		Log.e(TAG, "onCharacteristicWrite error " + status);
		if (request instanceof WriteRequest) {
			request.notifyFail(gatt.getDevice(), status);
			// Automatically abort Reliable Write when write error happen
			if (requestQueue instanceof ReliableWriteRequest)
				requestQueue.cancelQueue();
		}
		awaitingRequest = null;
		onError(gatt.getDevice(), ERROR_WRITE_CHARACTERISTIC, status);
	}
	checkCondition();
	nextRequest(true);
}