简体   繁体   English

是否连接其他Bluetooth LE设备而不进行扫描?

[英]Connect other Bluetooth LE device without scanning?

Is it possible to connect with other Bluetooth LE device with out scanning. 是否可以不扫描而与其他Bluetooth LE设备连接。

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. 蓝牙处于打开状态时,我正在开发应用程序,然后在进入任何Beacon设备(主要是Bluetooth LE)在您的范围内的市场时,都会自动收到通知。 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. 因为我们的要求是蓝牙不仅仅扫描,所以只要有新的BLE在范围内显示警报或任何通知。

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. 您可以使用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. 如果蓝牙设备不在附近,则在BluetoothGattCallback的onConnectionStateChange中将报告BluetoothGatt.STATE_DISCONNECTED。

BluetoothDevice's creator has packages scope. BluetoothDevice的创建者具有包范围。 But the source code of BluetoothAdapter's getRemoteDevice is: 但是BluetoothAdapter的getRemoteDevice的源代码是:

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. 有一个重载startLeScan(UUID [],...),您可以在其中传递您感兴趣的服务的UUID。

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. 否则,如果您只想连接到具有已知BT地址的设备,则可以创建具有该地址的Bluetooth设备对象并在其上调用connectGatt()。 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. BluetoothDevice的创建者具有包范围。 so you can not create BluetoothDevice. 因此您无法创建BluetoothDevice。 And although BluetoothDevice implements Parcelable, it cannot be created from a file. 而且,尽管BluetoothDevice实现了Parcelable,但无法从文件创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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