2013-09-27 5 views
17

दर्ज किए बिना ब्लूटूथ डिवाइस को जोड़ता है ब्लूटूथ डिवाइस जो मैं कनेक्ट करने का प्रयास कर रहा हूं वह हमेशा एक ही पिनकोड होता है। यह पिन प्रोग्रामेटिक रूप से पिन सेट करके डिवाइस को जोड़ना संभव बनाना चाहिए।प्रोग्रामिंग रूप से उपयोगकर्ता को पिन

खोज करने के लिए यह कैसे किया जा सकता है की कोशिश कर के बाद, मैं नीचे दिए गए कोड के साथ समाप्त हो गया:

BluetoothDevice device = getDevice(); 

//To avoid the popup notification: 
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); 
device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device, true); 
byte[] pin = ByteBuffer.allocate(4).putInt(1234).array(); 
//int pinn = 1234; 

//Entering pin programmatically: 
Method ms = device.getClass().getMethod("setPin", byte[].class); 
//Method ms = device.getClass().getMethod("setPasskey", int.class); 
ms.invoke(device, pin); 

//Bonding the device: 
Method mm = device.getClass().getMethod("createBond", (Class[]) null); 
mm.invoke(device, (Object[]) null); 

cancelPairingUserInput मुझे एक NoSuchMethodException है, जो अजीब है क्योंकि विधि BluetoothDevice कक्षा में मौजूद है देता है।

Setpin या SetPasskey जैसा कुछ भी नहीं दिखता है। डिवाइस सिर्फ जोड़ी नहीं है। मैन्युअल रूप से पिन दर्ज करने के बाद यह केवल जोड़े।

तो कोड है कि काम करता है की केवल लाइन है:

//Bonding the device: 
Method mm = device.getClass().getMethod("createBond", (Class[]) null); 
mm.invoke(device, (Object[]) null); 

Logcat उत्पादन: गलत

09-27 12:34:46.408: ERROR/App(11671): cancelPairingUserInput [boolean] 
     java.lang.NoSuchMethodException: cancelPairingUserInput [boolean] 
     at java.lang.Class.getConstructorOrMethod(Class.java:460) 
     at java.lang.Class.getMethod(Class.java:915) 
     at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.pair(BluetoothDiscoveryAndPairing.java:97) 
     at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.access$000(BluetoothDiscoveryAndPairing.java:25) 
     at test.app.bluetooth.model.BluetoothDiscoveryAndPairing$1.onReceive(BluetoothDiscoveryAndPairing.java:79) 
     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:756) 
     at android.os.Handler.handleCallback(Handler.java:615) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4921) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
     at dalvik.system.NativeStart.main(Native Method) 

तो मैं क्या कर रहा हूँ?

+0

@DuncanJones मैं अपने startpost को स्टैकट्रेस गयी। – user1816451

उत्तर

11

छुपा विधि रद्द करेंफेयरिंग उपयोगकर्ता इनपुट आपके डिवाइस में मौजूद नहीं है। इसका इस्तेमाल न करें।

  1. आप
  2. कॉल createBond()
  3. प्रतीक्षा android.bluetooth.device.action.PAIRING_REQUEST
  4. के लिए BroadcastReceiver रजिस्टर चाहिए BroadcastReceiver BroadcastReceiver में
  5. को गति प्रदान करने के लिए करता है, तो कार्रवाई android.bluetooth.device.action है .PAIRING_REQUEST कॉल इस विधि
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(); 
     } 
} 

यह एंड्रॉइड के जेली बीन संस्करण (4.1.2) के साथ एक डिवाइस पर भी काम करता है।

+2

क्या यह 1 9 से पहले एपीआई स्तर में किया जा सकता है? Android.bluetooth.device.action.PAIRING_REQUEST केवल एपीआई 1 9 – FOliveira

+0

पर पेश किया गया था क्यों सेटपेयरिंग कॉन्फ़िगरेशन() की आवश्यकता है? एंड्रॉइड दस्तावेज़ कहते हैं कि यह केवल PAIRING_VARIANT_PASSKEY_CONFIRMATION के लिए लागू होता है, विरासत युग्मन नहीं। इसके अलावा, एंड्रॉइड 6 पर इसे BLUETOOTH_PRIVILEGED –

2

यह है मेरे लिए काम करता है:

IntentFilter filter2 = new IntentFilter(
      "android.bluetooth.device.action.PAIRING_REQUEST"); 
    mActivity.registerReceiver(
      pairingRequest, filter2); 

private final BroadcastReceiver pairingRequest = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) { 
      mBluetoothDevice = needed; 
       try { 
        byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "1234"); 
        Method m = mBluetoothDevice.getClass().getMethod("setPin", byte[].class); 
        m.invoke(mBluetoothDevice, pin); 
        mBluetoothDevice.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(mBluetoothDevice, true); 
} 
    catch(Exception e) 
{ 

    e.printStackTrace(); 

} 
+0

की आवश्यकता है क्या यह कोड केवल उस डिवाइस द्वारा आवश्यक है जो युग्मित करने की कोशिश कर रहा है? क्या हमें अन्य डिवाइस पर किसी भी कोड की आवश्यकता नहीं है जिसे जोड़ा जा रहा है? –

+0

आपको किसी अन्य डिवाइस पर किसी भी कोड की आवश्यकता नहीं है। आपको बस कनेक्शन स्वीकार करना चाहिए। (मैंने एंड्रॉइड फोन के लिए यह कोड लिखा है जो मोबाइल प्रिंटर से जुड़ा हुआ है।) –

संबंधित मुद्दे