接上文:
1. 獲取藍芽介面卡:
// 得到本機藍芽裝置
mbluetoothadapter = bluetoothadapter.getdefaultadapter();
if (mbluetoothadapter != null) else
2. 新增監聽:
void registerbleutoothstatereceiver()
3. 獲取裝置:
已繫結:
// 獲得已配對的遠端藍芽裝置的集合
setdevices = mbluetoothadapter.getbondeddevices();
掃瞄:
/**
* 在開始掃瞄前, 注意檢測是否正在掃瞄.
* start discovery bluetooth devices
* @param force
*/void startdiscovery(boolean force)else
}mbluetoothadapter.startdiscovery();
}
掃瞄到裝置後, 會有廣播:
if(bluetoothdevice.action_found.equals(action))
獲取已連線裝置:
//可以理解為兩個**,
private bluetoothheadset mbluetoothheadset=null;
private bluetootha2dp mbluetootha2dp=null;
private bluetoothprofile.servicelistener mheadsetprofileservicelistener = new bluetoothprofile.servicelistener()
@override
public void onserviceconnected(int arg0, bluetoothprofile profile)
}; private bluetoothprofile.servicelistener mprofileservicelistener = new bluetoothprofile.servicelistener()
//get profile after bluetooth was enabled
mbluetoothadapter.getprofileproxy(activitybluetooth.this, mprofileservicelistener, bluetoothprofile.a2dp);
mbluetoothadapter.getprofileproxy(activitybluetooth.this, mheadsetprofileservicelistener, bluetoothprofile.headset);
/** 記得在用完後, 去close 掉 **/
if(mbluetootha2dp != null)mbluetoothadapter.closeprofileproxy(bluetoothprofile.a2dp, mbluetootha2dp);
if(mbluetoothheadset != null)mbluetoothadapter.closeprofileproxy(bluetoothprofile.headset, mbluetoothheadset);
4. 配對裝置:
呼叫bluetoothdevice.createbond();
處理狀態廣播:
if(bluetoothdevice.action_bond_state_changed.equals(action))
5. 連線裝置:
boolean debugconnect = true;
/** connect bluetooth device **/
boolean connectdevice(bluetoothdevice device)
if(debugconnect)alog.d(tag, "try connect to " + device.getname());
boolean result = false;
try catch (illegalacces***ception e) catch (illegalargumentexception e) catch (invocationtargetexception e) catch (nosuchmethodexception e)
return result;
}
監聽連線狀態:
if(bluetoothadapter.action_connection_state_changed.equals(action))else if(state == bluetoothadapter.state_connecting)else
}
6. 取消配對:
boolean debugunpair = true;
/** unpair device **/
void unpairdevice()elseelse if(device.getbondstate() == bluetoothdevice.bond_bonded)
if(debugunpair)alog.d(tag, "unpair device " + device.getname() + (result ? " success" : " failed"));
} catch (exception e)
} }
// 取消正在配對
private boolean cancelbondprocess(class bclass,
bluetoothdevice device) throws exception
private boolean removebond(class blass, bluetoothdevice device)
throws exception
7. 斷開連線:
boolean disconnectheadset(bluetoothdevice device)
} catch (nosuchmethodexception e) catch (illegalacces***ception e) catch (illegalargumentexception e) catch (invocationtargetexception e)
return false; }
boolean disconnecta2dp(bluetoothdevice device)
} catch (nosuchmethodexception e) catch (illegalacces***ception e) catch (illegalargumentexception e) catch (invocationtargetexception e)
return true;
}
ps: 在做其它操作前, 注意取消掃瞄:
if (mbluetoothadapter.isdiscovering()) ;
以上**僅適應藍芽音箱或藍芽耳機, 其它種類的裝置暫未相容測試.
另外, 小公尺三星等廠商對wifi/bt這一部分的**或平台相容性上做了不少事情.
以上**相容性上存在不少問題, 僅僅為了整理和記錄,
本文完.
藍芽學習(一) 低功耗藍芽和經典藍芽概述
經典藍芽 classic bluetooth 和低功耗藍芽 bluetooth low energy,簡稱ble或者le 兩者有什麼區別?為什麼他們都叫 藍芽 bluetooth low energy和bluetooth smart兩者又有什麼區別?我的應用到底該選經典藍芽技術還是低功耗藍芽技術?這...
藍芽通訊(一) 開啟和關閉藍芽
移步 android藍芽通訊 新增許可權 android name android.permission.bluetooth 開啟藍芽 呼叫系統對話方塊開啟藍芽 param view public void openbluetoothbysystem view view 效果圖 新增許可權 andr...
調教xine和藍芽
win32codecs usr lib codecs xine lib extras nonfree 配合kaffeine,對藍芽的支援fine。totem不能更改alsa.device。太傻瓜了。況且是gtk的。mplayer貌似影音同步有問題。關於藍芽 hcitool scan 是搜尋藍芽裝置的...