[英]Pairing to a Bluetooth Low Energy device in Android
Is it possible to automatically connect to Bluetooth Low Energy (BLE) devices? 是否可以自动连接蓝牙低功耗(BLE)设备?
The Android documentation indicates that the [ BluetoothDevice.connectGatt()
]( https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context , boolean, android.bluetooth.BluetoothGattCallback)) has a autoConnect parameter: Android文档表明[
BluetoothDevice.connectGatt()
]( https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt (android.content.Context,boolean,android.bluetooth.BluetoothGattCallback) )有一个autoConnect参数:
boolean indicating whether to automatically connect to the BLE device as soon as it becomes available
boolean指示是否在可用时自动连接到BLE设备
However, to call this, you need a BluetoothDevice
first. 但是,要调用它,首先需要一个
BluetoothDevice
。 AFAIK the only way to get this is by scanning available devices. AFAIK获得此功能的唯一方法是扫描可用的设备。 Setting up a scan every time to connect to a device doesn't seem like a desirable way.
每次设置扫描以连接到设备似乎都不是一种理想的方式。 Also, I tried using nRF Control Master Panel to connect to my peripheral using the
autoConnect = true
, but this does not connect to the device. 此外,我尝试使用nRF控制主面板使用
autoConnect = true
连接到我的外围设备,但这不连接到设备。 Connecting without the autoConnect
however does make it connect, and I've managed to read and write data from and to my peripheral this way with success. 然而,没有
autoConnect
连接确实使它连接,并且我成功地以这种方式从我的外设读取和写入数据。
The general way in Bluetooth to have two devices paired. 蓝牙的一般方式是将两个设备配对。 However, searching for my BLE device and using
BluetoothDevice.createBond()
does not seem to work. 但是,搜索我的BLE设备并使用
BluetoothDevice.createBond()
似乎不起作用。 In my ACTION_BOND_STATE_CHANGED
-callback, the EXTRA_BOND_STATE
and EXTRA_PREVIOUS_BOND_STATE
just go from BOND_BONDING
to BOND_NONE
and back. 在我的
ACTION_BOND_STATE_CHANGED
EXTRA_BOND_STATE
, EXTRA_BOND_STATE
和EXTRA_PREVIOUS_BOND_STATE
只是从BOND_BONDING
转到BOND_NONE
然后返回。 I don't read out an error or anything - so maybe I'm missing something here. 我没有读出错误或任何东西 - 所以也许我在这里遗漏了一些东西。 Here's the callback:
这是回调:
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);
Log.e(TAG, "prevState " + prevState + ", state " + state);
}
}
};
So this type of bonding does not seem to work. 所以这种类型的粘合似乎不起作用。
My question is: am I doing something wrong for pairing or the autoConnect? 我的问题是:我是否在配对或autoConnect上做错了什么? Or is how I currently have it working the only correct way?
或者我现在如何以唯一正确的方式工作? It seems like a real pain (and battery-drain) to have to scan for devices every time, see if the device is there, if so read data and check back tomorrow, otherwise check back in an hour or so.
每次必须扫描设备似乎是一个真正的痛苦(和电池消耗),看看设备是否在那里,如果这样读取数据并明天检查,否则在一小时左右检查。 The point of Bluetooth is that it should pair directly whenever it is near, isn't it?
蓝牙的意义在于它应该在它附近时直接配对,不是吗?
It does work without rescan. 没有重新扫描它确实有效。 You do not need pairing at all.
你根本不需要配对。 Just call
BluetoothGatt.connect()
again for gatt object you aquired from first connection. 只需再次为您从第一次连接获取的gatt对象调用
BluetoothGatt.connect()
。 You will receive onConnectionStateChange
event in your BluetoothGattCallback
as soon as ble device will be available again. 一旦ble设备再次可用,您将在
BluetoothGattCallback
收到onConnectionStateChange
事件。 If you use autoconnect option, you don't even need to call BluetoothGatt.connect()
method. 如果使用autoconnect选项,则甚至不需要调用
BluetoothGatt.connect()
方法。 Just monitor your cllback, and don't forget to close BluetoothGatt with close()
if you don't see any connection for too long. 只需监控你的cllback,如果你没有看到任何连接太久,别忘了用
close()
关闭BluetoothGatt。
And yes, to obtain first connection you should scan for ble devices with BluetoothAdapter.startLeScan
, not the common bluetooth devices scan. 是的,要获得第一个连接,您应该使用
BluetoothAdapter.startLeScan
扫描设备,而不是常见的蓝牙设备扫描。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.