蓝牙配对

public void BTPairAsHost(Activity activity)
{
        Log.e(TAG,"BTPairAsHost");
        try{
            int delayTime = 3000;
            adapter = BluetoothAdapter.getDefaultAdapter();
            String mac = adapter.getAddress();
            boundDeviceAddress = "B8:BC:1B:1E:F5:D9";
            Log.e(TAG,"mac : "+mac);
            if(adapter != null)
            {
                    IntentFilter filter1 = new     IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);

IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter2.addAction(ACTION_PAIRING_REQUEST);
                                        filter2.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
final ConditionVariable done = new ConditionVariable();
BluetoothStateChangeReceiver receiver1 = new BluetoothStateChangeReceiver(done);
BluetoothDiscoveryReceiver receiver2 = new BluetoothDiscoveryReceiver(done);
activity.registerReceiver(receiver1, filter1);
activity.registerReceiver(receiver2, filter2);

Log.e(TAG,"start enable\n");

if(!adapter.isEnabled())
{
        //2.打开设备
        if(adapter.enable())
        {
                //等待设备打开完毕(在   BluetoothStateChangeReceiver中完成)
                        done.block();
                        done.close();
       }
        SystemClock.sleep(delayTime);
}                       

Display.appendInfo("start discovery...\n");

//3.搜索蓝牙设备
                adapter.startDiscovery();       
                //等待扫描到指定设备并配对(4.进行配对)
                done.block();
                done.close();
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }

广播:
class BluetoothStateChangeReceiver extends BroadcastReceiver{
        private ConditionVariable done;
        BluetoothStateChangeReceiver(ConditionVariable done)
        {
            this.done = done;
        }
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            Log.e("state onReceive","Action = "+arg1.getAction());
            if(arg1.getAction() == BluetoothAdapter.ACTION_STATE_CHANGED)
            {
                int state = arg1.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                //蓝牙打开完成或关闭完成
                Log.e(TAG,"state : "+state);
                if(state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_OFF)
                {
                    Log.e(TAG,"open success\n");
                    done.open();
                }
            }
        }
    }
    class BluetoothDiscoveryReceiver extends BroadcastReceiver{
        private ConditionVariable done;
    BluetoothDiscoveryReceiver(ConditionVariable done)
        {
            this.done = done;
        }
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            Log.e("Discovery onReceive","Action = "+arg1.getAction());
            if(arg1.getAction() == BluetoothDevice.ACTION_FOUND)//搜索到蓝牙设备
            {
                try{
                    String name = arg1.getStringExtra(BluetoothDevice.EXTRA_NAME);
                    BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

//搜索到指定MAC地址设备
                    if(boundDeviceAddress.equalsIgnoreCase(device.getAddress()))
{
                        Log.e(TAG,"address match\n");
                        c2 = Calendar.getInstance();
                        if(adapter.isDiscovering())
                            adapter.cancelDiscovery();
                        int connectState = device.getBondState();
                        Log.e(TAG,"connectState : "+connectState);
                        switch (connectState) {  
                            case BluetoothDevice.BOND_NONE:
                                Display.appendInfo("create bond...\n");
                                //发送配对请求
                                Method createBondMethod = BluetoothDevice.class.getMethod("createBond");  
                                createBondMethod.invoke(device);   
                                break;  
                            case BluetoothDevice.BOND_BONDED:  
                                Display.appendInfo("BOND_BONDED\n");
                                remoteDevice = device;
                                done.open();
                                break;
                        } 
                    }
                }catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            else if(arg1.getAction() == BluetoothDevice.ACTION_BOND_STATE_CHANGED)//配对状态改变
            {
                BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
                if (boundDeviceAddress.equalsIgnoreCase(device.getAddress())) {   
                    int connectState = device.getBondState();  
                    switch (connectState) {  
                        case BluetoothDevice.BOND_NONE:  
                            break;  
                        case BluetoothDevice.BOND_BONDING:  
                            break;                          
                        //配对完成
                        case BluetoothDevice.BOND_BONDED:  
                            c3 = Calendar.getInstance();
                            Display.appendInfo("BOND_BONDED\n");
                            remoteDevice = device;
                            done.open();
                            break;
                    }  
                }  
            }
            else if(arg1.getAction() == ACTION_PAIRING_REQUEST)//收到配对请求
            {
                BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
                            if(boundDeviceAddress.equalsIgnoreCase(device.getAddress()))
                {
                    int mType = arg1.getIntExtra(EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
                    switch (mType) {
                    case PAIRING_VARIANT_PASSKEY_CONFIRMATION:
                        int passkey =
                                arg1.getIntExtra(EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
                        //确认进行配对
                        try{
                            Method setPairingConfirmationMethod = BluetoothDevice.class.getMethod("setPairingConfirmation",new Class[]{boolean.class});
                            setPairingConfirmationMethod.invoke(device,true);
                        }catch(Exception e)
                        {
                            e.printStackTrace();
                        }
                        break;
                    case PAIRING_VARIANT_PIN:
                        break;
                    default:
                        break;
                    }
                } 
            }
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值