前言
如何把手機通訊錄裏的聯繫人備份到另一個手機裏,或許大家都會有不同的方法。其一:一個一個的輸入,好吧,太麻煩;其二:通過雲端備份工具,如百度雲,微雲等,這個確實很方便,但是不得不把自己聯繫人信息存到人家的服務器,有些人就不願意幹了。
那麼,既能很方便又能保護隱私的方法那就是今天我開源的一個應用了-藍牙備份聯繫人。只需要兩個手機都安裝這個應用那麼就可以通過這個軟件進行通訊錄的備份。
先給大家看幾張軟件截圖:
在軟件的主界面會首先顯示你手機裏的全部聯繫人,包括已經刪除的,然後你可以在菜單選擇建立藍牙連接也就死右側的圖片。建立連接之後你就可以選擇要備份的聯繫人,然後點擊右下方的按鈕進行發送,對方手機收到之後會有提示,點擊就可以進行備份了。因爲整個過程都不用連接網絡所以隱私的安全就不會有問題。
下面我就說一下開發中的要點:
藍牙如何用代碼開啓?
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 0);
這樣就可以出現是否開啓藍牙的提示。
如何獲取已經配對的藍牙設備?
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = ba.getBondedDevices();
如何獲取周圍沒有配對的藍牙設備?
ba.startDiscovery();//開始搜索周圍的藍牙設備
如何知道搜索的結果呢?我們需要註冊一個廣播接收器來接收搜索的結果。
private final BroadcastReceiver mReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
/**
* 搜索到一個藍牙設備
*/
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED){
String address = device.getAddress();
boolean isExist = false;
for(BluetoothDevice bluetoothDevice :mUnMatchedDevices){
if(device.getName().equals(bluetoothDevice.getName()))
isExist = true;
}
if (address.length()>10&&!isExist){
mUnMatchedDevices.add(device);
mUnMatchedAdapter.notifyDataSetChanged();
}
}
//l藍牙設備搜索完成
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
if(mUnMatchedDevices.size()==0){
Toast.makeText(SearchBlueToothActivity.this,"沒有發現設備",Toast.LENGTH_SHORT).show();
}
mSearchBtn.setText("搜索設備");
}
}
};
然後我們去註冊它:
private void initRegisterReceiver(){
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
}
當得到未配對的設備的時候,該如何去配對呢?
藍牙配對
private void requestBondToDevice(int position) {
BluetoothDevice device = mUnMatchedDevices.get(position);
ToastUtil.showMsg(this,"正在請求與"+device.getName()+"配對");
if(device.getBondState()==BluetoothDevice.BOND_NONE){
try {
Method creMethod = BluetoothDevice.class.getMethod("createBond");
creMethod.invoke(device);
/**
* 開啓一個線程用來監聽配對的結果
*/
if(mListenBondThread==null){
mListenBondThread = new ListenBondThread(device);
mListenBondThread.start();
}
} catch (Exception e){
e.printStackTrace();
}
}
}
這是用來監聽配對結果的線程:
class ListenBondThread extends Thread{
private BluetoothDevice mDevice;
public ListenBondThread(BluetoothDevice device){
mDevice = device;
}
@Override
public void run(){
while (true&&!mThreadExit){
if(mDevice.getBondState()==BluetoothDevice.BOND_NONE) {
// Logger.e("none");
}else if(mDevice.getBondState()==BluetoothDevice.BOND_BONDING){
// Logger.e("連接中...");
}
else if (mDevice.getBondState()==BluetoothDevice.BOND_BONDED) {
// Logger.e("連接成功");
bondSuccess(mDevice);
break;
}else{
bondFail();
// Logger.e("連接失敗");
break;
}
}
}
}
這些都不是太難的,難的是藍牙設備之間的建立連接和發送數據。這裏我就不能多說了,因爲這裏我使用的是網上的一個別人分享的一個類,裏面封裝了連接和發送數據的操作。雖然自己看倒是可以看懂,但是寫出來確實有些吃力,因爲有些細節確實可能不會處理的太好,所以大家可以自己去看看,看不懂也沒關係,因爲封裝的很好,可以直接拿來使用的。
好了,就說到這裏吧,我把源碼貢獻出來,如何大家有興趣可以自己研究。
軟件下載:http://download.csdn.net/detail/programchangesworld/9459255