2013-07-20 10 views
11

कनेक्ट नहीं कर सकता है मुझे थोड़ी देर के लिए यह समस्या हो रही है और इसे समझने में सक्षम नहीं है।एंड्रॉइड ब्लूटूथ

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

मैं अपने पते के साथ उपकरणों की सूची कोई समस्या नहीं प्राप्त कर सकता हूं। समस्या यह है कि एक बार जब मैं कनेक्ट करने का प्रयास करता हूं तो मुझे सॉकेट.कनेक्ट() पर IOException मिलता है;

त्रुटि संदेश इस प्रकार है: गया "की विफल रही कनेक्ट, सॉकेट बंद या टाइमआउट सकता है, सेवानिवृत्त पढ़ें: -1"

यहाँ मेरी कोड है। कोई भी सुझावों की सराहना की जाएगी। मैं इस पर बहुत अटक गया हूँ।

fyi: "onEvent" विधियां एक लाइब्रेरी है जो कॉलबैक को सरल बनाती है ... वह हिस्सा काम करता है। जब उपयोगकर्ता क्लिक करता है एक सूची वस्तुओं पर इस विधि कहा जाता है "सार्वजनिक शून्य onEvent (EventMessage.DeviceSelected घटना)"

public class EcoDashActivity extends BaseActivity { 

public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 


private BluetoothAdapter mBluetoothAdapter; 
private int REQUEST_ENABLE_BT = 100; 
private ArrayList<BluetoothDevice> mDevicesList; 
private BluetoothDeviceDialog mDialog; 
private ProgressDialog progressBar; 
private int progressBarStatus = 0; 
private Handler progressBarHandler = new Handler(); 


@Override 
public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.main); 

    mDevicesList = new ArrayList<BluetoothDevice>(); 

    // Register the BroadcastReceiver 
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    registerReceiver(mReceiver, filter); 

    setupBluetooth(); 
} 

private void setupBluetooth() { 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (mBluetoothAdapter == null) { 
     // Device does not support Bluetooth 
     Toast.makeText(this, "Device does not support Bluetooth", Toast.LENGTH_SHORT).show(); 
    } 

    if (!mBluetoothAdapter.isEnabled()) { 
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
    } else { 
     searchForPairedDevices(); 
     mDialog = new BluetoothDeviceDialog(this, mDevicesList); 
     mDialog.show(getFragmentManager(), ""); 
    } 

} 

private void searchForPairedDevices() { 

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
    // If there are paired devices 
    if (pairedDevices.size() > 0) { 
     // Loop through paired devices 
     for (BluetoothDevice device : pairedDevices) { 
      // Add the name and address to an array adapter to show in a ListView 
      mDevices.add(device.getName() + "\n" + device.getAddress()); 
      mDevicesList.add(device); 
     } 
    } 
} 


private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     // When discovery finds a device 
     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      // Get the BluetoothDevice object from the Intent 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      // Add the name and address to an array adapter to show in a ListView 
      mDevicesList.add(device); 
     } 
    } 
}; 


@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    unregisterReceiver(mReceiver); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (requestCode == REQUEST_ENABLE_BT) { 
     if (resultCode == RESULT_OK) { 
      Toast.makeText(this, "BT turned on!", Toast.LENGTH_SHORT).show(); 
      searchForPairedDevices(); 

      mDialog = new BluetoothDeviceDialog(this, mDevicesList); 
      mDialog.show(getFragmentManager(), ""); 
     } 
    } 

    super.onActivityResult(requestCode, resultCode, data); 
} 


public void onEvent(EventMessage.DeviceSelected event) { 

    mDialog.dismiss(); 

    BluetoothDevice device = event.getDevice(); 

    ConnectThread connectThread = new ConnectThread(device); 
    connectThread.start(); 
} 


public class ConnectThread extends Thread { 
    private final BluetoothSocket mmSocket; 
    private final BluetoothDevice mmDevice; 

    public ConnectThread(BluetoothDevice device) { 
     // Use a temporary object that is later assigned to mmSocket, 
     // because mmSocket is final 
     BluetoothSocket tmp = null; 
     mmDevice = device; 

     // Get a BluetoothSocket to connect with the given BluetoothDevice 
     try { 
      // MY_UUID is the app's UUID string, also used by the server code 
      tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { } 
     mmSocket = tmp; 
    } 

    public void run() { 
     setName("ConnectThread"); 
     // Cancel discovery because it will slow down the connection 
     mBluetoothAdapter.cancelDiscovery(); 

     try { 
      // Connect the device through the socket. This will block 
      // until it succeeds or throws an exception 
      Log.d("kent", "trying to connect to device"); 
      mmSocket.connect(); 
      Log.d("kent", "Connected!"); 
     } catch (IOException connectException) { 
      // Unable to connect; close the socket and get out 
      try { 
       Log.d("kent", "failed to connect"); 

       mmSocket.close(); 
      } catch (IOException closeException) { } 
      return; 
     } 

     Log.d("kent", "Connected!"); 
    } 

    /** Will cancel an in-progress connection, and close the socket */ 
    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { } 
    } 
} 

यहाँ मेरी logcat है। काफी कम।

07-22 10:37:05.129: DEBUG/kent(17512): trying to connect to device 
07-22 10:37:05.129: WARN/BluetoothAdapter(17512): getBluetoothService() called with no BluetoothManagerCallback 
07-22 10:37:05.129: DEBUG/BluetoothSocket(17512): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[98]} 
07-22 10:37:40.757: DEBUG/dalvikvm(17512): GC_CONCURRENT freed 6157K, 9% free 62793K/68972K, paused 7ms+7ms, total 72ms 
07-22 10:38:06.975: DEBUG/kent(17512): failed to connect 
07-22 10:38:06.975: DEBUG/kent(17512): read failed, socket might closed or timeout, read ret: -1 

अंतिम पंक्ति आज़माएं/कैच के "कैच" खंड में है यही कारण है कि ... मैं सिर्फ त्रुटि संदेश लॉगिन कर रहा हूं।

कृपया ध्यान दें, वहाँ "डिवाइस से कनेक्ट करने की कोशिश कर रहा है" और

+0

एंड्रॉइड का कौन सा संस्करण? जेली बीन के साथ एक पूरी तरह से अलग ब्लूटूथ स्टैक, अनपेयर और पहले युग्मित करने का प्रयास करें और फिर – Slartibartfast

+0

पोस्ट लॉगकैट – Slartibartfast

+0

@Slartibartfast मैं 4.2.2 नेक्सस 4 पर काम कर रहा हूं। मैं लॉगकैट के साथ प्रश्न अपडेट करूंगा। –

उत्तर

14

जेली बीन ब्लूटूथ ढेर अन्य संस्करणों से स्पष्ट रूप से अलग है "कनेक्ट नहीं हो पाई" के बीच के बारे में 20 सेकंड के अंतराल है।

यह मदद कर सकता है: http://wiresareobsolete.com/wordpress/2010/11/android-bluetooth-rfcomm/

सार में: UUID एक मूल्य है कि आपके एम्बेडेड डिवाइस पर एक प्रकाशित सेवा को इंगित करना चाहिए है, यह सिर्फ अनियमित रूप से उत्पन्न नहीं है। आरएफसीओएमएम एसपीपी कनेक्शन जिसे आप एक्सेस करना चाहते हैं, में एक विशिष्ट यूयूआईडी है जो यह उस सेवा की पहचान करने के लिए प्रकाशित होती है, और जब आप सॉकेट बनाते हैं तो उसे उसी यूयूआईडी से मेल खाना चाहिए।

यदि आप 4.0.3 डिवाइस और ऊपर लक्षित कर रहे हैं, तो सभी प्रकाशित सेवाओं और उनके संबंधित यूयूआईडी मानों को ढूंढने के लिए fetchUuidsWithSdp() और getUuids() का उपयोग करें। पिछड़े संगतता के लिए लेख

+0

GetUUIDs() आईडी की एक सूची देता है। आप कैसे जानते हैं कि किस का उपयोग किया जाना चाहिए? –

+0

मैंने बस हर यूयूआईडी की कोशिश की कि यह लौट आया और एक ही समस्या थी। –

+0

क्या आप एक पूर्ण कोड उदाहरण के बारे में जानते हैं कि मैं इसे जांचने के लिए एक नए ऐप में कॉपी/पेस्ट कर सकता हूं? –

0

सॉकेट को दूसरी बार को जोड़ने के बाद मुझे एक ही त्रुटि संदेश मिला। मैंने बस जांच की है कि सॉकेट पहले से कनेक्ट है या नहीं।

if(!mmSocket.isConnected()) 
      mmSocket.connect(); 

मैं एंड्रॉइड 4.4.2 (मोटो जी) पर परीक्षण कर रहा था।

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