我想连接到蓝牙设备,并以最快的方式写入特性值。现在我的代码给出了不好的结果:蓝牙LE连接时间/缓存
1)BLE扫描 - 正如你可以看到我有硬编码MAC地址的设备,我想连接到。我也获得connectGatt方法调用的时间戳。
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
if(device.getAddress().equals("F1:9F:EE:6B:AB:83")) {
mConnectedGatt = device.connectGatt(this, false, mGattCallback);
start = System.currentTimeMillis()/1000;
};
}
2)onConnectionChanged - 我得到enother时间戳和它们。减去结识很多连接操作如何持续
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
end = System.currentTimeMillis()/1000;
Log.d("QWERTY", "TIME " + (end - start));
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
Log.d("QWERTY","Success");
gatt.discoverServices();
} else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.d("QWERTY","Disconnected");
} else if (status != BluetoothGatt.GATT_SUCCESS) {
Log.d("QWERTY","Connection error");
gatt.disconnect();
}
}
还有我的主要问题是。 device.connectGatt和onConnectionStateChange之间的时间需要3到8秒,这对我来说太长了。如果它在1秒左右,我会很高兴。 写入特征在onServicesDiscovered侦听器中占用的时间少于秒,所以它不是问题。
问题1):有什么办法可以使连接时间更快?
问题2)有没有办法将蓝牙连接与设备存储在缓存中,所以它在第一次连接时会持续很长时间,在以后的调用中会更快?
问题3):有什么可以使其工作更快的建议?也许我的做法很糟糕。
谢谢你的回应。我会尝试一些事情,让你知道如果帮助:) – F1sher