Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
蓝牙RFCOMM连接Linux_Linux_Bluetooth_Serial Port_Processing_Rfcomm - Fatal编程技术网

蓝牙RFCOMM连接Linux

蓝牙RFCOMM连接Linux,linux,bluetooth,serial-port,processing,rfcomm,Linux,Bluetooth,Serial Port,Processing,Rfcomm,我正在尝试使用蓝牙连接将可穿戴设备连接到处理IDE。首先,我要说的是,我已经让它在Windows操作系统上运行,但我正在努力用LinuxMint实现它。为了让它运行,我让事情变得更糟,所以我将尽可能清楚地解释我自己 第一个问题 起初,我能够从处理连接到rfcomm0端口,但它只向我发送包含255个内容的字节。例如,如果我发送了一个命令,如myPort.write(0x03),以获取我的可穿戴设备的流式采样,它没有这样做,它只发送了一个带有255的字节。在同一个程序中,但在windows中运行,我

我正在尝试使用蓝牙连接将可穿戴设备连接到处理IDE。首先,我要说的是,我已经让它在Windows操作系统上运行,但我正在努力用LinuxMint实现它。为了让它运行,我让事情变得更糟,所以我将尽可能清楚地解释我自己

第一个问题

起初,我能够从处理连接到rfcomm0端口,但它只向我发送包含255个内容的字节。例如,如果我发送了一个命令,如myPort.write(0x03),以获取我的可穿戴设备的流式采样,它没有这样做,它只发送了一个带有255的字节。在同一个程序中,但在windows中运行,我获得了我向设备请求的所有数据,因此rfcomm0端口肯定有问题。我的/etc/bluetooth如下所示:

rfcomm0 {
    # Automatically bind the device at startup
    bind yes;

    # Bluetooth address of the device
    device 00:06:66:42:10:44;

    # RFCOMM channel for the connection
    channel 1;

    # Description of the connection
    comment "Example Bluetooth device";
}
设备已经配对,因此执行rfcomm bind 0 00:06:66:42:10:44并以管理员身份运行处理,我能够连接到端口(尽管没有收到我所说的正确数据)。我真的是新的串行端口,但我的猜测是,可能通道号1不是正确的一个我如何知道我需要使用哪个频道?

第二个问题

尝试修复它时,我使用了rfcomm release rfcomm0,更改了rfcomm0文件设置通道0(以测试另一个通道),并在再次绑定(rfcomm bind 0 00:06:66:42:10:44 0,以确保它连接到其他通道)和运行处理后,我在尝试连接时不断收到此错误:

Error opening serial port /dev/rfcomm0: Port not found
该端口的文件实际上存在。我已将该文件的权限掩码设置为/dev/rfcomm0 to crw rw rw-1 root dialout 216,0 Apr 30 17:04/dev/rfcomm0,以防出现问题,但运气不佳

当我使用命令rfcomm时,它给出以下信息:

rfcomm0: 00:06:66:42:10:44 channel 0 clean
有什么帮助吗

致意

编辑

第二个问题仅在我将rfcomm连接到通道0时出现。因此:

-Channel 0 -> Port not found
-Channel 1 -> Bytes with 255.
-Channel X -> Nothing.
请记住,Windows中的相同代码可以正常工作

编辑2

好的,看起来我得到了一些东西…当我从DRAW()方法发送消息时,可穿戴设备只向我发送信息字节。但这种情况很糟糕,因为我需要从setup方法执行此操作(从DRAW方法向StartStreaming发送消息没有意义,它会一直发送此消息)

编辑3

似乎有些时间问题…如果我在设置功能上执行此操作:

  for(int i = 0; i < 50; i++){
    delay(200);
    getSampleRate();
  }
for(int i=0;i<50;i++){
延迟(200);
getSampleRate();
}
我只是得到了4-5倍的答案…我真的不知道发生了什么

这是全部代码:

    void setup() 
{
  size(windowW, windowH);
  smooth();  

  // List all the available serial ports:
  println(Serial.list());

  try {
    myPort = new Serial(this, Serial.list()[0], 115200);

  } catch (Exception e) {

      println(e.getMessage());
      connectionError = true;
  } 

  delay(2000);

  /*
  for(int i = 0; i < 50000; i++){
    delay(200);
    getSampleRate();
  }
  */
}

void draw(){

  background(255);

}

// Called whenever there is something available to read
void serialEvent(Serial port) {

  int bytesAvailable = myPort.available();
  for(int i = 0; i < bytesAvailable; i++){
    println("Byte:", port.read());
  } 
}
void setup()
{
大小(windowW、windowH);
光滑的();
//列出所有可用的串行端口:
println(Serial.list());
试一试{
myPort=new Serial(这是Serial.list()[0],115200);
}捕获(例外e){
println(如getMessage());
connectionError=true;
} 
延迟(2000年);
/*
对于(int i=0;i<50000;i++){
延迟(200);
getSampleRate();
}
*/
}
作废提款(){
背景(255);
}
//每当有可阅读的内容时调用
无效串行事件(串行端口){
int bytesavable=myPort.available();
for(int i=0;i
最终编辑


嗯,经过大量研究和测试,我认为问题在于Linux中使用蓝牙的端口连接。我在Windows和MAC中尝试了相同的代码,一切都运行得很好…

我在与/etc/bluetooth绑定时遇到了类似的情况

我不知道为什么会这样,但这就是我工作的原因

我把/etc/bluetooth留空了

我用代码做了所有的连接

插入USB蓝牙后,您需要使用:

bzero(temp, 256);
sprintf(temp, "sudo hciconfig hci0 reset");
system((char*)temp);
仅当USB首次插入时,而不是每次连接时,才需要执行上述操作。有时EMI会导致USB“拔下并重新插入”,在这种情况下,您需要再次运行上述操作

这将连接到设备:

bzero(temp, 256);
sprinttf(temp, "sudo rfcomm -r connect 00:06:66:42:10:44 1");
system((char*)temp);
然后,我用它来检查是否建立了连接:

for(timeout = 0;; timeout++){
    access_status = access("/dev/rfcomm1", F_OK);
    if(access_status != -1) break; /* file exist */
    if(timeout >= 10) {
        printf("Failed to connect.");
        return;
    }
    sleep(1);
}
要关闭连接,我使用以下方法:

bzero(temp, 256);
sprintf(temp, "sudo rfcomm release /dev/rfcomm1");
system((char*)temp);
不用说,您的sudo需要配置,因此运行rfcomm和hciconfig不需要密码