bluetooth 藍芽事件監聽

2022-08-13 06:06:19 字數 4498 閱讀 5863

今天在做專案的時候,需要監聽乙個藍芽耳機的連線狀態。就寫了乙個小的測試方法。記錄如下

看**這要處理的是藍芽監聽事件

package

com.example.alert;

import

android.bluetooth.bluetoothadapter;

import

android.bluetooth.bluetoothheadset;

import

android.bluetooth.bluetoothprofile;

import

android.content.broadcastreceiver;

import

android.content.context;

import

android.content.intent;

import

android.util.log;

import

android.widget.toast;

public

class headsetplugreceiver extends

broadcastreceiver

else

if(action.equals(bluetoothadapter.action_state_changed))

} else

if(action

.equals(bluetoothheadset.action_connection_state_changed)) }}

}

藍芽的監聽,需要加入許可權,如下

<

uses-permission

android:name

="android.permission.bluetooth"

/>

<

uses-permission

android:name

="android.permission.bluetooth_admin"

/>

<

uses-permission

android:name

="android.permission.bluetooth_privileged"

/>

下面是我們列印的結果

1.沒有藍芽耳機連線的情況下

藍芽從斷開到連線

1

09-21

11:45:27.223: e/123(15118

): bluetoothadapter.action_state_changed 2

09-21

11:45:27.223: e/123(15118

): bluetoothadapter.state_turning_on 3

09-21

11:45:27.707: e/123(15118

): bluetoothadapter.action_state_changed 4

09-21

11:45:27.708: e/123(15118): bluetoothadapter.state_on

從連線到斷開

1

09-21

11:49:09.126: e/123(15118

): bluetoothadapter.action_state_changed 2

09-21

11:49:09.126: e/123(15118

): bluetoothadapter.state_turning_off 3

09-21

11:49:09.271: e/123(15118

): bluetoothadapter.action_state_changed 4

09-21

11:49:09.271: e/123(15118): bluetoothadapter.state_off

2.有藍芽耳機連線的情況

關閉藍芽耳機(藍芽保持開啟)

1

09-21

22:12:15.439: e/123(17042

): bluetoothheadset.action_connection_state_changed 2

09-21

22:12:15.449: e/123(17042

): bluetoothprofile.state_disconnected 3

09-21

22:12:16.457: e/123(17042): bluetoothadapter.action_connection_state_changed

連線藍芽耳機

1

09-21

22:14:22.356: e/123(17042

): bluetoothheadset.action_connection_state_changed 2

09-21

22:14:22.356: e/123(17042

): bluetoothprofile.state_connected 3

09-21

22:14:22.357: e/123(17042): bluetoothadapter.action_connection_state_changed

關閉藍芽

1

09-21

22:15:12.558: e/123(17042

): bluetoothadapter.action_state_changed 2

09-21

22:15:12.559: e/123(17042

): bluetoothadapter.state_turning_off 3

09-21

22:15:13.557: e/123(17042

): bluetoothheadset.action_connection_state_changed 4

09-21

22:15:13.557: e/123(17042

): bluetoothprofile.state_disconnected 5

09-21

22:15:13.908: e/123(17042

): bluetoothadapter.action_state_changed 6

09-21

22:15:13.908: e/123(17042): bluetoothadapter.state_off

開啟藍芽

1

09-21

22:16:06.976: e/123(17042

): bluetoothheadset.action_connection_state_changed 2

09-21

22:16:06.976: e/123(17042

): bluetoothprofile.state_connecting 3

09-21

22:16:06.978: e/123(17042

): bluetoothadapter.action_connection_state_changed 4

09-21

22:16:07.036: e/123(17042

): bluetoothadapter.action_state_changed 5

09-21

22:16:07.036: e/123(17042

): bluetoothadapter.state_on 6

09-21

22:16:10.616: e/123(17042

): bluetoothheadset.action_connection_state_changed 7

09-21

22:16:10.616: e/123(17042

): bluetoothprofile.state_connected 8

09-21

22:16:10.619: e/123(17042): bluetoothadapter.action_connection_state_changed

從上面的結果我們能夠明白藍芽監聽相關事件

的職責1.bluetoothadapter.action_state_changed:這個主要是用來監聽藍芽開啟與否的狀態,它穿過來的引數包含藍芽正在斷開、斷開、正在連線、連線的狀態,每次狀態更改,都會觸發這個廣播

2.bluetoothadapter.action_connection_state_changed:這個主要是監聽藍芽裝置連線狀態。它同樣包含四種狀態

3.bluetoothheadset.action_connection_state_changed:這個就是監聽藍芽耳機的連線狀態了。

至於他們監聽的順序,目前還不大清楚,似乎沒有乙個嚴格的先後順序。就是感覺是

如果裝置處於連線狀態,優先順序比較高

如果裝置處於斷開狀態,它的存在條件優先順序比較高

bluetooth 藍芽事件監聽

今天在做專案的時候,需要監聽乙個藍芽耳機的連線狀態。就寫了乙個小的測試方法。記錄如下 看 這要處理的是藍芽監聽事件 package com.example.alert import android.bluetooth.bluetoothadapter import android.bluetooth...

bluetooth 藍芽事件監聽

今天在做專案的時候,需要監聽乙個藍芽耳機的連線狀態。就寫了乙個小的測試方法。記錄如下 看 這要處理的是藍芽監聽事件 package com.example.alert import android.bluetooth.bluetoothadapter import android.bluetooth...

BlueTooth 藍芽比拼Zigbee

作者 張國斌 曾幾何時,以低功耗和組網優勢而著稱的後來者zigbee喧囂甚上,大有取代藍芽的勢頭,不過,隨著2010年6月藍芽4.0的發布,這一局面已經大為改觀,藍芽實現驚天大逆轉,並有可能乾掉zigbee!至少在智慧型家居領域已經進基本做到了,藍芽是如何實現這個驚天逆轉的?一 人多力量大 1995...