将数据发送到远程蓝牙设备

时间:2014-02-05 02:10:11

标签: android bluetooth

您是否知道如何将数据从Android手机发送到远程蓝牙设备(通过OutputStream),而无需等待InputStream读取所需数量的字节?

我尝试实现如下所示的代码,将数据发送到远程蓝牙设备,但没有发送任何内容。

编辑:已在connectedThread代码中添加:

ConnectedThread.java:

public class ConnectedThread extends Thread{
        public final BluetoothSocket mmSocket;
        public final InputStream mmInStream;
        public 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];  // buffer store for the stream
            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                } catch (IOException e) {
                    break;
                }
            }  
        }


        /* Call this from the main activity to send data to the remote device */
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) { }
        }   

public void sendData() throws IOException{
            write("1");
        }

ConnectThread.java:

public class ConnectThread extends Thread{
        public final BluetoothSocket mmSocket;
        public final BluetoothDevice mmDevice;
        public final BluetoothAdapter mmAdapter;

        BluetoothConnection mConnectedThread = new BluetoothConnection();

        public ConnectThread(BluetoothDevice device, BluetoothAdapter mAdapter) throws SecurityException, NoSuchMethodException,  IllegalArgumentException, IllegalAccessException, InvocationTargetException {
            mmAdapter = mAdapter;
            mmDevice = device = mmAdapter.getRemoteDevice("00:06:66:4F:90:F8");
            BluetoothSocket tmp = null;

        try{
            tmp = mmDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
            }catch(IOException e){
                System.out.println("Failed to create Rfcomm and connect to remote device");
            }
            mmSocket = tmp;
        }

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

            //sleep to allow time for bluetooth device to get ready 
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e5) {
                e5.printStackTrace();
            }

            //handler get message from arduino: that bluetooth device is ready 

            //Get BluetoothDevice object
            try{
                //Connect to device 
                mmSocket.connect();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }catch(IOException e){
            try{
                //Connect to device 
                mmSocket.connect();
            }catch(IOException e1){
                }
                return; 
        }
            return;
        }

            mConnectedThread.ConnectedThread(mmSocket);
        }

    }

BluetoothConnection.java:

public class BluetoothConnection extends Service {

public final BluetoothAdapter mAdapter = null;
public ConnectThread mConnectThread;
    public ConnectedThread mConnectedThread;

private final IBinder mBinder = new BluetoothService();

public class BluetoothService extends Binder{       
    BluetoothConnection getService(){
            return BluetoothConnection.this;
    }
}

@Override 
public IBinder onBind (Intent intent){
    Log.d("Blueooth Connection", "Intent Binder");
    return mBinder;
} 

public synchronized void connect(BluetoothDevice device, BluetoothAdapter mAdapter)     throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{

    try{
        mConnectThread = new ConnectThread(device, mAdapter);
        mConnectThread.start();

    }catch(SecurityException e){
        System.out.println("SecurityException at connect()");
    } catch(IllegalArgumentException e){
        System.out.println("IllegalArgumentException at connect()");
    }catch(NullPointerException e)
 {System.out.println("Null Pointer Exception at connect()");
    }
}

public synchronized void ConnectedThread(BluetoothSocket socket){
    mConnectedThread = new ConnectedThread(socket);
    mConnectedThread.start();
}

public void write(String out){
    //create temporary object 
    ConnectedThread r;
    synchronized(this){
        r = mConnectedThread;
    }           
    r.write(out);
}    

@Override
public void onDestroy() {
}
}

在另一个活动中: 我叫sendData();写“1”给Arduino。

1 个答案:

答案 0 :(得分:0)

编辑:

也许这不是造成问题的原因,但你不应该做这样的事情:

    //Get BluetoothDevice object
    try{
        //Connect to device 
        mmSocket.connect();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }catch(IOException e){
        try{
            //Connect to device 
            mmSocket.connect();
        }catch(IOException e1){
        }
        return; 
    }

您可能会以未连接的mmSocket结束,因为您只是忽略IOException并再次尝试。尝试在第一个try / catch块中e.printStackTrace()查看是否存在连接问题。

通常,您不应该只忽略异常,因为它们代表程序流程中的异常。在最坏的情况下,打印错误消息以帮助您进行调试。

编辑2:

请更改

public void write(byte[] bytes) {
    try {
        mmOutStream.write(bytes);
    } catch (IOException e) { }
}   

public void write(byte[] bytes) {
    try {
        mmOutStream.write(bytes);
    } catch (IOException e) { 
        e.printStackTrace();
    }
}   

并查看logcat输出以查看它是否显示错误。

相关问题