2015-03-03 104 views
1

我知道有很多這樣的問題,但沒有一個能夠解決我的問題。我試圖在兩個設備之間以編程方式實現藍牙配對。我能夠配對設備,但問題是,它需要用戶按下我不想在我的應用程序中的按鈕。我希望他們在沒有任何用戶交互的情況下進行連接,或者如果這不可行,它應該只請求一個設備按下配對按鈕並且應該配對。我有一臺三星電視,其工作原理是當我嘗試將我的Android手機的藍牙與它連接時。在我的手機上彈出一個雙引腳請求,當我配對時它會配對,但不會在電視上顯示任何內容而且他們倆都是配對的。我需要在我的應用程序中實現這一點。我已經試過這段代碼,但它沒有取得密碼,並且連接它仍然要求輸入密碼。如何在沒有用戶交互的情況下以編程方式配對藍牙設備?

public void setBluetoothPairingPin(BluetoothDevice device) 
{ 
    byte[] pinBytes = convertPinToBytes("0000"); 
    try { 
      Log.d(TAG, "Try to set the PIN"); 
      Method m = device.getClass().getMethod("setPin", byte[].class); 
      m.invoke(device, pinBytes); 
      Log.d(TAG, "Success to add the PIN."); 
      try { 
       device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); 
       Log.d(TAG, "Success to setPairingConfirmation."); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       Log.e(TAG, e.getMessage()); 
       e.printStackTrace(); 
      } 
     } catch (Exception e) { 
      Log.e(TAG, e.getMessage()); 
      e.printStackTrace(); 
     } 
} 

我一直在尋找了近一個星期now.Any想法的解決方案/建議,以實現上述將非常感激。

回答

5

你應該試試這個......!

private void pairDevice(BluetoothDevice device) { 
      try { 
       Method method = device.getClass().getMethod("createBond", (Class[]) null); 
       method.invoke(device, (Object[]) null); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

mAdapter.setData(mDeviceList); 
     mAdapter.setListener(new DeviceListAdapter.OnPairButtonClickListener() {    
      @Override 
      public void onPairButtonClick(int position) {    
       BluetoothDevice device = mDeviceList.get(position); 

       if (device.getBondState() == BluetoothDevice.BOND_BONDED) { 
        unpairDevice(device); 
       } else { 
        showToast("Pairing..."); 
        pairDevice(device);//here is calling 
       } 
      } 
     }); 
+1

是兄弟,我已經嘗試過這...幫助不列入它 – 2015-03-03 10:50:24

0
`You should try this code on while pair button clicked, 

String strPsw = "FBK645NKL" 
mAdapter.setData(mDeviceList); 
mAdapter.setListener(new OnPairButtonClickListener() { 

      @Override 
      public void onPairButtonClick(int position) { 
       BluetoothDevice device = mDeviceList.get(position); 

       if (device.getBondState() == BluetoothDevice.BOND_BONDED) { 
        unpairDevice(device); 
       } else { 
        Log.d("mylog", "HAS BOND_BONDED"); 
        try { 
         ClsUtils.createBond(device.getClass(), device); 
         ClsUtils.setPin(device.getClass(), device, strPsw); // 
         ClsUtils.createBond(device.getClass(), device); 
//      remoteDevice = device; // ?remoteDevice 
         result = true; 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         Log.d("mylog", "setPiN failed!"); 
         e.printStackTrace(); 
        } 
       } 
       return; 
      } 

     }); 

and You must write a code for ClsUtils.java 

//ClsUtils code for Bluetooth Device Pairing and Bonding... 

import java.lang.reflect.Field; 
import java.lang.reflect.Method; 

import android.bluetooth.BluetoothDevice; 
import android.util.Log; 

public class ClsUtils { 

    /** 
    * ?platform/packages/apps/Settings.git 
    * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java 
    */ 
    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    static public boolean createBond(Class btClass, BluetoothDevice btDevice) 
      throws Exception { 
     Method createBondMethod = btClass.getMethod("createBond"); 
     Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice); 
     return returnValue.booleanValue(); 
    } 

    /** 
    * ?platform/packages/apps/Settings.git 
    * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java 
    */ 
    @SuppressWarnings("unchecked") 
    static public boolean removeBond(Class btClass, BluetoothDevice btDevice) 
      throws Exception { 
     Method removeBondMethod = btClass.getMethod("removeBond"); 
     Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice); 
     return returnValue.booleanValue(); 
    } 

    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    static public boolean setPin(Class btClass, BluetoothDevice btDevice, 
      String str) throws Exception { 
     try { 
      Method removeBondMethod = btClass.getDeclaredMethod("setPin", 
        new Class[] { byte[].class }); 
      Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, 
        new Object[] { str.getBytes() }); 
      Log.e("returnValue", "" + returnValue); 
     } catch (SecurityException e) { 
      // throw new RuntimeException(e.getMessage()); 
      e.printStackTrace(); 
     } catch (IllegalArgumentException e) { 
      // throw new RuntimeException(e.getMessage()); 
      e.printStackTrace(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return true; 

    } 

    // 
    @SuppressWarnings("unchecked") 
    static public boolean cancelPairingUserInput(Class btClass, 
      BluetoothDevice device) 

    throws Exception { 
     Method createBondMethod = btClass.getMethod("cancelPairingUserInput"); 
     // cancelBondProcess() 
     Boolean returnValue = (Boolean) createBondMethod.invoke(device); 
     return returnValue.booleanValue(); 
    } 

    // 
    @SuppressWarnings("unchecked") 
    static public boolean cancelBondProcess(Class btClass, 
      BluetoothDevice device) 

    throws Exception { 
     Method createBondMethod = btClass.getMethod("cancelBondProcess"); 
     Boolean returnValue = (Boolean) createBondMethod.invoke(device); 
     return returnValue.booleanValue(); 
    } 

    /** 
    * 
    * @param clsShow 
    */ 
    static public void printAllInform(Class clsShow) { 
     try { 
      // 
      Method[] hideMethod = clsShow.getMethods(); 
      int i = 0; 
      for (; i < hideMethod.length; i++) { 
       Log.e("method name", hideMethod[i].getName() + ";and the i is:" 
         + i); 
      } 
      // 
      Field[] allFields = clsShow.getFields(); 
      for (i = 0; i < allFields.length; i++) { 
       Log.e("Field name", allFields[i].getName()); 
      } 
     } catch (SecurityException e) { 
      // throw new RuntimeException(e.getMessage()); 
      e.printStackTrace(); 
     } catch (IllegalArgumentException e) { 
      // throw new RuntimeException(e.getMessage()); 
      e.printStackTrace(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 


     @SuppressWarnings("unchecked") 
     static public boolean autoBond(Class btClass,BluetoothDevice device,String strPin) throws Exception { 

     Method autoBondMethod = btClass.getMethod("setPin", 
       new Class[] { byte[].class }); 
     Boolean result = (Boolean) autoBondMethod.invoke(device, 
       new Object[] { strPin.getBytes() }); 
     return result; 

    } 

} 


This code is useful for Automatically pair and connect Bluetooth Devices without user-interaction. 

Please be share improving Answer... ` 
+0

工作而無需用戶交互。但是彈出窗口仍然出現一秒鐘。有什麼辦法可以避免彈出窗口的可見性? – 2017-02-16 05:50:41

相關問題