Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android应用程序中,调用disconnect()和close()是正确的方法吗?_Java_Android_Bluetooth Lowenergy_Android Bluetooth_Bluetooth Gatt - Fatal编程技术网

Java 在Android应用程序中,调用disconnect()和close()是正确的方法吗?

Java 在Android应用程序中,调用disconnect()和close()是正确的方法吗?,java,android,bluetooth-lowenergy,android-bluetooth,bluetooth-gatt,Java,Android,Bluetooth Lowenergy,Android Bluetooth,Bluetooth Gatt,我正在努力实现一个Android应用程序,该应用程序可以连接到BLE设备,但是缺乏适当的文档让我感到非常痛苦。如果我只运行一次应用程序,似乎没有任何东西能以同样的方式运行两次,它可能会工作,但下一次会在某个地方停止(不知道在哪里),但我有点得出结论,我可能没有按正确的顺序使用disconnect()和close() 假设发生错误,我首先调用disconnect(): public void disconnect() { mScanning = true; mConnected

我正在努力实现一个Android应用程序,该应用程序可以连接到BLE设备,但是缺乏适当的文档让我感到非常痛苦。如果我只运行一次应用程序,似乎没有任何东西能以同样的方式运行两次,它可能会工作,但下一次会在某个地方停止(不知道在哪里),但我有点得出结论,我可能没有按正确的顺序使用
disconnect()
close()

假设发生错误,我首先调用disconnect():

 public void disconnect() {
    mScanning = true;
    mConnected = false;
    startedConnect = false;
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        scanLeDevice(true);
        return;
    }
    mBluetoothGatt.disconnect();
    scanLeDevice(true);
}
public void close() {
    if (mBluetoothGatt == null) {
        return;
    }
    mBluetoothGatt.close();
    mBluetoothGatt = null;
}
然后我调用close():

 public void disconnect() {
    mScanning = true;
    mConnected = false;
    startedConnect = false;
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        scanLeDevice(true);
        return;
    }
    mBluetoothGatt.disconnect();
    scanLeDevice(true);
}
public void close() {
    if (mBluetoothGatt == null) {
        return;
    }
    mBluetoothGatt.close();
    mBluetoothGatt = null;
}

这是正确的做法还是错误的做法?请注意,我正在呼叫
scanlevice(true),但随后调用了
close()
,我认为这只是“完成”所有操作并正确停止扫描?

当您在
蓝牙设备上调用
connectGatt
创建
蓝牙gatt
对象时,您声称是32个对象中的一个(在当前Android版本中)蓝牙堆栈中的可用插槽

在这个对象上,你现在可以调用
connect
disconnect
来更改“目标”的状态,如果安卓试图保持与设备的连接或不保持连接。在您呼叫断开连接之前,Android将(永远)尝试连接到设备,并在连接因任何原因中断时自动重新连接到设备。在
断开连接后再次调用
连接
,将使其再次尝试连接

注意:在初始的
connectGatt
调用中将
autoConnect
设置为
false
,将为第一次隐式连接尝试设置初始超时(通常为30秒),如果设备连接后连接断开,则不会自动尝试重新连接

因此,
BluetoothGatt
对象可以处于“断开”状态,但仍然占用蓝牙堆栈中32个插槽中的一个。调用
close
时,释放
BluetoothGatt
对象的所有资源,并将插槽返回到Bluetooth堆栈。因此,关闭也意味着断开连接。要解决一些有缺陷的Android设备的潜在问题,您可以在
close
之前调用
BluetoothGatt
对象上的
disconnect
。请注意,一旦调用了
close
,就不能对该
BluetoothGatt
对象调用任何其他方法

注意:关闭蓝牙意味着自动销毁所有
BluetoothGatt
对象

回到你的问题上来,我真的不明白你的BLE扫描跟什么有关系
连接/断开/关闭。扫描与连接和
BluetoothGatt
对象完全分离,同时连接和执行扫描没有问题。要停止扫描,请在
BluetoothLeScanner

上调用
stopScan
,您是否尝试在google上查找示例?我使用了搜索词“java android bluetooth”并找到了许多。@NomadMaker我做了,但没有真正起作用,要么太旧,缺乏解释,而且我的应用程序与我找到的有点不同。你是否将lowenergy添加到搜索词列表中?@NomadMaker当然可以。我已经为此研究了几个星期,但可能是因为我对安卓系统还不熟悉。你对我的问题有答案吗?没有。我只是给出了我的发现。这些链接包含文档和示例。感谢您的回复。我在不同的论坛上读过关于BLE操作的不同内容。很多人说我必须在连接之前停止扫描,因为扫描会干扰连接,但你是说它不会?!我不明白你说的另一件事“注意,一旦你调用了close,你就不能对BluetoothGatt对象调用任何其他方法了”,但是我怎么能重新调用scan()?请检查我的代码,并给我一个答案,如果我在那里做得正确,如果没有,请使用我的代码编辑你的答案,这样我就可以更清楚,因为我有点困惑。不,扫描不会干扰连接。大约六年前,一些廉价手机在这方面有一些缺陷,但这应该在很久以前解决。关于“如何重新调用扫描”,您有什么问题?请显示您的完整代码,因为现在您不显示扫描代码。还请展示您尝试过的内容。BLE扫描的文档可以在上找到。我只想知道,每当我想断开并重新启动扫描时,这样做是否正确:断开(),关闭(),然后开始扫描()。在
蓝牙GATT
对象上调用
断开连接
,然后调用
关闭
,然后开始BLE扫描是非常好的。在你自己的
disconnect
方法中,我不明白你为什么要在那里开始扫描。对我来说,在
关闭后启动扫描更有意义(即使启动扫描时没有限制)。