嗨,有人可以帮我这个吗?
答案 0 :(得分:0)
我认为使用 BLE(蓝牙法能量)你无法从Android设备发送数据,但你只能接收 BLE DATA ,如 UUID,MajorId ,MinorID 您必须从 iphone 或任何其他正在发送数据的设备继续发送数据。如果你想收到数据,那么尝试从here下载含酸的代码并享受: - )
答案 1 :(得分:0)
这就是我的工作方式 -
输入流,输出流,发送和接收命令的ConnectedThread类
private class ConnectedThread extends Thread {
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = new DataInputStream(tmpIn);
mmOutStream = new DataOutputStream(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
if(mmInStream.available() > 0)
{
bytes = mmInStream.read(buffer);
if(mHandler == null)
mHandler = new BBHandler(MyFragment.this);
else
mHandler.setTarget(MyFragment.this);
mHandler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
}
}
catch (IOException e)
{
Log.e(TAG, "BLE: Error reading from mmInStream");
setSetting("STATUS", "Disconnected");
break;
}
catch (NullPointerException e)
{
Log.e(TAG, "BLE: Null Pointer Exception mmInStream read or mHandler");
}
}
}
public void writeByte(char[] buffer)
{
for(int k=0; k < buffer.length; k++)
{
try
{
mmOutStream.writeByte(buffer[k]);
}
catch (IOException e)
{
Log.e(TAG, "BLE: IOException during writeByte", e);
}
catch (NullPointerException e)
{
Log.e(TAG, "BLE: NullPointerException during writeByte", e);
}
}
}
public void cancel() {
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "BlackBox: close() of connect socket failed", e);
}
}
}
这是BBHandler类 -
static class BBHandler extends Handler{
WeakReference<MyFragment> mFrag;
BBHandler(MyFragment aFragment) {
mFrag = new WeakReference<MyFragment>(aFragment);
}
public void setTarget(MyFragment aFragment) {
mFrag.clear();
mFrag = new WeakReference<MyFragment>(aFragment);
}
@Override
public void handleMessage(Message message) {
MyFragment theFrag = mFrag.get();
if (theFrag != null) {
theFrag.blackBoxData(message);
}
}
}
注意: MyFragment
是您自己的片段,blackBoxData
是调用执行步骤的方法。你的所有逻辑都应该在这里。