android - 蓝牙连接另一台安卓设备一段时间后一直断开

标签 android bluetooth

我有一台 Xperia Z3C,Android 5.1.1,我正在尝试通过蓝牙将它连接到 Nexus 4 Android 6.0.1。可以配对它们,但当我尝试连接它们时,它显示“已连接”不到一秒钟,然后自动断开连接。

这是一个已知问题吗?我还没有找到任何相关信息。

这是 logcat 的内容:

03-14 17:25:55.308: E/bt-btif(14245): bte_scan_filt_param_cfg_evt, 1
03-14 17:25:55.308: E/bt-btif(14245): bte_scan_filt_param_cfg_evt, 1
03-14 17:25:55.990: E/bt-btm(14245): new address: xx:xx:xx:xx:xx:xx
03-14 17:25:55.990: E/bt-btm(14245): new address: xx:xx:xx:xx:xx:xx
03-14 17:25:56.712: E/bt-btm(14245): new address: xx:xx:xx:xx:xx:xx
03-14 17:25:57.464: E/bt-btif(14245): bte_scan_filt_param_cfg_evt, 1
03-14 17:25:57.814: E/bt-btm(14245): Ignoring RNR as the state is not BTM_SEC_STATE_GETTING_NAME
03-14 17:25:57.933: E/NetlinkEvent(318): Unknown ifindex 41 in RTM_DELADDR
03-14 17:25:57.963: E/dhcpcd(15974): bt-pan: interface not found or invalid
03-14 17:25:57.986: E/BluetoothPanServiceJni(14245): ## ERROR : disconnectPanNative(L224): Failed disconnect pan channel, status: 1##
03-14 17:25:58.139: E/BluetoothTetheringNetworkFactory(14245): DHCP request error:DHCP result was failed
03-14 17:26:01.382: E/bt-btm(14245): btm_sec_disconnected - Clearing Pending flag

最佳答案

我在尝试从我的 Android 应用程序扫描蓝牙设备时遇到了同样的问题。有趣的是,它不会抛出任何异常(捕获或未捕获)并默默地失败! API 级别 >= 23 (Android 6.0) 的设备会出现此问题。这是我的 Logcat 日志的摘录:

03-30 14:07:52.099 192-211/? I/BufferQueueProducer: [org.manios.testbt/org.manios.testbt.printing.BluetoothPrinterDiscovery](this:0x7fa16de400,id:1467,api:1,p:26262,c:192) queueBuffer: fps=0.19 dur=5154.29 max=5154.29 min=5154.29
03-30 14:07:52.114 192-192/? I/SurfaceFlinger: [Built-in Screen (type:0)] fps:1.091767,dur:1831.89,max:999.66,min:832.23
03-30 14:07:52.137 774-1001/? D/InputReader: AP_PROF:AppLaunch_dispatchPtr:Up:89556864, ID:0, Index:2056797440
03-30 14:07:52.137 774-1001/? I/PerfService: PerfServiceNative_boostEnableTimeoutMsAsync:3, 100
03-30 14:07:52.138 26262-26262/org.manios.testbt D/SettingsInterface:  from settings cache , name = sound_effects_enabled , value = 0
03-30 14:07:52.138 26262-26262/org.manios.testbt D/BluetoothAdapter: isEnabled
03-30 14:07:52.140 26262-26262/org.manios.testbt D/BluetoothAdapter: getName
03-30 14:07:52.141 774-1636/? D/BluetoothManagerService: checkIfCallerIsForegroundUser: valid=true callingUser=0 parentUser=-10000 foregroundUser=0
03-30 14:07:52.142 21807-21818/? D/BluetoothAdapterProperties: getName: mName = Lenovo TAB3 8
03-30 14:07:52.144 26262-666/org.manios.testbt D/BluetoothAdapter: isEnabled
03-30 14:07:52.146 26262-666/org.manios.testbt D/BluetoothAdapter: isDiscovering
03-30 14:07:52.148 26262-666/org.manios.testbt D/BluetoothAdapter: 109507751: getState(). Returning 12
03-30 14:07:52.152 21807-21817/? D/BluetoothAdapterProperties: isDiscovering: mDiscovering = false
03-30 14:07:52.156 26262-666/org.manios.testbt D/BluetoothAdapter: startDiscovery
03-30 14:07:52.158 26262-666/org.manios.testbt D/BluetoothAdapter: 109507751: getState(). Returning 12
03-30 14:07:52.160 21807-21830/? W/bt_btif: bta_dm_check_av:0
03-30 14:07:52.172 21807-21830/? E/bt_btif: bte_scan_filt_param_cfg_evt, 1
03-30 14:07:52.181 21807-21823/? I/BluetoothAdapterProperties: Callback:discoveryStateChangeCallback with state:1
03-30 14:07:52.182 774-2924/? V/ActivityManager: Broadcast: Intent { act=android.bluetooth.adapter.action.DISCOVERY_STARTED flg=0x10 } ordered=false userid=0 callerApp=ProcessRecord{c376192 21807:com.android.bluetooth/1002}
03-30 14:07:52.184 1057-1265/? V/BluetoothEventManager: Received android.bluetooth.adapter.action.DISCOVERY_STARTED
03-30 14:07:52.185 1057-1265/? D/BluetoothEventManager: scanning state change to true
03-30 14:07:52.186 21983-21983/? D/ActivityThread: BDC-Calling onReceive: intent=Intent { act=android.bluetooth.adapter.action.DISCOVERY_STARTED flg=0x10 cmp=com.android.settings/.bluetooth.BluetoothDiscoveryReceiver }, receiver=com.android.settings.bluetooth.BluetoothDiscoveryReceiver@b02fe63
03-30 14:07:52.186 21983-21983/? V/BluetoothDiscoveryReceiver: Received: android.bluetooth.adapter.action.DISCOVERY_STARTED
03-30 14:07:52.187 21983-21983/? D/ActivityThread: BDC-RECEIVER handled : 0 / ReceiverData{intent=Intent { act=android.bluetooth.adapter.action.DISCOVERY_STARTED flg=0x10 cmp=com.android.settings/.bluetooth.BluetoothDiscoveryReceiver } packageName=com.android.settings resultCode=-1 resultData=null resultExtras=null}
03-30 14:07:52.218 774-1001/? D/InputReader: AP_PROF:AppLaunch_dispatchPtr:Down:89556946, ID:0, Index:2056794048
03-30 14:07:52.219 774-1001/? I/PerfService: PerfServiceNative_boostEnableAsync:3

我使用这个 issue 进行了一番搜索后发现, 在 API 级别 >= 23 (Android 6.0) 中,我们必须向 AndroidManifext.xml 添加以下权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

以便扫描和其他蓝牙操作正常运行。

在 Android 文档中,它在 BluetoothAdapter.startDiscovery() 中说明它需要 BLUETOOTH_ADMIN没有更多细节的许可。但是如果我们看一下 BluetoothLeScanner.startScan() ,它明确指出:

Requires BLUETOOTH_ADMIN permission. An app must hold ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to get results.

希望这对您有所帮助!

关于android - 蓝牙连接另一台安卓设备一段时间后一直断开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35997445/

相关文章:

java - 有没有办法为我的主要 Activity 设定条件?

javascript - 查找距离我当前位置最近的 friend

java - 在 Java 中为 Android 开发时 "shared library"究竟意味着什么? (`*.jar` 或 `*.so`)

android - WorkManager - 如何按顺序执行作业

ios - 同一台iDevice上同时运行的两个应用程序可以通过蓝牙进行通信吗?

android - 连接到具有最佳 RSSI 的蓝牙设备

android - SpeechRecognizer 突然无法离线工作

bluetooth - _serial_.bufferUntil(byte) 有什么作用,它如何与serialEvent 协同作用?

android - 将 Android Nexus One 与 Arduino + BlueSmirf 连接起来

android - 需要以编程方式打开蓝牙并在 Android 中发现设备