0

我已经坚持了好几天了。我想出了一个代码来向我的蓝牙模块发送数据字节。我没有收到任何错误,但我不确定数据是否实际正在发送。我用示波器检查了模块上的 RX 和 TX 引脚,但什么也没得到。有谁知道出了什么问题?这是我的代码:

import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Modes extends Activity {
    Button gaming;
    Button graphics;
    Button sound;
    Button text;
    BluetoothDevice mmDevice;
    BluetoothSocket mmSocket;
    OutputStream mmOutputStream;
    BluetoothAdapter mBluetoothAdapter;
    byte val;
    private final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modes);
        setUp();
        gaming.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View v) {
                System.out.println("gkghfgh");
                val = 0;
                try{
                    connect();
                    senData();
                 }catch(IOException ex){}

            }
        });

        graphics.setOnClickListener(new View.OnClickListener() {
             @Override
            public void onClick(View v) {
                    val = 1;
                try{
                      connect();
                    senData();
                }catch(IOException ex){}

            }
        });

        sound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                val = 8;
                try{
                    connect();
                    senData();
                }catch(IOException ex){}
            }
        });

         text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                val = 9;
                try{
                    connect();
                    senData();
                }catch(IOException ex){}
                startActivity(new Intent(Modes.this,Text.class));
            }
         });
     }

    private void setUp() {
        gaming = (Button) findViewById(R.id.button1);
        graphics = (Button) findViewById(R.id.button2);
        sound = (Button) findViewById(R.id.button3);
        text = (Button) findViewById(R.id.button4);
        gaming.setText("Gaming");
        graphics.setText("Graphics");
        sound.setText("Sound");
        text.setText("Text");
     }

    void connect() throws IOException{
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if(pairedDevices.size() > 0) {
            for(BluetoothDevice device : pairedDevices) {
                if(device.getName().equals("RN42-58C1")) {
                    mmDevice = device;
                    break;
                }
            }
         }
        mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);      
        mmSocket.connect();

    }

    void senData() throws IOException{
        mmOutputStream.write(val);
        System.out.println("Data Sent");
    }
 }
4

2 回答 2

0

尝试调用“mmOutputStream.flush();” 在“mmOutputStream.write(val);”之后。

于 2013-07-12T16:55:21.583 回答
0

我认为问题出在您的蓝牙设备名称上。您需要输入 12 位数字的蓝牙 ID。具有字母数字值。

希望有效。祝你好运。

于 2013-04-09T18:49:47.600 回答