0

私は何日もこれにこだわっています。Bluetooth モジュールにデータのバイトを送信するコードを思いつきました。エラーは発生しませんが、データが実際に送信されているかどうかはわかりません。オシロスコープを使用してモジュールの 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

問題はBluetoothデバイス名にあると思います。12 桁の番号である Bluetooth ID を書き込む必要があります。英数字の値を持つ。

うまくいくことを願っています。幸運を。

于 2013-04-09T18:49:47.600 に答える