Android的蓝牙RFCOMM连接

Android的蓝牙RFCOMM连接

本文介绍了如何$死于.connect后立即()p $ pvent Android的蓝牙RFCOMM连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经解决了!非常感谢布拉德,丹尼斯和迷!你是英雄! :)

这是工作code。它连接到的Zeemote并从中读取数据。

===== code =====

公共类ZeeTest延伸活动{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        尝试 {
            的for(int i = 0;我3;;我++){
                测试();
            }
        }赶上(例外五){
            e.printStackTrace();
        }
    }

    私人布尔连接= FALSE;
    私有的BluetoothSocket袜子;
    私人的InputStream的;
    公共无效测试()抛出异常{
        如果(已连接){
            返回;
        }
        BluetoothDevice类ZEE = BluetoothAdapter.getDefaultAdapter()。
            getRemoteDevice(00:1C:4D:02:A6:55);
        方法M = zee.getClass()。GetMethod的(createRfcommSocket
            新等级[] {int.class});
        袜子=(的BluetoothSocket)m.invoke(ZEE,Integer.valueOf(1));
        Log.d(ZeeTest,++++连接);
        sock.connect();
        Log.d(ZeeTest,++++连接);
        在= sock.getInputStream();
        byte []的缓冲区=新的字节[50];
        INT读= 0;
        Log.d(ZeeTest,++++听......);
        尝试 {
            而(真){
                读= in.read(缓冲区);
                连接=真正的;
                StringBuilder的BUF =新的StringBuilder();
                的for(int i = 0; I<读;我++){
                    INT B =缓冲区[1]  -  0xFF的;
                    如果(B<为0x10){
                        buf.append(0);
                    }
                    。buf.append(Integer.toHexString(B))追加();
                }
                Log.d(ZeeTest,++++读+阅读+字节:+ buf.toString());
            }
        }赶上(IOException异常E){}
        Log.d(ZeeTest,++++完成:试验());
    }
    @覆盖
    公共无效的onDestroy(){
        尝试 {
            如果(在!= NULL){
                附寄();
            }
            如果(袜子!= NULL){
                sock.close();
            }
        }赶上(IOException异常E){
            e.printStackTrace();
        }
        super.onDestroy();
    }
}

=====原来的问题=====

我试图连接到来自摩托罗拉的Droid运行2.0.1固件的Zeemote(http://zeemote.com/)游戏控制器。测试应用程序下面没有连接到设备(LED闪烁),但连接之后立即删除。

我在下面粘贴两个测试的应用程序:一个真正试图从输入流和第二个,只是坐在那里,等待设备在5秒钟后断开阅读。是的,我确实有第三个版本:)这将首先等待ACL_CONNECTED,然后打开插槽但没有什么新在它的行为。

一些背景信息:我可以从我的笔记本电脑完全正常使用bluez的工具(登录连接以及)连接的Zeemote。我知道肯定的Droid的的谈得来的Zeemote也因为游戏临从市场​​的正常工作与它(但它是一个驱动程序/服务,所以也许它使用较低级别的API? )。

我注意到,亚行错误报告报告中的的Zeemote既不UUID也不RFCOMM通道,而它这样做的所有其他设备(包括摩托罗拉HS815耳机,另一哑装置,该装置'SDP浏览报告中没有)。此外,当设备启动,的Zeemote的优先级为0(更为优先100+)。

我挺在这里的损失,我的工作了这么久,我跑出去的想法,所以任何帮助将是非常美联社preciated(即使你不知道的的答案:))

谢谢,马克斯

测试应用程序第1号

该应用程序会尝试实际上是从设备读取。

===== code =====

公共类ZeeTest延伸活动{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        尝试 {
            测试();
        }赶上(IOException异常E){
            e.printStackTrace();
        }
    }

    私有的BluetoothSocket袜子;
    私人的InputStream的;
    公共无效测试()抛出IOException异常{
        BluetoothDevice类ZEE = BluetoothAdapter.getDefaultAdapter()。
                      getRemoteDevice(00:1C:4D:02:A6:55);
        袜子= zee.createRfcommSocketToServiceRecord(
                      UUID.fromString(8e1f0cf7-508f-4875-b62c-fbb67fd34812));
        Log.d(ZeeTest,++++连接);
        sock.connect();
        Log.d(ZeeTest,++++连接);
        在= sock.getInputStream();
        byte []的缓冲区=新的字节[1];
        INT字节= 0;
        INT X = 0;
        Log.d(ZeeTest,++++听......);
        而(X 2){
            X ++;
            尝试 {
                字节= in.read(缓冲区);
                Log.d(ZeeTest,++++阅读+字节+字节);
            }赶上(IOException异常E){
                e.printStackTrace();
                尝试{视频下载(100); }赶上(InterruptedException的IE){}
            }
        }
        Log.d(ZeeTest,++++完成:试验());
    }
    @覆盖
    公共无效的onDestroy(){
        尝试 {
            如果(在!= NULL){
                附寄();
            }
            如果(袜子!= NULL){
                sock.close();
            }
        }赶上(IOException异常E){
            e.printStackTrace();
        }
        super.onDestroy();
    }
}

=====登录=====

04-19 22:27:01.147:DEBUG / ZeeTest(8619):++++连接
04-19 22:27:04.085:信息/ USBD(1062):process_usb_uevent_message():缓冲区=加@ /设备/虚拟/蓝牙/ hci0 / hci0:1
04-19 22:27:04.085:信息/ USBD(1062):主要():调用选择(...)
04-19 22:27:04.327:ERROR / BluetoothEventLoop.cpp(4029):event_filter:接收信号org.bluez.Device:从的PropertyChanged /组织/的bluez / 4121 / hci0 / dev_00_1C_4D_02_A6_55
04-19 22:27:04.491:VERBOSE / BluetoothEventRedirector(7499):收到android.bleutooth.device.action.UUID
04-19 22:27:04.905:DEBUG / ZeeTest(8619):++++连接
04-19 22:27:04.905:DEBUG / ZeeTest(8619):++++听...
04-19 22:27:05.538:WARN / System.err的(8619):java.io.IOException异常:软件导致连接中止
04-19 22:27:05.600:WARN / System.err的(8619):在android.bluetooth.BluetoothSocket.readNative(本机方法)
...
04-19 22:27:05.717:WARN / System.err的(8619):java.io.IOException异常:软件导致连接中止
04-19 22:27:05.717:WARN / System.err的(8619):在android.bluetooth.BluetoothSocket.readNative(本机方法)
...
04-19 22:27:05.819:DEBUG / ZeeTest(8619):++++完成:测试()
04-19 22:27:07.155:VERBOSE / BluetoothEventRedirector(7499):收到android.bleutooth.device.action.UUID
04-19 22:27:09.077:信息/ USBD(1062):process_usb_uevent_message():缓冲区=删除@ /设备/虚拟/蓝牙/ hci0 / hci0:1
04-19 22:27:09.085:信息/ USBD(1062):主要():调用选择(...)
04-19 22:27:09.139:ERROR / BluetoothEventLoop.cpp(4029):event_filter:接收信号org.bluez.Device:从的PropertyChanged /组织/的bluez / 4121 / hci0 / dev_00_1C_4D_02_A6_55

测试应用程序第2号

此测试连接,并等待 - 可用于显示自动断开的问题。

===== code =====

公共类ZeeTest延伸活动{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        getApplicationContext()。registerReceiver(接收器,
                    新的IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
        getApplicationContext()。registerReceiver(接收器,
                    新的IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
        尝试 {
            BluetoothDevice类ZEE = BluetoothAdapter.getDefaultAdapter()。
                            getRemoteDevice(00:1C:4D:02:A6:55);
            袜子= zee.createRfcommSocketToServiceRecord(
                            UUID.fromString(8e1f0cf7-508f-4875-b62c-fbb67fd34812));

            Log.d(ZeeTest,++++连接);
            sock.connect();
            Log.d(ZeeTest,++++连接);
        }赶上(IOException异常E){
            e.printStackTrace();
        }
    }

    私有静态最后LogBroadcastReceiver接收器=新LogBroadcastReceiver();
    公共静态类LogBroadcastReceiver扩展的BroadcastReceiver {
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            Log.d(ZeeReceiver,intent.toString());
            捆绑额外= intent.getExtras();
            对于(字符串K:extras.keySet()){
                Log.d(ZeeReceiver,号外:+ extras.get(K)的ToString());
            }
        }
    }

    私有的BluetoothSocket袜子;
    @覆盖
    公共无效的onDestroy(){
        。getApplicationContext()unregisterReceiver(接收器);
        如果(袜子!= NULL){
            尝试 {
                sock.close();
            }赶上(IOException异常E){
                e.printStackTrace();
            }
        }
        super.onDestroy();
    }
}

=====登录=====

04-19 22:06:34.944:DEBUG / ZeeTest(7986型):++++连接
04-19 22:06:38.202:信息/ USBD(1062):process_usb_uevent_message():缓冲区=加@ /设备/虚拟/蓝牙/ hci0 / hci0:1
04-19 22:06:38.202:信息/ USBD(1062):主要():调用选择(...)
04-19 22:06:38.217:ERROR / BluetoothEventLoop.cpp(4029):event_filter:接收信号org.bluez.Device:从的PropertyChanged /组织/的bluez / 4121 / hci0 / dev_00_1C_4D_02_A6_55
04-19 22:06:38.428:VERBOSE / BluetoothEventRedirector(7499):收到android.bleutooth.device.action.UUID
04-19 22:06:38.968:DEBUG / ZeeTest(7986型):++++连接
04-19 22:06:39.061:DEBUG / ZeeReceiver(7986型):意向{行为= android.bluetooth.device.action.ACL_CONNECTED(有群众演员)}
04-19 22:06:39.108:DEBUG / ZeeReceiver(7986型):额外:00:1C:4D:02:A6:55
04-19 22:06:39.538:信息/ ActivityManager(4029):显示活动zee.test / .ZeeTest:5178毫秒(共5178毫秒)
04-19 22:06:41.014:VERBOSE / BluetoothEventRedirector(7499):收到android.bleutooth.device.action.UUID
04-19 22:06:43.038:信息/ USBD(1062):process_usb_uevent_message():缓冲区=删除@ /设备/虚拟/蓝牙/ hci0 / hci0:1
04-19 22:06:43.038:信息/ USBD(1062):主要():调用选择(...)
04-19 22:06:43.069:ERROR / BluetoothEventLoop.cpp(4029):event_filter:接收信号org.bluez.Device:从的PropertyChanged /组织/的bluez / 4121 / hci0 / dev_00_1C_4D_02_A6_55
04-19 22:06:43.124:DEBUG / ZeeReceiver(7986型):意向{行为= android.bluetooth.device.action.ACL_DISCONNECTED(有群众演员)}
04-19 22:06:43.124:DEBUG / ZeeReceiver(7986型):额外:00:1C:4D:02:A6:55

系统日志

=====终端登录=====

$ sdptool可以浏览
查询 ...
浏览00:1C:4D:02:A6:55 ...

$记录的sdptool 00:1C:4D:02:A6:55
服务名称:Zeemote相兼容
服务RecHandle:0x10015
服务类ID列表:
  UUID 128:8e1f0cf7-508f-4875-b62c-fbb67fd34812
协议描述符表:
  L2CAP(0100)
  RFCOMM(为0x0003)
    通道:1
语言基础的Attr列表:
  code_ISO639:0x656e
  编码:的0x6A
  base_offset:为0x100

$ RFCOMM连接的/ dev / tty10 00:1C:4D:02:A6:55
连接的/ dev / rfcomm0为00:1C:4D:02:A6:55通道1
preSS CTRL-C为挂机

#RFCOMM显示为/ dev / tty10
rfcomm0:00:1F:3A:E4:8:40  - > 00:1C:4D:02:A6:连接55通道1 [复用DLC发布-ON-吸氢TTY附加]

#猫的/ dev / tty10
(这里没有什么)

#hcidump
HCI嗅探器 - 蓝牙数据包分析器版本1.42
设备:hci0 snap_len:1028过滤器:为0xffffffff
< HCI命令:创建连接(0×01 | 0×0005)PLEN 13
> HCI事件:命令状态(为0x0F)PLEN 4
> HCI事件:将完成(03)PLEN 11
< HCI指令:读取远程支持的功能(0×01 | 0x001b)PLEN 2
> HCI事件:读取远程支持的功能(0x0B中)PLEN 11
< ACL数据:处理11标志0X02 DLEN 10
    L2CAP(S):信息REQ:2型
> HCI事件:命令状态(为0x0F)PLEN 4
> HCI事件:页扫描重复模式切换(0x20的)PLEN 7
> HCI事件:最大插槽更改(0x1b)PLEN 3
< HCI命令:远程名称请求(0×01 | 0x0019)PLEN 10
> HCI事件:命令状态(为0x0F)PLEN 4
> ACL数据:处理11标志0X02 DLEN 16
    L2CAP(S):信息RSP:2型结果0
      扩展功能面具为0x0000
< ACL数据:处理11标志0X02 DLEN 12
    L2CAP(S):连接REQ:每平方米3 SCID 0x0040
> HCI事件:已完成数据包(0x13)均禁用PLEN 5号
> ACL数据:处理11标志0X02 DLEN 16
    L2CAP(S):连接RSP:DCID 0x04fb SCID 0x0040结果1状态2
      连接待决 - 授权待定
> HCI事件:远程名称所需物品完成(0×07)PLEN 255
> ACL数据:处理11标志0X02 DLEN 16
    L2CAP(S):连接RSP:DCID 0x04fb SCID 0x0040结果0状态0
      连接成功
< ACL数据:处理11标志0X02 DLEN 16
    L2CAP(S):配置REQ:DCID 0x04fb标志为0x00克伦4
      MTU 1013
(事件使用的bluez正确接收)

=====亚行错误报告的一部分=====

--Known devices--
00:19:A1:2D:16:EA保税(0)LG U830
    00001105-0000-1000-8000-00805f9b34fb RFCOMM通道= 17
00:1C:4D:02:A6:55保税(0)Zeemote JS1手柄
00:0B:2E:6E:6F:00接合(0)摩托罗拉HS815
    00001108-0000-1000-8000-00805f9b34fb RFCOMM通道= 1
    0000111e-0000-1000-8000-00805f9b34fb RFCOMM通道= 2
00:1F:3A:E4:C8:40接合(0)BRCM BT4X
    00001105-0000-1000-8000-00805f9b34fb RFCOMM通道= 9
00:18:42:EC:E2:99保税(0)N95
    00001105-0000-1000-8000-00805f9b34fb RFCOMM通道= 9

=====从启动日志摘录=====

04-18 21:55:10.382:VERBOSE / BluetoothEventRedirector(1985年):收到android.bluetooth.adapter.action.STATE_CHANGED
04-18 21:55:10.421:DEBUG / BT HSHFP(1237):负载优先级00:19:A1:2D:16:EA = 100
04-18 21:55:10.428:DEBUG / BT HSHFP(1237):负载优先级00:1C:4D:02:A6:55 = 0
04-18 21:55:10.444:DEBUG / BT HSHFP(1237):负载优先级00:0B:2E:6E:6F:00 = 101
04-18 21:55:10.749:DEBUG / BT HSHFP(1237):负载优先级00:1F:3A:E4:8:40 = 100
04-18 21:55:10.780:DEBUG / BT HSHFP(1237):负载优先级00:18:42:EC:E2:99 = 100

解决方案

试着改变你的code创建RfcommSocket:

 袜子= zee.createRfcommSocketToServiceRecord(
                      UUID.fromString(8e1f0cf7-508f-4875-b62c-fbb67fd34812));
 

这code:

 方法M = zee.getClass()实现getMethod(createRfcommSocket,新的等级[] {} int.class)。
袜子=(的BluetoothSocket)m.invoke(设备,1);
 

也可以尝试改变参数值范围在1-3本m.invoke(设备,1);当连接将被连接,但是当你尝试阅读中止,在一些循环()再次调用您的方法测试。就这么简单:

 的for(int i = 0;我3;;我++){if测试()(testDone!); }
 

This problem has been solved! Thanks a lot to Brad, Denis and junkie! You're the heroes! :)

This is the working code. It connects to Zeemote and reads data from it.

===== Code =====

public class ZeeTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            for (int i = 0; i < 3; i++) {
                test();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean connected = false;
    private BluetoothSocket sock;
    private InputStream in;
    public void test() throws Exception {
        if (connected) {
            return;
        }
        BluetoothDevice zee = BluetoothAdapter.getDefaultAdapter().
            getRemoteDevice("00:1C:4D:02:A6:55");
        Method m = zee.getClass().getMethod("createRfcommSocket",
            new Class[] { int.class });
        sock = (BluetoothSocket)m.invoke(zee, Integer.valueOf(1));
        Log.d("ZeeTest", "++++ Connecting");
        sock.connect();
        Log.d("ZeeTest", "++++ Connected");
        in = sock.getInputStream();
        byte[] buffer = new byte[50];
        int read = 0;
        Log.d("ZeeTest", "++++ Listening...");
        try {
            while (true) {
                read = in.read(buffer);
                connected = true;
                StringBuilder buf = new StringBuilder();
                for (int i = 0; i < read; i++) {
                    int b = buffer[i] & 0xff;
                    if (b < 0x10) {
                        buf.append("0");
                    }
                    buf.append(Integer.toHexString(b)).append(" ");
                }
                Log.d("ZeeTest", "++++ Read "+ read +" bytes: "+ buf.toString());
            }
        } catch (IOException e) {}
        Log.d("ZeeTest", "++++ Done: test()");
    }
    @Override
    public void onDestroy() {
        try {
            if (in != null) {
                in.close();
            }
            if (sock != null) {
                sock.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }
}

===== Original question =====

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'm pasting two test apps below: one that actually tries to read from input stream and second one that just sits there, waiting for the device to disconnect after 5 seconds. And yes, I do have third version :) which first waits for ACL_CONNECTED and then opens socket but there's nothing new in its behaviour.

Some background info:I can connect to the Zeemote from my laptop perfectly fine using bluez tools (log attached as well). I know for sure that Droid is able to talk to Zeemote as well because 'Game Pro' from the Market works fine with it (but then it's a driver/service so maybe it uses lower-level API?).

I noticed that 'adb bugreport' reports neither UUID nor RFCOMM channel for Zeemote while it does so for all other devices (including Moto HS815 headset, another dumb device for which 'sdp browse' reports nothing). Also, when the device boots, Zeemote's priority is 0 (others have priority 100+).

I'm quite at a loss here, I worked on it for so long that I ran out of ideas so any help would be very much appreciated (even if you don't know the answer :) )

Thanks,Max

Test application No. 1

This app tries to actually read from the device.

===== 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();
        }
    }

    private BluetoothSocket sock;
    private InputStream in;
    public void test() throws IOException {
        BluetoothDevice zee = BluetoothAdapter.getDefaultAdapter().
                      getRemoteDevice("00:1C:4D:02:A6:55");
        sock = zee.createRfcommSocketToServiceRecord(
                      UUID.fromString("8e1f0cf7-508f-4875-b62c-fbb67fd34812"));
        Log.d("ZeeTest", "++++ Connecting");
        sock.connect();
        Log.d("ZeeTest", "++++ Connected");
        in = sock.getInputStream();
        byte[] buffer = new byte[1];
        int bytes = 0;
        int x = 0;
        Log.d("ZeeTest", "++++ Listening...");
        while (x < 2) {
            x++;
            try {
                bytes = in.read(buffer);
                Log.d("ZeeTest", "++++ Read "+ bytes +" bytes");
            } catch (IOException e) {
                e.printStackTrace();
                try { Thread.sleep(100); } catch (InterruptedException ie) {}
            }
        }
        Log.d("ZeeTest", "++++ Done: test()");
    }
    @Override
    public void onDestroy() {
        try {
            if (in != null) {
                in.close();
            }
            if (sock != null) {
                sock.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }
}

===== Log =====

04-19 22:27:01.147: DEBUG/ZeeTest(8619): ++++ Connecting
04-19 22:27:04.085: INFO/usbd(1062): process_usb_uevent_message(): buffer = add@/devices/virtual/bluetooth/hci0/hci0:1
04-19 22:27:04.085: INFO/usbd(1062): main(): call select(...)
04-19 22:27:04.327: ERROR/BluetoothEventLoop.cpp(4029): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/4121/hci0/dev_00_1C_4D_02_A6_55
04-19 22:27:04.491: VERBOSE/BluetoothEventRedirector(7499): Received android.bleutooth.device.action.UUID
04-19 22:27:04.905: DEBUG/ZeeTest(8619): ++++ Connected
04-19 22:27:04.905: DEBUG/ZeeTest(8619): ++++ Listening...
04-19 22:27:05.538: WARN/System.err(8619): java.io.IOException: Software caused connection abort
04-19 22:27:05.600: WARN/System.err(8619):     at android.bluetooth.BluetoothSocket.readNative(Native Method)
...
04-19 22:27:05.717: WARN/System.err(8619): java.io.IOException: Software caused connection abort
04-19 22:27:05.717: WARN/System.err(8619):     at android.bluetooth.BluetoothSocket.readNative(Native Method)
...
04-19 22:27:05.819: DEBUG/ZeeTest(8619): ++++ Done: test()
04-19 22:27:07.155: VERBOSE/BluetoothEventRedirector(7499): Received android.bleutooth.device.action.UUID
04-19 22:27:09.077: INFO/usbd(1062): process_usb_uevent_message(): buffer = remove@/devices/virtual/bluetooth/hci0/hci0:1
04-19 22:27:09.085: INFO/usbd(1062): main(): call select(...)
04-19 22:27:09.139: ERROR/BluetoothEventLoop.cpp(4029): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/4121/hci0/dev_00_1C_4D_02_A6_55

Test application No. 2

This test connects and waits -- useful for displaying the auto-disconnect issue.

===== Code =====

public class ZeeTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getApplicationContext().registerReceiver(receiver,
                    new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
        getApplicationContext().registerReceiver(receiver,
                    new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
        try {
            BluetoothDevice zee = BluetoothAdapter.getDefaultAdapter().
                            getRemoteDevice("00:1C:4D:02:A6:55");
            sock = zee.createRfcommSocketToServiceRecord(
                            UUID.fromString("8e1f0cf7-508f-4875-b62c-fbb67fd34812"));

            Log.d("ZeeTest", "++++ Connecting");
            sock.connect();
            Log.d("ZeeTest", "++++ Connected");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static final LogBroadcastReceiver receiver = new LogBroadcastReceiver();
    public static class LogBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("ZeeReceiver", intent.toString());
            Bundle extras = intent.getExtras();
            for (String k : extras.keySet()) {
                Log.d("ZeeReceiver", "    Extra: "+ extras.get(k).toString());
            }
        }
    }

    private BluetoothSocket sock;
    @Override
    public void onDestroy() {
        getApplicationContext().unregisterReceiver(receiver);
        if (sock != null) {
            try {
                sock.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        super.onDestroy();
    }
}

===== Log =====

04-19 22:06:34.944: DEBUG/ZeeTest(7986): ++++ Connecting
04-19 22:06:38.202: INFO/usbd(1062): process_usb_uevent_message(): buffer = add@/devices/virtual/bluetooth/hci0/hci0:1
04-19 22:06:38.202: INFO/usbd(1062): main(): call select(...)
04-19 22:06:38.217: ERROR/BluetoothEventLoop.cpp(4029): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/4121/hci0/dev_00_1C_4D_02_A6_55
04-19 22:06:38.428: VERBOSE/BluetoothEventRedirector(7499): Received android.bleutooth.device.action.UUID
04-19 22:06:38.968: DEBUG/ZeeTest(7986): ++++ Connected
04-19 22:06:39.061: DEBUG/ZeeReceiver(7986): Intent { act=android.bluetooth.device.action.ACL_CONNECTED (has extras) }
04-19 22:06:39.108: DEBUG/ZeeReceiver(7986):     Extra: 00:1C:4D:02:A6:55
04-19 22:06:39.538: INFO/ActivityManager(4029): Displayed activity zee.test/.ZeeTest: 5178 ms (total 5178 ms)
04-19 22:06:41.014: VERBOSE/BluetoothEventRedirector(7499): Received android.bleutooth.device.action.UUID
04-19 22:06:43.038: INFO/usbd(1062): process_usb_uevent_message(): buffer = remove@/devices/virtual/bluetooth/hci0/hci0:1
04-19 22:06:43.038: INFO/usbd(1062): main(): call select(...)
04-19 22:06:43.069: ERROR/BluetoothEventLoop.cpp(4029): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/4121/hci0/dev_00_1C_4D_02_A6_55
04-19 22:06:43.124: DEBUG/ZeeReceiver(7986): Intent { act=android.bluetooth.device.action.ACL_DISCONNECTED (has extras) }
04-19 22:06:43.124: DEBUG/ZeeReceiver(7986):     Extra: 00:1C:4D:02:A6:55

System logs

===== 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)

===== Part of adb bugreport =====

--Known devices--
00:19:A1:2D:16:EA     bonded (0) LG U830
    00001105-0000-1000-8000-00805f9b34fb RFCOMM channel = 17
00:1C:4D:02:A6:55     bonded (0) Zeemote JS1
00:0B:2E:6E:6F:00     bonded (0) Motorola HS815
    00001108-0000-1000-8000-00805f9b34fb RFCOMM channel = 1
    0000111e-0000-1000-8000-00805f9b34fb RFCOMM channel = 2
00:1F:3A:E4:C8:40     bonded (0) BRCM BT4X
    00001105-0000-1000-8000-00805f9b34fb RFCOMM channel = 9
00:18:42:EC:E2:99     bonded (0) N95
    00001105-0000-1000-8000-00805f9b34fb RFCOMM channel = 9

===== Excerpt from boot log =====

04-18 21:55:10.382: VERBOSE/BluetoothEventRedirector(1985): Received android.bluetooth.adapter.action.STATE_CHANGED
04-18 21:55:10.421: DEBUG/BT HSHFP(1237): Loaded priority 00:19:A1:2D:16:EA = 100
04-18 21:55:10.428: DEBUG/BT HSHFP(1237): Loaded priority 00:1C:4D:02:A6:55 = 0
04-18 21:55:10.444: DEBUG/BT HSHFP(1237): Loaded priority 00:0B:2E:6E:6F:00 = 101
04-18 21:55:10.749: DEBUG/BT HSHFP(1237): Loaded priority 00:1F:3A:E4:C8:40 = 100
04-18 21:55:10.780: DEBUG/BT HSHFP(1237): Loaded priority 00:18:42:EC:E2:99 = 100
解决方案

Try to change your code for creating RfcommSocket:

sock = zee.createRfcommSocketToServiceRecord(
                      UUID.fromString("8e1f0cf7-508f-4875-b62c-fbb67fd34812"));

for this code:

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

Also try to change argument value in range 1-3 in this m.invoke(device, 1);When connection will be Connected, but aborted when you try reading, call in some loop your method test() again.As simple:

for(int i=0;i<3;i++){  if(!testDone) test(); }

这篇关于如何$死于.connect后立即()p $ pvent Android的蓝牙RFCOMM连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 04:43