简体   繁体   中英

Connect other Bluetooth LE device without scanning?

Is it possible to connect with other Bluetooth LE device with out scanning.

I am working on app when bluetooth is ON and then automatically received the notification when I enter in any marketPlace that Beacon device(basically Bluetooth LE) is in your range. without my scanning Bluetooth Le. My Bluetooth is just ON. no scanning.

Because our requirements are that bluetooth doesn't scan just on, when ever new BLE is in range show alert or any notification.

I implement some scan method

startScan(){} 
stopScan(){}
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {}

but i don't want that i directly want to get connection message.

Please help me in form of pieces of code and also with little bit explanation
Thanks

You can use BluetoothAdapter.

BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mac);
device.connectGatt(mContext, false, mGattCallback);
// TODO your code

If the bluetooth device is not around, in BluetoothGattCallback's onConnectionStateChange will report BluetoothGatt.STATE_DISCONNECTED.

BluetoothDevice's creator has packages scope. But the source code of BluetoothAdapter's getRemoteDevice is:

public BluetoothDevice getRemoteDevice(String address) {
    return new BluetoothDevice(address);
}

Do you just want to discover devices that support a particular service? There is an overload startLeScan(UUID[], ...), where you can pass the UUID's of the services that you are interested in.

Otherwise, if you just want to connect to a device of a known BT address, you maybe able to create a Bluetooth device object with that address and call connectGatt() on it. Just an idea, not sure if this would work :-)

Creating a bluetooth device object with address is not possible! BluetoothDevice's creator has packages scope. so you can not create BluetoothDevice. And although BluetoothDevice implements Parcelable, it cannot be created from a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM