Is it Android gatt connect or Android scan(getBluetoothLeScanner) which results in the Scan Request and Response? If we know the BLE Device address , can we directly connect it to without discovering the service?
Asked
Active
Viewed 1,124 times
1 Answers
1
In Android BLE scan, the result for scan request will return in the way you want, for example
List<ScanFilter> filters = new ArrayList<ScanFilter>();
ScanFilter filter = new ScanFilter.Builder()
.setServiceUuid(uuid)
.setDeviceAddress(address)
.setDeviceName(name)
.build();
filters.add(filter);
And scan response will return at
onScanResult(int callbackType, ScanResult result)
ScanCallBack mCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
if (result != null){
BluetoothDevice device = result.getDevice();
mDeviceList.add(device);
removeDuplicateWithOrder(mDeviceList);
adapter.notifyDataSetChanged();
}
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
Log.e("TAG", "Scan failed " + errorCode);
}
};
If we know the BLE Device address , can we directly connect it to without discovering the service?
The answer is YES and you can follow this example
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
return false;
}
// Previously connected device. Try to reconnect.
if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
&& mBluetoothGatt != null) {
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
if (mBluetoothGatt.connect()) {
return true;
} else {
return false;
}
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
// We want to directly connect to the device, so we are setting the autoConnect
// parameter to false.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
Log.d(TAG, "Trying to create a new connection.");
mBluetoothDeviceAddress = address;
return true;
}
Hope this can help.

Duy Kyou
- 11
- 3
-
The answer to the second question is a bit more advanced. You can only reliably connect to a device address if you have either scanned it since the last Bluetooth reset or it is bonded, due to lack of address type parameter in the API. – Emil Aug 30 '17 at 07:44
-
@Emil, in that case, I can use another callback to scan and connect redirect to device without discovering the service. `BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback()`. This callback will allow me to scan and add any device it found. – Duy Kyou Aug 30 '17 at 07:56
-
@DuyKyou if I am not wrong LeScanCallback is used till Android J and for future it is getBluetoothScanner().startScan() – Raulp Aug 30 '17 at 09:47
-
@Emil yes it has already been connected and now trying to connect again but at different service but before that after disconnection I have called refresh cache. – Raulp Aug 30 '17 at 09:53
-
@Raulp yes, I'm using `mBluetoothLeScanner.startScan(...);`, because from the android API 21, LeScanCallback is deprecated. – Duy Kyou Aug 30 '17 at 10:15
-
@Raulp for more detail about BLE advertise and scan , you can visit my project at https://github.com/hoaiduyit/advertisediscoverBLE – Duy Kyou Aug 30 '17 at 10:21