简体   繁体   中英

How to send command in Android via Bluetooth?

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?

After a few research, I found out that you are using BluetoothSPP library from https://github.com/akexorcist/Android-BluetoothSPPLibrary .

I don't know why you are using this library or even if you have to use it (like a requirement) but looking at the source code, I can see it's only a wrapper around Android built-in Bluetooth Classic SDK for RFCOMM communication (also known as Serial Port Protocol - SPP ).

Anyway, all the code relative to sending / receiving data seems to be handled in BluetoothService

What I would recommand is that you create your own wrapper so that you control the stream of the ongoing Bluetooth connexion. Then you will be able to send what ever you want because it gets down to simply write bytes into the streams and reading them on the other end. However, it is not certain what is the remote Bluetooh chip you are using.

If there is some sort of a documentation on what to send, follow it when you send the bytes over the stream.

Again, looking at the source code of the BluetoothSPP library, I only see a simple wrapper around Google's Bluetooth Chat Example . That's it, no magic here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM