gpt4 book ai didi

java - 使用 Android 通过蓝牙发送数据

转载 作者:行者123 更新时间:2023-11-30 09:43:47 27 4
gpt4 key购买 nike

我编写了通过蓝牙连接 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();

最佳答案

您似乎遇到了同步问题。我在类里面必须做的是实现一个计时器。计时器基本上控制一切,它检查 BT 是否已连接,网络是否已连接等...但我的 Activity 需要长时间运行 BT/网络连接,我认为你的不需要。

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

if (mMetawatchConnected.isConnected())
{

mMetawatchConnected.getBmp();
}

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

关于java - 使用 Android 通过蓝牙发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8213495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com