2

我正在开发一个简单地将手机与计算机连接起来的 Android 应用程序。

我已经用这个命令(从命令行/控制台)连接了两台计算机:服务器监听这个:

sdptool add --channel=15 SP
rfcomm listen rfcomm4 15

客户端通过以下方式连接到服务器:

rfcomm connect rfcomm4 11:11:11:11:11:11 15

这工作正常。(我正在使用带有 bluez 的 Linux)它在每台机器的 /dev 中生成一个 rfcomm4 设备,我可以像串行端口一样发送和接收它。

当我想从安卓手机上做到这一点时,问题就来了。首先,在一台计算机上,我像这样启动服务器:

sdptool add --channel=15 SP
rfcomm listen rfcomm4 15

然后,在一个新线程中的 android 应用程序(使用 eclipse 和 android sdk 制作)中,我执行以下操作:

...
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
...
BluetoothSocket tmp = remoteDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);
...
mBluetoothAdapter.cancelDiscovery();
...
mmSocket.connect();
...

但是连接永远不会出现在服务器上......有些变量我无法完全理解。那个 UUID ......什么是精确的?我已经阅读并阅读,它说频道进入那个长字符串......但是......如何?如何将频道 15(我在服务器中收听的频道)放入 uuid。其他文章说 uuid 是预先建立的。而对于 rfcomm 它去那个 uuid ......

我不确定我是否正在使用 bluez 2.25 在 android 手机和 linux PC 之间进行简单的通信(如聊天)。(不能更改服务器中的此版本)。如果我的方法是错误的,我会感谢有人告诉我该怎么做。

非常感谢!


@ashish 谢谢回复!!

我像你在电脑上说的那样做 hcidump,当我用 hciconfig hci0 up 设备时,如果给我这个:

[root@FriendlyARM /]# hcidump -i hci0 -t
HCI sniffer - Bluetooth packet analyzer ver 2.5
device: hci0 snap_len: 1500 filter: 0xffffffff
2000-01-02 05:25:26.472669 < HCI Command: Read Local Supported Features (0x04|0x0003) plen 0
2000-01-02 05:25:26.474602 > HCI Event: Command Complete (0x0e) plen 12
    Read Local Supported Features (0x04|0x0003) ncmd 1
    status 0x00
    Features: 0xff 0xff 0x8d 0xfe 0x9b 0xf9 0x00 0x80
2000-01-02 05:25:26.474711 < HCI Command: Read Local Version Information (0x04|0x0001) plen 0
2000-01-02 05:25:26.476613 > HCI Event: Command Complete (0x0e) plen 12
    Read Local Version Information (0x04|0x0001) ncmd 1
    status 0x00
    HCI Version: 2.0 (0x3) HCI Revision: 0x3000
    LMP Version: 2.0 (0x3) LMP Subversion: 0x420b
    Manufacturer: Broadcom Corporation (15)
2000-01-02 05:25:26.479366 < HCI Command: Read Buffer Size (0x04|0x0005) plen 0
2000-01-02 05:25:26.481614 > HCI Event: Command Complete (0x0e) plen 11
    Read Buffer Size (0x04|0x0005) ncmd 1
    status 0x00
    ACL MTU 1017:8 SCO MTU 64:0
2000-01-02 05:25:26.481753 < HCI Command: Read BD ADDR (0x04|0x0009) plen 0
2000-01-02 05:25:26.483597 > HCI Event: Command Complete (0x0e) plen 10
    Read BD ADDR (0x04|0x0009) ncmd 1
    status 0x00 bdaddr 00:1B:10:00:2A:EC
2000-01-02 05:25:26.483719 < HCI Command: Read Class of Device (0x03|0x0023) plen 0
2000-01-02 05:25:26.485593 > HCI Event: Command Complete (0x0e) plen 7
    Read Class of Device (0x03|0x0023) ncmd 1
    status 0x00 class 0x000000
2000-01-02 05:25:26.485714 < HCI Command: Read Local Name (0x03|0x0014) plen 0
2000-01-02 05:25:26.503570 > HCI Event: Command Complete (0x0e) plen 252
    Read Local Name (0x03|0x0014) ncmd 1
    status 0x00 name 'BCM2045B'
2000-01-02 05:25:26.503671 < HCI Command: Read Voice Setting (0x03|0x0025) plen 0
2000-01-02 05:25:26.505593 > HCI Event: Command Complete (0x0e) plen 6
    Read Voice Setting (0x03|0x0025) ncmd 1
    status 0x00 voice setting 0x0060
2000-01-02 05:25:26.505715 < HCI Command: Set Event Filter (0x03|0x0005) plen 1
    type 0 condition 0
    Clear all filters
2000-01-02 05:25:26.507599 > HCI Event: Command Complete (0x0e) plen 4
    Set Event Filter (0x03|0x0005) ncmd 1
    status 0x00
2000-01-02 05:25:26.507711 < HCI Command: Write Page Timeout (0x03|0x0018) plen 2
    timeout 32768
2000-01-02 05:25:26.509594 > HCI Event: Command Complete (0x0e) plen 4
    Write Page Timeout (0x03|0x0018) ncmd 1
    status 0x00
2000-01-02 05:25:26.509700 < HCI Command: Write Connection Accept Timeout (0x03|0x0016) plen 2
    timeout 32000
2000-01-02 05:25:26.511604 > HCI Event: Command Complete (0x0e) plen 4
    Write Connection Accept Timeout (0x03|0x0016) ncmd 1
    status 0x00
2000-01-02 05:25:26.512423 < HCI Command: Write Scan Enable (0x03|0x001a) plen 1
    enable 3
2000-01-02 05:25:26.514583 > HCI Event: Command Complete (0x0e) plen 4
    Write Scan Enable (0x03|0x001a) ncmd 1
    status 0x00

然后......当我尝试连接(从 android-phone 到带有我在第一篇文章中使用通用 RFCOMM UUID 提供的代码的计算机)时,hcidump 给了我这个:

2000-01-02 05:33:50.147694 > HCI Event: Connect Request (0x04) plen 10
    bdaddr CC:FE:3C:D3:83:7A class 0x5a020c type ACL
2000-01-02 05:33:50.147884 < HCI Command: Accept Connection Request (0x01|0x0009) plen 7
    bdaddr CC:FE:3C:D3:83:7A role 0x01
    Role: Slave
2000-01-02 05:33:50.150700 > HCI Event: Command Status (0x0f) plen 4
    Accept Connection Request (0x01|0x0009) status 0x00 ncmd 1
2000-01-02 05:33:50.305680 > HCI Event: Connect Complete (0x03) plen 11
    status 0x00 handle 11 bdaddr CC:FE:3C:D3:83:7A type ACL encrypt 0x00
2000-01-02 05:33:50.305816 < HCI Command: Read Remote Supported Features (0x01|0x001b) plen 2
    handle 11
2000-01-02 05:33:50.307692 > HCI Event: Command Status (0x0f) plen 4
    Read Remote Supported Features (0x01|0x001b) status 0x00 ncmd 1
2000-01-02 05:33:50.308689 > HCI Event: Read Remote Supported Features (0x0b) plen 11
    status 0x00 handle 11
    Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
2000-01-02 05:33:50.308865 < HCI Command: Remote Name Request (0x01|0x0019) plen 10
    bdaddr CC:FE:3C:D3:83:7A mode 2 clkoffset 0x0000
2000-01-02 05:33:50.311689 > HCI Event: Command Status (0x0f) plen 4
    Remote Name Request (0x01|0x0019) status 0x00 ncmd 1
2000-01-02 05:33:50.331693 > HCI Event: Max Slots Change (0x1b) plen 3
    handle 11 slots 5
2000-01-02 05:33:50.346676 > ACL data: handle 11 flags 0x02 dlen 10
    L2CAP(s): Info req: type 2
2000-01-02 05:33:50.346801 < ACL data: handle 11 flags 0x02 dlen 16
    L2CAP(s): Info rsp: type 2 result 0
      Extended feature mask 0x00b8
        Enhanced Retransmission mode
        Streaming mode
        FCS Option
        Fixed Channels
2000-01-02 05:33:50.352689 > ACL data: handle 11 flags 0x02 dlen 10
    L2CAP(s): Info req: type 3
2000-01-02 05:33:50.352797 < ACL data: handle 11 flags 0x02 dlen 20
    L2CAP(s): Info rsp: type 3 result 0
      Fixed channel list 0x00000002
        L2CAP Signalling Channel
2000-01-02 05:33:50.356688 > ACL data: handle 11 flags 0x02 dlen 12
    L2CAP(s): Connect req: psm 1 scid 0x0045
2000-01-02 05:33:50.356797 < ACL data: handle 11 flags 0x02 dlen 16
    L2CAP(s): Connect rsp: dcid 0x0000 scid 0x0045 result 2 status 0
      Connection refused - PSM not supported
2000-01-02 05:33:50.366652 > HCI Event: Remote Name Req Complete (0x07) plen 255
    status 0x00 bdaddr CC:FE:3C:D3:83:7A name 'BigDaddy'
2000-01-02 05:33:50.596654 > HCI Event: Number of Completed Packets (0x13) plen 5
        handle 11 packets 3
2000-01-02 05:33:54.310068 < HCI Command: Disconnect (0x01|0x0006) plen 3
    handle 11 reason 0x13
    Reason: Remote User Terminated Connection
2000-01-02 05:33:54.312334 > HCI Event: Command Status (0x0f) plen 4
    Disconnect (0x01|0x0006) status 0x00 ncmd 1
2000-01-02 05:33:54.460326 > HCI Event: Disconn Complete (0x05) plen 4
    status 0x00 handle 11 reason 0x16
    Reason: Connection Terminated by Local Host

但是计算机服务器保持不变:

[root@FriendlyARM /]# sdptool add --channel=15 SP
[root@FriendlyARM /]# rfcomm listen rfcomm4 15
Waiting for connection on channel 15

所以电话设备无法与计算机连接......(显然)但是......为什么?你知道我做错了什么吗?

我的目标是在每个设备中获得一个 rfcommX 端口来更改消息...(就像聊天一样)

4

0 回答 0