private void pairDevice(BluetoothDevice device) {

        try {

            Method method = device.getClass().getMethod("createBond", (Class[]) null);

            method.invoke(device, (Object[]) null);

        } catch (Exception e) {

            e.printStackTrace();

        }

}

 

private void unpairDevice(BluetoothDevice device) {

        try {

            Method method = device.getClass().getMethod("removeBond", (Class[]) null);

            method.invoke(device, (Object[]) null);

        } catch (Exception e) {

            e.printStackTrace();

        }

}

 

private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();

            if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {

                 final int state        = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);

                 final int prevState    = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);

 

                 if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {

                     showToast("Paired");

                 } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){

                     showToast("Unpaired");

                 }

            }

        }

    };

 

 

IntentFilter intent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);

registerReceiver(mPairReceiver, intent);

 

 

 

 

arrow
arrow
    全站熱搜

    小彬彬 發表在 痞客邦 留言(0) 人氣()