如何通过蓝牙将串行数据发送到未指定的设备? [英] How to send serial data via Bluetooth to an unspecified device?

查看:44
本文介绍了如何通过蓝牙将串行数据发送到未指定的设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Arduino Uno 使用以下代码:

I use the following code for an Arduino Uno:

#include <SoftwareSerial.h>

SoftwareSerial device(2, 3); 

void setup()  
{
  device.begin(9600);
}

void loop() 
{
  device.println("33,89,156,203,978,0,0;");
}

未设置要发送到的特定设备.

如果我想在我的笔记本电脑(运行 Ubuntu 14.04)上接收数据,我只需调用:

If I want to receive the data on my laptop (running Ubuntu 14.04) I simply call:

sudo rfcomm bind rfcomm0 [MAC ADDRESS] 1

screen /dev/rfcomm0 

在另一个终端实例中,它可以工作.

in another terminal instance and it works.

如何使用 Android 应用实现相同的行为?以下示例代码指定了一个设备.我找不到任何其他代码.此外,它仅在我在笔记本电脑上监听这样的传入连接时才有效:

How can I achieve the same behaviour with an Android app? The following example code specifies a device. I cannot find any other code. Additionally it only works when I listen on the laptop for an incoming connection like this:

sudo rfcomm listen rfcomm0 [MAC ADDRESS]

我希望我的 Android 应用程序能够像 Arduino 示例一样工作.我怎样才能做到这一点?

I want my Android app to work exactly like the Arduino example. How can I achieve that?

推荐答案

不幸的是,Android 似乎没有允许您执行广播类型行为的低级经典蓝牙 API.这是有道理的,因为 Android 旨在进入功率受限的设备,并且有源无线电使用能量.如果您需要使用经典蓝牙 (3.x) 和 Android 来处理发送或接收广播类型的行为,您可能需要编写自定义 ROM.

Unfortunately Android doesn't appear to have low level Classic Bluetooth APIs which would allow you to do broadcast type behavior. This makes some sense as Android is intended to go into a power limited devices and active radios use energy. If you are required to use Classic Bluetooth (3.x) and Android to handle sending or receiving broadcast type behavior you'll probably need to write a custom ROM.

但是,有一种称为低功耗蓝牙 (4.x) 的规范允许更低的能耗但更慢/更少的数据吞吐量.特别是广告模式.正在扫描的 Android 设备可以获取由称为信标"的设备广播的短广告数据包.使用这种模式的协议有 Apple 的 iBeacon 和 Google 的 Eddystone.

However there is specification called Bluetooth Low Energy (4.x) allows for less energy consumption but slower/less data throughput. Specifically the Advertising mode. Android devices which are scanning can pick up the short advertised data packet broadcasted by a device called a 'beacon'. Protocols which use this mode are Apple's iBeacon and Google's Eddystone.

在此处查找涉及广告的示例应用:

Look here for sample apps involving Advertisements:

另请注意,Android 设备并不总是支持 BLE 外设模式,这是您的 Android 设备像信标一样运行所需的.

Also note that Android devices don't always support BLE Peripheral Mode which is what you will need for your Android device to act like beacon.

相关链接:Android 设备可以充当 iBeacon 吗?

还有一个很好的列表,列出了哪些设备已经过测试:http://altbeacon.github.io/android-beacon-library/beacon-transmitter-devices.html

And a nice list of what devices have been tested: http://altbeacon.github.io/android-beacon-library/beacon-transmitter-devices.html

因此,在您的情况下,您仍然可以将 Sony Xperia Z3 用作扫描仪,但必须购买/构建一个用于测试的信标.

So in your case you should still be able to use your Sony Xperia Z3 as a Scanner, but will have to buy/build a beacon for testing.

这篇关于如何通过蓝牙将串行数据发送到未指定的设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆