问题描述
香港专业教育学院的书面code连接和发送数据通过蓝牙连接Android设备到另一个地方。
这一切工作从类中运行时。当我创建类的实例数据被发送到前连接创建完成输出流。
我如何检查连接到了发送数据之前?
包com.flightscope.app.metawatch;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口java.lang.reflect.InvocationTargetException;
进口java.lang.reflect.Method中;
进口com.flightscope.app.metawatch.Crc;
进口android.bluetooth.BluetoothAdapter;
进口android.bluetooth.BluetoothDevice;
进口android.bluetooth.BluetoothSocket;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.Typeface;
进口android.util.Log;
公共类MetawatchConnected {
//公共静态最终字符串变量=MetawatchConnected;
私有的BluetoothSocket mSocket;
私人BluetoothDevice类mMetawatchDevice;
私人最终BluetoothAdapter mAdapter;
//私人最终的InputStream mInput;
//私人最终的OutputStream mOutput;
ConnectThread mConnectThread = NULL;
ConnectedThread mConnectedThread = NULL;
最终私人字符串变量=MetaWatch连接螺纹;
公共MetawatchConnected(字符串地址)
{
Log.d(TAG,开始);
mAdapter = BluetoothAdapter.getDefaultAdapter();
mMetawatchDevice = mAdapter.getRemoteDevice(地址);
如果(mConnectThread == NULL)
{
mConnectThread =新ConnectThread(mMetawatchDevice);
mConnectThread.start();
}
}
公共无效getBmp()
{
BMP位= Bitmap.createBitmap(96,96,Bitmap.Config.RGB_565);
帆布油画=新的Canvas(BMP);
涂料粉刷=新的油漆();
paint.setColor(Color.BLACK);
paint.setTextSize(14);
字样TP = Typeface.DEFAULT;
paint.setTypeface(TP);
canvas.drawColor(Color.WHITE);
canvas.drawText(搬运距离,0,14,0,50,油漆);
INT [] pixelArray =新INT [96 * 96]。
bmp.getPixels(pixelArray,0,96,0,0,96,96);
INT []缓冲区=新的INT [1152];
的for(int i = 0; I< buffer.length;我++)
{
INT []字节=新INT [8]。
对于(INT J = 0; J< bytes.length; J ++)
{
如果(pixelArray [(I * 8)+ J]!= Color.BLACK)
{
字节[J] = 0;
}
其他
{
字节[J] = 1;
}
缓冲液[I] =(128 *字节[7])+(64 *字节[6])+(32 *字节[5])+(16 *字节[4])+
(8 *字节[3])+(4 *字节[2])+(2 *字节[1])+(1 *字节[0]);
}
}
// fillScreen();
对于(INT Z = 30; Z< 96; Z + = 2)
{
// INT []缓冲液= {0×01,为0x20,0X40,0x00时,(i)中,为0xD8,0xb6,0x6d,0xdb,0xb6,0x6d,
// 0xdb,0xb6,0x6d,0xdb,0xb6,0X0D,第(i + 1),0×00,为0x00,0x00时,0×00,
// 0×00,0×00,0×00,0×00,0×00,0×00,0×00,0×00};
INT [] =发送新的INT [30];
Log.d(TAG,+ Z);
发[0] = 0×01;
发[1] = 0x20的;
发[2] = 0X40;
发送[3] = 0×00;
发[4] = Z;
对于(INT J = 0; J< 12; J ++)
{
发[5 + J] =缓冲[((Z * 12)+ J)];
}
发送[17] =(Z + 1);
对于(INT J = 0; J< 12; J ++)
{
发[18 + J] =缓冲[(12 * Z)+ 12 + J]。
}
尝试
{
最后的CRC SENDMSG =新的CRC(发送);
mConnectedThread.write(sendMsg.message);
}
赶上(例外五){Log.d(TAG,编写异常); }
}
}
公共无效fillScreen()
{
INT []字节=新INT [5];
字节[0] = 0×01;
字节[1] =(字节)(bytes.length + 2); //数据包长度
字节[2] = 0x44;
字节[3] =(字节)0×02;
字节[4] =(字节)为0x00;
}
类ConnectThread继承Thread {
公共ConnectThread(BluetoothDevice类设备)
{
的BluetoothSocket TEMP = NULL;
mMetawatchDevice =设备;
尝试
{
//临时= btDevice.createRfcommSocketToServiceRecord(myUUID);
方法M = mMetawatchDevice.getClass()实现getMethod(createRfcommSocket,新的等级[] {} int.class)。
临时=(的BluetoothSocket)m.invoke(mMetawatchDevice,1);
} //赶上(IOException异常E){}
赶上(SecurityException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(NoSuchMethodException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(抛出:IllegalArgumentException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IllegalAccessException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(的InvocationTargetException E){
// TODO自动生成的catch块
e.printStackTrace();
}
mSocket =温度;
}
公共无效的run()
{
mAdapter.cancelDiscovery();
尝试
{
如果(mSocket!= NULL){
Log.d(TAG,试图通过蓝牙进行连接);
mSocket.connect();
Log.d(TAG,\通过蓝牙nConnected);
mConnectedThread =新ConnectedThread(mSocket);
mConnectedThread.start();
Log.d(TAG,连接线程启动);
}
}
赶上(IOException异常的ConnectException)
{
尝试
{
Log.d(TAG,尝试关闭);
mSocket.close();
Log.d(TAG,关闭socket);
Log.d(TAG,异常:+ connectException.getMessage());
}
赶上(IOException异常closeException){Log.d(TAG,打开和关闭失败); }
}
}
公共无效取消()
{
尝试
{
Log.d(TAG,试图关闭ConnectingThread插座);
mSocket.close();
Log.d(TAG,ConnectingThread插座关闭);
}
赶上(IOException异常E){Log.d(TAG,无法关闭ConnectThread插座); }
}
}
类ConnectedThread继承Thread {
私人最终的BluetoothSocket mmSocket;
私人最终的InputStream mmInput;
私人最终的OutputStream mmOutput;
公共ConnectedThread(的BluetoothSocket插座)
{
mmSocket =插座;
InputStream的tmpIn = NULL;
OutputStream的tmpOut = NULL;
尝试
{
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
}
赶上(IOException异常E){}
mmInput = tmpIn;
mmOutput = tmpOut;
}
公共无效的run()
{
byte []的缓冲区=新的字节[1024];
诠释字节;
而(真)
{
尝试
{
字节= mmInput.read(缓冲区);
Log.d(TAG,收稿日期:+字节);
}
赶上(IOException异常E){打破; }
}
}
公共无效写入(字节[]字节)
{
尝试
{
mmOutput.write(字节);
Log.d(TAG,发送字节);
}
赶上(IOException异常E){Log.d(TAG,字节未发送); }
}
公共无效取消()
{
尝试
{
Log.d(TAG,试图关闭ConnectedThread插座);
mmSocket.close();
Log.d(TAG,ConnectedThread插座关闭);
}
赶上(IOException异常E){Log.d(TAG,ConnectedThread未能收); }
}
}
}
MetawatchConnected会从不同的类调用,那么之后,我从发送调用类使用getBmp()数据。
调用code
字符串地址= mActiveProfile.metawatchAddressBluetooth;
如果(地址!= NULL)
{
如果(mMetawatchConnected == NULL)
{
mMetawatchConnected =新MetawatchConnected(地址);
}
}
mMetawatchConnected.getBmp();
看来你有一个同步的问题。我不得不这样做在我的课是实现一个定时器。计时器基本上控制了一切,它会检查是否BT连接,如果网络连接,等等...但需要我的活动长时间运行BT /网络连接,我不认为你做。
你为什么不添加 isConnected()
方法,你MetawatchConnected类?连接完成后,将其设置为true。然后,您可以:
如果(mMetawatchConnected.isConnected())
{
mMetawatchConnected.getBmp();
}
您仍然必须处理同步问题,如等待初始连接,但我想你明白了吧。
Ive written code that connects and sends data from an Android device to another via Bluetooth.
It all works when run from that class. When I create an instance of the class the data gets sent to the output stream before the connection creation has finished.
How do I check that the connection is up before sending data?
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 gets called from a different class, then right after that I send data using getBmp() from the calling class
Calling code
String address = mActiveProfile.metawatchAddressBluetooth;
if(address != null)
{
if(mMetawatchConnected == null)
{
mMetawatchConnected = new MetawatchConnected(address);
}
}
mMetawatchConnected.getBmp();
It seems that you have a synchronization issue. What I had to do in my class was implement a timer. The timer basically controls everything, it checks if BT is connected, if the network is connected, etc... But my activity required long running BT / network connections, I don't think yours does.
Why don't you add an isConnected()
method to your MetawatchConnected class? After connection is complete, set it to true. Then you can:
if (mMetawatchConnected.isConnected())
{
mMetawatchConnected.getBmp();
}
You will still have to deal with synchronization issues such as waiting for the initial connection, but I think you get the point.
这篇关于通过蓝牙与Android正在发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!