導航:首頁 > 操作系統 > android檢測藍牙狀態

android檢測藍牙狀態

發布時間:2022-11-22 15:26:11

1. android藍牙怎麼檢測連接狀態

首先,要操作藍牙,先要在AndroidManifest.xml里加入許可權

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

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

然後,看下api,Android所有關於藍牙開發的類都在android.bluetooth包下,如下圖,只有8個類

而我們需要用到了就只有幾個而已:

1.BluetoothAdapter 顧名思義,藍牙適配器,直到我們建立bluetoothSocket連接之前,都要不斷操作它BluetoothAdapter里的方法很多,常用的有以下幾個:cancelDiscovery() 根據字面意思,是取消發現,也就是說當我們正在搜索設備的時候調用這個方法將不再繼續搜索disable()關閉藍牙enable()打開藍牙,這個方法打開藍牙不會彈出提示,更多的時候我們需要問下用戶是否打開,一下這兩行代碼同樣是打開藍牙,不過會提示用戶:Intemtenabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enabler,reCode);//同startActivity(enabler);

getAddress()獲取本地藍牙地址getDefaultAdapter()獲取默認BluetoothAdapter,實際上,也只有這一種方法獲取BluetoothAdaptergetName()獲取本地藍牙名稱getRemoteDevice(String address)根據藍牙地址獲取遠程藍牙設備getState()獲取本地藍牙適配器當前狀態(感覺可能調試的時候更需要)isDiscovering()判斷當前是否正在查找設備,是返回true***isEnabled()判斷藍牙是否打開,已打開返回true,否則,返回false***(String name,UUID uuid)根據名稱,UUID創建並返回BluetoothServerSocket,這是創建BluetoothSocket伺服器端的第一步startDiscovery()開始搜索,這是搜索的第一步2.BluetoothDevice看名字就知道,這個類描述了一個藍牙設備(UUIDuuid)根據UUID創建並返回一個BluetoothSocket這個方法也是我們獲取BluetoothDevice的目的——創建BluetoothSocket

這個類其他的方法,如getAddress(),getName(),同BluetoothAdapter

3.BluetoothServerSocket如果去除了Bluetooth相信大家一定再熟悉不過了,既然是Socket,方法就應該都差不多,這個類一種只有三個方法

兩個重載的accept(),accept(inttimeout)兩者的區別在於後面的方法指定了過時時間,需要注意的是,執行這兩個方法的時候,直到接收到了客戶端的請求(或是過期之後),都會阻塞線程,應該放在新線程里運行!

還有一點需要注意的是,這兩個方法都返回一個BluetoothSocket,最後的連接也是伺服器端與客戶端的兩個BluetoothSocket的連接

close()這個就不用說了吧,翻譯一下——關閉!4.BluetoothSocket,跟BluetoothServerSocket相對,是客戶端一共5個方法,不出意外,都會用到

close(),關閉connect()連接getInptuStream()獲取輸入流getOutputStream()獲取輸出流getRemoteDevice()獲取遠程設備,這里指的是獲取bluetoothSocket指定連接的那個遠程藍牙設備

2. android 怎麼判斷藍牙連接狀態

在設置——藍牙里,在顯示的藍牙列表裡有已連接或者斷開狀態

3. Android怎麼檢測藍牙的連接狀態如果一段斷開,我這邊怎麼檢測得到

按照藍牙規范,一旦超時連接斷開,會返回上層Disconnect complete Event with reason code: supervision timeout.可以根據該原因碼,檢測連接狀態,具體方法為:
1、BluetoothAdapter 顧名思義,藍牙適配器,直到建立bluetoothSocket連接之前,都要不斷操作它BluetoothAdapter里的方法很多,常用的有以下幾個:cancelDiscovery() 根據字面意思,是取消發現,也就是說當正在搜索設備的時候調用這個方法將不再繼續搜索disable()關閉藍牙enable()打開藍牙,這個方法打開藍牙不會彈出提示,更多的時候需要問下用戶是否打開,一下這兩行代碼同樣是打開藍牙,不過會提示用戶:Intemtenabler=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler,reCode);//同startActivity(enabler);

getAddress()獲取本地藍牙地址getDefaultAdapter()獲取默認BluetoothAdapter,實際上,也只有這一種方法獲取BluetoothAdaptergetName()獲取本地藍牙名稱。
2、getRemoteDevice(String address)根據藍牙地址獲取遠程藍牙設備getState()獲取本地藍牙適配器當前狀態(感覺可能調試的時候更需要)isDiscovering()判斷當前是否正在查找設備,是返回true***isEnabled()判斷藍牙是否打開,已打開返回true,否則,返回false***(String name,UUID uuid)根據名稱,UUID創建並返回BluetoothServerSocket,這是創建BluetoothSocket伺服器端的第一步startDiscovery()開始搜索,這是搜索的第一步2.BluetoothDevice看名字就知道,這個類描述了一個藍牙設備(UUIDuuid)根據UUID創建並返回一個BluetoothSocket這個方法也是我們獲取BluetoothDevice的目的——創建BluetoothSocket

這個類其他的方法,如getAddress(),getName(),同BluetoothAdapter。

4. Android藍牙開發怎麼檢查遠程藍牙設備是否打開

通過下面的代碼判斷藍牙是否鏈接:
BluetoothHeadset mBluetoothHeadset;

// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);

// Define Service Listener of BluetoothProfile
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
};

//call functions on mBluetoothHeadset to check if Bluetooth SCO audio is connected.
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
for ( final BluetoothDevice dev : devices ) {
return mBluetoothHeadset.isAudioConnected(dev);
}

// finally Close proxy connection after use.
mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset);

5. android怎麼來判斷藍牙開、關的狀態求代碼

Android 藍牙編程的基本步驟:

  1. 獲取藍牙適配器BluetoothAdapterblueadapter=BluetoothAdapter.getDefaultAdapter();

  2. 如果BluetoothAdapter 為null,說明android手機沒有藍牙模塊。

    判斷藍牙模塊是否開啟,blueadapter.isEnabled() true表示已經開啟,false表示藍牙並沒啟用。

  3. 啟動配置藍牙可見模式,即進入可配對模式Intentin=newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

    in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,200);

    startActivity(in); ,200就表示200秒。

  4. 獲取藍牙適配器中已經配對的設備Set<BluetoothDevice>device=blueadapter.getBondedDevices();

  5. 還需要在androidManifest.xml中聲明藍牙的許可權

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

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


接下來就是根據自己的需求對BluetoothAdapter的操作了。

閱讀全文

與android檢測藍牙狀態相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:144
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:737
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163