I have a problem with send command to Bluetooth chip via Bluetooth.
I have smartphone and BT chip paired good. because I can send "text"
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());
}
}
Heat is Button on xml file. Heat do Only thise code.
But I don't know how I must remodel to prove send and receive these commands :
↓
Send: "$$$" Receive: "CMD"
Send: "S&,0404\r" Receive: "AOK"
Send: "S&,0400\r" Receive: "AOK"
Send: "---\r" Receive: "END"
I see so send text is successfully because chip have LED which turn-on if accept some information.
Please give me advice or example.
On create
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();
}
I can comunicate with chip, but i don't know how i can send command. I think you just remodel bt.send.. If?