2013-06-18 11 views
30

मैं एक ऐसा एप्लीकेशन विकसित कर रहा हूं जहां मैं ब्लूटूथ डिवाइस मुख्य समस्या को जोड़ना चाहता हूं, मैं नहीं चाहता कि उपयोगकर्ता आवश्यक पिन दर्ज करे, इसके बजाए एप्लिकेशन को स्वयं ही ऐसा करना चाहिए ... मैं नहीं करता कोई कनेक्शन संबंधित समस्या है ... केवल आवेदन द्वारा पिन प्रमाणीकरण प्रक्रिया को सम्मिलित करना और पूरा करना चाहते हैं।ब्लूटूथ डिवाइस प्रोग्रामेटिक रूप से एंड्रॉइड को कैसे जोड़ा जाए

मुझे निम्नलिखित कोड मिला मुझे यकीन है कि यह काम कर रहा है लेकिन इस कोड में पिन जोड़ने के बारे में सुनिश्चित नहीं है ??

private void pairDevice(BluetoothDevice device) { 
     try { 
      Log.d("pairDevice()", "Start Pairing..."); 
      Method m = device.getClass().getMethod("createBond", (Class[]) null); 
      m.invoke(device, (Object[]) null); 
      Log.d("pairDevice()", "Pairing finished."); 
     } catch (Exception e) { 
      Log.e("pairDevice()", e.getMessage()); 
     } 
    } 

किसी को कैसे ऊपर कोड या समस्या को हल करने के लिए कोई समान कोड में पिन दर्ज करने के लिए पता है .. आप

+0

शायद यह आपकी मदद करेगा। http://stackoverflow.com/questions/5885438/bluetooth-pairing-without-user-confirmation चीयर्स, –

+0

@ManolescuSebastian - मैं सुरक्षित कनेक्शन बनाने के लिए ... चाहते –

+0

मेरा उत्तर की कोशिश करो। मुझे आशा है कि यह आपके लिए काम करेगा –

उत्तर

24

How can I avoid or dismiss Android's Bluetooth pairing notification when I am doing programmatic pairing?

यह आपको जवाब देने के लिए लगता है धन्यवाद, पिन दर्ज करने के साथ और सभी। इसमें जब भी आपको संदेश मिलता है तो .setPin() भेजना शामिल है।

public void pairDevice(BluetoothDevice device) 
{ 
    String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST"; 
    Intent intent = new Intent(ACTION_PAIRING_REQUEST); 
    String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE"; 
    intent.putExtra(EXTRA_DEVICE, device); 
    String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT"; 
    int PAIRING_VARIANT_PIN = 0; 
    intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(intent); 
} 

Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST); 
intent.putExtra(EXTRA_DEVICE, device); 
int PAIRING_VARIANT_PIN = 272; 
intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN); 
sendBroadcast(intent); 

Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); 
startActivityForResult(intent, REQUEST_PAIR_DEVICE); 

मुझे आशा है कि इस मदद करता है

संदर्भ::

3

इस कोड का प्रयास करें http://pastebin.com/N8dR4Aa1

+0

कृपया मेरी मदद करें .. ... मैं उस डिवाइस की सूची चाहता हूं जिसमें बीटी सक्षम है और विशेष ब्लूटूथ डिवाइस पर क्लिक करने के बाद, यह हमारे डिवाइस –

+2

के साथ जोड़ा गया है, मुझे पहली विधि मिलती है लेकिन आप सुझाव देते हैं कि हम कोड के दो अन्य ब्लॉक के साथ क्या करते हैं? – gregm

1

इस प्रयास करें,

BluetoothDevice device = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE"); 
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); 
device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device); 
8

तो, मैं इस सवाल था, किसी की जरूरत है एंड्रॉइड 4.4.2 में काम करने का जवाब।

IntentFilter filter = new IntentFilter(
       "android.bluetooth.device.action.PAIRING_REQUEST"); 


     /* 
     * Registering a new BTBroadcast receiver from the Main Activity context 
     * with pairing request event 
     */ 
     registerReceiver(
       new PairingRequest(), filter); 

और रिसीवर के लिए कोड।

public static class PairingRequest extends BroadcastReceiver { 
     public PairingRequest() { 
      super(); 
     } 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) { 
       try { 
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
        int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0); 
        //the pin in case you need to accept for an specific pin 
        Log.d("PIN", " " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0)); 
        //maybe you look for a name or address 
        Log.d("Bonded", device.getName()); 
        byte[] pinBytes; 
        pinBytes = (""+pin).getBytes("UTF-8"); 
        device.setPin(pinBytes); 
        //setPairing confirmation if neeeded 
        device.setPairingConfirmation(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

और मैनिफेस्ट फ़ाइल में।

<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

और प्रसारण रिसीवर।

<receiver android:name=".MainActivity$PairingRequest"> 
       <intent-filter> 
        <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> 
        <action android:name="android.bluetooth.device.action.PAIRING_CANCEL" /> 
       </intent-filter> 
</receiver> 
1
BluetoothSocket bluetoothSocket = null; 
    try { 
     bluetoothSocket = device.createRfcommSocketToServiceRecord(UUID.fromString(UUID_DIVING)); 
    } catch (IOException e) { 
     Log.i("Bluetooth", "IOException = " + e.getMessage()); 
     e.printStackTrace(); 
    } 

    try { 
     byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "0000"); 
     Method m = device.getClass().getMethod("setPin", byte[].class); 
     m.invoke(device, (Object) pin); 
     device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); 
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { 
     Log.i("Bluetooth", "IOException = " + e.getMessage()); 
     e.printStackTrace(); 
    } 

    try { 
     if (bluetoothSocket != null) { 
      bluetoothSocket.connect(); 
      Log.i("Bluetooth", "bluetoothSocket.connect() "); 
      InputStream inputStream = bluetoothSocket.getInputStream(); 
      OutputStream outputStream = bluetoothSocket.getOutputStream(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
0

कैसे स्थापित करने के लिए पिन कोड से ऊपर उत्तर दिया गया है (और है कि मुझे मदद की)। फिर भी, मैं नीचे अपना सरल कोड साझा करता हूं जो एंड्रॉइड 6:

BluetoothAdapter mBTA = BluetoothAdapter.getDefaultAdapter(); 
if (mBTA.isDiscovering()) mBTA.cancelDiscovery(); 
mBTA.startDiscovery(); 
... 

/** In a broadcast receiver: */ 

if (BluetoothDevice.ACTION_FOUND.equals(action)) { // One device found. 

    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
    Log.d(TAG, "Start Pairing... with: " + device.getName()); 
    device.createBond(); 
} 

// If you want to auto-input the pin#: 
else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){ 

        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
        device.setPin("1234".getBytes()); 
} 
संबंधित मुद्दे