【开源-Android】基于蓝牙备份联系人

前言

  如何把手机通讯录里的联系人备份到另一个手机里,或许大家都会有不同的方法。其一:一个一个的输入,好吧,太麻烦;其二:通过云端备份工具,如百度云,微云等,这个确实很方便,但是不得不把自己联系人信息存到人家的服务器,有些人就不愿意干了。

  那么,既能很方便又能保护隐私的方法那就是今天我开源的一个应用了-蓝牙备份联系人。只需要两个手机都安装这个应用那么就可以通过这个软件进行通讯录的备份。

  先给大家看几张软件截图:


  

  在软件的主界面会首先显示你手机里的全部联系人,包括已经删除的,然后你可以在菜单选择建立蓝牙连接也就死右侧的图片。建立连接之后你就可以选择要备份的联系人,然后点击右下方的按钮进行发送,对方手机收到之后会有提示,点击就可以进行备份了。因为整个过程都不用连接网络所以隐私的安全就不会有问题。

  下面我就说一下开发中的要点:
  蓝牙如何用代码开启?

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

源码下载:https://github.com/JcMan/BlueToothContacts

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值