2011-11-21 90 views
2

我编写的代码通过蓝牙连接并将数据从Android设备发送到另一个设备。通过蓝牙通过Android发送数据

这一切从这个类运行时工作。当我创建一个类的实例时,数据在连接创建完成之前被发送到输出流。

如何在发送数据之前检查连接是否已启动?

package com.flightscope.app.metawatch; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 

import com.flightscope.app.metawatch.Crc; 

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Typeface; 
import android.util.Log; 

public class MetawatchConnected { 

// public static final String TAG = "MetawatchConnected"; 

    private BluetoothSocket mSocket; 
    private BluetoothDevice mMetawatchDevice; 
    private final BluetoothAdapter mAdapter; 
// private final InputStream mInput; 
// private final OutputStream mOutput; 

    ConnectThread mConnectThread = null; 
    ConnectedThread mConnectedThread = null; 

    final private String TAG = "MetaWatch Connection Thread"; 


    public MetawatchConnected(String address) 
    { 
     Log.d(TAG, "Start"); 
     mAdapter = BluetoothAdapter.getDefaultAdapter(); 
     mMetawatchDevice = mAdapter.getRemoteDevice(address); 

     if(mConnectThread == null) 
     { 
      mConnectThread = new ConnectThread(mMetawatchDevice); 
      mConnectThread.start(); 
     } 

    } 

    public void getBmp() 
    { 
     Bitmap bmp = Bitmap.createBitmap(96, 96, Bitmap.Config.RGB_565); 
     Canvas canvas = new Canvas(bmp); 
     Paint paint = new Paint(); 

     paint.setColor(Color.BLACK); 
     paint.setTextSize(14); 
     Typeface tp = Typeface.DEFAULT; 
     paint.setTypeface(tp); 
     canvas.drawColor(Color.WHITE); 
     canvas.drawText("Carry Distance", 0, 14, 0, 50, paint); 

     int[] pixelArray = new int[96 * 96]; 
     bmp.getPixels(pixelArray, 0, 96, 0, 0, 96, 96); 

     int[] buffer = new int[1152]; 

     for(int i = 0; i < buffer.length; i++) 
     { 
      int[] bytes = new int[8]; 

      for(int j = 0; j < bytes.length; j++) 
      { 
       if(pixelArray[(i * 8) + j ] != Color.BLACK) 
       { 
        bytes[j] = 0; 
       } 
       else 
       { 
        bytes[j] = 1; 
       } 

       buffer[i] = (128 * bytes[7]) + (64 * bytes[6]) + (32* bytes[5]) + (16 * bytes[4]) + 
          (8 * bytes[3]) + (4 * bytes[2]) + (2 * bytes[1]) + (1 * bytes[0]); 

      } 

     } 

     //fillScreen(); 

     for(int z = 30; z < 96; z +=2) 
     { 
    //  int[] buffer = {0x01, 0x20, 0x40, 0x00, (i), 0xd8, 0xb6, 0x6d, 0xdb, 0xb6, 0x6d, 
    //      0xdb, 0xb6, 0x6d, 0xdb, 0xb6, 0x0d, (i + 1), 0x00, 0x00, 0x00, 0x00, 
    //      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 

      int[] send = new int[30]; 
      Log.d(TAG, ""+z); 
      send[0] = 0x01; 
      send[1] = 0x20; 
      send[2] = 0x40; 
      send[3] = 0x00; 
      send[4] = z; 
      for(int j = 0; j < 12; j++) 
      { 
       send[5+j] = buffer[((z * 12) + j)]; 
      } 

      send[17] = (z + 1); 
      for(int j = 0; j < 12; j++) 
      { 
       send[18+j] = buffer[(12 * z) + 12 + j]; 
      } 

      try 
      { 
       final Crc sendMsg = new Crc(send); 
       mConnectedThread.write(sendMsg.message); 
      } 
      catch(Exception e) { Log.d(TAG, "Write Exception"); } 
     } 






    } 

    public void fillScreen() 
    { 
     int[] bytes = new int[5]; 

     bytes[0] = 0x01; 
     bytes[1] = (byte) (bytes.length+2); // packet length 
     bytes[2] = 0x44; 
     bytes[3] = (byte) 0x02; 
     bytes[4] = (byte) 0x00; 
    } 



class ConnectThread extends Thread { 


    public ConnectThread(BluetoothDevice device) 
     { 
      BluetoothSocket temp = null; 
      mMetawatchDevice = device; 
      try 
      { 
       //temp = btDevice.createRfcommSocketToServiceRecord(myUUID); 

       Method m = mMetawatchDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
       temp = (BluetoothSocket) m.invoke(mMetawatchDevice, 1); 

      } //catch(IOException e) { } 
      catch (SecurityException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 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(); 
      } 
      mSocket = temp; 
     } 

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

      try 
      { 
       if(mSocket!=null){ 
        Log.d(TAG, "Attempting to Connect via Bluetooth"); 
       mSocket.connect(); 
       Log.d(TAG,"\nConnected via Bluetooth"); 

       mConnectedThread = new ConnectedThread(mSocket); 
       mConnectedThread.start(); 
       Log.d(TAG, "Connected Thread Started"); 
       } 
      } 
      catch (IOException connectException) 
      { 
       try 
       { 
        Log.d(TAG, "Attempt Close"); 
        mSocket.close(); 
        Log.d(TAG, "Socket Closed"); 
        Log.d(TAG, "Exception : "+connectException.getMessage()); 
       } 
       catch(IOException closeException) { Log.d(TAG, "Open and Close Failed"); }    
      } 
     } 

     public void cancel() 
     { 
      try 
      { 
       Log.d(TAG, "Attempting To Close ConnectingThread Socket"); 
       mSocket.close(); 
       Log.d(TAG, "ConnectingThread Socket Closed"); 
      } 
      catch(IOException e) { Log.d(TAG, "Failed To Close ConnectThread Socket"); } 
     } 
    } 

class ConnectedThread extends Thread { 

    private final BluetoothSocket mmSocket; 
    private final InputStream mmInput; 
    private final OutputStream mmOutput; 

    public ConnectedThread(BluetoothSocket socket) 
    { 
     mmSocket = socket; 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

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

     mmInput = tmpIn; 
     mmOutput = tmpOut; 
    } 

    public void run() 
    { 
     byte[] buffer = new byte[1024]; 
     int bytes; 

     while(true) 
     { 
      try 
      { 
       bytes = mmInput.read(buffer); 
       Log.d(TAG, "Received : "+bytes); 
      } 
      catch(IOException e) { break; } 
     } 
    } 

    public void write(byte[] bytes) 
    { 
     try 
     { 
      mmOutput.write(bytes); 
      Log.d(TAG, "Bytes Sent"); 
     } 
     catch (IOException e) { Log.d(TAG, "Bytes Not Sent"); } 
    } 

    public void cancel() 
    { 
     try 
     { 
      Log.d(TAG, "Attempting to Close ConnectedThread Socket"); 
      mmSocket.close(); 
      Log.d(TAG, "ConnectedThread Socket Closed"); 
     } 
     catch(IOException e) { Log.d(TAG, "ConnectedThread Failed To Close"); } 
    } 

} 




} 

MetawatchConnected会从不同的类中调用,然后之后,我从调用类

调用代码发送使用getBmp()数据

String address = mActiveProfile.metawatchAddressBluetooth; 

     if(address != null) 
     { 
      if(mMetawatchConnected == null) 
      { 
       mMetawatchConnected = new MetawatchConnected(address); 
      } 

     } 

       mMetawatchConnected.getBmp(); 
+1

我正在查找我最近的BT代码启用的活动......实际上发布您的当前代码(从BT连接阶段到BT连接/发送数据阶段的所有内容) – Jack

+0

发布您如何从其他class – Jack

+0

嘿,你可以在这里发表你的CRC课吗? – anddev

回答

2

看来你有一个同步问题。我在班上所要做的就是实现一个计时器。计时器基本上控制着一切,它检查BT是否连接,网络是否连接等等。但是我的活动需要长时间运行BT /网络连接,我不认为你的确如此。

为什么不在你的MetawatchConnected类中添加isConnected()方法?连接完成后,将其设置为true。那么你可以:

if (mMetawatchConnected.isConnected()) 
{ 

    mMetawatchConnected.getBmp(); 
} 

你仍然需要处理同步问题,如等待初始连接,但我认为你明白了。

+0

我做了最初的连接之后,我会打更多的电话,而不仅仅是那一个。我在哪里可以阅读如何等待初始连接。即它叫什么? –