2013-03-13 41 views
8

Tôi đang phát triển một ứng dụng cho Android. Ứng dụng này sẽ giao tiếp với thiết bị Bluetooth (BT) (gửi một số byte). Tôi gặp sự cố khi gỡ lỗi/chạy ứng dụng này trên thiết bị của mình (Samsung Galaxy mini). Khi tôi tạo một ổ cắm BT và ngừng gỡ lỗi, hãy đóng băng điện thoại và tôi phải khởi động lại bằng cách tháo pin ra. Trong trường hợp chạy ứng dụng này (từ Eclipse) mọi thứ đều ổn, nhưng khi tôi thử chạy lại nó, điện thoại bị đóng băng và ứng dụng chưa được cài đặt. Nếu tôi cố gắng cài đặt ứng dụng này không cần thiết trước khi chạy lần thứ hai, điện thoại sẽ đóng băng lại. Đây là một mã có vấn đề:Ổ cắm Bluetooth đóng băng điện thoại

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; 
} 

Đây là một hàm tạo chuỗi. Khi tôi nhận xét dòng

tmp = mmDevice.createRfcommSocketToServiceRecord(uuid); 

điện thoại không bị đóng băng nên vấn đề là tạo ổ cắm (không kết nối). Khởi động lại điện thoại sau mỗi lần gỡ lỗi hoặc chạy là khá khó chịu và tôi phải làm rất nhiều việc.

Nếu tôi chạy ứng dụng này từ điện thoại (ngắt kết nối với Eclipse), nó hoạt động mà không có bất kỳ vấn đề gì. Bất kỳ ý tưởng nào có thể là vấn đề hoặc cách khắc phục? Cảm ơn bạn.

+3

Âm thanh như một lỗi firmware, phải không? –

+0

@CodePainters: lỗi firmware hoặc IDE. Tôi đã tìm thấy một chủ đề tương tự: http://stackoverflow.com/questions/4408287/android-bluetooth-socket-freeze-application. Vì vậy, nếu tôi tắt BT trong onDestroy gọi lại, mọi thứ đều OK. – DanielH

+1

IDE? Không chắc. Và Android có đầy đủ các lỗi ... –

Trả lời

0

Tôi đang sử dụng mini SGSIII để phát triển. Các mã sau hoạt động tốt đối với tôi:

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

Tôi cũng phải đối mặt với cùng một vấn đề, bạn có thể sử dụng phương pháp Reflection nó sẽ làm việc

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
BluetoothSocket socket = socket = (BluetoothSocket) m.invoke(device, 1); 
Các vấn đề liên quan