2013-03-13 35 views
8

Android için bir uygulama geliştiriyorum. Bu uygulama bir Bluetooth (BT) cihazı ile iletişim kurmalı (bazı baytlar göndererek). Cihazımda bu uygulamayı hata ayıklama/çalıştırmayla ilgili bir sorunum var (Samsung Galaxy mini). Bir BT soketi oluşturduğumda ve hata ayıklamayı durdurduğunda, telefonu donduğumda ve pili çıkartarak yeniden başlatmam gerekiyor. Bu uygulamayı çalıştırırken (Eclipse'den) her şey yolundadır, ancak tekrar çalıştırmayı denediğimde, telefon dondurması ve uygulaması yüklü değildir. İkinci uygulamayı çalıştırmadan önce bu uygulamayı manuel olarak kaldırmaya çalışırsam, telefonu tekrar dondurun. İşte sorunlu bir kod:Bluetooth soketi donma telefonu

private final BluetoothDevice mmDevice; 
private UUID uuid; 

public ConnectionThread(BluetoothDevice device) { 
    Log.d(TAG, "create ConnectionThread"); 

    uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    BluetoothSocket tmp = null; 
    mmDevice = device; 

    try { 
     tmp = mmDevice.createRfcommSocketToServiceRecord(uuid); 
    } catch (IOException e) { } 
    mmSocket = tmp; 
    socketConnected = true; 
} 

Bu, bir iş parçacığı oluşturucusudur. Çizgiyi

tmp = mmDevice.createRfcommSocketToServiceRecord(uuid); 

telefon açıklama yaparken gelmiyor.Fark sorunu (bağlanmıyor) soket oluşturma ile yani dondurun. Her hata ayıklamasından veya çalıştırılmasından sonra telefonu yeniden başlatmak oldukça can sıkıcı ve henüz çok iş yapmam gerekiyor.

Bu uygulamayı bir telefondan (Eclipse ile bağlantısı kesilmiş) çalıştırırsam, sorunsuz çalışır. Herhangi bir fikir nerede sorun olabilir veya nasıl düzeltilebilir? Teşekkür ederim.

+3

yazılımındaki bir hatadan benziyor çalışacak, öyle değil mi? –

+0

@CodePainters: ürün yazılımı veya IDE hatası. Aynı konuyu buldum: http://stackoverflow.com/questions/4408287/android-bluetooth-socket-freeze-application. Yani, BT'yi onGeroy geri aramada kapatırsam, her şey yolunda. – DanielH

+1

IDE? Olası olmayan. Android zaten hatalarla dolu ... –

cevap

0

SGSIII mini'yi de geliştirme için kullanıyorum. Aşağıdaki kod benim için iyi çalışıyor:

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

    public ConnectThread(BluetoothDevice device) { 
     mmDevice = device; 

     BluetoothSocket tmp = null; 

     // Get a BluetoothSocket for a connection with the 
     // given BluetoothDevice 
     try { 
      //tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
      tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "create() failed", e); 
     } 
     mmSocket = tmp; 

     Main.myBluetoothSocket = mmSocket; 
     Main.myBluetoothDevice = mmDevice; 
    } 

    @Override 
    public void run() { 
     Log.i(LOG_TAG, "BEGIN mConnectThread"); 
     setName("ConnectThread"); 

     // Always cancel discovery because it will slow down a connection 
     mAdapter.cancelDiscovery(); 

     // Send a failure message back to the Activity 
     Message msg = mHandler.obtainMessage(MESSAGE_TOAST); 
     Log.e(LOG_TAG, "Attempting connection to " + mmSocket.getRemoteDevice().getName()); 
     String ss = "Attempting connection to " + mmSocket.getRemoteDevice().getName(); 
     Bundle bundle = new Bundle(); 
     bundle.putString(TOAST, ss); 
     msg.setData(bundle); 
     mHandler.sendMessage(msg); 

     // Make a connection to the BluetoothSocket 
     try { 
      // This is a blocking call and will only return on a 
      // successful connection or an exception 
      mmSocket.connect(); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "*+*+*+* Connection Failed"); 
      connectionFailed(); 
      // Close the socket 
      try { 
       mmSocket.close(); 
      } catch (IOException e2) { 
       Log.e(LOG_TAG, "unable to close() socket during connection failure", e2); 
      } 
      // Start the service over to restart listening mode 
      BluetoothCommandService.this.start(); 
      return; 
     } 

     // Reset the ConnectThread because we're done 
     synchronized (BluetoothCommandService.this) { 
      mConnectThread = null; 
     } 

     // Start the connected thread 
     connected(mmSocket, mmDevice); 
    } 

    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { 
      Log.e(LOG_TAG, "close() of connect socket failed", e); 
     } 
    } 
} 
0

Ben de Yansıma yöntemi kullanabilirsiniz aynı sorunla karşı karşıya am o

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
BluetoothSocket socket = socket = (BluetoothSocket) m.invoke(device, 1);