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

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

问题描述

我将以下代码用于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似乎没有低级的Classic Bluetooth API,该API允许您执行广播类型的行为.这是有一定道理的,因为Android打算进入功率受限的设备,而有源无线电会消耗能量.如果需要使用Classic Bluetooth(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:

  • https://github.com/googlesamples/android-BluetoothAdvertisements
  • https://github.com/devunwired/accessory-samples/blob/master/bluetoothadvertiser

还请注意,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天全站免登陆