Android 检查服务器上是否存在spp uuid

Android 检查服务器上是否存在spp uuid,android,bluetooth,Android,Bluetooth,嗨,我是android新手,我正在尝试创建一个蓝牙应用程序来控制我的aruino RC,一旦我单击四个按钮中的任何一个,我就会得到错误(检查服务器上是否存在spp uuid)。我四处寻找解决方案,但似乎没有一个对我有效,所以你能帮忙吗:) Java代码 XML代码 package com.example.omaradel.andromeda; import java.io.IOException; import java.io.OutputStream; import java.lan

嗨,我是android新手,我正在尝试创建一个蓝牙应用程序来控制我的aruino RC,一旦我单击四个按钮中的任何一个,我就会得到错误(检查服务器上是否存在spp uuid)。我四处寻找解决方案,但似乎没有一个对我有效,所以你能帮忙吗:)


Java代码
XML代码

package com.example.omaradel.andromeda;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.UUID;


import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String TAG = "Andromeda";


    private BluetoothAdapter btAdapter = null;
    private BluetoothSocket btSocket = null;
    private OutputStream outStream = null;


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

    // MAC-address of Bluetooth module (you must edit this line)
    private static String address = "64:5A:04:C9:8D:98";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
//        IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
//        IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
//        IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
//        this.registerReceiver(mReceiver, filter1);
//        this.registerReceiver(mReceiver, filter2);
//        this.registerReceiver(mReceiver, filter3);


        btAdapter = BluetoothAdapter.getDefaultAdapter();
        checkBTState();


    }
    public void forward (View v){
        sendData("2");
    }
    public void backward (View v){
        sendData("3");
    }
    public void right (View v){
        sendData("4");
    }
    public void left (View v){
        sendData("5");
    }
//    The BroadcastReceiver that listens for bluetooth broadcasts
//    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            TextView connectionStatus = (TextView) findViewById(R.id.status);
//            String action = intent.getAction();
//
//            if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//                //Do something if connected
//                Toast.makeText(getApplicationContext(), "Bluetooth Connected", Toast.LENGTH_SHORT).show();
//                connectionStatus.setText("connected");
//                connectionStatus.setTextColor(Color.parseColor("#00ff2a"));
//                connectionStatus.setBackgroundResource(R.drawable.connected);
//                connectionStatus.setPadding(16, 16, 16, 16);
//            } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//                //Do something if disconnected
//                Toast.makeText(getApplicationContext(), "Bluetooth Disconnected", Toast.LENGTH_SHORT).show();
//                connectionStatus.setText("disconnected");
//                connectionStatus.setTextColor(Color.parseColor("#ff3a4f"));
//                connectionStatus.setBackgroundResource(R.drawable.disconnected);
//                connectionStatus.setPadding(16, 16, 16, 16);
//            }
//            //else if...
//        }
//    };


    private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
        if(Build.VERSION.SDK_INT >= 10){
            try {
                final Method  m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
                return (BluetoothSocket) m.invoke(device, MY_UUID);
            } catch (Exception e) {
                Log.e(TAG, "Could not create Insecure RFComm Connection",e);
            }
        }
        return  device.createRfcommSocketToServiceRecord(MY_UUID);
    }

    @Override
    public void onResume() {
        super.onResume();

        Log.d(TAG, "...onResume - try connect...");

        // Set up a pointer to the remote node using it's address.
        BluetoothDevice device = btAdapter.getRemoteDevice(address);

        // Two things are needed to make a connection:
        //   A MAC address, which we got above.
        //   A Service ID or UUID.  In this case we are using the
        //     UUID for SPP.

        try {
            btSocket = createBluetoothSocket(device);
        } catch (IOException e1) {
            errorExit("Fatal Error", "In onResume() and socket create failed: " + e1.getMessage() + ".");
        }

        // Discovery is resource intensive.  Make sure it isn't going on
        // when you attempt to connect and pass your message.
        btAdapter.cancelDiscovery();

        // Establish the connection.  This will block until it connects.
        Log.d(TAG, "...Connecting...");
        try {
            btSocket.connect();
            Log.d(TAG, "...Connection ok...");
        } catch (IOException e) {
            try {
                btSocket.close();
            } catch (IOException e2) {
                errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
            }
        }

        // Create a data stream so we can talk to server.
        Log.d(TAG, "...Create Socket...");

        try {
            outStream = btSocket.getOutputStream();
        } catch (IOException e) {
            errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
        }
    }

    @Override
    public void onPause() {
        super.onPause();

        Log.d(TAG, "...In onPause()...");

        if (outStream != null) {
            try {
                outStream.flush();
            } catch (IOException e) {
                errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
            }
        }

        try     {
            btSocket.close();
        } catch (IOException e2) {
            errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
        }
    }

    private void checkBTState() {
        // Check for Bluetooth support and then check to make sure it is turned on
        // Emulator doesn't support Bluetooth and will return null
        if(btAdapter==null) {
            errorExit("Fatal Error", "Bluetooth not support");
        } else {
            if (btAdapter.isEnabled()) {
                Log.d(TAG, "...Bluetooth ON...");
            } else {
                //Prompt user to turn on Bluetooth
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, 1);
            }
        }
    }

    private void errorExit(String title, String message){
        Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
        finish();
    }

    private void sendData(String message) {
        byte[] msgBuffer = message.getBytes();

        Log.d(TAG, "...Send data: " + message + "...");

        try {
            outStream.write(msgBuffer);
        } catch (IOException e) {
            String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
            if (address.equals("00:00:00:00:00:00"))
                msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 35 in the java code";
            msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";

            errorExit("Fatal Error", msg);
        }
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:clickable="false"
    android:background="@drawable/greenbackground">
    <TextView
        android:layout_width="wrap_content"
        android:id="@+id/status"
        android:layout_height="wrap_content"
        android:text="Disconnected"
        android:textSize="18sp"
        android:textAllCaps="true"
        android:layout_margin="16dp"
        android:padding="16dp"
        android:textStyle="bold"
        android:textColor="#ff3a4f"
        android:background="@drawable/disconnected"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"
        android:text="0 cm/s"
        android:textSize="24sp"
        android:textColor="#ff0000"
        android:padding="8dp"
        android:layout_alignParentTop="true"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:id="@+id/left2"
        android:background="@drawable/left"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="24dp"
        android:layout_marginBottom="40dp"
        android:onClick="left"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:id="@+id/right"
        android:background="@drawable/right"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        android:layout_toRightOf="@id/left2"
        android:layout_marginLeft="24dp"
        android:onClick="right"


        />

    <Button
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        android:layout_height="80dp"
        android:layout_marginRight="32dp"
        android:id="@+id/backward"
        android:background="@drawable/down"
        android:onClick="backward"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:id="@+id/forward"
        android:background="@drawable/up"
        android:layout_alignParentRight="true"
        android:layout_marginRight="32dp"
        android:layout_above="@id/backward"
        android:layout_marginBottom="22dp"
        android:onClick="forward"

        />



</RelativeLayout>