80,351
社区成员
发帖
与我相关
我的任务
分享
if (btAdapt.getState() == BluetoothAdapter.STATE_OFF)
{
// 如果蓝牙还没开启
Toast.makeText(Yepef.this, "请先打开蓝牙", 1000).show();
return;
}
if (connFlag!=1)
{
// 如果蓝牙还没连接
Toast.makeText(Yepef.this, "蓝牙未连接", 1000).show();
return;
}
byte[] outbyte = new byte[]{(byte)0x68,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,
(byte)0x69,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF};
try {
outStream = btSocket.getOutputStream();
outStream.write(outbyte);
outStream.flush();
//延时
inStream = btSocket.getInputStream();
//此处接收的代码如何写
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
备注:上面接收的代码我用如下代码,接收不全
byte[] buffer = new byte[1024];
int bytes=-1;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
while (true)
{
try {
// Read from the InputStream
while ((bytes = inStream.read(buffer)) >0)
{
byteStream.write(buffer, 0, bytes-1);
}
//以下代码始终没有执行
byte[] data = byteStream.toByteArray();
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Yxset.MESSAGE_READ, len, -1, data).sendToTarget();
Log.i(TAG, "ok");
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}