简单的蓝牙数据接收器的Andr​​oid [英] Simple Bluetooth data receiver Android

查看:121
本文介绍了简单的蓝牙数据接收器的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Android的编程初学者我有一个问题: 欲接收从连接到微处理器蓝牙/串行模块的数据(两个字节)。这些数据应被打印到屏幕和更新说每秒一次。我发现这已经:<一href="http://stackoverflow.com/questions/2660968/how-to-$p$pvent-android-bluetooth-rfcomm-connection-from-dying-immediately-after">How从后.connect()立即凋零prevent Android的蓝牙RFCOMM连接? 这应该是一个工作code,但什么也没有发生。 我改变了MAC地址为00:11:12:05:03:67,这应该与我的蓝牙模块对应

As a beginner in Android programming I have a question: I want to receive data (two bytes) from a bluetooth/serial module connected to a microprocessor. This data should be printed to the screen and updated say once per second. I found this already: How to prevent Android bluetooth RFCOMM connection from dying immediately after .connect()? Which should be a working code but nothing is happening. I changed the mac address to 00:11:12:05:03:67, which should correspond with my bluetooth module.

我现在去往完全错误的方向呢?我想我要的是很基本的:蓝牙只是一个简单的单向数据传输

Am I heading completely in the wrong direction with this? I guess what I want is quite basic: just a simple one way data transmission over bluetooth.

有什么想法会非常多AP preciated。

Any thoughts will be very much appreciated.

最良好的祝愿,

凯文

推荐答案

您已经提到有这个问题是非常有用的,因为它提供了一个解决方法的情况下通常调用 .createRfcommSocketToServiceRecord( )无法工作。我曾亲自使用的一个项目,我现在在做的解决方法解决方案。我做的是我尝试 .createRfcommSocketToServiceRecord()先打电话,如果失败,我的code,那么尝试用连接。 。的getClass()实现getMethod(createRfcommSocket,新的等级[] {int.class}); 解决方法。已经尝试了一些蓝牙转串口电路板,其中一些往往是有点尴尬,而后者反射法的工作原理,当 .createRfcommSocketToServiceRecord()做不。

The question you've referred to there is very useful as it provides a workaround for a situation where the usual call to .createRfcommSocketToServiceRecord() fails to work. I have personally used that workaround solution in a project I'm doing right now. What I do is I attempt the .createRfcommSocketToServiceRecord() call first, and if that fails, my code then attempts to connect with the .getClass().getMethod("createRfcommSocket", new Class[]{int.class}); workaround. Having experimented with a number of Bluetooth-to-serial PCBs, some of them tend to be a bit 'awkward' and the latter reflection method works when the .createRfcommSocketToServiceRecord() does not.

然而的......虽然你提到​​这个问题,确实提供了非常实用的紧凑一块code,我不认为这是对你最好的地方开始。你应该开始的位置是在 Android的蓝牙文档,这也解释了整个工艺极为出色,包括如何使用单独的主题处理发现,连接等,其实它真的很容易使用的蓝牙聊天源$ C ​​$ C 。利用这一点,你可以得到启动和运行,并连接到蓝牙串行模块的速度非常快。你只需要确保你的UUID更改为所需的串行端口配置文件(SPP):

However... although that question you referenced does provide a very useful compact piece of code, I don't think it's the best place for you to start. The place you should start is at Android's Bluetooth documentation, which explains the whole process extremely well, including how to use separate Threads to handle discovery, connection, etc. In fact it's really easy to get started using the Bluetooth Chat source code. Using that, you can get up and running and connected to a Bluetooth-serial module very quickly. You just need to make sure you change the UUID to that required for Serial Port Profile (SPP):

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

这将有希望成为足有一个简单的应用程序,它会跟你的蓝牙串口模块。蓝牙聊天示例应用程序还提供了对话的处理设备发现,配对,和所有的好东西,所以你不必做手脚硬编码在设备的MAC地址,就像你一直。

That will hopefully be enough to have a simple application that will talk to your Bluetooth serial module. The Bluetooth Chat example application also provides you with dialogs that handle device discovery, pairing, and all that good stuff, so you don't have to mess about hard-coding in your device's MAC address like you have been.

如果您有连接,那么你需要具体了解什么是实际发生的问题;也就是说,什么你得到,等等异常。

If you have problems connecting then you need to be specific about what's actually happening; i.e., what exceptions you're getting, and so forth.

这篇关于简单的蓝牙数据接收器的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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