1

Bluetooth SPP 接続を含む Android アプリケーションを開発しています。Android から送信された、PC 上の RFCOMM からの文字列の受信に関する投稿は、本当に役に立ちました。

Eclipse のログでさえ、「6 バイトのうち 6 バイトを書き込みました」と何度も言ってから、ソケットが閉じられます。しかし、ハイパーターミナルを使用して PC で (送信したものは何でも) 受信したいと考えています。それ、どうやったら出来るの?コードで仮想 COM ポートを指定するには?

USBデバッグモードでのテストにSamsung SGH-T759を使用しています。

これが私のコードです:

public class BluetoothActivity extends Activity {

private int bluetooth = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth);
    TextView text = (TextView) findViewById(R.id.text);
    text.setText("Click on the button to access devices through Bluetooth");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_bluetooth, menu);
    return true;
}

public void bluetoothActivate(View view) {
    int REQUEST_ENABLE_BT = 1;
    int RESULT_ENABLE_BT  = 0;
    //TextView text = (TextView) findViewById(R.id.text);
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    System.out.println("Button clicked...");
    if (mBluetoothAdapter == null) {
        //txtView.setText("This device does not support Bluetooth");
        CharSequence txt = "This device does not support Bluetooth";
        Toast toast = Toast.makeText(getApplicationContext(), txt, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();

    }
    else {
        //CharSequence text = "Bluetooth is supported!!!";
        System.out.println("Bluetooth is supported!!!");
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            onActivityResult(REQUEST_ENABLE_BT, RESULT_ENABLE_BT, enableBtIntent);
        }
        else 
            bluetooth = 1;

        device_access(view);
    }
}

public void device_access(View view) {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    TextView text = (TextView) findViewById(R.id.text);
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    Spinner btDevices = (Spinner) findViewById(R.id.btDevices);

    System.out.println("Bluetooth devices...");

    if (bluetooth == 1)
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            System.out.println("Bluetooth paired devices...");
            final ArrayAdapter<CharSequence> mArrayAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1);
            ArrayAdapter<CharSequence> deviceName = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1);
            mArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            deviceName.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            for (BluetoothDevice device : pairedDevices) {

                // Add the name and address to an array adapter to show in a ListView
                mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                deviceName.add(device.getName());
            }
            btDevices.setVisibility(1);
            btDevices.setAdapter(mArrayAdapter);
            //txtView.append("\nPaired Bluetooth Devices Found...");


            /*// Create a BroadcastReceiver for ACTION_FOUND
            final BroadcastReceiver mReceiver = new BroadcastReceiver() {
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    // When discovery finds a device
                    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                        // Get the BluetoothDevice object from the Intent
                        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        // Add the name and address to an array adapter to show in a ListView
                        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                    }
                }
            };
            // Register the BroadcastReceiver
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
             */         

            String[] dvc = btDevices.getSelectedItem().toString().split("\n");
            final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection
            String mac = "90:00:4E:DC:41:9D"; //my laptop's mac adress
            //mac = dvc[1];
            text.append("\n Data sent to " + btDevices.getSelectedItem().toString());
            BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired


            // Get a BluetoothSocket to connect with the given BluetoothDevice
            BluetoothSocket socket = null;
            OutputStream out = null;
            InputStream inp = null;
            //try {
            //socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
            Method m;
            try {
                m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                socket = (BluetoothSocket) m.invoke(device, 1); 
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //}catch (IOException e) {}

            try {     
                mBluetoothAdapter.cancelDiscovery();
                socket.connect(); 
                out = socket.getOutputStream();
                inp = socket.getInputStream();
                //now you can use out to send output via out.write
                String outputValue = "Hi...\n",inputValue;
                byte[] op = outputValue.getBytes(),buffer = null;
                int inpBytes;
                for (int i=0; i<1000000; i++) {
                    out.write(op);
                    out.flush();
                }   
                System.out.println("Data written!!");
                /*while (true) {
                    try {
                        // Read from the InputStream
                        inpBytes = inp.read(buffer);

                        inputValue = buffer.toString();
                        text.append(inpBytes+ " " + inputValue);

                    } catch (IOException e) {

                    }

                }*/ }catch (IOException e) {} finally {
                    try {
                        socket.close();
                        System.out.println("Socket closed...");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                }
        }

}

}

4

1 に答える 1

0

私自身が解決しました。listenUsingRfcommWithServiceRecord(NAME,SERIAL_UUID) を使用して、携帯電話の SPP 機能をラップトップに登録する必要がありました。次に、私のラップトップは私の電話との SPP 通信用の COM ポートを割り当てました。次に、このポートを AccessPort137 (ハイパーターミナルに似ていて、いくつかの点でそれより優れている別のアプリケーション) で指定し、適切に通信を確立しました。私の作業コードは次のとおりです。

package com.example.bluetoothtest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.UUID;

import com.example.bluetoothtest.R;
import com.example.bluetoothtest.DynamicGraph;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class BluetoothActivity extends Activity {

    public final static String EXTRA_MESSAGE = "com.example.bluetoothtest.MESSAGE";
    public int bluetooth = 0, mState = 0;
    public boolean runThread = false;
    private TextView text;
    public static final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection
    public static final int STATE_DISCONNECTED = 0;
    public static final int STATE_CONNECTED = 1;
    public static final int MESSAGE_READ = 2;
    public static final int START_INTENT = 3;
    public static final int REQ_CODE_DYNAMIC = 4;

    public static BluetoothAdapter mBluetoothAdapter = null;
    public static BluetoothDevice device = null;

    BluetoothServerSocket mmServerSocket = null;
    BluetoothSocket socket = null,finalSocket = null;
    OutputStream out = null;
    //InputStream aStream = null;
    InputStreamReader aReader = null;
    BufferedReader mBufferedReader = null;

    ConnectedThread con = null;
    AcceptThread accept = null;
    ConnectThread connect = null;

    Intent intent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth);

        if (con != null) {con.cancel(); con = null;}

        mState = STATE_DISCONNECTED;

        // Start the thread to listen on a BluetoothServerSocket
        if (accept!= null) {
            accept = null;
        }


        text = (TextView) findViewById(R.id.text);
        text.setText("Click on the button to access devices through Bluetooth");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_bluetooth, menu);
        return true;
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        runThread = false;
    }

    public void bluetoothActivate(View view) {
        int REQUEST_ENABLE_BT = 1;
        int RESULT_ENABLE_BT  = 0;

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        System.out.println("Button clicked...");
        if (mBluetoothAdapter == null) {

            CharSequence txt = "This device does not support Bluetooth";
            Toast toast = Toast.makeText(getApplicationContext(), txt, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();

        }
        else {

            System.out.println("Bluetooth is supported!!!");
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                onActivityResult(REQUEST_ENABLE_BT, RESULT_ENABLE_BT, enableBtIntent);
            }
            else 
                bluetooth = 1;

            device_access(view);
        }
    }

    public void bluetoothDisconnect(View view) {

        runThread = false;  
        con.cancel();
        //connect.cancel();
        System.out.println("Connection disconnected");
        text.append("\n Connection disconnected");
    }

    public void device_access(View view) {

        System.out.println("Bluetooth devices...");

        if (bluetooth == 1)  {

            //String mac = "F0:08:F1:36:D3:5B"; //my other phone's mac adress
            String mac = "90:00:4E:DC:41:9D"; //my laptop's mac adress
            //String mac = "A0:4E:04:B8:1D:62";
            //mac = dvc[1];
            //text.append("\n Data sent to " + btDevices.getSelectedItem().toString());

            //device = mBluetoothAdapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired

            //text.append("device = " + device);
            text.append("\n Bluetooth started...");
            System.out.println("Bluetooth Started...");
            //ensureDiscoverable();

            String backup_file = "dynamic_bluetooth.csv";

            intent = new Intent(this, DynamicGraph.class);
            intent.putExtra(EXTRA_MESSAGE, backup_file);

            System.out.println("Dynamic intent about to start...");

            /*connect = new ConnectThread(device);
            connect.start();*/


                    CheckBox plotGraph = (CheckBox) findViewById(R.id.plotGraph);
            if (plotGraph.isChecked())
                startActivityForResult(intent,REQ_CODE_DYNAMIC);
            else {
                accept = new AcceptThread();
                accept.start();
            }


        }

    }


    private class ConnectThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) {
            this.mmDevice = device;
            BluetoothSocket tmp1 = null;
            try {
                tmp1 = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
            } catch (IOException e) {
                e.printStackTrace();
            }
            mmSocket = tmp1;
        }

        @Override
        public void run() {
            setName("ConnectThread");
            mBluetoothAdapter.cancelDiscovery();
            try {
                mmSocket.connect();
                System.out.println("Connected with the device");
            } catch (IOException e) {
                try {
                    mmSocket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

                return;

            }
            /*synchronized (PrinterService.this) {
                mConnectThread = null;
            }*/
            //connected(mmSocket, mmDevice);
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e("PrinterService", "close() of connect socket failed", e);
            }
        }
    }


    private void ensureDiscoverable() {

        if (mBluetoothAdapter.getScanMode() !=
                BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            startActivity(discoverableIntent);
        }
        System.out.println("Device set discoverable");

        text.append("\n Device set discoverable");
        return;
    }

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

            case MESSAGE_READ:
                /*byte[] readBuf = (byte[]) msg.obj;
                // construct a string from the valid bytes in the buffer
                String readMessage = new String(readBuf, 0, msg.arg1);*/
                //float readM = Float.parseFloat(readMessage);

                System.out.println("Into handler...");
                String readMessage = (String) msg.obj;
                text.append("\n" + readMessage);
                try{
                    float readM = Float.parseFloat(readMessage);
                    text.append(" " + readM);
                }catch (NumberFormatException e) {
                    text.append(" - Number Format Exception!!");
                    e.printStackTrace();
                }
                break;

            case START_INTENT:
                System.out.println("Dynamic intent about to start...");
                //startActivityForResult(intent,REQ_CODE_DYNAMIC);
                break;
            }

        }

    };

    private class AcceptThread extends Thread {

        public AcceptThread() {

            BluetoothServerSocket tmp = null;

            try {
                System.out.println("Listening...");
                tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("SPP", SERIAL_UUID);

            }catch (IOException e) {
                System.out.println("Listening Failed");


            }

            mmServerSocket = tmp;
            if (mmServerSocket != null)
                System.out.println("Server socket established: tmp = " + tmp);
            else
                System.out.println("Server socket NOT established: tmp = " + tmp);


        }

        public void run() {

            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try {     
                //socket = mmServerSocket.accept();

                System.out.println("AcceptThread Run");
                while (true) {
                    if ((mmServerSocket != null) && (mState != STATE_CONNECTED)) {  
                        try {
                            socket = mmServerSocket.accept();
                            device = socket.getRemoteDevice();
                        } catch (IOException e) {
                            System.out.println("Socket not received");
                            break;
                        }

                        if (socket!= null)  {

                            System.out.println("Device Address: " + device.getAddress());
                            runThread = true;
                            con = new ConnectedThread(socket);
                            con.start();
                            mState = STATE_CONNECTED;

                            break;
                        }
                    }
                    else {
                        System.out.println("Code incomplete. Repeat Listening");
                        break;
                    }


                }
            } 
            finally {
                /*try {
                                con.cancel();
                                mmServerSocket.close();
                                System.out.println("Socket closed...");
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                System.out.println("Server Socket close problem...");
                                e.printStackTrace();
                            } */
            }
        }

        public void cancel() {

            try {
                mmServerSocket.close();
                accept.start();
            } catch (IOException e) {

            }
        }
    }


    private class ConnectedThread extends Thread {

        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;


        public ConnectedThread(BluetoothSocket socket) {

            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {

            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {

            byte[] buffer = new byte[1024],readBuffer = new byte[1024];
            int bytesAvailable,readBufferPosition;
            char[] readMsg = new char[8192];
            /*try {
                aStream = socket.getInputStream();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("InputStream problem...");
                e.printStackTrace();
            }*/

            /*aReader = new InputStreamReader( mmInStream );
            mBufferedReader = new BufferedReader( aReader );*/ 

            String aString = "---";

            System.out.println("Waiting for input...");

            /*try {
                Thread.sleep(10000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                System.out.println("Sleeping problem...");
                e1.printStackTrace();
            }*/


            while (runThread) {
                try {

                    /*aReader = new InputStreamReader( mmInStream );

                    mBufferedReader = new BufferedReader( aReader );
                    //aString = mBufferedReader.readLine();

                    mBufferedReader.read(readMsg);
                    aString = new String(readMsg);
                    readMsg = new char[8192];
                    System.out.println(aString);*/

                    aString = "---";
                    byte delimiter = 'N';     //New line
                    readBuffer = new byte[1024];
                    readBufferPosition = 0;
                    bytesAvailable = mmInStream.read(buffer);
                    boolean copied = false;

                    if (bytesAvailable > 0)
                        System.out.println(bytesAvailable + " bytes available");
                    else
                        System.out.println("Bytes not available");

                    for(int i=0;i<bytesAvailable;i++)
                    {
                        byte b = buffer[i];
                        System.out.println("Byte = "+ b);
                        if(b == delimiter)
                        {
                            byte[] encodedBytes = new byte[readBufferPosition];
                            System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                            aString = new String(encodedBytes, "US-ASCII");
                            readBufferPosition = 0;
                            copied= true;
                            break;

                        }
                        else
                        {
                            readBuffer[readBufferPosition++] = b;
                        }
                    }

                    /*aString = new String(buffer);
                    aString = aString.trim();*/
                    //float rx_data = Float.parseFloat(aString);

                    System.out.println("aString = " + aString);


                    /*mHandler.obtainMessage(BluetoothActivity.MESSAGE_READ, bytes, -1, buffer)
                    .sendToTarget();*/

                    //buffer = new byte[1024];

                    if (copied && (aString != "---"))
                        mHandler.obtainMessage(BluetoothActivity.MESSAGE_READ, aString)
                        .sendToTarget();



                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.out.println("mBufferedReader problem...");
                    e.printStackTrace();
                    break;
                }

            }


            //cancel();

            /*out = socket.getOutputStream();
            //now you can use out to send output via out.write
            String outputValue = "Hi...\n",inputValue;
            byte[] op = outputValue.getBytes(),buffer = null;
            int inpBytes;
            for (int i=0; i<1000000; i++) {
                out.write(op);
                out.flush();
            }   
            System.out.println("Data written!!");


            while (true) {
    try {
        // Read from the InputStream
        inpBytes = inp.read(buffer);

        inputValue = buffer.toString();
        text.append(inpBytes+ " " + inputValue);

    } catch (IOException e) {

    }

} */
        }

        public void cancel() {
            try {

                /*aReader.close();
                mBufferedReader.close();*/
                runThread = false;
                mState = STATE_DISCONNECTED;
                mmSocket.close();
                mmServerSocket.close();
                System.out.println("Socket closed in thread...");
                accept = new AcceptThread();
                accept.start();
            } catch (IOException e) {
                System.out.println("Socket close problem...");
            }
        }

    }



}

双方向通信が必要な場合も、ConnectThread のコメントを外して実行します。値を受け取るだけで済みました。ということで、コメントアウトしました。ただし、ラップトップで携帯電話の COM ポートを確立するには、このスレッドを実行する必要があります。これは、Bluetooth SPP を使用するこのようなアプリケーションが電話で実行され、(この ConnectThread を使用して) ラップトップとペアリングしようとした場合にのみ、ラップトップが電話の SPP 機能を登録するためです。これを確認してラップトップ側から SPP を有効にするには、次の手順を実行します。

  1. タスクバーの Bluetooth アイコンを右クリックし、[Bluetooth デバイスの表示] をクリックします。

  2. 開いたウィンドウで、携帯電話を見つけ (この SPP アプリケーションを実行する前に、すでにラップトップとペアリングされている必要があることに注意してください)、右クリックして [プロパティ] に移動します。

  3. [サービス] タブに移動すると、listenUsingRfcommWithServiceRecord() メソッドの最初のパラメーターとして指定した名前で呼び出される新しいエントリが見つかります。私にとっては「SPP」です。

  4. その新しいサービスをチェックして、ラップトップと電話の間で SPP を有効にします。

  5. ここで、[ハードウェア] タブをチェックすると、ハイパーターミナルまたはそのようなアプリケーションで使用して通信を開始できる COM ポートが指定されます。

また、これはまだ Windows 8 ではできないことを覚えておいてください! ここで述べたことはすべて、Windows 7 に関するものです。ほとんど同じ方法ですが、Windows XP ではこれを実行していません。

私の解決策が役立つことを願っています!そして、これらの分野の専門家からの私の (初心者の) コードへのコメントを歓迎します。

于 2013-04-20T09:05:00.830 に答える