我有通过蓝牙发送命令到蓝牙芯片的问题。如何通过蓝牙在Android中发送命令?
我有智能手机和BT芯片配对好。因为我可以发送“文本”
BluetoothSPP bt;
void Heat() {
heat.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
bt.send("Text", true);
}
}
);
}
public void send(String data, boolean CRLF) {
if(mChatService.getState() == BluetoothState.STATE_CONNECTED) {
if(CRLF)
data += "\r\n";
mChatService.write(data.getBytes());
}
}
热火是xml文件按钮。热做只有这个代码。
但我不知道我该怎么改造,以证明发送和接收这些命令:
↓
Send: "$$$" Receive: "CMD"
Send: "S&,0404\r" Receive: "AOK"
Send: "S&,0400\r" Receive: "AOK"
Send: "---\r" Receive: "END"
我看到这么发短信成功是因为芯片有LED其开启,如果接受一些信息。
请给我建议或示例。
上创建
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autoconnect);
Log.i("Check", "onCreate");
bt = new BluetoothSPP(this);
if(!bt.isBluetoothAvailable()) {
Toast.makeText(getApplicationContext()
, "Bluetooth is not available"
, Toast.LENGTH_SHORT).show();
finish();
}
bt.setBluetoothConnectionListener(new BluetoothConnectionListener() {
public void onDeviceConnected(String name, String address) {
Toast.makeText(getApplicationContext()
, "Connected to " + name
, Toast.LENGTH_SHORT).show();
}
public void onDeviceDisconnected() {
Toast.makeText(getApplicationContext()
, "Connection lost"
, Toast.LENGTH_SHORT).show();
}
public void onDeviceConnectionFailed() {
Log.i("Check", "Unable to connect");
}
});
bt.setAutoConnectionListener(new BluetoothSPP.AutoConnectionListener() {
public void onNewConnection(String name, String address) {
Log.i("Check", "New Connection - " + name + " - " + address);
}
public void onAutoConnectionStarted() {
Log.i("Check", "Auto menu_connection started");
}
});
TextView btnConnect = (TextView)findViewById(R.id.btnConnect5);
btnConnect.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (bt.getServiceState() == BluetoothState.STATE_CONNECTED) {
bt.disconnect();
/** Toast.makeText(getApplicationContext()
, "pripojene"
, Toast.LENGTH_SHORT).show(); **/
// bt.disconnect();
} else {
/** Toast.makeText(getApplicationContext()
, "nepripojene"
, Toast.LENGTH_SHORT).show(); **/
Intent intent = new Intent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}
}
});
Log.i("Check", "onCreate");
textStatus = (TextView)findViewById(R.id.textStatus2);
bt = new BluetoothSPP(this);
if(!bt.isBluetoothAvailable()) {
Toast.makeText(getApplicationContext()
, "Bluetooth is not available"
, Toast.LENGTH_SHORT).show();
finish();
}
我可以用芯片comunicate,但我不知道我可以发送命令。我想你只是重塑bt.send ..如果?
你能添加更多的代码,比如heat和bt变量声明/库引用吗? –
我可以与芯片交流,但我不知道我可以如何发送命令。我想你只是重塑bt.send ..如果? –