gpt4 book ai didi

android - 如果在 Android 中连接 BLE 设备失败,如何通知用户?

转载 作者:行者123 更新时间:2023-11-29 02:41:39 26 4
gpt4 key购买 nike

除了等待和测试连接之外,如果用户尝试连接的设备未连接,我如何通知用户。尝试 connectGatt() 后似乎没有任何连接失败的回调。谢谢!

最佳答案

方法:connGATT()有3个参数。第三个是回调:

mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

如果你想知道连接是否成功,你应该添加覆盖方法:

@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState)

这是链接。 https://developer.android.com/guide/topics/connectivity/bluetooth-le.html

整个回调看起来像这样:

    private final BluetoothGattCallback mGattCallback =
new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());

} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}

@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}

@Override
// Result of a characteristic read operation
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
...
};
...
}

关于android - 如果在 Android 中连接 BLE 设备失败,如何通知用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43666731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com