Android BLE - 双“配对请求”对话框/弹出窗口 - 第一次尝试导致连接失败
Posted
技术标签:
【中文标题】Android BLE - 双“配对请求”对话框/弹出窗口 - 第一次尝试导致连接失败【英文标题】:Android BLE - Double 'Pairing Request' Dialog / Popup - Causing Failed Connection On 1st Try 【发布时间】:2020-10-12 18:58:22 【问题描述】: 我正在进一步开发一个 android 应用程序,作为我们公司为我们的某些产品生产的这些遥控器/FOB 的无线固件更新程序。这些 FOB 可以通过蓝牙连接到我们的应用程序,这使我们能够根据需要更新固件。我将为以下场景附上 Logcat 的副本:当我成功连接到先前连接的 FOB 时,以及当我第一次无法连接到 NEW FOB 时。我可以在这里添加一些代码,但这是一个大文件,不知道是否有人想通过800行代码。我们遇到了以下问题,并且已经工作了几天试图查明问题(注意,不是原始应用设计者,但我们正在梳理代码以试图解决问题):
案例 1:
如果我们之前有通过App连接过的FOB,可以将FOB设置为配对模式,打开APP,搜索我们的FOB,点击ID连接,连接成功,开始加载最新的固件文件。案例 2:
假设我们需要打开一盒从制造商处收到的 FOB,并更新他们的固件。这些 FOB 之前未连接到此应用程序,但仍应连接相同。我们将 FOB 置于配对模式,打开应用程序,选择要连接的 FOB ID - 这次我们会看到两个配对对话框。选择 FOB 后,Android 会询问“您要与此设备配对吗?”然后我们选择“配对”。接下来,我们会看到另一个对话框,要求我们配对,这次是输入 PIN 码。每次我们在连接时出现这两个对话框时,BluetoothGatt 服务都会超时并在 Logcat 中报告“与服务断开连接”。HOWEVER, the strange part is - after this fail we can close & reopen the app and it connects perfectly with no issues whatsoever...
我们已经确定这个问题是主要活动进入后台的结果,好吧 - 在 Logcat 中,我们配对并输入 PIN 后,我们可以看到获得了 BLE 特征,我们得到 'Connected to远程服务”,然后是“输入通道被破坏”,然后是“应用程序进入后台”,然后再经过几行,它运行一个方法来“销毁BluetoothGatt”,然后是“不活动,与服务断开连接”。 这一切都发生得很快,我没有足够的 Java 技能来理解我应该如何解决这个问题。
LOGCAT FOR SUCCESSFUL CONNECTION ON PRIORLY CONNECTED FOB
I/System.out: we are inside the **public void onServiceConnected(ComponentName
componentName, IBinder service)
D/BluetoothAdapter: STATE_ON
D/BluetoothGatt: connect() - device: 00:60:37:65:D3:D2, auto: false
D/BluetoothAdapter: isSecureModeEnabled
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=8ef8051c-1036-496b-9389-9be8eb40a350
D/BluetoothLeService: Trying to create a new connection.
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=8
device=00:60:37:65:D3:D2
D/BluetoothGatt: discoverServices() - device: 00:60:37:65:D3:D2
I/BluetoothLeService: ******* On Connection State onConnectionStateChange 2 Status 0
I/System.out: Devices click1
I/System.out: we are inside the bluetooth le listener and
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 10
D/BluetoothGatt: onConnectionUpdated() - Device=00:60:37:65:D3:D2 interval=6 latency=0
timeout=500 status=0
D/BluetoothGatt: onSearchComplete() = Device=00:60:37:65:D3:D2 Status=0
I/System.out: we are inside the onServicesDiscovered(BluetoothGatt gatt, int status),
on bluetoothleservice
I/BluetoothLeService: Find BleoTa Characteristique 49646561-7346-6c6f-6174-40882af10001
And Value 49646561-7346-6c6f-6174-40882af10001
I/System.out: Devices click5
D/com.example.interactivefirmwareupdate.ConnectFragment: *************************
Services Discovered*****************
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID From
*********** 00001801-0000-1000-8000-00805f9b34fb
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID From
*********** 00001800-0000-1000-8000-00805f9b34fb
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID From
*********** 49646561-7346-6c6f-6174-41882ef00000
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID IQ
*********** 49646561-7346-6c6f-6174-41882ef00000
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID From
*********** 49646561-7346-6c6f-6174-40882af10000
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID OTA
*********** 49646561-7346-6c6f-6174-40882af10000
D/BluetoothGatt: setCharacteristicNotification() - uuid: 49646561-7346-6c6f-6174-
40882af10001 enable: true
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID From
*********** 0000180a-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onConnectionUpdated() - Device=00:60:37:65:D3:D2 interval=36 latency=0
timeout=500 status=0
D/BluetoothGatt: setCharacteristicNotification() - uuid: 49646561-7346-6c6f-6174-
40882af10001 enable: true
I/System.out: ********************We are inside OnPause Line 293 - ConnectFrag and the
value of connectingtopin is true
D/BluetoothGatt: onConnectionUpdated() - Device=00:60:37:65:D3:D2 interval=6 latency=0
timeout=500 status=0
D/FA: Connected to remote service
D/InputTransport: Input channel destroyed: fd=74
D/FA: Application going to the background
D/InputTransport: Input channel constructed: fd=74
D/ViewRootImpl@ef4a73[MainActivity]: stopped(false) old=false
I/System.out: Devices click4
D/com.example.interactivefirmwareupdate.ConnectFragment: ******
ChangeCharacteristic49646561-7346-6c6f-6174-40882af10001Data [B@973abf2
D/com.example.interactivefirmwareupdate.ConnectFragment: *************************
Services showAlertDialog()*****************
D/Dialog: mIsSamsungBasicInteraction = false
D/Dialog: mIsSamsungBasicInteraction = false, isMetaDataInActivity = false
I/DecorView: createDecorCaptionView >> DecorView@c016708[], isFloating: true,
isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: false
D/InputTransport: Input channel constructed: fd=77
I/com.example.interactivefirmwareupdate.ConnectFragment: Item for List loadFile()
iq_9d27Path bleota
LOGCAT FOR FAILED CONNECTION ON NEW/NEVER CONNECTED FOB
D/BluetoothAdapter: stopLeScan()
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothLeScanner: Stop Scan with callback
D/AbsListView: in onLayout changed
D/ViewRootImpl@ef4a73[MainActivity]: ViewPostIme pointer 0
D/ViewRootImpl@ef4a73[MainActivity]: ViewPostIme pointer 1
D/AbsListView: onTouchUp() mTouchMode : 0
I/com.example.interactivefirmwareupdate.ConnectFragment: Click
I/System.out: we are inside the **public void onServiceConnected(ComponentName componentName, IBinder service)
D/BluetoothLeService: BluetoothAdapter not initialized or unspecified address.com.example.interactivefirmwareupdate.ConnectFragment$5@7a7fcb6 Two Handler (com.example.interactivefirmwareupdate.library.BluetoothLeHandler) c4a3db7
D/BluetoothAdapter: STATE_ON
D/BluetoothGatt: connect() - device: 00:60:37:58:A0:7F, auto: false
D/BluetoothAdapter: isSecureModeEnabled
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=e3f7f816-c25c-4c0f-a5ef-e7775b46f48d
D/BluetoothLeService: Trying to create a new connection.
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=8 device=00:60:37:58:A0:7F
D/BluetoothGatt: discoverServices() - device: 00:60:37:58:A0:7F
I/BluetoothLeService: ******* On Connection State onConnectionStateChange 2 Status 0
I/System.out: Devices click1
I/System.out: we are inside the bluetooth le listener and &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 11(bonding)
I/System.out: ********************We are inside OnPause Line 293 - ConnectFrag and the value of connectingtopin is true
D/ViewRootImpl@ef4a73[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
D/InputMethodManager: prepareNavigationBarInfo() DecorView@903cf1b[MainActivity]
D/InputMethodManager: getNavigationBarColor() -855310
D/FA: Connected to remote service
D/BluetoothGatt: onConnectionUpdated() - Device=00:60:37:58:A0:7F interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt: onSearchComplete() = Device=00:60:37:58:A0:7F Status=0
I/System.out: we are inside the onServicesDiscovered(BluetoothGatt gatt, int status), on bluetoothleservice
I/BluetoothLeService: Find BleoTa Characteristique 49646561-7346-6c6f-6174-40882af10001 And Value 49646561-7346-6c6f-6174-40882af10001
I/System.out: Devices click5
D/com.example.interactivefirmwareupdate.ConnectFragment: ************************* Services Discovered*****************
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID From *********** 00001801-0000-1000-8000-00805f9b34fb
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID From *********** 00001800-0000-1000-8000-00805f9b34fb
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID From *********** 49646561-7346-6c6f-6174-41882ef00000
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID IQ *********** 49646561-7346-6c6f-6174-41882ef00000
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID From *********** 49646561-7346-6c6f-6174-40882af10000
I/com.example.interactivefirmwareupdate.ConnectFragment: *********** Service UUID OTA *********** 49646561-7346-6c6f-6174-40882af10000
Service UUID From *********** 0000180a-0000-1000-8000-00805f9b34fb
D/BluetoothGatt: onConnectionUpdated() - Device=00:60:37:58:A0:7F interval=36 latency=0 timeout=500 status=0
\D/FA: Application going to the background
\D/ViewRootImpl@ef4a73[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 1
D/InputMethodManager: prepareNavigationBarInfo() DecorView@903cf1b[MainActivity]
D/InputMethodManager: getNavigationBarColor() -855310
D/ViewRootImpl@ef4a73[MainActivity]: stopped(false) old=false
D/BluetoothGatt: setCharacteristicNotification() - uuid: 49646561-7346-6c6f-6174-40882af10001 enable: true
I/System.out: ********************We are inside OnPause Line 293 - ConnectFrag and the value of connectingtopin is true
D/ViewRootImpl@ef4a73[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1
D/InputMethodManager: prepareNavigationBarInfo() DecorView@903cf1b[MainActivity]
D/InputMethodManager: getNavigationBarColor() -855310
D/BluetoothGatt: onConnectionUpdated() - Device=00:60:37:58:A0:7F interval=6 latency=0 timeout=500 status=0
D/InputTransport: Input channel destroyed: fd=74
D/ViewRootImpl@ef4a73[MainActivity]: MSG_RESIZED: frame=[0,0][1200,1920] ci=[0,36][0,578] vi=[0,36][0,578] or=1
D/ViewRootImpl@ef4a73[MainActivity]: Relayout returned: old=[0,0][1200,1920] new=[0,0][1200,1920] result=0x1 surface=true 3960862720 changed=false
D/FA: Application going to the background
如果有人对此有任何意见,我将不胜感激。另外,如果需要,我可以发布代码,我想我会先看看你们中的一些人要说什么,然后再制作一个充满令人困惑的内容的帖子。非常感谢。
【问题讨论】:
【参考方案1】:您可以尝试使用设置的密码调用 BluetoothDevice.createBond。一旦收到 BluetoothDevice.BOND_NONE 然后调用 BluetoothDevice.connectGatt。 搜索createBond Android,你会看到一些示例
【讨论】:
以上是关于Android BLE - 双“配对请求”对话框/弹出窗口 - 第一次尝试导致连接失败的主要内容,如果未能解决你的问题,请参考以下文章