面对从Android手机到笔记本电脑读取加速度计数据的难度(使用bluecove)

时间:2016-03-20 18:41:24

标签: java android bluetooth accelerometer bluecove

我想将实时加速度计数据从我的Android手机发送到我的笔记本电脑,这样我就可以使用这些数据进行进一步分析,但我这样做面临一些挑战...我无法坚持连接在机器人方面得到

W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
03-20 23:30:19.881 10050-13445/com.accel.bluetoothconnect W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
03-20 23:30:19.881 10050-13445/com.accel.bluetoothconnect W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:1016)
03-20 23:30:19.881 10050-13445/com.accel.bluetoothconnect W/System.err:     at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:973)
03-20 23:30:19.881 10050-13445/com.accel.bluetoothconnect W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:662)
03-20 23:30:19.881 10050-13445/com.accel.bluetoothconnect W/System.err:     at com.accel.bluetoothconnect.MainActivity$ConnectingThread.run(MainActivity.java:446)
03-20 23:30:19.951 10050-10050/com.accel.bluetoothconnect W/Accel Value(X)->: 0.0
03-20 23:30:19.951 10050-10050/com.accel.bluetoothconnect W/Accel Value(Y)->: 0.0
03-20 23:30:19.951 10050-10050/com.accel.bluetoothconnect W/Accel Value(Z)->: 10.289082
03-20 23:30:20.151 10050-10050/com.accel.bluetoothconnect W/Accel Value(X)->: 0.0
03-20 23:30:20.151 10050-10050/com.accel.bluetoothconnect W/Accel Value(Y)->: 0.0
03-20 23:30:20.151 10050-10050/com.accel.bluetoothconnect W/Accel Value(Z)->: 10.271125
03-20 23:30:20.351 10050-10050/com.accel.bluetoothconnect W/Accel Value(X)->: 0.0
03-20 23:30:20.351 10050-10050/com.accel.bluetoothconnect W/Accel Value(Y)->: 0.0
03-20 23:30:20.351 10050-10050/com.accel.bluetoothconnect W/Accel Value(Z)->: 10.289082

然而,在运行bluecove代码的笔记本电脑端,它能够连接并且线程等待数据。

BlueCove version 2.1.1-SNAPSHOT on winsock
0000110100001000800000805f9b34fb
waiting for connection...
waiting for connection...
waiting for input
finish process

我正在使用的代码,

对于Android -

public class MainActivity extends Activity implements SensorEventListener  {
    private final static UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
    private static final String TAG="BluetoothConnector";
    private static final int REQUEST_ENABLE_BT = 1;
    private static final int DISCOVERABLE_BT_REQUEST_CODE = 2;
    private static final int DISCOVERABLE_DURATION = 300;
    public static final int EXIT_CMD = -1;
    int conn_flag = 0;
    private Button onBtn;
    private Button offBtn;
    private Button listBtn;
    private Button findBtn;
    private TextView text;
    private BluetoothAdapter myBluetoothAdapter;
    private Set<BluetoothDevice> pairedDevices;
    private ListView myListView;
    private ArrayAdapter<String> BTArrayAdapter;
    BluetoothDevice bluetoothDevice;

private float lastX, lastY, lastZ;

private SensorManager sensorManager;
private Sensor accelerometer;

private float deltaXMax = 0;
private float deltaYMax = 0;
private float deltaZMax = 0;

private float deltaX = 0;
private float deltaY = 0;
private float deltaZ = 0;

private float vibrateThreshold = 0;

private TextView currentX, currentY, currentZ, maxX, maxY, maxZ;

public Vibrator v;



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

    // take an instance of BluetoothAdapter - Bluetooth radio
    myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if(myBluetoothAdapter == null) {
        onBtn.setEnabled(false);
        offBtn.setEnabled(false);
        listBtn.setEnabled(false);
        findBtn.setEnabled(false);
        text.setText("Status: not supported");

        Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth",
                Toast.LENGTH_LONG).show();
    } else {
        text = (TextView) findViewById(R.id.text);
        onBtn = (Button)findViewById(R.id.turnOn);
        onBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                on(v);
            }
        });

        offBtn = (Button)findViewById(R.id.turnOff);
        offBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                off(v);
            }
        });

        listBtn = (Button)findViewById(R.id.paired);
        listBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                list(v);
            }
        });

        findBtn = (Button)findViewById(R.id.search);
        findBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                find(v);
            }
        });

        myListView = (ListView)findViewById(R.id.listView1);

        // create the arrayAdapter that contains the BTDevices, and set it to the ListView
        BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        myListView.setAdapter(BTArrayAdapter);

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // ListView Clicked item value
                String  itemValue = (String) myListView.getItemAtPosition(position);

                String MAC = itemValue.substring(itemValue.length() - 17);

                bluetoothDevice = myBluetoothAdapter.getRemoteDevice(MAC);

                // Initiate a connection request in a separate thread
                ConnectingThread t = new ConnectingThread(bluetoothDevice);
                t.start();
            }
        });
    }

    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
        // success! we have an accelerometer

        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        vibrateThreshold = accelerometer.getMaximumRange() / 2;
    } else {
        // fai! we dont have an accelerometer!
    }

    //initialize vibration
    v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
}

public void on(View view){
    if (!myBluetoothAdapter.isEnabled()) {
        Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);

        Toast.makeText(getApplicationContext(),"Bluetooth turned on" ,
                Toast.LENGTH_LONG).show();
    }
    else{
        Toast.makeText(getApplicationContext(),"Bluetooth is already on",
                Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if(requestCode == REQUEST_ENABLE_BT){
        if(myBluetoothAdapter.isEnabled()) {
            text.setText("Status: Enabled");
            Toast.makeText(getApplicationContext(), "Ha! Bluetooth is now enabled." +
                            "\n" + "Scanning for remote Bluetooth devices...",
                    Toast.LENGTH_SHORT).show();

            // To discover remote Bluetooth devices
            discoverDevices();

            // Make local device discoverable by other devices
            makeDiscoverable();

            // Start a thread to create a  server socket to listen
            // for connection request
            ListeningThread t = new ListeningThread();
            t.start();
        } else {
            text.setText("Status: Disabled");
        }
    }else if (requestCode == DISCOVERABLE_BT_REQUEST_CODE){

        if (resultCode == DISCOVERABLE_DURATION){
            Toast.makeText(getApplicationContext(), "Your device is now discoverable by other devices for " +
                            DISCOVERABLE_DURATION + " seconds",
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Fail to enable discoverability on your device.",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

protected void discoverDevices(){
    // To scan for remote Bluetooth devices
    if (myBluetoothAdapter.startDiscovery()) {
        Toast.makeText(getApplicationContext(), "Discovering other bluetooth devices...",
                Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getApplicationContext(), "Discovery failed to start.",
                Toast.LENGTH_SHORT).show();
    }
}

protected void makeDiscoverable(){
    // Make local device discoverable
    Intent discoverableIntent = new
            Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVERABLE_DURATION);
    startActivityForResult(discoverableIntent, DISCOVERABLE_BT_REQUEST_CODE);
}


public void list(View view){
    // get paired devices
    pairedDevices = myBluetoothAdapter.getBondedDevices();

    // put it's one to the adapter
    for(BluetoothDevice device : pairedDevices)
        BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());

    Toast.makeText(getApplicationContext(),"Show Paired Devices",
            Toast.LENGTH_SHORT).show();

}

final BroadcastReceiver bReceiver = 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 the MAC address of the object to the arrayAdapter
            BTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            BTArrayAdapter.notifyDataSetChanged();
        }
        else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            conn_flag=1;
        }
    }
};

public void find(View view) {
    if (myBluetoothAdapter.isDiscovering()) {
        // the button is pressed when it discovers, so cancel the discovery
        myBluetoothAdapter.cancelDiscovery();
    }
    else {
        BTArrayAdapter.clear();
        myBluetoothAdapter.startDiscovery();

        registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    }
}

public void off(View view){
    myBluetoothAdapter.disable();
    text.setText("Status: Disconnected");

    Toast.makeText(getApplicationContext(),"Bluetooth turned off",
            Toast.LENGTH_LONG).show();
}

@Override
protected void onResume() {
    super.onResume();
    sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    // Register the BroadcastReceiver for ACTION_FOUND
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(bReceiver, filter);
}

@Override
protected void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
    this.unregisterReceiver(bReceiver);
}

@Override
public void onSensorChanged(SensorEvent event) {
    Log.w("Accel Value(X)->", String.valueOf(deltaX));
    Log.w("Accel Value(Y)->", String.valueOf(deltaY));
    Log.w("Accel Value(Z)->", String.valueOf(deltaZ));
        if(conn_flag == 1) {
            Log.w("Data Transfer", "Finally transferring data");
            ConnectingThread ct = new ConnectingThread(bluetoothDevice);
            ct.write((int) deltaX);
        }
    // get the change of the x,y,z values of the accelerometer
    deltaX = Math.abs(lastX - event.values[0]);
    deltaY = Math.abs(lastY - event.values[1]);
    deltaZ = Math.abs(lastZ - event.values[2]);

    // if the change is below 2, it is just plain noise
    if (deltaX < 2)
        deltaX = 0;
    if (deltaY < 2)
        deltaY = 0;
    if ((deltaZ > vibrateThreshold) || (deltaY > vibrateThreshold) || (deltaZ > vibrateThreshold)) {
        v.vibrate(50);
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

private class ListeningThread extends Thread {
    private final BluetoothServerSocket bluetoothServerSocket;

    public ListeningThread() {
        BluetoothServerSocket temp = null;
        try {
            temp = myBluetoothAdapter.listenUsingRfcommWithServiceRecord(getString(R.string.app_name), uuid);

        } catch (IOException e) {
            e.printStackTrace();
        }
        bluetoothServerSocket = temp;
    }

    public void run() {
        BluetoothSocket bluetoothSocket;
        // This will block while listening until a BluetoothSocket is returned
        // or an exception occurs
        while (true) {
            try {
                bluetoothSocket = bluetoothServerSocket.accept();
            } catch (IOException e) {
                break;
            }
            // If a connection is accepted
            if (bluetoothSocket != null) {

                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(), "A connection has been accepted.",
                                Toast.LENGTH_SHORT).show();
                    }
                });

                // Code to manage the connection in a separate thread
               /*
                   manageBluetoothConnection(bluetoothSocket);
               */

                try {
                    bluetoothServerSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    // Cancel the listening socket and terminate the thread
    public void cancel() {
        try {
            bluetoothServerSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private class ConnectingThread extends Thread {
    private BluetoothSocket bluetoothSocket;
    private final BluetoothDevice bluetoothDevice;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectingThread(BluetoothDevice device) {

        BluetoothSocket temp = null;
        bluetoothDevice = device;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {
            e.printStackTrace();
        }
        bluetoothSocket = temp;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the BluetoothSocket input and output streams
        try {
            tmpIn = bluetoothSocket.getInputStream();
            tmpOut = bluetoothSocket.getOutputStream();
        } catch (IOException e) {
            Log.e(TAG, "temp sockets not created", e);
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        myBluetoothAdapter.cancelDiscovery();

        try {
            // This will block until it succeeds in connecting to the device
            // through the bluetoothSocket or throws an exception
            bluetoothSocket.connect();
        } catch (IOException connectException) {
            connectException.printStackTrace();
            try {
                Log.e("","trying fallback...");

                bluetoothSocket =(BluetoothSocket) bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(bluetoothDevice,1);
                bluetoothSocket.connect();

                Log.e("","Connected");
            }
            catch (Exception e2) {
                Log.e("", "Couldn't establish Bluetooth connection!");
            }
          }
    }

    public void write(int out) {
        try {
            mmOutStream.write(out);
            } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

    // Cancel an open connection and terminate the thread
    public void cancel() {
        try {
            mmOutStream.write(EXIT_CMD);
            bluetoothSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}}

Android清单 -

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.VIBRATE" />

对于后备代码,我提到了 - getBluetoothService() called with no BluetoothManagerCallback for Android Nexus 5

和笔记本电脑方面(bluecove) -

WaitThread.java -

public class WaitThread implements Runnable{

    /** Constructor */
    public WaitThread() {
    }

    @Override
    public void run() {
        waitForConnection();        
    }

    /** Waiting for connection from devices */
    private void waitForConnection() {
        // retrieve the local Bluetooth device object
        LocalDevice local = null;

        StreamConnectionNotifier notifier;
        StreamConnection connection = null;

        // setup the server to listen for connection
        try {
            local = LocalDevice.getLocalDevice();
            local.setDiscoverable(DiscoveryAgent.GIAC);

            //UUID uuid = new UUID("04c6093b00001000800000805f9b34fb", false);
            UUID uuid = new UUID("0000110100001000800000805f9b34fb", false);
            System.out.println(uuid.toString());

            String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
            notifier = (StreamConnectionNotifier)Connector.open(url);
        } catch (BluetoothStateException e) {
            System.out.println("Bluetooth is not turned on.");
            e.printStackTrace();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        // waiting for connection
        while(true) {
            try {
                System.out.println("waiting for connection...");
                connection = notifier.acceptAndOpen();

                Thread processThread = new Thread(new ProcessConnectionThread(connection));
                processThread.start();

            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
    }
}

和ProcessConnectionThread.java -

public class ProcessConnectionThread implements Runnable{

    private StreamConnection mConnection;

    // Constant that indicate command from devices
    private static final int EXIT_CMD = -1;
    private static final int KEY_RIGHT = 1;
    private static final int KEY_LEFT = 2;

    public ProcessConnectionThread(StreamConnection connection)
    {
        mConnection = connection;
    }

    @Override
    public void run() {
        try {

            // prepare to receive data
            InputStream inputStream = mConnection.openInputStream();

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

            while (true) {
                int command = inputStream.read();

                if (command == EXIT_CMD)
                {   
                    System.out.println("finish process");
                    break;
                }

                processCommand(command);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Process the command from client
     * @param command the command code
     */
    private void processCommand(int command) {
        try {
            System.out.println("Value sent is: "+command);
            } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

现在我只是想在int中传递x轴数据(暂时)。请帮我解决。此外,这是我的第一个Android应用程序,我试图连接各种网站的作品,所以请求大家请轻松与我。

0 个答案:

没有答案