繁体   English   中英

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

[英]Connect other Bluetooth LE device without scanning?

是否可以不扫描而与其他Bluetooth LE设备连接。

蓝牙处于打开状态时,我正在开发应用程序,然后在进入任何Beacon设备(主要是Bluetooth LE)在您的范围内的市场时,都会自动收到通知。 没有我的扫描蓝牙乐。 我的蓝牙刚刚打开。 没有扫描。

因为我们的要求是蓝牙不仅仅扫描,所以只要有新的BLE在范围内显示警报或任何通知。

我实现了一些扫描方法

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

但我不希望我直接想获得连接消息。

请以代码段的形式帮助我,并提供一些解释
谢谢

您可以使用BluetoothAdapter。

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

如果蓝牙设备不在附近,则在BluetoothGattCallback的onConnectionStateChange中将报告BluetoothGatt.STATE_DISCONNECTED。

BluetoothDevice的创建者具有包范围。 但是BluetoothAdapter的getRemoteDevice的源代码是:

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

您是否只想发现支持特定服务的设备? 有一个重载startLeScan(UUID [],...),您可以在其中传递您感兴趣的服务的UUID。

否则,如果您只想连接到具有已知BT地址的设备,则可以创建具有该地址的Bluetooth设备对象并在其上调用connectGatt()。 只是一个想法,不确定这是否行得通:-)

无法使用地址创建蓝牙设备对象! BluetoothDevice的创建者具有包范围。 因此您无法创建BluetoothDevice。 而且,尽管BluetoothDevice实现了Parcelable,但无法从文件创建它。

暂无
暂无

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

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