简体   繁体   English

在PC上运行的Bluez应用程序用于侦听Android设备

[英]Bluez application running on PC for listening an Android Device

I want to develop an application that use the Linux Bluetooth API Bluez by mean allow an Android device to send and receive data. 我想开发一个使用Linux蓝牙API Bluez的应用程序,同时允许Android设备发送和接收数据。

For the PC side i have looked at this code: http://people.csail.mit.edu/albert/bluez-intro/x502.html because Android use RFCOMM protocol, or i guess that since is impossible to set manually the protocol. 对于PC端,我查看了以下代码: http : //people.csail.mit.edu/albert/bluez-intro/x502.html,因为Android使用RFCOMM协议,或者我猜想因为无法手动设置协议。

Since in Android is only possible to use the Java API (Bluez API locked) and the SDP in not working very well i have to use this call: 由于在Android中只能使用Java API(Bluez API已锁定)并且SDP不能很好地工作,因此我必须使用此调用:

InsecureBluetooth.createRfcommSocket(BlueDevice , Channel , false);

When i set the channel for example to 0xC (12) that is one of the channel returned by the command: 当我将通道设置为例如0xC(12)时,该通道是命令返回的通道之一:

sdptool browse local | grep Channel

the android application can connect correctly to the PC. android应用程序可以正确连接到PC。

The problem is that i can't figure out what are the parameter to allow the two applications to communicate. 问题是我无法弄清楚允许两个应用程序通信的参数是什么。 To the PC side i have only a port number with value 1. To the Android side i have a channel that is assigned to a service available by SDP. 在PC端,我只有一个值为1的端口号。在Android端,我有一个分配给SDP可用服务的通道。 But SDP is not working in Android so how i can do that ? 但是SDP在Android中不起作用,所以我该怎么做?

Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

I have solved in this way: 我已经用这种方式解决了:

For the PC side using the SDP record. 对于PC端,使用SDP记录。 We have to record the service to a well know channel that must be FREE, i mean not used by other service. 我们必须将服务记录到一个必须免费的众所周知的频道,我的意思是未被其他服务使用。 To chek it do the command: 要检查它,请执行以下命令:

sdptool record local

The code to do that is at http://people.csail.mit.edu/albert/bluez-intro/x604.html 要执行此操作的代码位于http://people.csail.mit.edu/albert/bluez-intro/x604.html

Section Example 4-9. 部分示例4-9。 Describing a service 描述服务

This two line must be added after // set the general service ID 必须在//设置常规服务ID之后添加这两行

// set the service class ID
sdp_list_t service_class = {NULL, &svc_uuid};
sdp_set_service_classes( record, &service_class);

and we have to change the channel to one not used by other service. 并且我们必须将频道更改为其他服务未使用的频道。 For example the channel 1 : 例如频道1:

uint8_t rfcomm_channel = 1;

Now that we have something listening to the PC side we have to connect with the android device directly to the channel one by the well know reflection method because unfortunately the SDP now work at all in android. 既然我们已经在PC端进行监听,我们就必须通过众所周知的反射方法将android设备直接连接到通道上,因为不幸的是,SDP现在完全可以在android中工作。

Method bluetoothConnect;

bluetoothConnect = BlueDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class} );
CommunicationChannel = (BluetoothSocket) bluetoothConnect.invoke(BlueDevice, 1);

CommunicationChannel.connect();

Of course BlueDevice is a device discored by the startDiscovery() method and the "1" stand for channel 1. 当然, BlueDevice是由startDiscovery()方法取消分配的设备,“ 1”代表通道1。

I know this doesn't answer your question, I think bluecove (http://bluecove.org/) would be better choice for this. 我知道这不能回答您的问题,我认为bluecove(http://bluecove.org/)将是更好的选择。 It supports most operating systems, and provides decent library for Java. 它支持大多数操作系统,并为Java提供了不错的库。

I don't think bluez will work for Windows, you need a WIDCOMM or BlueSoleil stack for it, and bluecove will use appropriate stack for you. 我认为bluez不适用于Windows,您需要WIDCOMM或BlueSoleil堆栈,bluecove会为您使用适当的堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM