我有一个简单的蓝牙代码来激活继电器。 它来自一个旧的项目,它曾经工作得很好,但我过去只是发送一点,这次我必须发送字符串。 使用此代码,我可以发送六个命令,然后设备不再响应。当我突然停止应用程序时,之前所有“未执行”的命令都由继电器板执行。如果我改变缓冲区的大小,它似乎对我可以发送的消息数量有一些影响。 我正在使用旧手机和Android 2和API 10的控制器 知道我应该调查的地方吗? 感谢
public class Menuuu extends Activity {
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
String a;
Editable b;
final byte[] buffer = new byte[2048];
private ToggleButton T1;
private static final String TAG = "MyActivity";
public void write_buf(byte[] buffer) {
try {
mmOutputStream.write(buffer, 0, 1024 );
mmOutputStream.flush();
} catch (IOException e) { }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ToggleButton T1 = (ToggleButton) findViewById(R.id.Button1);
T1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (T1.isChecked()) {
// TODO Auto-generated method stub
buffer[0] = 'r';
buffer[1] = 'e';
buffer[2] = 'l';
buffer[3] = 'a';
buffer[4] = 'y';
buffer[5] = ' ';
buffer[6] = 'o';
buffer[7] = 'n';
buffer[8] = ' ';
buffer[9] = '0';
buffer[10] = ' ';
buffer[11] = '\r';
buffer[12] = '\n';
}
if (!T1.isChecked()) {
// TODO Auto-generated method stub
buffer[0] = 'r';
buffer[1] = 'e';
buffer[2] = 'l';
buffer[3] = 'a';
buffer[4] = 'y';
buffer[5] = ' ';
buffer[6] = 'o';
buffer[7] = 'f';
buffer[8] = 'f';
buffer[9] = ' ';
buffer[10] = '0';
buffer[11] = '\r';
buffer[12] = '\n';
}
write_buf(buffer);
}
});
void findBT() throws IOException
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 1);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals(a))
{
mmDevice = device;
break;
}
}
}
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard //SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater h= getMenuInflater();
h.inflate(R.menu.hardmenu,menu);
return true;
}