Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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
将Android Nexus One与Arduino接口+;布鲁斯米尔夫_Android_Bluetooth_Arduino_Bluesmirf - Fatal编程技术网

将Android Nexus One与Arduino接口+;布鲁斯米尔夫

将Android Nexus One与Arduino接口+;布鲁斯米尔夫,android,bluetooth,arduino,bluesmirf,Android,Bluetooth,Arduino,Bluesmirf,我对这一切都有点陌生,所以请容忍我-我真的很感谢你的帮助 我正在尝试将Android Nexus One与连接到BlueSmirf的arduino(Duemilanove)连接起来。我有一个程序,只需将字符串“Hello Bluetooth”输出到BlueSmirf连接到的任何设备。以下是Arduino项目: 无效设置(){ 序列号开始(115200); int i; } void loop(){Serial.print(“Hello Bluetooth!”); 延迟(1000); } 一个我的

我对这一切都有点陌生,所以请容忍我-我真的很感谢你的帮助

我正在尝试将Android Nexus One与连接到BlueSmirf的arduino(Duemilanove)连接起来。我有一个程序,只需将字符串“Hello Bluetooth”输出到BlueSmirf连接到的任何设备。以下是Arduino项目:

无效设置(){ 序列号开始(115200); int i; }

void loop(){Serial.print(“Hello Bluetooth!”); 延迟(1000); }

一个我的电脑BT终端,我可以看到信息,连接没有问题。问题在于我的android代码。我可以用android连接设备,但当我查看日志时,它没有显示“Hello Bluetooth”。以下是调试日志:


04-09 16:27:49.022:错误/BTArduino(17288):FireFly-2583已连接
04-09 16:27:49.022:错误/BTArduino(17288):开始连接插座
04-09 16:27:55.705:错误/BTArduino(17288):收到:16
04-09 16:27:56.702:错误/BTArduino(17288):收到:1
04-09 16:27:56.712:错误/BTArduino(17288):收到:15
04-09 16:27:57.702:错误/BTArduino(17288):收到:1
04-09 16:27:57.702:错误/BTArduino(17288):收到:15
04-09 16:27:58.704:错误/BTArduino(17288):收到:1
04-09 16:27:58.704:错误/BTArduino(17288):收到:15

等等

这是代码,我试图只输入相关代码,但如果您需要更多,请让我知道:

private class ConnectThread extends Thread {
    private final BluetoothSocket mySocket;
    private final BluetoothDevice myDevice;

    public ConnectThread(BluetoothDevice device) {
        myDevice = device;
        BluetoothSocket tmp = null;
        try {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "CONNECTION IN THREAD DIDNT WORK");
        }
        mySocket = tmp;
    }
    public void run() {
        Log.e(TAG, "STARTING TO CONNECT THE SOCKET");
        InputStream inStream = null;
        boolean run = false;
        //...More Connection code here...
更多相关代码如下所示:

        byte[] buffer = new byte[1024];
        int bytes;

        // handle Connection
        try {
            inStream = mySocket.getInputStream();
            while (run) {
                try {
                    bytes = inStream.read(buffer);
                    Log.e(TAG, "Received: " + bytes);
                } catch (IOException e3) {
                    Log.e(TAG, "disconnected");
                }
            }
我正在读取字节=流内读取(缓冲区)。我知道字节是一个整数,所以我尝试通过蓝牙发送整数,因为“字节”是一个整数,但仍然没有意义

它几乎看起来发送了错误的波特率。这是真的吗

任何帮助都将不胜感激。非常感谢。

您看到这个项目了吗?

Cheers

read()返回它成功读入缓冲区的字节数。因此,在这一行代码中:

bytes = inStream.read(buffer);
…您的消息将在
缓冲区
的第一个
字节中找到(假设其他所有内容都正确)。您可以将其转换为字符串,如下所示:

String message = new String(buffer, 0, bytes);

我在这里介绍了一些东西(编码、连接多个缓冲区等),但这应该可以让您开始了。

用writeln/readln组合试试。