Android 自動連線藍芽

2021-09-04 10:55:01 字數 1351 閱讀 5186

藍芽是手機和智慧型硬體通訊常用的方式,藍芽通訊可分為傳統藍芽和低功耗藍芽(ble)。其中ble的連線斷開等操作可以通過android sdk中提供的api進行操作,而傳統藍芽部分sdk並沒有提供相關的api進行連線斷開,只能使用者自己通過手機的設定介面連線藍芽。

那麼我們如何做到在**中自動連線傳統藍芽呢?我們可以利用反射呼叫android中的私有api進行連線斷開操作。

private bluetoothdevice device;

private bluetoothadapter bluetoothadapter;

private bluetoothheadset mbluetoothheadset;

private bluetootha2dp mbluetootha2dp;

bluetoothadapter = bluetoothadapter.getdefaultadapter();

bluetoothadapter.getprofileproxy(this, new bluetoothprofile.servicelistener()

}public void onservicedisconnected(int profile)

}},bluetoothprofile.headset);

bluetoothadapter.getprofileproxy(this, new bluetoothprofile.servicelistener()

}public void onservicedisconnected(int profile)

}},bluetoothprofile.a2dp);

private void connect() 

if (device == null)

try catch (exception e)

}

private void connect2() 

if (device == null)

try catch (exception e)

}

同時斷開hfp和a2dp。

private void disconnect() 

if (device == null)

try catch (exception e)

if (mbluetootha2dp == null)

try catch (exception e)

}

至此所有關鍵**都已經完成,在使用過程中,可能會有出現連線不上的問題。實驗證明先斷開然後在分別連線hfp和a2dp會穩定很多。

Android 藍芽連線

一 概述 藍芽是一種無線技術標準,可實現固定裝置 移動裝置和樓宇個人域網之間的短距離資料交換。最多可以同時和7個其它藍芽裝置建立連線,進行通訊。藍芽可分為兩大類 傳統藍芽 藍芽3.0規範之前 低功耗藍芽 藍芽4.0規範之後 android 從4.3版本 api level 18 開始支援低功耗藍芽b...

Android 藍芽連線

今天要做乙個藍芽4.0的通訊,先做個小test,之後再看看具體的api override public void onclick view v 3,掃瞄附近的裝置 list devicelist new arraylist if bluetoothadapter.isdiscovering else...

Android藍芽自動配對授權連線的實現方法

如果有其他藍芽裝置申請對此裝置進行連線,不需要彈出需要使用者確認的對話方塊和pin code對話方塊,實現自動連線的一種實現方法 新增變數 private static final int notification id android.r.drawable.stat sys data blueto...