मैंने टैबलेट के साथ काम नहीं किया है, लेकिन मैंने एक ऐप लिखा है जो एंड्रॉइड फोन के लिए एसपीपी का इस्तेमाल करता है। मुझे जो मिला वह यह था कि ब्लूटूथ स्थिर होने के लिए, मुझे उस डिवाइस के साथ मैन्युअल रूप से बंधन करना है जिसके साथ मैं संवाद करना चाहता हूं। हमने ऐप के भीतर से शुरू होने वाले बॉन्डिंग के लिए नीचे दिए गए कोड का उपयोग किया था, और इसे बॉन्डिंग को सुरक्षित रखना चाहिए जैसे कि आपने सेटिंग मेनू के माध्यम से मैन्युअल रूप से जोड़ा है। 1) एक BroadcastReceiver रजिस्टर आप एक BluetoothDevice वस्तु होना चाहिए BluetoothDevice.ACTION_BOND_STATE_CHANGED
2) के बाद डिवाइस खोज के लिए सुनने के लिए:
यहाँ सामान्य प्रवाह है।
3) का प्रयोग करें एक BluetoothDeviceObject
3a पर 'createBond' विधि कॉल करने के) खोलने सॉकेट
BluetoothDevice device = {obtained from device discovery};
Method m = device.getClass().getMethod("createBond", (Class[])null);
m.invoke(device, (Object[])null);
int bondState = device.getBondState();
if (bondState == BluetoothDevice.BOND_NONE || bondState == BluetoothDevice.BOND_BONDING)
{
waitingForBonding = true; // Class variable used later in the broadcast receiver
// Also...I have the whole bluetooth session running on a thread. This was a key point for me. If the bond state is not BOND_BONDED, I wait here. Then see the snippets below
synchronized(this)
{
wait();
}
}
4) से पहले बांड स्थिति परिवर्तन की घटनाओं के लिए प्रतीक्षा करें रुको बंधन राज्य के लिए BOND_BONDED
को BOND_BONDING से बदलने के लिए प्रतिबिंब
एक BroadcastReciever के अंदर:
public void onReceive(Context context, Intent intent)
{
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction()))
{
int prevBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
if (waitingForBonding)
{
if (prevBondState == BluetoothDevice.BOND_BONDING)
{
// check for both BONDED and NONE here because in some error cases the bonding fails and we need to fail gracefully.
if (bondState == BluetoothDevice.BOND_BONDED || bondState == BluetoothDevice.BOND_NONE)
{
// safely notify your thread to continue
}
}
}
}
}
5) ओपन सॉकेट और
संवाद
आप जोड़ीकरण सूची से अपने डिवाइस को हटाने के लिए प्रतिबिंब के माध्यम से 'removeBond' विधि भी कर सकते हैं।
आशा है कि इससे मदद मिलती है!
मैं इस अस्थायी जोड़ी व्यवहार के लिए विवरण कहां देखूँगा; एंड्रॉइड प्रलेखन या ब्लूटूथ 2।1 विनिर्देश दस्तावेज? यदि यह एंड्रॉइड मुद्दा है, तो यह सभी फोन पर पुन: उत्पन्न होगा। या यह ब्लूटूथ स्टैक पर निर्माता कार्यान्वयन से संबंधित है? – Ashwini
यह निर्माता निर्भर करता है कि यह किस विकल्प को जोड़ी/बॉन्ड बनाम अस्थायी कनेक्शन के लिए प्रमाणित करता है। युग्मन के लिए आमतौर पर स्पष्ट कार्रवाई (जो कि युग्मित डिवाइस को सहेजने का तात्पर्य है सिस्टम ब्लूटूथ - सेटिंग्स मेनू के माध्यम से है) –