连接其他蓝牙LE设备而不扫描?

时间:2014-03-04 10:02:48

标签: android bluetooth bluetooth-lowenergy

是否可以在不扫描的情况下连接其他蓝牙LE设备。

当蓝牙打开时,我正在使用应用程序,然后当我进入任何marketPlace,即Beacon设备(基本上是蓝牙LE)在您的范围内时,我会自动收到通知。没有我扫描蓝牙乐。我的蓝牙只是开启。没有扫描。

因为我们的要求是蓝牙不能直接扫描,所以当新的BLE处于范围显示警报或任何通知时。

我实现了一些扫描方法

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

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

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

3 个答案:

答案 0 :(得分:3)

您可以使用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);
}

答案 1 :(得分:2)

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

否则,如果您只想连接到已知BT地址的设备,您可以使用该地址创建蓝牙设备对象并在其上调用connectGatt()。只是一个想法,不确定这是否会起作用: - )

答案 2 :(得分:0)

无法创建带地址的蓝牙设备对象! BluetoothDevice的创建者具有包范围。所以你不能创建BluetoothDevice。 虽然BluetoothDevice实现了Parcelable,但无法从文件创建它。