如何通过蓝牙发送 Android 中的命令?

How to send command in Android via Bluetooth?

我在通过蓝牙向蓝牙芯片发送命令时遇到问题。

我的智能手机和 BT 芯片配对良好。因为我可以发送 "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 是 xml 文件上的按钮。 Heat 只做这个代码。

但我不知道我必须如何改造才能证明发送和接收这些命令:

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();
    }

我可以与芯片通信,但我不知道如何发送命令。我想你只是改造 bt.send.. 如果?

经过一些研究,我发现您正在使用来自 https://github.com/akexorcist/Android-BluetoothSPPLibraryBluetoothSPP 库。

我不知道你为什么要使用这个库,或者即使你必须使用它(如要求),但查看源代码,我可以看到它只是 [ 的包装器=39=] 内置蓝牙经典 SDK 用于 RFCOMM 通信(也称为 Serial Port Protocol - SPP)。

无论如何,所有与发送/接收数据相关的代码似乎都在BluetoothService

中处​​理

我建议您创建自己的包装器,以便控制正在进行的蓝牙连接的 。然后你就可以发送你想要的任何东西,因为它可以简单地将 bytes 写入流并在另一端读取它们。但是,不确定您使用的是什么遥控器Bluetooh chip

如果有关于发送内容的某种文档,请在通过流发送字节时遵循它。

再次查看 BluetoothSPP 库的源代码,我只看到 Google 的 蓝牙聊天示例 的简单包装。就是这样,这里没有魔法。