How to prevent Android bluetooth RFCOMM connection from dying immediately after .connect()?

Posted by Gilead on Stack Overflow See other posts from Stack Overflow or by Gilead
Published on 2010-04-18T03:09:23Z Indexed on 2010/04/18 3:13 UTC
Read the original article Hit count: 1499

Filed under:
|
|

I'm trying to connect to a Zeemote (http://zeemote.com/) gaming controller from Moto Droid running 2.0.1 firmware. The test application below does connect to the device (LED flashes) but connection is dropped immediately after that. I can connect to the device perfectly fine using bluez tools (log attached as well).

I'm quite at a loss here, I work on it for so long that I ran out of ideas so any help would be very much appreciated.

Thanks, Max

=========================================== Code:

public class ZeeTest extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
      test();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public void test() throws IOException {
    BluetoothDevice zee = BluetoothAdapter.getDefaultAdapter().
        getRemoteDevice("00:1C:4D:02:A6:55");
    Log.d("ZeeTest", "++++ Creating socket");
    BluetoothSocket sock = zee.createRfcommSocketToServiceRecord(
        UUID.fromString("8e1f0cf7-508f-4875-b62c-fbb67fd34812"));
    Log.d("ZeeTest", "++++ Connecting");
    sock.connect();
    Log.d("ZeeTest", "++++ Connected");

    final InputStream in = sock.getInputStream();
    new Thread() {
        @Override
        public void run() {
          byte[] buffer = new byte[32];
          int bytes = 0;
          int x = 0;
          Log.d("ZeeTest", "++++ Listening...");
          while (x < 200) {
            x++;
            try {
              bytes = in.read(buffer);
              Log.d("ZeeTest", "++++ Read "+ bytes +" bytes");
            } catch (IOException e) {
              // java.io.IOException: Software caused connection abort
              if (x % 50 == 0) {
                Log.d("ZeeTest", "Tried "+ x +" times ("+ bytes +")");
              }
              try { Thread.sleep(100); } catch (InterruptedException ie) {}
            }
          }
          Log.d("ZeeTest", "++++ Done: thread exit");
        }
      }.start();
    Log.d("ZeeTest", "++++ Done: test()");
  }
}

=========================================== Log:

I/ActivityManager( 1169): Start proc zee.test for activity zee.test/.ZeeTest: pid=4294 uid=10084 gids={3002, 3001, 3003}
I/dalvikvm( 4294): Debugger thread not active, ignoring DDM send (t=0x41504e4d l=38)
D/dalvikvm( 4287): LinearAlloc 0x0 used 640700 of 5242880 (12%)
I/dalvikvm( 4294): Debugger thread not active, ignoring DDM send (t=0x41504e4d l=20)
D/ZeeTest ( 4294): ++++ Creating socket
D/ZeeTest ( 4294): ++++ Connecting
E/BluetoothEventLoop.cpp( 1169): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/1240/hci0/dev_00_1C_4D_02_A6_55
I/usbd    ( 1068): process_usb_uevent_message(): buffer = add@/devices/virtual/bluetooth/hci0/hci0:1
I/usbd    ( 1068): main(): call select(...)
E/BluetoothEventLoop.cpp( 1169): event_filter: Received signal org.bluez.Adapter:DeviceFound from /org/bluez/1240/hci0
V/BluetoothEventRedirector( 1242): Received android.bluetooth.device.action.FOUND
V/BluetoothEventRedirector( 1242): Received android.bleutooth.device.action.UUID
D/ZeeTest ( 4294): ++++ Connected
D/ZeeTest ( 4294): ++++ Done: test()
D/ZeeTest ( 4294): ++++ Listening...
I/ActivityManager( 1169): Displayed activity zee.test/.ZeeTest: 2296 ms (total 2296 ms)
E/BluetoothEventLoop.cpp( 1169): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/1240/hci0/dev_00_1C_4D_02_A6_55
I/usbd    ( 1068): process_usb_uevent_message(): buffer = remove@/devices/virtual/bluetooth/hci0/hci0:1
I/usbd    ( 1068): main(): call select(...)
V/BluetoothEventRedirector( 1242): Received android.bleutooth.device.action.UUID
D/ZeeTest ( 4294): Tried 50 times (0)
D/ZeeTest ( 4294): Tried 100 times (0)
D/ZeeTest ( 4294): Tried 150 times (0)
D/ZeeTest ( 4294): Tried 200 times (0)
D/ZeeTest ( 4294): ++++ Done: thread exit

=========================================== Terminal log:

$ sdptool browse
Inquiring ...
Browsing 00:1C:4D:02:A6:55 ...

$ sdptool records 00:1C:4D:02:A6:55
Service Name: Zeemote
Service RecHandle: 0x10015
Service Class ID List:
  UUID 128: 8e1f0cf7-508f-4875-b62c-fbb67fd34812
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 1
Language Base Attr List:
  code_ISO639: 0x656e
  encoding:    0x6a
  base_offset: 0x100

$ rfcomm connect /dev/tty10 00:1C:4D:02:A6:55
Connected /dev/rfcomm0 to 00:1C:4D:02:A6:55 on channel 1
Press CTRL-C for hangup

# rfcomm show /dev/tty10
rfcomm0: 00:1F:3A:E4:C8:40 -> 00:1C:4D:02:A6:55 channel 1 connected [reuse-dlc release-on-hup tty-attached]

# cat /dev/tty10
(nothing here)

# hcidump
HCI sniffer - Bluetooth packet analyzer ver 1.42
device: hci0 snap_len: 1028 filter: 0xffffffff
< HCI Command: Create Connection (0x01|0x0005) plen 13
> HCI Event: Command Status (0x0f) plen 4
> HCI Event: Connect Complete (0x03) plen 11
< HCI Command: Read Remote Supported Features (0x01|0x001b) plen 2
> HCI Event: Read Remote Supported Features (0x0b) plen 11
< ACL data: handle 11 flags 0x02 dlen 10
    L2CAP(s): Info req: type 2
> HCI Event: Command Status (0x0f) plen 4
> HCI Event: Page Scan Repetition Mode Change (0x20) plen 7
> HCI Event: Max Slots Change (0x1b) plen 3
< HCI Command: Remote Name Request (0x01|0x0019) plen 10
> HCI Event: Command Status (0x0f) plen 4
> ACL data: handle 11 flags 0x02 dlen 16
    L2CAP(s): Info rsp: type 2 result 0
      Extended feature mask 0x0000
< ACL data: handle 11 flags 0x02 dlen 12
    L2CAP(s): Connect req: psm 3 scid 0x0040
> HCI Event: Number of Completed Packets (0x13) plen 5
> ACL data: handle 11 flags 0x02 dlen 16
    L2CAP(s): Connect rsp: dcid 0x04fb scid 0x0040 result 1 status 2
      Connection pending - Authorization pending
> HCI Event: Remote Name Req Complete (0x07) plen 255
> ACL data: handle 11 flags 0x02 dlen 16
    L2CAP(s): Connect rsp: dcid 0x04fb scid 0x0040 result 0 status 0
      Connection successful
< ACL data: handle 11 flags 0x02 dlen 16
    L2CAP(s): Config req: dcid 0x04fb flags 0x00 clen 4
      MTU 1013
(events are properly received using bluez)

© Stack Overflow or respective owner

Related posts about android

Related posts about bluetooth