Java Code Examples for android.bluetooth.BluetoothAdapter#checkBluetoothAddress()

The following examples show how to use android.bluetooth.BluetoothAdapter#checkBluetoothAddress() . 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: BluetoothCentral.java    From blessed-android with MIT License 6 votes vote down vote up
/**
 * Set a fixed PIN code for a peripheral that asks fir a PIN code during bonding.
 * <p>
 * This PIN code will be used to programmatically bond with the peripheral when it asks for a PIN code.
 * Note that this only works for devices with a fixed PIN code.
 *
 * @param peripheralAddress the address of the peripheral
 * @param pin               the 6 digit PIN code as a string, e.g. "123456"
 * @return true if the pin code and peripheral address are valid and stored internally
 */
public boolean setPinCodeForPeripheral(String peripheralAddress, String pin) {
    if (!BluetoothAdapter.checkBluetoothAddress(peripheralAddress)) {
        Timber.e("%s is not a valid address. Make sure all alphabetic characters are uppercase.", peripheralAddress);
        return false;
    }

    if (pin == null) {
        Timber.e("pin code is null");
        return false;
    }

    if (pin.length() != 6) {
        Timber.e("%s is not 6 digits long", pin);
        return false;
    }

    pinCodes.put(peripheralAddress, pin);
    return true;
}
 
Example 2
Source File: BleManager.java    From react-native-ble-manager with Apache License 2.0 6 votes vote down vote up
private Peripheral retrieveOrCreatePeripheral(String peripheralUUID) {
	Peripheral peripheral = peripherals.get(peripheralUUID);
	if (peripheral == null) {
		synchronized (peripherals) {
			if (peripheralUUID != null) {
				peripheralUUID = peripheralUUID.toUpperCase();
			}
			if (BluetoothAdapter.checkBluetoothAddress(peripheralUUID)) {
				BluetoothDevice device = bluetoothAdapter.getRemoteDevice(peripheralUUID);
				if (Build.VERSION.SDK_INT >= LOLLIPOP && !forceLegacy) {
					peripheral = new LollipopPeripheral(device, reactContext);
				} else {
					peripheral = new Peripheral(device, reactContext);
				}
				peripherals.put(peripheralUUID, peripheral);
			}
		}
	}
	return peripheral;
}
 
Example 3
Source File: BleGattImpl.java    From EasyBle with Apache License 2.0 6 votes vote down vote up
@Override
public void disconnect(String address) {
    if (!BluetoothAdapter.checkBluetoothAddress(address)) {
        return;
    }
    BluetoothGatt gatt = mGattMap.get(address);
    if (gatt == null) {
        return;
    }
    gatt.disconnect();
    //remove connection timeout message if a connection attempt currently is in progress
    mHandler.removeCallbacksAndMessages(address);
    Map.Entry<BleDevice, BleConnectCallback> entry = findMapEntry(mConnectCallbackMap, address);
    if (entry != null) {
        BleDevice d = entry.getKey();
        if (d.connecting) {
            d.connecting = false;
            removeDevice(d);
        }
    }
}
 
Example 4
Source File: BluetoothUtility.java    From SimpleBluetoothLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * Connects a device given a mac address.
 * @param macAddress the mac address of the device.
 */
public void connectDevice(String macAddress) {
    if(bluetoothAdapter != null) {
        bluetoothAdapter.cancelDiscovery();
        //check if bluetooth is enabled just in case.
        if(checkIfEnabled()) {
            //cancel all running threads.
            if(connectedThread != null) {
                connectedThread.cancel();
            }
            if(connectThread != null) {
                connectThread.cancel();
            }
            if(acceptThread != null) {
                acceptThread.cancel();
            }
            if(connectToServerThread != null) {
                connectToServerThread.cancel();
            }
            //check the mac address first.
            if(BluetoothAdapter.checkBluetoothAddress(macAddress)) {
                bluetoothDevice = bluetoothAdapter.getRemoteDevice(macAddress);
                connectThread = new ConnectDeviceThread(bluetoothDevice);
                connectThread.start();
            } else {
                //mac address not valid.
                if(mContext instanceof Activity) {
                    InvalidMacAddressDialog imad = InvalidMacAddressDialog.newInstance();
                    imad.show(((Activity)mContext).getFragmentManager(), "ERROR");
                }
            }

        }
    }
}
 
Example 5
Source File: SmoothBluetooth.java    From AndroidSmoothBluetooth with Apache License 2.0 5 votes vote down vote up
private void connect(String address, boolean android, boolean secure) {
    if (isConnecting) {
        return;
    }
    if (!isServiceAvailable()) {
        setupService();
    }
    startService(android, secure);
    if(BluetoothAdapter.checkBluetoothAddress(address)) {
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        mBluetoothService.connect(device);
    }
}
 
Example 6
Source File: BleRequestImpl.java    From Android-BLE with Apache License 2.0 5 votes vote down vote up
/**
 * 断开蓝牙
 *
 * @param address 蓝牙地址
 */
public void disconnect(String address) {
    if (verifyParams(address)) return;
    boolean isValidAddress = BluetoothAdapter.checkBluetoothAddress(address);
    if (!isValidAddress) {
        BleLog.e(TAG, "the device address is invalid");
        return;
    }
    gattHashMap.get(address).disconnect();
    notifyIndex = 0;
    notifyCharacteristics.clear();
    writeCharacteristicMap.remove(address);
    readCharacteristicMap.remove(address);
    otaWriteCharacteristic = null;
}
 
Example 7
Source File: BtUtils.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
public static String getBtAddress() {
    String btaddr = "";
    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
    if (btAdapter != null) {
        btaddr = btAdapter.getAddress();
        if (!BluetoothAdapter.checkBluetoothAddress(btaddr)) {
            btaddr = "";
        }
    }

    return btaddr;
}
 
Example 8
Source File: LoginActivity.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private void a(boolean flag)
{
    finish();
    i();
    Intent intent = new Intent();
    if (flag && Keeper.readPersonInfo().isValid())
    {
        BraceletBtInfo braceletbtinfo = Keeper.readBraceletBtInfo();
        if (braceletbtinfo != null && BluetoothAdapter.checkBluetoothAddress(braceletbtinfo.address))
        {
            intent.setClass(t, cn/com/smartdevices/bracelet/ui/MainUIActivity);
        } else
        {
            intent.setClass(t, cn/com/smartdevices/bracelet/ui/SearchSingleBraceletActivity);
        }
    } else
    {
        if (x != null)
        {
            PersonInfo personinfo = Keeper.readPersonInfo();
            if (x.miliaoIcon_320 != null && x.miliaoIcon_320.length() > 0)
            {
                personinfo.avatarUrl = x.miliaoIcon_320;
            } else
            {
                personinfo.avatarUrl = x.miliaoIcon;
            }
            Keeper.keepPersonInfo(personinfo);
        }
        intent.setClass(t, cn/com/smartdevices/bracelet/ui/person/PersonInfoSetGenderActivity);
    }
    startActivity(intent);
}
 
Example 9
Source File: BleManager.java    From EasyBle with Apache License 2.0 5 votes vote down vote up
/**
 * Return true if local device is connecting with the specific remote device
 */
public boolean isConnecting(String address) {
    if (!BluetoothAdapter.checkBluetoothAddress(address)) {
        return false;
    }
    return mGatt.isConnecting(address);
}
 
Example 10
Source File: BleManager.java    From EasyBle with Apache License 2.0 5 votes vote down vote up
/**
 * Return true if the specific remote device has connected with local device
 *
 * @param address device mac
 * @return true if local device has connected to the specific remote device
 */
public boolean isConnected(String address) {
    if (!BluetoothAdapter.checkBluetoothAddress(address)) {
        return false;
    }
    List<BleDevice> deviceList = getConnectedDevices();
    for (BleDevice d : deviceList) {
        if (address.equals(d.address)) {
            return true;
        }
    }
    return false;
}
 
Example 11
Source File: MainActivity.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private void a()
{
    Intent intent = new Intent();
    BraceletBtInfo braceletbtinfo = Keeper.readBraceletBtInfo();
    LoginData logindata = Keeper.readLoginData();
    PersonInfo personinfo = Keeper.readPersonInfoBase();
    if (Debug.isEnabled())
    {
        Debug.i("MainActivity", (new StringBuilder()).append("MainActivity onCreate, Person:").append(personinfo).append(", login: ").append(logindata).toString());
    }
    if (logindata == null || !logindata.isValid())
    {
        intent.setClass(this, cn/com/smartdevices/bracelet/activity/LoginActivity);
    } else
    if (personinfo == null || !personinfo.isValid())
    {
        intent.setClass(this, cn/com/smartdevices/bracelet/ui/person/PersonInfoSetGenderActivity);
    } else
    if ((braceletbtinfo == null || !BluetoothAdapter.checkBluetoothAddress(braceletbtinfo.address)) && Keeper.readNeedBind() == 1)
    {
        intent.setClass(this, cn/com/smartdevices/bracelet/ui/SearchSingleBraceletActivity);
    } else
    {
        intent.setClass(this, cn/com/smartdevices/bracelet/ui/MainUIActivity);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null)
        {
            intent.putExtras(bundle);
            Debug.i("MainActivity", (new StringBuilder()).append("extras is :").append(bundle).toString());
        } else
        {
            Debug.i("MainActivity", "extras is null!");
        }
    }
    startActivity(intent);
    finish();
    overridePendingTransition(0, 0);
}
 
Example 12
Source File: InputManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private String getDeviceAlias(String uniqueId) {
    if (BluetoothAdapter.checkBluetoothAddress(uniqueId)) {
        // TODO(BT) mBluetoothService.getRemoteAlias(uniqueId)
        return null;
    }
    return null;
}
 
Example 13
Source File: BluetoothCentral.java    From blessed-android with MIT License 5 votes vote down vote up
/**
 * Get a peripheral object matching the specified mac address.
 *
 * @param peripheralAddress mac address
 * @return a BluetoothPeripheral object matching the specified mac address or null if it was not found
 */
public BluetoothPeripheral getPeripheral(String peripheralAddress) {
    if (!BluetoothAdapter.checkBluetoothAddress(peripheralAddress)) {
        Timber.e("%s is not a valid address. Make sure all alphabetic characters are uppercase.", peripheralAddress);
        return null;
    }

    if (connectedPeripherals.containsKey(peripheralAddress)) {
        return connectedPeripherals.get(peripheralAddress);
    } else if (unconnectedPeripherals.containsKey(peripheralAddress)) {
        return unconnectedPeripherals.get(peripheralAddress);
    } else {
        return new BluetoothPeripheral(context, bluetoothAdapter.getRemoteDevice(peripheralAddress), internalCallback, null, callBackHandler);
    }
}
 
Example 14
Source File: BleManager.java    From EasyBle with Apache License 2.0 4 votes vote down vote up
private void checkBluetoothAddress(String address) {
    if (!BluetoothAdapter.checkBluetoothAddress(address)) {
        throw new IllegalArgumentException("Invalid address: " + address);
    }
}
 
Example 15
Source File: Utils.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
public static boolean isBinded()
{
    BraceletBtInfo braceletbtinfo = Keeper.readBraceletBtInfo();
    return braceletbtinfo != null && BluetoothAdapter.checkBluetoothAddress(braceletbtinfo.address);
}
 
Example 16
Source File: BluetoothClient.java    From ssj with GNU General Public License v3.0 4 votes vote down vote up
public BluetoothClient(UUID connID, String serverName, String serverAddr) throws IOException
{
    _name = "BluetoothClient";

    _adapter = BluetoothAdapter.getDefaultAdapter();
    if (_adapter == null)
    {
        Log.e("Device does not support Bluetooth");
        return;
    }

    if (!_adapter.isEnabled())
    {
        Log.e("Bluetooth not enabled");
        return;
    }

    Log.i("searching for server " + serverName);

    Set<BluetoothDevice> pairedDevices = _adapter.getBondedDevices();
    // If there are paired devices
    if (pairedDevices.size() > 0)
    {
        // Loop through paired devices
        for (BluetoothDevice d : pairedDevices)
        {
            if (d.getName().equalsIgnoreCase(serverName))
            {
                serverAddr = d.getAddress();
                _server = d;
            }
        }
    }
    //if we haven't found it
    if(_server == null)
    {
        Log.i("not found, searching for server " + serverAddr);

        if (!BluetoothAdapter.checkBluetoothAddress(serverAddr)) {
            Log.e("invalid MAC address: " + serverAddr);
            return;
        }

        _server = _adapter.getRemoteDevice(serverAddr);
    }

    _uuid = connID;
    Log.i("client connection to " + _server.getName() + " initialized");
}
 
Example 17
Source File: BleRequestImpl.java    From Android-BLE with Apache License 2.0 4 votes vote down vote up
/**
 * 连接蓝牙
 *
 * @param bleDevice BleDevice
 * @return Connection result
 */
public boolean connect(final T bleDevice) {
    String address = bleDevice.getBleAddress();
    if (connectedAddressList.contains(bleDevice.getBleAddress()) && bleDevice.isConnected()) {
        BleLog.e(TAG, "this is device already connected.");
        connectWrapperCallback.onConnectException(bleDevice, BleStates.ConnectedAlready);
        return false;
    }
    if (bluetoothAdapter == null) {
        BleLog.e(TAG, "bluetoothAdapter not available");
        connectWrapperCallback.onConnectException(bleDevice, BleStates.NotAvailable);
        return false;
    }
    // getRemoteDevice(address) will throw an exception if the device address is invalid,
    // so it's necessary to check the address
    if (!BluetoothAdapter.checkBluetoothAddress(address)) {
        BleLog.e(TAG, "the device address is invalid");
        connectWrapperCallback.onConnectException(bleDevice, BleStates.InvalidAddress);
        return false;
    }
    // Previously connected device. Try to reconnect. ()
    final BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        BleLog.e(TAG, "no device");
        connectWrapperCallback.onConnectException(bleDevice, BleStates.DeviceNull);
        return false;
    }
    //10s after the timeout prompt
    HandlerCompat.postDelayed(handler, new Runnable() {
        @Override
        public void run() {
            connectWrapperCallback.onConnectTimeOut(bleDevice);
            close(device.getAddress());
        }
    }, device.getAddress(), options.connectTimeout);
    bleDevice.setConnectionState(BleDevice.CONNECTING);
    bleDevice.setBleName(device.getName());
    connectWrapperCallback.onConnectionChanged(bleDevice);
    // We want to directly connect to the device, so we are setting the autoConnect parameter to false
    BluetoothGatt bluetoothGatt = device.connectGatt(context, false, gattCallback);
    if (bluetoothGatt != null) {
        gattHashMap.put(address, bluetoothGatt);
        BleLog.d(TAG, "Trying to create a new connection.");
        return true;
    }
    return false;
}
 
Example 18
Source File: OldBleDeviceAdapterImpl.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
@Override
public boolean checkBluetoothAddress(final String address) {
    return BluetoothAdapter.checkBluetoothAddress(address);
}
 
Example 19
Source File: ScanBuilder.java    From neatle with MIT License 3 votes vote down vote up
/**
 * Add a MAC address to the filter list and enables filtering of events by device MAC address.
 *
 * @param address device mac address. If the address is not valid {@link IllegalArgumentException}
 *                will be thrown.
 *
 * @return this builder instance.
 */
public ScanBuilder addDeviceAddress(String address) {
    if (!BluetoothAdapter.checkBluetoothAddress(address)) {
        throw new IllegalArgumentException("Invalid address:" + address);
    }
    scannerConfiguration.addDeviceAddress(address);
    return this;
}
 
Example 20
Source File: ScanFilter.java    From RxAndroidBle with Apache License 2.0 3 votes vote down vote up
/**
 * Set filter on device address.
 *
 * @param deviceAddress The device Bluetooth address for the filter. It needs to be in the
 *            format of "01:02:03:AB:CD:EF". The device address can be validated using
 *            {@link BluetoothAdapter#checkBluetoothAddress}.
 * @throws IllegalArgumentException If the {@code deviceAddress} is invalid.
 */
public ScanFilter.Builder setDeviceAddress(String deviceAddress) {
    if (deviceAddress != null && !BluetoothAdapter.checkBluetoothAddress(deviceAddress)) {
        throw new IllegalArgumentException("invalid device address " + deviceAddress);
    }
    mDeviceAddress = deviceAddress;
    return this;
}