2016-01-29 80 views
0

我有通过蓝牙发送命令到蓝牙芯片的问题。如何通过蓝牙在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 ..如果?

+0

你能添加更多的代码,比如heat和bt变量声明/库引用吗? –

+0

我可以与芯片交流,但我不知道我可以如何发送命令。我想你只是重塑bt.send ..如果? –

回答

0

经过一番研究,我发现你使用https://github.com/akexorcist/Android-BluetoothSPPLibraryBluetoothSPP库。

我不知道你为什么要使用这个库,或者即使你必须使用它(如需求),但看着源代码,我可以看到它只是一个包装Android内置Bluetooth Classic SDKRFCOMM通信(也称为Serial Port Protocol - SPP)。

无论如何,相对于发送所有代码/接收数据似乎BluetoothService

要处理什么我会推荐是你创建你自己的包装,让你控制的持续蓝牙联接的。然后,您将能够发送您想要的任何内容,因为它可以简单地将字节写入流中并在另一端读取它们。但是,不确定您使用的是什么远程Bluetooh chip

如果有关于要发送的内容的某种文档,请在通过流发送字节时按照它进行操作。

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

+0

好的我下载这个库,请在我需要的地方写 发送:“$$$”收到:“CMD” 发送:“S&,0404 \ r”收到:“AOK” 发送:“S&,0400 \ r”收到:“AOK” 发送:“--- \ r”收到:“END” 谢谢 –