通过蓝牙与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(); 

您似乎遇到了同步问题。 我class上要做的就是实现一个计时器。 计时器基本上控制一切,它检查BT是否连接,网络是否连接等…但我的活动需要长时间运行的BT /网络连接,我不认为你的。

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

 if (mMetawatchConnected.isConnected()) { mMetawatchConnected.getBmp(); } 

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