简体   繁体   English

Android低功耗蓝牙(BLE)断开/重新连接

[英]android Bluetooth low energy (BLE) disconnect / reconnect

The app activity displays list of available BLE devices and then connects with selected (I see notifications etc). 应用程序活动显示可用的BLE设备列表,然后与选定的设备连接(我看到通知等)。 The code: 编码:

private static BluetoothAdapter mAdapter=null;
if(mAdapter == null);
mAdapter = BluetoothAdapter.getDefaultAdapter();
mAdapter.startLeScan((BluetoothAdapter.LeScanCallback)mContext);

It gives entry of one device (as expected). 它给出了一个设备的输入(如预期的那样)。 However, I cannot properly disconnect. 但是,我无法正确断开连接。 Connection seams to remain when I close the app. 当我关闭应用程序时,连接缝保持不变。 The led indicating connection stays ON and on next run of the app the list of available devices is emply - to disconnect I had to restart device or phone. LED指示连接保持打开状态,并且在下次运行该应用时,可用设备的列表很完整-要断开连接,我必须重新启动设备或电话。

I've tried System. exit(0) 我试过System. exit(0) System. exit(0) and private static BluetoothGatt mGatt; mGatt.disconnect(); System. exit(0)private static BluetoothGatt mGatt; mGatt.disconnect(); private static BluetoothGatt mGatt; mGatt.disconnect(); and in Android Device Monitor stoping app instance (was there after BACK buttons from main activity, by the way - why it could be there - unregistered broadcast receiver?). 并在Android Device Monitor中停止应用程序实例(顺便说一句,为什么在主活动的“返回”按钮之后出现了-为什么它可能在那儿,未注册的广播接收器?) Still led of the device is ON and on next app start the list of available devices is empty (and no messages displayed on screen - notifications etc). 仍然带领该设备为ON,并且在下一个应用程序启动时,可用设备的列表为空(并且屏幕上不显示任何消息-通知等)。

How to reset BLE connection properly? 如何正确重置BLE连接? If more app code is needed I will add it. 如果需要更多应用程序代码,我会添加它。

It's not entirely clear from your description what is happening. 从您的描述中还不完全清楚发生了什么。 What notifications do you see? 您看到什么通知? How are you building and managing your device list? 您如何建立和管理设备列表? Under what conditions should the LED light turn on on your device? 在什么情况下设备上的LED灯应该亮起?

Regardless of this, almost certainly you should be calling mGatt.close() in your onDestroy() or onPause() if you aren't calling it before. 无论如何,如果您之前没有调用过,几乎可以肯定应该在onDestroy()onPause()调用mGatt.close()

Whether pressing the back button finishes the app depends on how it is set up and won't necessarily mean there is some resource that hasn't been released but it's something worth checking. 按下后退按钮是否完成应用程序取决于它的设置方式,并不一定意味着有一些资源尚未释放,但这值得检查。 It's more likely to be a Service that has been left running than an unregistered broadcast receiver which I think gets handled by the system (this doesn't mean you shouldn't deregister your receivers). 与未注册的广播接收器相比,它更可能是处于运行状态的服务,我认为这是由系统处理的(这并不意味着您不应该注销接收器)。

Disconnecting has nothing to do with setting your BluetoothAdapter to null. 断开连接与将BluetoothAdapter设置为null没有关系。

To disconnect from a device you have to call mGatt.close() 要断开与设备的连接,必须调用mGatt.close()

If you want to turn bluetooth off after that call mAdapter.disable() 如果要在此之后关闭蓝牙,请调用mAdapter.disable()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM